Solved

Split database output dynamically into two columns for layout

Posted on 2008-10-24
4
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22800656
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
ID: 22800825
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
ID: 22800853
Sorry, there was a bug. Change this:

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

... to this:

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

Author Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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