SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
Thread: {PHP} Repeat
-
Oct 29, 2006, 10:33 #1
{PHP} Repeat
Hi
I am having some problems atm, i am trying to get the following php code to go 4 across and then i want to use $i - $i++ to make it loop down. But when i try to make it go 4 across i get a problem the same result is shown 4 across and then another result which is the same across.
Here is my code:
PHP Code:<?php
l_display = '3';
$result = mysql_query("SELECT * FROM media ORDER BY Id DESC, RAND() LIMIT $l_display");
while($row = mysql_fetch_array($result))
{
echo '<a href="view.php?id='.$row['Id'].'">';
echo '<img src="img/'.$row['Img'].'" border="0" width="142" height="100"><br /><b><strong>';
echo $row['Title'];
echo "</a><br /></strong></b>";
echo $row['Description'];
echo "<br /><small>Submitted: ";
echo $row['Time'];
echo "</small><br />";
echo '<td>';
}
?>
-
Oct 29, 2006, 10:43 #2
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i think this is what you want, im having a hard time understanding you.
im cutting and pasting some code so you will need to adapt it
PHP Code:// how many columns we want our table to have
$columns = 4;
echo "<table>\n";
for ($i = 0; $row = mysql_fetch_assoc($result); $i++) {
if (($i % $columns) == 0) {
echo " <tr>\n";
}
echo " <td>" . $row['fieldname'] . "</td>\n";
if (($i % $columns) == ($columns - 1)) {
echo " </tr>\n";
}
}
// find out how many empty <td></td> we need to output now
// to make the html correct and well formed
$remainder = ($columns - ($i % $columns)) % $columns;
// do we have remainders?
if ($remainder > 0) {
echo str_repeat(" <td></td>\n", $remainder);
// or alternatively
// echo " <td colspan=\"$remainder\"></td>\n";
echo " </tr>\n";
}
echo "</table>\n";
-
Oct 29, 2006, 10:50 #3
So the bit where it says
echo " <td>" . $row['fieldname'] . "</td>\n";echo '<a href="view.php?id='.$row['Id'].'">';
echo '<img src="img/'.$row['Img'].'" border="0" width="142" height="100"><br /><b><strong>';
echo $row['Title'];
echo "</a><br /></strong></b>";
echo $row['Description'];
echo "<br /><small>Submitted: ";
echo $row['Time'];
echo "</small><br />";
echo '<td>';
-
Oct 29, 2006, 10:58 #4
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
yup
-
Oct 29, 2006, 11:38 #5
Well i one result with that, i did this:
// how many columns we want our table to have
$columns = 4;
echo "<table>\n";
for ($i = 0; $row = mysql_fetch_assoc($result); $i++) {
if (($i % $columns) == 0) {
echo " <tr>\n";
}
echo '<td><a href="view.php?id='.$row['Id'].'">';
echo '<img src="img/'.$row['Img'].'" border="0" width="142" height="100"><br /><b><strong>';
echo $row['Title'];
echo "</a><br /></strong></b>";
echo $row['Description'];
echo "<br /><small>Submitted: ";
echo $row['Time'];
echo "</small><br />";
echo '</td>';
if (($i % $columns) == ($columns - 1)) {
echo " </tr>\n";
}
}
// find out how many empty <td></td> we need to output now
// to make the html correct and well formed
$remainder = ($columns - ($i % $columns)) % $columns;
// do we have remainders?
if ($remainder > 0) {
echo str_repeat(" <td></td>\n", $remainder);
// or alternatively
// echo " <td colspan=\"$remainder\"></td>\n";
echo " </tr>\n";
}
echo "</table>\n";
}
-
Oct 29, 2006, 11:46 #6
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
sorry i dont understand?
-
Oct 29, 2006, 11:56 #7
Well when i used the code above i got only one result shown from the database and not 4 across
-
Oct 29, 2006, 12:02 #8
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$result = mysql_query("SELECT * FROM media ORDER BY Id DESC, RAND() LIMIT 8");
-
Oct 29, 2006, 12:07 #9
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
wait...
Code:// do you just need 4 across like 1 2 3 4 // or do you need 1 2 3 4 5 6 7 8 etc...
-
Oct 29, 2006, 12:23 #10
i need it like the second one:
1234
5678
-
Oct 29, 2006, 12:40 #11
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thats what it currently does. you need to feed at least 5 records into that code for it to wrap it to the next table row.
-
Oct 29, 2006, 12:46 #12
oh i currently got 3 into the database ill add around 15 and then get back to you.
Bookmarks