Page 2 of 2

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 4:16 pm
by jraede
Wherever you call your function to display the page links, pass mysql_num_rows($result) as the function parameter.

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 4:31 pm
by memsis
i tried it in a few places that i thought i belonged but all brought up parsing errors.

Code: Select all

		//Pagination Function Start			
				function navigate_images($total) {
					global $section, $pg;
					$pages = $total <= $section ? 1 : ceil($total / $section);
				 
					if(trim($_SERVER['QUERY_STRING']) != '') {
						if(stristr($_SERVER['QUERY_STRING'], 'pg='))
							$query = '?'.preg_replace('/pg=d+/', 'pg=', $_SERVER['QUERY_STRING']);
						else
							$query = '?'.$_SERVER['QUERY_STRING'].'&pg=';
					} else
						$query = '?pg=';
				 
					$first = '<a href="'.$_SERVER['PHP_SELF'].$query.'1">First Page</a>';
					$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">Previous</a>';
					$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">Next</a>';
					$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">Last Page</a>';
					
					echo '<div class="navigate_images">';
					echo $pg > 1 ? "$first | $prev |" : 'First Page | Previous |';
					
					// this is to display a max of 9 links
					$begin = $pg < 6 ? 1 : $pg - 4;
					$end = $begin + 8;
					while($end > $pages)
						$end--;
					for($i=$begin; $i<=$end; $i++)
						echo $i == $pg ? ' ['.$i.'] ' : ' <a href="'.$_SERVER['PHP_SELF'].$query.$i.'">'.$i.'</a> ';
					
					echo $pg < $pages ? "| $next | $last" : '| Next | Last Page';
					echo '</div>'."rn";
					
				}
?>
were does it need to go in here or do i need to change anything else other than placing the mentioned line of code in somewere?

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 4:41 pm
by jraede
Where did you put it, and what parsing errors did you get? From what I saw on your site, it looks like you put it in the right place, but didn't pass the $total variable correctly, so it thought there was only one page.

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 4:51 pm
by memsis
i put it just after this -

