Jump to content

How to get pictures to start on a new line


thereaper87

Recommended Posts

Hello there,

 

I am working on a pagination script and have hit a bit of a wall. Here is the script:

<?php include("header.php"); ?>
<?php


$host = "localhost"; //

$user = ""; //username to connect to database

$pass = ""; //password to connect to database

$db = ""; //the name of the database



mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());

mysql_select_db($db) or die("ERROR DB:".mysql_error()); 





$max = 25; //amount of articles per page.

$p = $_GET['p'];

if(empty($p))

{

$p = 1;

}

$limits = ($p - 1) * $max; 

//view the article!

if(isset($_GET['act']) && $_GET['act'] == "view")

{

$id = $_GET['id'];

$sql = mysql_query("SELECT * FROM 3dpics WHERE id = '$id'");

while($r = mysql_fetch_array($sql))

{

$title = $r['title'];

$url = $r['url'];


echo "<div align='center'><img src='$url' width='500' height='500'><br />$title<br /><br /><a href='test.php'>Back</a></div>";

}



}else{



//view all the articles in rows

$sql = mysql_query("SELECT * FROM 3dpics LIMIT ".$limits.",$max") or die(mysql_error());

//the total rows in the table

$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM 3dpics"),0);	


$totalpages = ceil($totalres / $max);
    
echo "<table name='myTable' cellpadding='5' cellspacing='5'>";
echo "<tr>";
//the table


while($r = mysql_fetch_array($sql))

{

$id = $r['id'];

$title = $r['title'];

$url = $r['url'];
echo "<div id='piccontentalign' align='center'>";
echo "<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>";
}
echo "</tr></table></div>";
//close up the table


for($i = 1; $i <= $totalpages; $i++){ 

//this is the pagination link

echo "<a href='test.php?p=$i'>$i</a>|";

}

}



?>

 

All this does is pull the url's of the images out of the database. But, it all shows up on 1 line across the screen! I tried echoing the rows instead, but then it just shows up as 1 line going horizontal. So i'm trying to figure out a way to get 5 pictures per row.

This is the code that displays them on the main page:

echo "<table name='myTable' cellpadding='5' cellspacing='5'>";
echo "<tr>";
//the table


while($r = mysql_fetch_array($sql))

{

$id = $r['id'];

$title = $r['title'];

$url = $r['url'];
echo "<div id='piccontentalign' align='center'>";
echo "<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>";
}
echo "</tr></table></div>";
//close up the table

Thanks for all your help!

Link to comment
Share on other sites

You need to use the modulus operator to add table rows. Something like this:

 

<?php
$numCols = 5;
$counter = 1;

echo "<table>";
echo "<tr>";

while(...) {

echo "<td>Cell content</td>"

if ($resultNum % $numCols == false) {
	echo "</tr>";
	echo "<tr>";
} // end if

} // end while

echo "</tr>";
echo "</table>";

?>

 

I've simplified it a bit to try and make it clearer.

 

Here is another very simple example of the modulus operator:

<?php
for ($i = 1; $i < 50; $i++) {
echo "<p>$i</p>";

if ($i % 5 == false) {
	// This will appear every 5 lines
	echo "<b>++++++++++</b>";
}
}
?>

 

Make sure the counter doesn't start at 0 because then the code in if statement will run as it will be true.

 

http://php.net/manual/en/language.operators.arithmetic.php

Link to comment
Share on other sites

Thank you for the reply! Here is what I've changed the code to:

$numCols = 5;
$counter = 1;

echo "<table>";
echo "<tr>";
//the table


while($r = mysql_fetch_array($sql))

{

$id = $r['id'];

$title = $r['title'];

$url = $r['url'];
echo "<td>Cell content</td>";
echo "<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>";

if ($resultNum % $numCols == false) {
	echo "</tr>";
	echo "<tr>";
} // end if

} // end while

echo "</tr>";
echo "</table></div>";

Is this correct? I hope not. It is just displaying a single horizontal line down the page.

