PDA

View Full Version : PHP Help +rep



iUnknown
06-07-2008, 01:46 PM
Hello,

I'm using this code to make a list of content in a table on a MySQL database:


$query = mysql_query("SELECT * FROM `staff`");

while($row = mysql_fetch_array($query)){

It's building a table and each entry goes in its own <td>. However, I want it to go onto a new line on the table (a new <tr>) every 4 mysql table rows (people).

How would I do this?

+rep to first correct/best solution.

Thank you.

Protege
06-07-2008, 02:01 PM
I think you should just revise the stuff you post before you keep opening new threads


<?php
$query = mysql_query("SELECT * FROM `staff`");
$number = 0;
while($row = mysql_fetch_array($query))
{
if ( $number <= 4 )
{
echo ( '<td></td>');
}
elseif ( $number === 4 )
{
echo ( '<tr>' );
}
else
{
echo ( '<tr>' );
$number = 0;
}
echo ( $lala );
$number++;
}


?>

I think thats what your looking for.

iUnknown
06-07-2008, 02:10 PM
Hmm... I might be not using that correctly but it doesn't seem to be making a difference.

Decode
06-07-2008, 02:20 PM
<?php
$query = mysql_query("SELECT * FROM `staff`") or die( mysql_error() );
echo ("<table>");
while($row = mysql_fetch_array($query)){
echo ("<tr><td>" . $row['x'] . "</td><td>" . $row['x'] . "</td></tr>");
}
echo ("</table>");
?>
$row['x'] are the col names of the table, you need to change them :)

iUnknown
06-07-2008, 02:25 PM
Thanks but I don't think you quite understood what I meant. I've done that, and I've got it coming out fine but all the entries go onto one <tr> - I want only 4 entries per line. This is the full code:


<?php

$query = mysql_query("SELECT * FROM `staff`");

while($row = mysql_fetch_array($query)){

echo '
<td><center><img src=http://www.habbo.co.uk/habbo-imaging/avatarimage?user=' . $row['name'] . '&action=0&direction=3&head_direction=3&img_format=gif&gesture=0&size=s><br><font face="Verdana" size="1"><b>' . $row['name'] . '</b></font><br><font face="Verdana" size="1"><i>DJ ' . $row['username'] . '</i></font></center></td>';
} ?>

Decode
06-07-2008, 02:32 PM
Sorry, i read it wrong :P Try this.



<?php
$query = mysql_query("SELECT * FROM `staff`");
echo '<div class="container">';
while($row = mysql_fetch_array($query)){
echo '<div class="box">';
echo '<img src=http://www.habbo.co.uk/habbo-imaging/avatarimage?user=' . $row['name'] . '&action=0&direction=3&head_direction=3&img_format=gif&gesture=0&size=s>';
echo '<br><b>' . $row['name'] . '</b><br><font face="Verdana" size="1"><i>DJ ' . $row['username'] . '</i></div>';
}
echo '</div>';
?> And add this to the head section;




<style type="text/css">
.box {width: 200px;
float: left;
font-family: verdana;
font-size: 12px;
text-align: center;
}
.container {width: 800px;
}
</style>

iUnknown
06-07-2008, 02:37 PM
Now nothing is appearing. Also, surely it must contain a "4" somewhere in the code and be more like Protege's?

Decode
06-07-2008, 02:40 PM
Fixed it :)

iUnknown
06-07-2008, 02:44 PM
Getting somewhere now. But, the first one that comes out, the font is size 2 instead of size 1 for some reason and the first one on the second row, when it's by itself, is under the 2nd one from the first row, and it should be under the 1st one on first row.... if that makes sense?

Note to self, I owe you 2 +rep.

Edit: fixed it - thanks so much. will write in signature to remind myself.

(edited Css:)


<style type="text/css">
.box {width: 200px;
float: left;
font-family: verdana;
font-size: 10px;
text-align: center;
}
.container {width: 800px;
}
</style>

Want to hide these adverts? Register an account for free!