Reputation: 31
I'm wanting to split the results from a database into 3 results per row. This is the code I have:
include("connect.php");
$result = mysql_query("SELECT * FROM movies ORDER BY title");
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$id = stripslashes(mysql_result($result,$i,"id"));
$title = stripslashes(mysql_result($result,$i,"title"));
$description = stripslashes(mysql_result($result,$i,"description"));
$length = stripslashes(mysql_result($result,$i,"length"));
$link = stripslashes(mysql_result($result,$i,"link"));
$rating = stripslashes(mysql_result($result,$i,"rating"));
$cover = stripslashes(mysql_result($result,$i,"icover"));
$row .= '<tr><td><a href="view.php?id='.$id.'"><img width=130 height=190 src="images/covers/'.$cover.'" /></a></td><td valign="top">'.$title.'<br />'.$rating.'<br />Run Time: '.$length.'</td><td><a href="update.php?id='.$id.'">Update</a></td><td><a href="delete.php?id='.$id.'">Delete</a></td></tr>';
++$i;
}
}
else {
$row = '<tr><td colspan="2" align="center">Nothing found</td></tr>';
}
mysql_close();
Upvotes: 3
Views: 871
Reputation: 1416
I think this may help you with what you are trying to do. Of course you will need to style the table to meet your needs. You can set the number of columns in the code to whatever fits for you.
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
$mysqli->close();
}
$values = $mysqli->query("SELECT * FROM movies ORDER BY title");
$cols = 3;
$i =1;
echo "<table>".PHP_EOL;
echo "<tr>".PHP_EOL;
while($row = $values->fetch_assoc())
{
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$length = $row['length'];
$link = $row['link'];
$rating = $row['rating'];
$cover = $row['icover'];
if (is_int($i / $cols))
{
echo "<td >".PHP_EOL;
//what ever you want to include goes here
echo "</td>".PHP_EOL;
echo "</tr>".PHP_EOL;
echo "<tr>".PHP_EOL;
}
else
{
echo "<td >".PHP_EOL;
//what ever you want to include goes here
echo "</td>".PHP_EOL;
}
$i++;
}
echo "</tr>".PHP_EOL;
echo "</table>".PHP_EOL;
Upvotes: 0
Reputation: 2678
I think you forget to define the $row over the if statement , in this case your variable will be local variable inside the while only
include("connect.php");
$result = mysql_query("SELECT * FROM movies ORDER BY title");
$num = mysql_num_rows ($result);
$row = '';
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$id = stripslashes(mysql_result($result,$i,"id"));
$title = stripslashes(mysql_result($result,$i,"title"));
$description = stripslashes(mysql_result($result,$i,"description"));
$length = stripslashes(mysql_result($result,$i,"length"));
$link = stripslashes(mysql_result($result,$i,"link"));
$rating = stripslashes(mysql_result($result,$i,"rating"));
$cover = stripslashes(mysql_result($result,$i,"icover"));
$row .= '<tr>
<td><a href="view.php?id='.$id.'"><img width=130 height=190 src="images/covers/'.$cover.'" /></a></td>
<td valign="top">'.$title.'<br />'.$rating.'<br />Run Time: '.$length.'</td>
<td><a href="update.php?id='.$id.'">Update</a></td>
<td><a href="delete.php?id='.$id.'">Delete</a></td>
</tr>';
++$i; }
} else { $row = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; }
mysql_close();
print($row);
Upvotes: 0
Reputation: 3830
As someone pointed out, if you want to do something every Nth time, you generally want a check of the form:
if ( $i > 0 && $i % $N == 0) {
// Do your Nth something here
}
Where $i is your current iteration number, and of course $N is how often you want to break. So in this case, $N = 3, and your breaking logic would go in the body of the if statement.
Having said that, it looks like there may be more involved than just that; your code already has quite a bit going on in your table, as you already have multiple columns per row. Did you really want 3 sets of those multiple columns, or did you mean something else, like row groupings?
Upvotes: 2