Forum Moderators: coopster

Message Too Old, No Replies

ordering mysql query into an html table

         

dragon master mokuba

3:14 am on Jan 21, 2008 (gmt 0)

10+ Year Member



i have a mysql database and i use php to query all the rows in the database. i bring the results into an html table like so:
______
¦_01_¦
¦_02_¦
¦_03_¦
¦_04_¦
¦_05_¦
¦_06_¦
¦_07_¦
¦_08_¦
¦_09_¦
¦_10_¦
¦_11_¦
¦_12_¦

how do i get it so the results to go into the html table like so:
_____________________
¦_01_¦_02_¦_03_¦_04_¦
¦_05_¦_06_¦_07_¦_08_¦
¦_09_¦_10_¦_11_¦_12_¦

thanks for any and all help!

venelin13

6:16 am on Jan 21, 2008 (gmt 0)

10+ Year Member



Here is a simple solution. Lets suppose you have the data from MySQL as an array.

//get the total number of elements at the array
$total = count($data);

//start a loop by 4 (to have 4 columns)
for($i = 0; $i <= $total; $i+=4){

//first column index
$a = $i;
$b = $i + 1;
$c = $i + 2;
$d = $i + 3;

echo "<tr>";

echo "<td>$data[$a]</td>";
echo "<td>$data[$b]</td>";
echo "<td>$data[$c]</td>";
echo "<td>$data[$d]</td>";

echo "</tr>";

}

dragon master mokuba

7:25 am on Jan 21, 2008 (gmt 0)

10+ Year Member



from what i understand about that script, is that i have to know how many results there are, and then i have to "$[variable] = $i + [#];" for each one? im not gonna know how many results there are gonna be, and there will be alot of results nonetheless.

venelin13

7:47 am on Jan 21, 2008 (gmt 0)

10+ Year Member



im not gonna know how many results there are gonna be

You run a database query and you do not know how many results are in the result set? Try again!

dragon master mokuba

8:01 am on Jan 21, 2008 (gmt 0)

10+ Year Member



well the way you wrote the code it seems as if i have to know exactly how many entries are in the database.

especially with :


//first column index
$a = $i;
$b = $i + 1;
$c = $i + 2;
$d = $i + 3;

if i dont know exactly how many there will be, how will i know what to put as the increasing variable? ie. $i+ 1, $i + 2, $i + 3, $i + 4...

the results im getting are different products from my online store with upwards of 30 to 40+ results easy.

phranque

8:38 am on Jan 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the php count function [php.net] determines exactly how many entries are in your array.

the only thing that is "known" is your requirement for 4 columns (note the "for" loop increment) and the only thing i see missing is how to fill incomplete rows.

if you have variable column width requirements then you should specify that as well in your problem statement.

HELLvin

2:36 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



this can be done more easily by just ending the table row after each 4 entries..

so lets say u put all the db data into an array named $entries

<table>
<tr>


$i = 0;

foreach($entries as $data)
{
echo "<td>".$data[title]."</td>";
$i++

if($i == 4)
{
echo "</tr><tr>";
$i = 0;
}
}


</tr>
</table

dragon master mokuba

6:56 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



i cant get anything to work. here is my code so far


<?php
$dbh=mysql_connect ("localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("dos_mydb");

$result = mysql_query("SELECT * FROM person ");
?>
<table border="1" cellspacing="1" cellpadding="1">
<tr>
<?php
$i = 0;
$entries = mysql_fetch_array($result);
foreach($entries as $data)
{
echo "<td>".$data[FirstName]."</td>";
$i++;

if($i == 4)
{
echo "</tr><tr>";
$i = 0;
}
}

mysql_close($dbh);
?>
</tr>
</table>

jatar_k

7:12 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this
[webmasterworld.com...]

dragon master mokuba

7:52 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



omgosh! thanks so much. it works pefectly!

phranque

11:08 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], HELLvin!

thanks for helping out here...