<<PREV 1 2 3 NEXT>> Error [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

<<PREV 1 2 3 NEXT>> Error [Solved]

Post by Dm7 »

Okay, it used to work before I added the prev/next pages feature (a tutorial from http://www.phpfreaks.com).
Now I get..

Code: Select all

No images available!
But I have 10 images saved in the database... it acts like it doesn't even recognize one row... even though it worked fine before the next/prev changes. Now what did I do wrong?

Code: Select all

<?php
#########################################
#       Admin Gallery System            #
#          gallery.php                  #
#########################################
include("gallery/functions_main.inc.php");
include("gallery/fields_login.inc.php");
include("gallery/gallery.css");
$table = "art";
$max_results = 8;


// if there's page number, use it
// if not, set one
if(!isset($_GET['page'])) {
	$n = 1;
} else {
	$n = $_GET['page'];
}

// Figure out the limit for the query based
//on the current page number.
$from = (($n * $max_results) - $max_results);

// connect to db and limiting it to $max_results
Connect_to_db("Vars.inc.php");
$sql = "SELECT * FROM $table ORDER BY date DESC LIMIT $from, $max_results";
$result = mysql_query($sql);
    if (!$result) {
      echo("<P>Error performing query: " .
           mysql_error() . "</P>");
      exit();
    }
?>
 
<p class="style3" align="left">Art Gallery</p>
<p align="center"><span class="style7"><strong>Notice: </strong></span>Clicking on a picture will open in a new window!</p>
<?php
// checks if there is available data in the database.
if ( mysql_num_rows($result) == 0 ) {
echo "<h2><center><font color='red'>No images available!</font></h2></center>"; }
else { ?>
<table border="0" align="center" cellpadding="5" cellspacing="5" width="80%">
<?php
// loops with remainder for table column calucation.
$x = 0;
while ( $row = mysql_fetch_array($result) ) {
		if ($x % 4 == '0') { echo "<tr align='center'>"; }
		?> 
		<td align="center" class="gallerytb"> 
		  <span class="style1"><?php echo("<a href='gallery/view.php?pix=" .$row["name"] . "' target='_new'><img border='0' src='" . $row["tbimage"] . "' alt='" . $row["title"] . "'></a><br><br><div class='titlebg'><b>" . $row['title'] . "</b></div>");
		$x = $x + 1; ?></span> </td> 
		<?php
}
?>
</tr></table> 
<?php } ?>
<br>
<?php
echo "<center>";
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM $table"),0);

// Figure out the total number of pages.
$total_pages = ceil($total_results / $max_results);

// Build Previous Link
if($n > 1) {
	$prev = ($n - 1);
	echo "<a href='" . $_SERVER['PHP_SELF'] ."?page=gallery&n=$prev'><<Previous</a>";
}

for($i = 1; $i <= $total_pages; $i++) {
	if (($n) == $i) {
		echo "$i ";
		} else {
			echo "<a href='". $_SERVER['PHP_SELF'] ."?page=gallery&n=$i'>$i</a> ";
	}
}

// Build Next Link
if ($n < $total_pages) {
	$next = ($n + 1);
	echo "<a href='". $_SERVER['PHP_SELF'] ."?page=gallery&n=$next'>NEXT>></a>";
}
echo "</center>";
mysql_close();
?>
<span align="center"><?php echo $page['bottom']; ?>
Last edited by Dm7 on Mon Oct 17, 2005 5:27 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

If n = 1, max_results = 8, then from = ( 1 * 8 ) - 8 = 0
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post by Dm7 »

well Limit was 0 to 8... that's what I wanted.
And when I just left it alone.. it'd be 8 to 8... and I got images there alright.. but the pages didn't work right.. (like 1 for 1-8 and 2 for 8-16... it stayed at 1-8 even though when it's on page 2... o_0 so I must did something wrong)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

echo $sql on every page. The problem is almost certainly with your limits.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post by Dm7 »

hehe thanks, I gotta do $sql debug more often. One variable was wrong... it ruined all lol. It works now so it's solved. Thanks again.
Post Reply