Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
aj2000
Forum Newbie
Posts: 11 Joined: Sun May 30, 2004 10:12 pm
Post
by aj2000 » Thu Jul 01, 2004 12:37 am
hi,
im trying to generate info from a database and it should list the items in a horizontal fashion with 3 records per row ...so it would look like this:
field1 field1 field1
field2 field2 field2
field1 field1
field2 field2
.... and so on..
so far i have it generating 1 row...but it does return to the second row ..the site can be found at
http://www.financialshopper.com/categor ... y_test.php
and here is my code so far:
Code: Select all
$query_cat_d = "SELECT c_name, c_image from category";
$result_cat_d = mysql_query($query_cat_d);
echo ("<TR>");
for ($j = 0; $j < mysql_num_rows($result_cat_d); $j++) {
$rowarray = mysql_fetch_row($result_cat_d);
$field_num = mysql_num_fields($result_cat_d);
//for ($j = 0; $j < $field_num; $j++) {
$remainder = mysql_num_rows($result_cat_d) % 3
echo ("<TD>");
echo ("<TABLE border=1>");
echo ("<TR><TD>") . $rowarray[1] . ("</TD></TR>");
echo ("<TR><TD>") . $rowarray[0] . ("</TD></TR>");
echo ("</TABLE>");
echo ("</TD>");
//}
}
echo ("</TR>");
any help would be appreciated
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Jul 01, 2004 9:57 am
in order to do this you'll need to store off all the data, and then stripe it back out.
(example)
Code: Select all
<?php
$query = mysql_query("SELECT col1, col2 FROM table") or die(mysql_error());
if(!mysql_num_rows($query)) die("No data found.");
$data = array();
while($data[] = mysql_fetch_assoc($query));
$keys = array_keys($data[0]);
$num = count($data);
echo '<table>';
foreach($keys as $key)
{
echo '<tr><td>'.$key.'</td>';
for($x = 0; $x < $num; $x++)
{
echo '<td>'.($data[$x][$key]).'</td>';
}
echo '</tr>'."\n";
}
echo '</table>';
?>
aj2000
Forum Newbie
Posts: 11 Joined: Sun May 30, 2004 10:12 pm
Post
by aj2000 » Fri Jul 02, 2004 7:31 pm
Hi, i tried your script..i still ended up with more than 3 entries on the row..im trying to get it so that 3 records are shown on the first row and then the next 3 on the second row...and so on.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jul 02, 2004 8:28 pm
for that.. you'll need a counter or two, to limit the and control the number displayed per row..
aj2000
Forum Newbie
Posts: 11 Joined: Sun May 30, 2004 10:12 pm
Post
by aj2000 » Sat Jul 03, 2004 1:54 pm
Hi! i finally got this to work. for anyone else with this same issue, heres what i have:
Code: Select all
$tmp_cnt = 0; // temporary variable
$per_row = 3;
for ($j = 0; $j < mysql_num_rows($result_cat_d); $j++) {
//echo ("<TR>");
if ($tmp_cnt % $per_row == 0)
echo ('<tr>');
$rowarray = mysql_fetch_row($result_cat_d);
$field_num = mysql_num_fields($result_cat_d);
echo ("<TD>");
echo ("<TABLE border=1>");
echo ("<TR><TD>") . $rowarray[1] . ("</TD></TR>");
echo ("<TR><TD>") . $rowarray[0] . ("</TD></TR>");
echo ("</TABLE>");
echo ("</TD>");
//}
$tmp_cnt = $tmp_cnt + 1;
if ($tmp_cnt % $per_row == 0)
echo ('</tr>');
}