Code: Select all

				function navigate_images($total) {
					global $section, $pg;
					$pages = $total <= $section ? 1 : ceil($total / $section);
the error i got was Parse error: syntax error, unexpected T_IF in /home/chris_wray88/thechristakeover.com/gallery.php on line 80

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 5:04 pm
by jraede
Your function needs the $total parameter. So you should define $total like I showed you, and then call the function, with $total as its parameter.

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 5:09 pm
by memsis
but i don't know how to do that though. i know you must be getting frustrated, but as i said, im a complete newbie at this. please break it down for me.

step by step, how do i do that?

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 5:16 pm
by jraede
I actually missed a step, but here's what you should do. Define your navigation_images function at the top of the page. Then, when you want to display it, do this:

Code: Select all

// Get the total # of images
$query = "SELECT * FROM images";
$total_images = mysql_num_rows(mysql_query($query));

// Now do the pagination
$page = $_GET['pg'];
if($page > 0) {
     $lim_start = 15 * ($page - 1);
}
else {
     $lim_start = '0';
}
$sql = 'SELECT * FROM images LIMIT '.$lim_start.', 15';
// submit the query
$result = mysql_query($sql) or die (mysql_error()); 
/****************
DISPLAY YOUR IMAGES HOWEVER YOU WANT
****************/

// Show pagination links
navigation_images($total_images);
 
Again, your navigation_images function is pretty confusing, but, assuming it works, this is how you would run it. Run this script, and then if you get an error with your function, we can take it from there.

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 5:24 pm
by memsis
i've added the new code and its brought up a new parse error - Parse error: syntax error, unexpected T_IF in /home/chris_wray88/thechristakeover.com/gallery.php on line 84

the entire page is as follows (thats the html and everything) -

Code: Select all

<?php
include('includes/title.inc.php');
// include MySQL connector function
if (! @include('includes/connection.inc.php'))	{
	echo 'Sorry, database unavailable';
	exit;
}
// create a connection to MySQL
$conn = dbConnect('query');
// Get the total # of images
$query = "SELECT * FROM images";
$total_images = mysql_num_rows(mysql_query($query));

// Now do the pagination
$page = $_GET['pg'];
if($page > 0) {
     $lim_start = 15 * ($page - 1);
}
else {
     $lim_start = '0';
}
$sql = 'SELECT * FROM images LIMIT '.$lim_start.', 15';
// submit the query
$result = mysql_query($sql) or die (mysql_error()); 
// extract the first record as an array

?>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TCTO | Image Gallery</title>
<link href="css/tcto.css" rel="stylesheet" type="text/css" />
<link href="css/tcto-gallery.css" rel="stylesheet" type="text/css" />
<LINK REL="BOOKMARK ICON" HREF="/favicon.ico">

</head>
<body>
	<div id="container">	
      <div id="header">
        
        </div>
    
        <div class="style_linkprim" id="nav">
    	<ul class="class1">
        <li>
        <a href="index.php" target="_self">Home</a>
       	<a href="gallery.php" target="_self">Gallery</a>
       	<a href="upload.php">Upload Yourself</a>
        <a href="contact.php" target="_self">Contact Me</a>
       	</li>
       	</ul>
      </div>
        
  <div id="article">
    <h1 class="h1">Image Gallery<span class="p"><a name="back_to_top"></a></span></h1>
    <p class="p">A variety of Chris's from a variety of places</p>
        <div id="maincontent">
        	<div id="gallery">
        		<div class="p" id="main_image">
 <!--               <a href="http://www.thechristakeover.com/portfolio/"><img src="images/tcto_me.jpg" width="400" height="300" border="0"></a>
                <p class="p">Chris Wray, Doagh</p>-->               
<?php
			//Image call start	
				while($row = mysql_fetch_assoc($result)){
					//get the name and caption for the main image
					$mainImage = $row['filename'];
					$caption = $row['caption'];
					// get the dimensions of the main image
					$imageSize = getimagesize('uploads/'.$mainImage);
					
					echo "<p><img src='uploads/".$mainImage."' alt='".$caption."' ".$imageSize[3]." /></p>";
					echo "<p>".$caption."</p>";
				}
				
			//Pagination Function Start
				navigation_images($total_images);
					function navigate_images($total) {
					global $section, $pg;
					$pages = $total <= $section ? 1 : ceil($total / $section);
					
					$total = mysql_num_rows($result) 
				 
					if(trim($_SERVER['QUERY_STRING']) != '') {
						if(stristr($_SERVER['QUERY_STRING'], 'pg='))
							$query = '?'.preg_replace('/pg=d+/', 'pg=', $_SERVER['QUERY_STRING']);
						else
							$query = '?'.$_SERVER['QUERY_STRING'].'&pg=';
					} else
						$query = '?pg=';
				 
					$first = '<a href="'.$_SERVER['PHP_SELF'].$query.'1">First Page</a>';
					$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">Previous</a>';
					$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">Next</a>';
					$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">Last Page</a>';
					
					echo '<div class="navigate_images">';
					echo $pg > 1 ? "$first | $prev |" : 'First Page | Previous |';
					
					// this is to display a max of 9 links
					$begin = $pg < 6 ? 1 : $pg - 4;
					$end = $begin + 8;
					while($end > $pages)
						$end--;
					for($i=$begin; $i<=$end; $i++)
						echo $i == $pg ? ' ['.$i.'] ' : ' <a href="'.$_SERVER['PHP_SELF'].$query.$i.'">'.$i.'</a> ';
					
					echo $pg < $pages ? "| $next | $last" : '| Next | Last Page';
					echo '</div>'."rn";
					
				}
?>
                </div>
           	</div>
            
            	<p class="p"><?php echo navigate_images($pg, $pages, $next, $last); ?></p>
        </div>
        		<p class="p"><a href="#back_to_top" class="p">Back to top</a></p>
  </div>
</body>
</html>

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 5:38 pm
by jraede
You're getting that error because you didn't put a semicolon at the end of line 82. However, you shouldn't even have that line, because your function itself requires $total as a parameter; you shouldn't be defining it again within the function if you've already defined it outside of the function.

A few other problems with your function. Where are you setting the global variables $section and $page? Seems to me like you should replace them with '15' and $page, respectively.

Also, you're trying to execute the function navigation_images() before you're defining it. Move the function definition (lines 78-11) to the top of the page, get rid of what was on line 82 ($total = mysql_num_rows($result)) and then tell me what happens.

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 5:46 pm
by memsis
ok, i think iv done everything you asked.

now i the error i get is 'Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/chris_wray88/thechristakeover.com/gallery.php on line 69'

my entire page of code looks like this now -

Code: Select all

<?php
include('includes/title.inc.php');
// include MySQL connector function
if (! @include('includes/connection.inc.php'))	{
	echo 'Sorry, database unavailable';
	exit;
}
// create a connection to MySQL
$conn = dbConnect('query');
// Get the total # of images
$query = "SELECT * FROM images";
$total_images = mysql_num_rows(mysql_query($query));

// Now do the pagination
$page = $_GET['pg'];
if($page > 0) {
     $lim_start = 15 * ($page - 1);
}
else {
     $lim_start = '0';
}
$sql = 'SELECT * FROM images LIMIT '.$lim_start.', 15';
// submit the query
$result = mysql_query($sql) or die (mysql_error()); 
// extract the first record as an array

?>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TCTO | Image Gallery</title>
<link href="css/tcto.css" rel="stylesheet" type="text/css" />
<link href="css/tcto-gallery.css" rel="stylesheet" type="text/css" />
<LINK REL="BOOKMARK ICON" HREF="/favicon.ico">

</head>
<body>
	<div id="container">	
      <div id="header">
        
        </div>
    
        <div class="style_linkprim" id="nav">
    	<ul class="class1">
        <li>
        <a href="index.php" target="_self">Home</a>
       	<a href="gallery.php" target="_self">Gallery</a>
       	<a href="upload.php">Upload Yourself</a>
        <a href="contact.php" target="_self">Contact Me</a>
       	</li>
       	</ul>
      </div>
        
  <div id="article">
    <h1 class="h1">Image Gallery<span class="p"><a name="back_to_top"></a></span></h1>
    <p class="p">A variety of Chris's from a variety of places</p>
        <div id="maincontent">
        	<div id="gallery">
        		<div class="p" id="main_image">
 <!--               <a href="http://www.thechristakeover.com/portfolio/"><img src="images/tcto_me.jpg" width="400" height="300" border="0"></a>
                <p class="p">Chris Wray, Doagh</p>-->               
<?php
			function navigate_images($total) {
					global $section, $pg;
					$pages = $total <= $section ? 1 : ceil($total / $section);
			
			//Image call start	
				while($row = mysql_fetch_assoc($result)){
					//get the name and caption for the main image
					$mainImage = $row['filename'];
					$caption = $row['caption'];
					// get the dimensions of the main image
					$imageSize = getimagesize('uploads/'.$mainImage);
					
					echo "<p><img src='uploads/".$mainImage."' alt='".$caption."' ".$imageSize[3]." /></p>";
					echo "<p>".$caption."</p>";
				}
				
			//Pagination Function Start
				 
					if(trim($_SERVER['QUERY_STRING']) != '') {
						if(stristr($_SERVER['QUERY_STRING'], 'pg='))
							$query = '?'.preg_replace('/pg=d+/', 'pg=', $_SERVER['QUERY_STRING']);
						else
							$query = '?'.$_SERVER['QUERY_STRING'].'&pg=';
					} else
						$query = '?pg=';
				 
					$first = '<a href="'.$_SERVER['PHP_SELF'].$query.'1">First Page</a>';
					$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">Previous</a>';
					$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">Next</a>';
					$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">Last Page</a>';
					
					echo '<div class="navigate_images">';
					echo $pg > 1 ? "$first | $prev |" : 'First Page | Previous |';
					
					// this is to display a max of 9 links
					$begin = $pg < 6 ? 1 : $pg - 4;
					$end = $begin + 8;
					while($end > $pages)
						$end--;
					for($i=$begin; $i<=$end; $i++)
						echo $i == $pg ? ' ['.$i.'] ' : ' <a href="'.$_SERVER['PHP_SELF'].$query.$i.'">'.$i.'</a> ';
					
					echo $pg < $pages ? "| $next | $last" : '| Next | Last Page';
					echo '</div>'."rn";
					
				}
?>
                </div>
           	</div>
            
            	<p class="p"><?php echo navigate_images($pg, $pages, $next, $last); ?></p>
        </div>
        		<p class="p"><a href="#back_to_top" class="p">Back to top</a></p>
  </div>
</body>
</html>

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 5:59 pm
by jraede
You're getting that error because you're trying to access $result from within a function - you'd need to define it as global within the function. However, this is irrelevant - Why did you put the code for displaying the images inside the navigate_images function? Put it back to how it was, just bring the entire function to the top of the page, and you can then call the function underneath where you display the images.

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 6:05 pm
by memsis
iv moved that code back again and now everything displays but the navigation still isn't working as it should
http://www.thechristakeover.com/gallery.php

if you scroll to the bottom of the page and try to use the next button youl see what i mean. both the next link and last page link go from page 1 to 11. do you know how to fix it so it works as it should?

all of the navigation code is below -

Code: Select all

				function navigate_images($total) {
					global $section, $pg;
					$pages = $total <= $section ? 1 : ceil($total / $section);
			
				 
					if(trim($_SERVER['QUERY_STRING']) != '') {
						if(stristr($_SERVER['QUERY_STRING'], 'pg='))
							$query = '?'.preg_replace('/pg=d+/', 'pg=', $_SERVER['QUERY_STRING']);
						else
							$query = '?'.$_SERVER['QUERY_STRING'].'&pg=';
					} else
						$query = '?pg=';
				 
					$first = '<a href="'.$_SERVER['PHP_SELF'].$query.'1">First Page</a>';
					$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">Previous</a>';
					$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">Next</a>';
					$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">Last Page</a>';
					
					echo '<div class="navigate_images">';
					echo $pg > 1 ? "$first | $prev |" : 'First Page | Previous |';
					
					// this is to display a max of 9 links
					$begin = $pg < 6 ? 1 : $pg - 4;
					$end = $begin + 8;
					while($end > $pages)
						$end--;
					for($i=$begin; $i<=$end; $i++)
						echo $i == $pg ? ' ['.$i.'] ' : ' <a href="'.$_SERVER['PHP_SELF'].$query.$i.'">'.$i.'</a> ';
					
					echo $pg < $pages ? "| $next | $last" : '| Next | Last Page';
					echo '</div>'."rn";
					
				}

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 6:23 pm
by jraede
I think you need to do some reading on PHP variables. You're using $pg in your function...where does $pg come from? You set a variable called $page when you were doing pagination - is that what you want instead of $pg? Also, I'm still not sure if you're defining $section. If you're going to use variables, make sure you declare and define them. They don't get set automatically; you have to physically set them.

Re: PHP based Pagination

Posted: Tue Apr 27, 2010 6:25 pm
by memsis
ok. ill have a read through it and see if i can get it working. thanks very much for taking the time to go through it all with me. its very much appreciated :mrgreen: