Mysql fetch array, table results
Solution 1
$row
will update on each iteration of the loop:
$result = mysql_query('SELECT * FROM member');
echo '<tr>';
for($i = 0; $row = mysql_fetch_array($result); $i = ($i+1)%3){
echo '<td width="270px">'.$row['names'].'</td>';
if($i == 2)
echo '</tr><tr>';
}
echo '</tr>';
Solution 2
$result = mysql_query('SELECT * FROM member');
echo'<tr>'
while($row = mysql_fetch_array($result))
{
echo '<td width="270px">'.$row['names'].'</td>';
}
echo '</tr>';
Solution 3
Lets say you have an array of the names called $names
, then you could do what you wanted with code that looks something like this:
<tr>
<?php
foreach($names as $name) {
print "<td>$name</td>";
}
?>
</tr>
That would put all the names on the same row.
In order to start a new row say, every 3 names, you could put an if statement in the for loop like this:
// assume we have these variables available.
$row_number;
$max_cols = 3;
// this goes at the top of the foreach
if($row_number % $max_cols == 0) {
print '</tr><tr>';
}
Solution 4
You know accomplish this by assigning the row count before you start the variable and few flags to know initialized loop or started/ended loop.
$i = 1;
$initFlag = false;
$flag = "closed";
while($row = mysql_fetch_array($result)) {
if($i%3 == 0) $initFlag = false;
if($flag == "closed" && ($i == 1 || $i % 3 == 1)) {
echo "<tr>"; $flag = "started"; $initFlag = true;
}
echo '<td width="270px">'.$row['names'].'</td>';
if(!initFlag && $flag == "started" && $i % 3 ==0) {
echo "</tr>"; $flag = "closed";
}
$i++;
}
Solution 5
$result = mysql_query('SELECT * FROM member');
echo '<tr>;
while($row = mysql_fetch_array($result))
{
echo '<td width="270px">'.$row['names'].'</td>';
}
echo '</tr>';
Crays
Updated on June 04, 2022Comments
-
Crays almost 2 years
i'm pretty new to this and not sure how should i do it,
I've got a database with a column called "names"
how can i make it display in so?
<tr> <td width="270px">Henry</td> <td width="270px">Jeffrey</td> <td width="270px">Hansel</td> </tr> <tr> <td width="270px">Michelle</td> <td width="270px">Jackson</td> <td width="270px">Ivan</td> </tr>
I only know how i should do it if the records goes one after another vertically.
$result = mysql_query('SELECT * FROM member'); while($row = mysql_fetch_array($result)) { echo' <tr> <td width="270px">'.$row['names'].'</td> <td width="270px">Jeffrey</td> <td width="270px">Hansel</td> </tr>';
Kinda stucked here...
Sorry i forgot to put this.
what i want is that it should loop the tags along. So i can have a 3 column table with infinite rows.
what bout this one? What if i want this to loop instead?
<tr> <td><img src="images/ava/A.png" /></td> <td>A</td> <td width="2px" rowspan="3"></td> <td><img src="images/ava/B.png" /></td> <td>B</td> </tr> <tr> <td>A</td> <td>B</td> </tr>
-
Crays almost 13 yearsThanks, but i've editted what i meant, forgot to put that in.
-
Crays almost 13 yearswhat i want is that it should loop the <tr></tr> tags along. So i can have a 3 column table with infinite rows.
-
Crays almost 13 yearsThanks Paul :) Works exactly how i wanted it to, but if you mind to explain it a little to me i would be grateful! What does ($i+1)%3 actually do?
-
Paul almost 13 years% is the modulus operator. It returns the remainder of a division. So
10 % 4 == 2
and9 % 3 == 0
($i+1) is just to increment i,% 3
is to set it back to 0 every time it reaches 3 soi
counts like:0,1,2,0,1,2,0,1...
-
Crays almost 13 yearsLovely :) Thanks a bunch for that info and help!
-
Crays almost 13 yearsHi paul, sorry to bother again, but what if i have a cell with row span and i wanna do something similar stated above? say something like the new one i editted above.