...

View Full Version : PHP Formatting Data from mySQL



peteyb383
03-01-2008, 06:18 AM
Hi,

I am pulling data from an SQL database. The data is file paths for image files on the server, the below code shows how the image tags are assembled. I have no problem assembling the code and showing the pictures, however; if there are more than 4 pictures, the table that the pictures are in expands width-wise ruining my site layout.



<table width="535"><tr>
<?php

//SQL Connection...

$result = mysql_query("SELECT * FROM photos WHERE picteam='Amherst'");

while($row = mysql_fetch_array($result))
{
echo '<td><img src="' . $row['path'] . '" height="83" width="125" /></td>';
}

?>

</tr></table>

As I said, the code works, but only for up to 4 pictures, because a new row for the table is not created. If I tried to add the <tr> tags in the loop, a new row would be created for each picture, which I don't want.

I am simply (or not so simply) looking for a method to automatically create a new row so that there are four pictures per row.

Thank you for your time,
Petey

Fumigator
03-01-2008, 06:33 AM
It is simple, really. Use the modulus (&#37;) operator, which is like the division operator, but it returns the remainder of the division. For example, $a = 6 / 4 returns 1.5, but using the modulus like this: $a = 6 % 4 will return 2 (the remainder of the division).

You may be wonding how the hell this applies to your situation?!? Well I'll tell you! If you increment a variable inside your loop that creates the <img> tag, then check the modulus of that variable % 4, it will only be 0 one out of every 4 times! So you can then put it in an "if" statement that says if modulus is 0, print the <tr> tag!



<?php
print "<table><tr>";
for ($i = 1; $i <= mysql_num_rows($result); $i++) {
if(($i % 4) == 0) { //time to print the <tr>
print "</tr><tr>";
}
print "<td><img . . . . .</td> " // blah blah <img> tag
}
print "</tr></table>";
?>

_Aerospace_Eng_
03-01-2008, 06:57 AM
Hmm I think $i needs to be 0.

<table width="535">
<tr>
<?php

//SQL Connection...

$result = mysql_query("SELECT * FROM photos WHERE picteam='Amherst'") or die(mysql_error());
$i = 0;
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
if(($i % 4) == 0 && $i != 0)
{
echo "</tr>\n<tr>\n";
}
echo '<td><img src="' . $row['path'] . '" height="83" width="125" /></td>';
$i++;
}
}
else
{
echo "<td>There are no images to show.</td>\n";
}
?>

</tr></table>

Fumigator
03-01-2008, 09:07 AM
You'll have an empty <tr></tr> if you start with 0, if you start with 1 you won't.

_Aerospace_Eng_
03-01-2008, 04:26 PM
Try it. He already starts with a <tr>. Output of your code

<table>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>

</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>

<td>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>

<td>test</td>
<td>test</td>
</tr>
<tr>
<td>test</td>
</tr>
</table>
output with the code I posted

<table>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>

<td>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>

<td>test</td>
<td>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td>test</td>
</tr>
<tr>
<td>test</td>

<td>test</td>
<td>test</td>
<td>test</td>
</tr>
</table>

peteyb383
03-01-2008, 04:39 PM
Thank you, to the both of you for your help. Aerospaces' code works how I need it to, but thanks Fumigator.

I was thinking of a mod idea when the problem arose, but i just didn't know how to implement it.

Again thanks for the help,
Petey

Fumigator
03-01-2008, 07:24 PM
Ah I see where I went wrong; you da man.
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum