Jump to content

Displaying database data in three vertical columns


wchamber22

Recommended Posts

Hello freaks,

 

Got a task here which is displaying correctly (I believe).  I only have 3 data entries in the db right now.  Like I said (I think) the display of the code below yields the right layout but it repeats the first db entry over and over.

 

I got the display to work correctly like this:

 

1 2 3

4 5 6

7 8 9

 

I am trying to get this display result (so it is more eligable for the end user!)

 

1 4 7

2 5 8

3 6 9

 

Thanks in advance!

 

CODE

<?php
include_once "connect_to_mysql.php";
$cols = 3;
$result = mysql_query("SELECT plantID, botanicalName FROM plants ORDER BY botanicalName");
$numrows = mysql_num_rows($result);
$rows_per_col = ceil($numrows / $cols);

$c = 1;
$r = 1;

while ($row = mysql_fetch_array($result)) {
$plantID = $row["plantID"];
$botanicalName = $row["botanicalName"];

if ($r == $rows_per_col) {
	$c++;
	$r = 1;
} else {
	$r++;	
}
}
$dyn_table = '<table width="750" cellpadding="0" cellspacing="0" border="0">';
for ($r = 1; $r <= $rows_per_col; $r++) {
$dyn_table .= '<tr>';

for ($c = 1; $c <= $cols; $c++) {
	$dyn_table .= '<td><a href="plant_details.php?plantID = ' . $plantID . '" id="plantLink">' .  $botanicalName . '</a></td>';
}
$dyn_table .= '</tr>';
}
$dyn_table .= '</table>';
?>

Link to comment
Share on other sites

You can't really do that, HTML tables are drawn row by row.

 

You could try to pivot the result set so that it goes in the order: 1,4,7,2,5,8,3,6,9.

 

You could also print three tables side by side.

 

Most sites just do what you're already doing: display is left-to-right and then top-to-bottom.  It cuts down on scrolling as well.

Link to comment
Share on other sites

try

<?php
include_once "connect_to_mysql.php";
$cols = 3;
$result = mysql_query("SELECT plantID, botanicalName FROM plants ORDER BY botanicalName");
$numrows = mysql_num_rows($result);
$rows_per_col = ceil($numrows / $cols);

$c = 1;
$r = 1;

while ($row = mysql_fetch_array($result)) {
$plantID[$r][$c] = $row["plantID"];
$botanicalName[$r][$c] = $row["botanicalName"];

if ($r == $rows_per_col) {
	$c++;
	$r = 1;
} else {
	$r++;	
}
}
$dyn_table = '<table width="750" cellpadding="0" cellspacing="0" border="0">';
for ($r = 1; $r <= $rows_per_col; $r++) {
$dyn_table .= '<tr>';

for ($c = 1; $c <= $cols; $c++) {
	$dyn_table .= '<td><a href="plant_details.php?plantID = ' . $plantID[$r][$c] . '" id="plantLink">' .  $botanicalName[$r][$c] . '</a></td>';
}
$dyn_table .= '</tr>';
}
$dyn_table .= '</table>';
?>

Link to comment
Share on other sites

I think this is a little more strait-forward.

 

include_once "connect_to_mysql.php";

$result = mysql_query("SELECT plantID, botanicalName FROM plants ORDER BY botanicalName");
$numrows = mysql_num_rows($result);

$max_cols = 3;
$max_rows = ceil($numrows / $max_cols);

$count = 0;
$tdData = array();
while ($row = mysql_fetch_array($result))
{
    $colIdx = floor($count/$max_rows);
    $rowIdx = $count - ($colIdx * $max_rows);
    $tdData[$rowIdx][$colIdx] = "<td><a href=\"plant_details.php?plantID={$row['plantID']}\" id=\"plantLink_{$row['plantID']}\">{$row['botanicalName']}</a></td>\n";
    $count++;
}

$dyn_table = '<table width="750" cellpadding="0" cellspacing="0" border="0">';
foreach ($tdData as $rowAry)
{
    $dyn_table .= "<tr>\n" . implode("", $rowAry) . "</tr>\n";
}
$dyn_table .= '</table>';

 

Note: I changed the output to make the ID parameter of the anchor tags unique. You are not supposed to have multiple elements on a page with the same ID.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.