Solved

Split database output dynamically into two columns for layout

Posted on 2008-10-24
4
223 Views
Last Modified: 2013-12-13
I have a simple php script querying a database a listing everything out in one long column. For layout purposes I would like to break it into two columns. Can this be done dynamically so the columns are automatically balanced or do I need to choose an item half way down the table and break there manually?
<?php

$con = mysql_connect("","","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }
 

mysql_select_db("", $con);
 

$result = mysql_query("SELECT * FROM vendors ORDER BY vendor ASC");
 

while($row = mysql_fetch_array($result))

  {

  

  print"<div><img src=\"vendorlogos/{$row['logo']}\" height=\"45\" alt=\"\"/><br /><br /><u><strong>{$row['vendor']}</strong></u><br />{$row['description']}<br />Category: {$row['category']}<br /><br /><img src=\"http://www.domain.com/images/mini_arrow.jpg\"/>&nbsp;Product PDFs:<br/><br/><a href=\"http://www.domain.com/vendorpdfs/{$row['pdf']}\">{$row['pdf']}</a><br /><a href=\"http://www.domain.com/vendorpdfs/{$row['pdf2']}\">{$row['pdf2']}</a><br /><a href=\"http://www.domain.com/vendorpdfs/{$row['pdf3']}\">{$row['pdf3']}</a><br /><br /><br />Visit vendor<a href=\"http://{$row['website']}\" target=\"_blank\">Website</a><hr><br /></div>";

  

  }
 

mysql_close($con);

?>

Open in new window

0
Comment
Question by:guy4graphics
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
Maybe something like this could work:
$result = mysql_query("SELECT * FROM ...");

$rowcount = mysql_num_rows($result);

if($rowcount > 0) {

  echo '<table><tr><td>';

  $count = 0;

  while($row = mysql_fetch_array($result)) {

    print "<div>output goes here</div>";

    $count += 1;

    if($count == floor($rowcount/2)) 

      echo '</td></td>';

  }

  echo '</td></tr></table>';

}

Open in new window

0
 
LVL 1

Author Comment

by:guy4graphics
Comment Utility
This just lists them in one column as before in no particular order. Am I doing something wrong?
 <?php

$con = mysql_connect("","","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }
 

mysql_select_db("", $con);
 

             

$result = mysql_query("SELECT * FROM vendors ORDER BY vendor ASC");
 

$rowcount = mysql_num_rows($result);

if($rowcount > 0) {

  echo '<table><tr><td>';

  $count = 0;

  while($row = mysql_fetch_array($result)) {

    

    print"<div><img src=\"vendorlogos/{$row['logo']}\" height=\"45\" alt=\"\"/><br /><br /><u><strong>{$row['vendor']}</strong></u><br />{$row['description']}<br />Category: {$row['category']}<br /><br /><img src=\"http://www.domain.com/images/mini_arrow.jpg\"/>&nbsp;Product PDFs:<br/><br/><a href=\"http://www.domain.com/vendorpdfs/{$row['pdf']}\">{$row['pdf']}</a><br /><a href=\"http://www.domain.com/vendorpdfs/{$row['pdf2']}\">{$row['pdf2']}</a><br /><a href=\"http://www.domain.com/vendorpdfs/{$row['pdf3']}\">{$row['pdf3']}</a><br /><br /><br />Visit vendor<a href=\"http://{$row['website']}\" target=\"_blank\">Website</a><hr><br />

  

  </div>";

    $count += 1;

    if($count == floor($rowcount/2)) 

      echo '</td></td>';

  }

  echo '</td></tr></table>';

}
 

mysql_close($con);

?>

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
Comment Utility
Sorry, there was a bug. Change this:

      echo '</td></td>';

... to this:

      echo '</td><td>';
0
 
LVL 1

Author Comment

by:guy4graphics
Comment Utility
I should have seen that - thanks for your help! Works great now!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now