Link to comment
Share on other sites

I forgot to add the counter incrementation to the end of the while loop :P :

 

$numCols = 5;
$counter = 1;

echo "<table>";
echo "<tr>";
//the table


while($r = mysql_fetch_array($sql))

{

$id = $r['id'];

$title = $r['title'];

$url = $r['url'];
echo "<td>Cell content</td>";
echo "<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>";

if ($resultNum % $numCols == false) {
	echo "</tr>";
	echo "<tr>";
} // end if

$counter++; // This line

} // end while

echo "</tr>";
echo "</table></div>";

Link to comment
Share on other sites

Check against $counter, not $resultNum

 

$numCols = 5;
$counter = 1;

echo "<table>";
echo "<tr>";
//the table

while($r = mysql_fetch_array($sql))  {

$id = $r['id'];
$title = $r['title'];
$url = $r['url'];
echo "<td>Cell content</td>";
echo "<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>";
if ($counter % $numCols == 0) {
echo "</tr>";
echo "<tr>";
} // end if

$counter++; // This line

} // end while

echo "</tr>";
echo "</table></div>"

 

 

Link to comment
Share on other sites

This should work:

 

<?php
$host = "localhost"; //

$user = ""; //username to connect to database

$pass = ""; //password to connect to database

$db = ""; //the name of the database

mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());

mysql_select_db($db) or die("ERROR DB:".mysql_error()); 

$max = 25; //amount of articles per page.

$p = $_GET['p'];

if(empty($p)) {
$p = 1;
}

$limits = ($p - 1) * $max; 

//view the article!

if(isset($_GET['act']) && $_GET['act'] == "view") {
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM 3dpics WHERE id = '$id'");

while($r = mysql_fetch_array($sql)) {
	$title = $r['title'];
	$url = $r['url'];
	echo "<div align='center'><img src='$url' width='500' height='500'><br />$title<br /><br /><a href='test.php'>Back</a></div>";
}



} else {

//view all the articles in rows
$sql = mysql_query("SELECT * FROM 3dpics LIMIT ".$limits.",$max") or die(mysql_error());

//the total rows in the table
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM 3dpics"),0);	

$totalpages = ceil($totalres / $max);

//
$numCols = 5;
$counter = 1;
//

echo "<table name='myTable' cellpadding='5' cellspacing='5'>";
echo "<tr>";

//the table
while($r = mysql_fetch_array($sql)) {
	$id = $r['id'];
	$title = $r['title'];
	$url = $r['url'];
	echo "<div id='piccontentalign' align='center'>";
	echo "<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>";

	//
	if ($counter % $numCols == false && $counter != $max) {
		echo "</tr>";
		echo "<tr>";
	}

	$counter++;
	//
}

echo "</tr></table></div>";

//close up the table
for($i = 1; $i <= $totalpages; $i++) {
//this is the pagination link
	echo "<a href='test.php?p=$i'>$i</a>|";
}
}
?>

 

The extra lines I added are in between empty comments. I also added an extra argument in the if statement to prevent an extra table row being added on the end.

 

It should give you markup something like:

 

<table name='myTable' cellpadding='5' cellspacing='5'>
<tr>

<!-- while loop -->
	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

<!-- if statement -->
</tr>
<tr>
<!-- end if statement -->

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

<!-- if statement -->
</tr>
<tr>
<!-- end if statement -->

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>

	<div id='piccontentalign' align='center'>
	<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>
<!-- endwhile loop -->

</tr>
</table>

Link to comment
Share on other sites

Also, on a side note, your code has a couple of other small errors :examine: :

 

echo "<div id='piccontentalign' align='center'>";
echo "<td><img src='$url' width='225' height='225'><br /><div align='center'><a href='test.php?act=view&id=$id'>$title</a></div></td>";
echo "</div>"; // This closes the <div id='piccontentalign' align='center'>. But wrapping a table cell in a div isn't valid, I'm sure you don't need it 

} // end while
echo "</tr></table></div>"; // The </div> is erroneous

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.