PDA

View Full Version : Displaying DB Results Vertical


epic1231
08-28-2009, 11:47 PM
I have this code which is a form and gives me the ability to show users already stored in the database


<?

echo '<div id="top"></div>';

echo '<table align=center width=95% class="itemTable" border=0>';
echo '<tr>';
$res = query("SELECT * FROM `appusers` WHERE `userid`!=$user ORDER BY RAND() LIMIT 0,20");
$i=0;
while( $row = mysql_fetch_array($res) ) {
echo '<td><P class=bigBoldP><a href="'.$appCanvasUrl.'stat.php?id='.$row[userid].'"><fb:profile-pic uid='.$row[userid].' size=square width=75 height=75 linked=false /></a><BR><a href="'.$appCanvasUrl.'stat.php?id='.$row[userid].'">Agent '.$row[uCharName].'</P>';

if( $row[uHealth] <= 20 ){
echo '<BR><P class=bigBoldP>Too weak to fight</P>';
}
else {
echo '<BR><P class=bigBoldP>';
echo '<form ACTION="'.$appCanvasUrl.'fight/" method="POST" id="myform'.$i.'">';
echo '<input type=hidden name=oponentId id="opponentid'.$i.'" value="'.$row[userid].'">';
echo '<input type="hidden" name="top" value="#top">';
echo '<input type=hidden name="fightOponent" value="Attack"><input type="button" name="fightOponentg" value="Attack" onclick=\'submit_a("myform'.$i.'", "fight_response", "opponentid'.$i.'", "fightagain'.$i.'")\' id="fightagain'.$i.'" class="submitButton">';
echo '</form><BR>';
echo '</P>';
$i++;
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
echo '<BR>';

require_once '../bottom.php';

?>



For some reason with changing my td's and tr's I can't seem to get it to output 4 rows of 5 column's each = the 20 items I am looking to pull

Any suggestions, I have done it before but lost the code and am drawing a complete blank this evening while working on it.

Thanks very much in advance!
-Bill

Old Pedant
08-29-2009, 02:41 AM
I don't code in PHP, but I came up with an answer in JSP and ASP that I like a lot: STOP using the WHILE loop and use nested for loops!

Again, not a PHP user, so pardon any goofs. But something like this:

?>
<div id="top"></div>
<table align="center" width="95%" class="itemTable" border="0">
<?php
$res = query("SELECT * FROM `appusers` WHERE `userid`!=$user ORDER BY RAND() LIMIT 0,20");
$i=0;
$row = mysql_fetch_array($res) ) {
for( $rownum = 1; $rownum <= 4; ++$rownum )
{
if ( $row == null ) break;
echo( "<tr>\n" );
for ( $colnum = 1; $colnum <= 5; ++$colnum )
{
if ( $row == null ) break;
?>
<td><P class="bigBoldP">
<a href="<?=$appCanvasUrl?>stat.php?id=<?=$row[userid]?>">
<fb:profile-pic uid="<?=$row[userid]?>" size="square" width="75" height="75" linked="false" />
</a>
<BR>
<a href="<?=appCanvasUrl?>stat.php?id=<?=$row[userid]?>">Agent <?=$row[uCharName]?> </a>
</P>
<?php
if( $row[uHealth] <= 20 ){
echo '<BR><P class=bigBoldP>Too weak to fight</P>';
} else {
?>
<BR>
<P class=bigBoldP>
<form ACTION="<?=$appCanvasUrl?>fight" method="POST" id="myform<?=$i?>">
<input type=hidden name=oponentId id="opponentid<?=$i?>" value="<?=$row[userid]?>">
<input type="hidden" name="top" value="#top">
<input type=hidden name="fightOponent" value="Attack">
<input type="button" name="fightOponentg" value="Attack"
onclick="submit_a(this.form,'fight_response', 'opponentid<?=$i?>', 'fightagain<?=$i?>');" id="fightagain<?=$i?>" class="submitButton">
</form><BR>
</P>
<?php

$i++;
}
echo " </td>\n";
$row = mysql_fetch_array($res); // get next row...tested at top of the two for loops
} // end of loop on colnum
echo "</tr>\n";
} // end of loop on rownum
?>
</table>
<BR>
<?php
require_once '../bottom.php';
?>


Note that I make extensive use of <?=...?> which I understand not all installations of PHP support. (Why not??? Why would anybody choose to NOT use that so-convenient form??) Obviously, each of them can be changed to <?php echo ... ; ?> ugly as that looks.

But anyway, the important "trick" is to loop the rows, putting <tr> at the start and </tr> at the end of each loop, and then loop the columns inside each row. Makes multiple columns dirt simple to control!

I'm sure you can see how trivial it would be to use variables in place of 4 and 5 to easily alter the number of rows and columns.