Forum Moderators: coopster

Message Too Old, No Replies

Echo results into multiple rows?

Erm, yeah, erm, how?

         

diegomh7

2:21 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



Ok so i got a client who wants something which i know will fall into noob territory for most of you young padawans however.

All i need to do is bring up results from a mysql database and then list the results into a table using a while loop, thats the easy bit and ive done that loads a times before, however this time rather than making a while loop that draws a table fills it then for the next database entry draws another table below it and echos the second entry into it, im buggering it up cos i need the second entry to fill the second column rather than draw a new table,
i need rows of 3 or 4 results then go to the next row

ID 1,2,3 got into column 1,2,3 then ID 4,5,6 go into the second row columns 1,2,3.

Feel like ive been noobed :D

Do i need an IF clause somewhere in the while or an extra variable for the counter or what? Doh!

This is what happens when your self taught and learn it one way always do it that way then get bedazzled by summat that should be so simple,

ideas would be appreciated :D

diegomh7 aka dildego

p.s thx to all for the dynamic page generation post :D

Heres how i need it to look more or less anyway, the next 3 entries from the database need to appear on a row below and so on...

<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center"><?php echo "$VT"?></div></td>
<td><div align="center"><?php echo "$VT"?></div></td>
<td><div align="center"><?php echo "$VT"?></div></td>
</tr>
<tr>
<td><div align="center"><img name="" src="" width="200" height="220" alt=""></div></td>
<td><div align="center"><img name="" src="" width="200" height="220" alt=""></div></td>
<td><div align="center"><img name="" src="" width="200" height="220" alt=""></div></td>
</tr>
<tr>
<td><div align="center">
<form name="form3" method="post" action="">
Prices = &pound; <?php echo "$uk_pound"?> ¦ <?php echo "$euros"?> Euros<br>
<br>
<select name="select" class="boxes">
<option selected>Please Choose Your Size</option>
</select>
<br>
<a href="#" class="boxes">More Info</a>
<input name="Submit" type="submit" class="boxes" value="Buy Now">
</form>
</div></td>
<td><div align="center">
<form action="" method="post" name="form4" id="form4">
Prices = &pound; <?php echo "$uk_pound"?> ¦ <?php echo "$euros"?> Euros<br>
<br>
<select name="select" class="boxes">
<option selected>Please Choose Your Size</option>
</select>
<br>
<a href="#" class="boxes">More Info</a>
<input name="Submit" type="submit" class="boxes" value="Buy Now">
</form>
</div></td>
<td><div align="center">
<form action="" method="post" name="form5" id="form5">
Prices = &pound; <?php echo "$uk_pound"?> ¦ <?php echo "$euros"?> Euros<br>
<br>
<select name="select" class="boxes">
<option selected>Please Choose Your Size</option>
</select>
<br>
<a href="#" class="boxes">More Info</a>
<input name="Submit" type="submit" class="boxes" value="Buy Now">
</form>
</div></td>
</tr>
</table>

Feel free to copy and paste to see it in the design view :D

Sarah Atkinson

4:12 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



i had to do a calander similar to this a few months ago here is my table I made. hope it helps

<Table class='printTable' cellspacing="0" >
<tr><th>Monday</th><th>Tuesday</th><th>Wednesday</th> <th>Thursday</th> <th>Friday</th></tr>

<?php
$pdate=0; //previouse day
$pday=0; //the day of the last day of the previous month
mysql_data_seek($result, 0);
$g=$weekday; //day first falls on
$h=1;
if($g!=6){
while($g>$h){
echo "<td></td>";
++$h;
} }
while ($row = mysql_fetch_array($result)) {
$dbday=$row["day_string"]; //day of the week
$dbdate=$row["date_string"]; //numerical date
if($dbday == 1){ //if monday opens new row
echo '<tr>';
}
echo '<td>'; //starts the cell
echo "<strong>$dbdate</strong><br />"; //prints # date
for ($i=3;$i<11;++$i){ //prints the menu while filtering out empty entries
if($row[$i]!= "")
echo $row[$i] . '<br />';
}
echo '</td>'; //closes cell
if($dbday==5){ //checks to see if it is friday.. if so closes row
echo '</tr>';
}
}
?> </tr>
</table>

jd01

5:51 pm on Aug 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a basic counter should be fine. Like this:

Blah DB Stuff...

echo "Some Table Stuff";
$cols=0;

while($stuff = $otherstuff) {
$cols++; // I like my counters all together just before and at the top of a loop - keeps them all in one place, you can move these areound to fit your style =)

if($cols==1) { echo "<tr>"; } // If this is col 1 we will need a row
echo "<td>"; // we will always need a col

// some variables and stuff go here

echo "</td>"; // we will always need to close the col
if($cols==3) { echo "</tr>"; $cols=0; } // if this is the third time through, start over.

} // end while

if($cols < 3 && $cols!=0) { while($cols <= 3) { echo "<td>&nbsp;</td>"; $cols++ } echo "</tr>"; } // make sure we have everything closed if we have an extra col, but no more information - if there will always be the correct number of results, this is not necessary.

You can use this for multiple rows/sections in the loop, by repeating the conditional code.

EG

if($cols==1) { echo "<tr>"; }
echo "<td>";

// some variables and stuff go here for Row 1

echo "</td>";
if($cols==3) { echo "</tr>"; } // remove the counter reset from here

if($cols==1) { echo "<tr>"; }
echo "<td>";

// some variables and stuff go here for Row 2

echo "</td>";
if($cols==3) { echo "</tr>"; $cols=0; }

Hope this helps.

Justin