Jump to content

Pagination


fly757

Recommended Posts

Hello.  I am using a script I bought back in January. I am new to PHP and have been learning it ever since.  I have been able to modify the script quite a bit, however there are a few problems I am still struggling with.  One of those problems is my page link setup.  I will try to explain this as easy as possible through walking you through visually what my problem is:

 

Click this URL:

 

www.airplanephotozone.com/photos_main.php

 

Scroll to the bottom leaving all the drop downs on "All aircraft, all airlines..etc"

 

Hit the search button. Notice I have up to 32 pages displayed.  I want to have that broken up so on all searches the max number of pages displayed is 10.  Essentially in the future if you hit search under all categories it would display:

 

First | Previous 1 2 3 4 5 6 7 8 9 10 Next | Last

 

Where the first would obviously be page 1, the next would take you to pages 11-20, and the last button would take you to the last page of available results whether it be page 29 or 500.

 

Here is my existing code for the search results page:

 

<?php

define("PER_PAGE", 20);

$results = gsearchresults();


$i = 0;
$j = 0;
$page = 1;
$pages = ceil(sizeof($results)/PER_PAGE);

if(isset($_GET["page"]))
	$page = (int)$_GET["page"];

if($page == 1)
{
	$start = 1;
	if(($start + (PER_PAGE-1)) < sizeof($results))
	{
		$end = $start + (PER_PAGE-1);
	}
	else
	{
		$end = sizeof($results);
	}
}
else
{
	$start = ($page-1)*PER_PAGE + 1;
	if(($start + (PER_PAGE-1)) < sizeof($results))
	{
		$end = $start + (PER_PAGE-1);
	}
	else
	{
		$end = sizeof($results);
	}
}

echo "<!-- search_grid: output a 4x1 row containing search results -->";
echo "<tr>";

$link = "search.php?";

foreach($_GET as $k=>$v) {
	if($k != "page")
		$link .= $k . "=" . $v . "&";
}

$spages = "";

for($k = 1; $k <= $pages; $k++)
{
	if($page == $k)
		$spages .= $k . " | ";
	else
		$spages .= "<a href='" . $link . "page=" . $k . "'>" . $k . "</a> | ";			
}

$spages = eregi_replace("\| $", "", $spages);

if(sizeof($results) > 0) {
	printf(" <tr><td align=\"left\" class=\"heading\" width=\"200px\"><a href=\"http://www.airplanephotozone.com/photos_main.php\" style=\"text-decoration:none\"><b>New Search</b></a></td>");
	printf(" <td align=\"center\" class=\"heading\">Displaying Photos %d to %d of %d</td>", $start, $end, sizeof($results));
	printf(" <td align=\"right\" class=\"heading\" width=\"200px\"> </td></tr>");	
	{
	if(sizeof($results) > 20)
		printf(" <tr><td align=\"center\" colspan=\"3\" class=\"heading\" align=\"right\">Pages: %s   </td></tr>", $spages);
	else
		echo "";
	}
}
else
	echo " <td colspan=\"3\" class=\"heading\">No Photos Found</td>";
echo "</tr>";

 

This problem has been bothering me for months! Any help please!!!

 

Thanks,

Ben

Link to comment
Share on other sites

Not necessarily trying to save space.  Just trying to clean it up.  Having lots of page results return will be a lot, especially when there are a lot more photos on there.

 

Any ideas on how to do this?

 

Ben

Link to comment
Share on other sites

Well, for starters, you can very easily link to the first page by passing in a static link like this:

<?php
echo "<a href=\"?page=1\">First</a>\n";
?>

 

If you are accurately throwing your page numbering, the next and previous would also be very easy:

<?php
// default to page 1
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;

// First
echo "<a href=\"?page=1\">First</a>\n";

// Previous
if ($page > 1) echo "<a href=\"?page=" . ($page - 1) . ">Prev</a>\n";
else echo "Prev\n";

// Do all your pages here

// Next
if ($page < $max_page) echo "<a href=\"?page=" . ($page + 1) . ">Next</a>\n";
else "Next\n";

// Last
echo "<a href=\"?page={$max_page}\">Last</a>\n";
?>

 

Good luck.

Link to comment
Share on other sites

but what your asking. to have a maximum of 10 pages would mean that the amount on each page would considerably increase, which would be bad for slow-connection users. i think that have more pages would be a lot easier. just add the pages to a drop down box or something.

Link to comment
Share on other sites

ProjectFear,

 

I think you are misunderstanding what I want.  I get what you are saying that having a maximum of 10 pages would be a lot of results per page. I want only 10 pages available at a time. So that would mean when you do a search say 700 results are returned.  When you search, at first there are pages 1-10 displayed with the first,previous,next, and last links accordingly. So page 1, results 1-20 would be displayed, page 2 results 11-20 etc...Then once you hit page 10, you would have to click the "Next" link which would link you to pages 11-20 in the results which would be 201-220 i believe. Then that would go thru to page 20, then the next link would take you to pages 31-40...etc etc.  Is that the misunderstanding? I don't want 10 pages total, yes that would be a lot of results per page.

 

Ben

Link to comment
Share on other sites

Why not display the current 10 pages at a time instead? If you do it that way, the list of page numbers can scroll as you jump from page to page:

<?php
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;

$first = $page - 5;
$last = $page + 5;

for ($i = $first; $i <= $last; $i++) {
  if ($i > 0 && $i <= $max_pages) {
    if ($i == $page) echo " $i ";
    else echo "<a href=\"?page={$i}\">$i</a>\n";
  }
}
?>

 

If you calculate your page numbering from the currently selected page like that, you can always show the previous 5 and next 5 pages instead of only showing in groups of 10.

Link to comment
Share on other sites

Obsidian,

 

That is a great idea.  I like it a lot. Thank you for the idea.  As I mentioned in the first post, I am still new to PHP.  I have placed your code on the page with the code I provided with the first post.  It is telling me I have $max_pages as an undefined variable?  Is there a variable already in the code in the first post that would be what is already defined as $max_pages ?  Or is there anywhere else I would need to define this, such as in the functions or at the top of the page?

 

Any help appreciated, new to PHP. I really truly appreciate it.

 

Ben

Link to comment
Share on other sites

Obsidian,

 

I think I may have got it to work. If I did I am very impressed with myself.

 

Take a look and let me know if u see any flaws. Click on "Ben Graden" on the right hand side under photographers. Here you will see the results displayed. The pagination change is in the upper left hand corner.

 

http://www.airplanephotozone.com/photos_main.php

Link to comment
Share on other sites

Woops,

 

One little problem overlooked.  I seem to have not stopped the page limit at the end of the total returned results.  If you hit the "search" button on http://www.airplanephotozone.com/photos_main.php and leaving all the drop downs on the page in the "All" position you will return 32 pages (visible with the old pagination).  At the top left of the page you see the new pagination showing only pages 1-4.  When you click on page 32 underneath "Displaying Photos etc" you will see the upper left pagination change with it. However, the pages keep going up to 33, 34, 35, etc...and I don't have enough results that it needs to go that high.  I will paste the code I am working with below.  You should be able to see which pagination is which:

 

<?php

define("PER_PAGE", 20);

$results = gsearchresults();
---------------------------------------------------------------------
// NEW PAGINATION YOU PROVIDED ME WITH
$link = "search.php?";
foreach($_GET as $k=>$v) {
	if($k != "page")
		$link .= $k . "=" . $v . "&";
}

$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
$pages = ceil(sizeof($results)/PER_PAGE);

$start = $page - 3;
$end = $page + 3;

for ($k = $start; $k <= $end; $k++) {
  	if ($k > 0 && $k <= $end) {
    if ($k == $page) echo " $k ";
    else echo "<a href='" . $link . "page=" . $k . "'>" . $k . "</a>\n";
  }
}
-----------------------------------------------------------------------
//OLD PAGINATION

$i = 0;
$j = 0;
$page = 1;
$pages = ceil(sizeof($results)/PER_PAGE);

if(isset($_GET["page"]))
	$page = (int)$_GET["page"];

if($page == 1)
{
	$start = 1;
	if(($start + (PER_PAGE -1)) < sizeof($results))
	{
		$end = $start + (PER_PAGE +1);
	}
	else
	{
		$end = sizeof($results);
	}
}
else
{
	$start = ($page-1)*PER_PAGE + 1;
	if(($start + (PER_PAGE-1)) < sizeof($results))
	{
		$end = $start + (PER_PAGE-1);
	}
	else
	{
		$end = sizeof($results);
	}
}

echo "<!-- search_grid: output a 4x1 row containing search results -->";
echo "<tr>";

$link = "search.php?";

foreach($_GET as $k=>$v) {
	if($k != "page")
		$link .= $k . "=" . $v . "&";
}

$spages = "";

for($k = 1; $k <= $pages; $k++)
{
	if($page == $k)
		$spages .= $k . " | ";
	else
		$spages .= "<a href='" . $link . "page=" . $k . "'>" . $k . "</a> | ";			
}

$spages = eregi_replace("\| $", "", $spages);
--------------------------------------------------------------------------------------------------

if(sizeof($results) > 0) {
	printf(" <tr><td align=\"left\" class=\"heading\" width=\"200px\"><a href=\"http://www.airplanephotozone.com/photos_main.php\" style=\"text-decoration:none\"><b>New Search</b></a></td>");
	printf(" <td align=\"center\" class=\"heading\">Displaying Photos %d to %d of %d</td>", $start, $end, sizeof($results));
	printf(" <td align=\"right\" class=\"heading\" width=\"200px\"> </td></tr>");	
	{
	if(sizeof($results) > 20)
		printf(" <tr><td align=\"center\" colspan=\"3\" class=\"heading\" align=\"right\">Pages: %s   </td></tr>", $spages);
	else
		echo "";
	}
}
else
	echo " <td colspan=\"3\" class=\"heading\">No Photos Found</td>";
echo "</tr>";

echo "<tr>";

if(sizeof($results) > 0) {
	echo " <td colspan=\"3\">";
	echo "<table width=800px height=150px cellspacing=\"0\" cellpadding=\"0\">";

	foreach($results as $photo) {
		$i++;
		$j++;

		if($i >= $start && $i <= $end)
		{
			printf("<tr><td valign=\"top\" width=\"50%%\" class=\"grid border\"><center>
<table border=0 width=700px bgcolor=#5B5B5B><tr><td width=150px><div class='date' align='center'>      Total Photo Views: " . $photo["views"] . "</div></td>
<td valign=\"top\" rowspan=2 width=550px colspan=3><table border=0px width=550px height=165px cellspacing=0px cellpadding=0px bgcolor=#666666 bordercolor=#666666>
<td valign=\"top\" width=\"274px\" bgcolor=#777777><font face=Verdana size=1 color=#FFFFFF><center>Photo ID: 000" . $photo["photoid"] . "</center></font></td><td width=\"2px\" bgcolor=#5B5B5B></td>
<td valign=\"top\" width=\"274px\" bgcolor=#777777><font face=Verdana size=1 color=#FF0000><center>" . $photo["buyprint"] . "</center></font></td>
<tr><td valign=\"top\" width=\"274px\" bgcolor=#666666><font face=Verdana size=2 color=#CC992E><center><b>Airline / Type</b></center></font></td><td width=\"2px\" bgcolor=#5B5B5B></td>
<td valign=\"top\" width=\"274px\" bgcolor=#666666><font face=Verdana size=2 color=#CC992E><center><b>Location / Date</b></center></font></td>
<tr><td valign=\"top\" width=\"274px\" bgcolor=#777777><font face=Verdana size=1 color=#FFFFFF><a title='See more " . $photo["airline"] . " photos' href=\"http://www.airplanephotozone.com/search.php?Airline=" . $photo["airlines"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["airline"] . "</a>
<br><a title='See more " . $photo["airline2"] . " photos' href=\"http://www.airplanephotozone.com/search.php?Airline=" . $photo["airline2s"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["airline2"] . "</a><br><br><a title='See more " . $photo["aircrafts"] . " photos' href=\"http://www.airplanephotozone.com/search.php?Aircraft=" . $photo["aircrafts"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["aircraft"] . "</a></font></td><td width=\"2px\" bgcolor=#5B5B5B></td>
<td valign=\"top\" width=\"274px\" bgcolor=#777777><font face=Verdana size=1 color=#FFFFFF><a title='See more photos from " . $photo["airport"] . "'href=\"http://www.airplanephotozone.com/search.php?Airport=" . $photo["airports"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["airport"] . "</a>
<br><a title='See more photos from " . $photo["nation"] . "'href=\"http://www.airplanephotozone.com/search.php?keywords=" . $photo["nations"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["nation"] . "</a>
<a title='See more photos from " . $photo["states"] . "' href=\"http://www.airplanephotozone.com/search.php?keywords=" . $photo["states"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["state"] . "</a>
<br><br><a title='See more photos taken on " . $photo["date"] . "' href=\"http://www.airplanephotozone.com/search.php?keywords=" . $photo["date"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["date"] . "</a></font></td> 
<tr><td valign=\"top\" width=\"274px\" bgcolor=#666666><font face=Verdana size=2 color=#CC992E><center><b>Registration</b></center></font></td><td width=\"2px\" bgcolor=#5B5B5B></td><td valign=\"top\" width=\"274px\" bgcolor=#666666><font face=Verdana size=2 color=#CC992E><center><b>Photographer</b></center></font></td>
<tr><td valign=\"top\" width=\"274px\" bgcolor=#777777><font face=Verdana size=1 color=#FFFFFF><a title='See more photos of aircraft " . $photo["registration"] . "' href=\"http://www.airplanephotozone.com/search.php?keywords=" . $photo["registration"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["registration"] . "</a></font></td><td width=\"2px\" bgcolor=#5B5B5B></td>
<td valign=\"top\" width=\"274px\" bgcolor=#777777><font face=Verdana size=1 color=#FFFFFF><a title='See more photos by " . $photo["photosby"] . "' href=\"http://www.airplanephotozone.com/search.php?Photographer=" . $photo["contact"] . "\" onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $photo["photog"] . "</a>
<br><br><A title='Contact photographer " . $photo["contact"] . "' HREF=\"javascript:popUp('http://www.airplanephotozone.com/email/" . $photo["contact"] . ".php')\">Contact</a></font></td>


</table></td></tr><tr><td style='padding-right:0px'><a title='Click to view full size photo by " . $photo["contact"] . "' target=_blank href=\"%s\"><img src=\"photos/med_%s\" width=\"200\" border=\"0\" />", glink($photo["photoid"], LINK_PHOTO), $photo["filename"]);

		echo "<br /></a></td></tr><hr width=760px><tr><td width=150px height=15px><div class='date' align='center'>
		<font face='Verdana' size=1>Uploaded: " . date($GLOBALS["gallarific_recent_date_format"], $photo["dateuploaded"]) . "</font></div></td>
<td valign=\"top\" align=\"center\" height=15px width=225px><font size=1 color=#FFFFFF><a href=\"http://www.airplanephotozone.com/airport/" . $photo["ainfo"] .".php\"  target=\"_blank\" style=\"text-decoration: none\"><font size=1 color=#FFFFFF>
Airport Info</font></a>  <font color=\"#777777\"><b>|</b></font> <a target=_blank href=correct.php?id=" . $photo["photoid"] . " style=\"text-decoration:none\"><font size=1 color=#FFFFFF>
Correct Photo Info</font></a></td><td valign=\"top\" align=\"center\" height=\"15px\" width=\"225px\"><A HREF=\"javascript:popUp('http://www.airplanephotozone.com/profile/" . $photo["contact"] . ".php') \" style=\"text-decoration:none\"><font face=\"Verdana\" color=\"#FFFFFF\" size=\"1\">Photographer Profile</font></td></tr></table></td></tr>";
		}
	}

	echo "</table>";

}
else {
	echo " <td class=\"pad\" colspan=\"3\">";
	echo "Your search returned no photos.";
	echo "<br><br><center><a href=\"http://www.airplanephotozone.com/photos_main.php\" style=\"text-decoration:none\"><font color=\"#CCCCCC\" size=\"2\"><b>New Search</b></font></a></center>";
}

echo " </td>";
echo "</tr>";

if(sizeof($results) > 0) {
	printf(" <tr><td align=\"left\" class=\"heading\" width=\"200px\"><a href=\"http://www.airplanephotozone.com/photos_main.php\" style=\"text-decoration:none\"><b>New Search</b></a></td>");
	printf(" <td align=\"center\" class=\"heading\">Displaying Photos %d to %d of %d</td>", $start, $end, sizeof($results));
	printf(" <td align=\"right\" class=\"heading\" width=\"200px\"> </td></tr>");
	{
	if(sizeof($results) > 20)
		printf(" <tr><td align=center colspan=3 class=heading align=right>Pages: %s   </td></tr>", $spages);
	else
		echo "";
	}
}
else
	echo " <td colspan=\"3\" class=\"heading\">No Photos Found</td>";

echo "<!-- search_grid_end -->";
?>

 

 

 

Any idea on how to incorporate the old code to place a stop at the max search results. I.E. page 32 for the available total results I have now?

 

Thanks,

 

Ben

Link to comment
Share on other sites

Another suggestion to build onto ProjectFear's:

 

You could say something like 'Showing page X of Y' and have the pagination links next to it.... Then next to that you could have a simple something like:

 

<form action="" method="GET">

<input type="hidden" name="Photographer" value="<?php echo $_GET['Photographer']; ?>" />

<input type="text" name="page" value="<?php echo $_GET['page'] + 1; ?>" />

<input type="submit" value="Go!" />

</form>

Link to comment
Share on other sites

hmmm, yes i c. Displaying Photos 221 to 212 of 212

 

you really need to put something like this in your code fly757:

 

if($page > $total_pages){
$page = $total_pages;
}

 

so if people enter an absurd amount of pages GREATER then your total pages then you will display the last page.

 

Link to comment
Share on other sites

My problem is I need to make this original code:

 

	$spages = "";

for($k = 1; $k <= $pages; $k++)
{
	if($page == $k)
		$spages .= $k . " | ";
	else
		$spages .= "<a href='" . $link . "page=" . $k . "'>" . $k . "</a> | ";			
}

$spages = eregi_replace("\| $", "", $spages);

 

Works with this newer code:

	for ($k = $start; $k <= $end; $k++) {
  	if ($k > 0 && $k <= $pages) {
    if ($k == $page) echo " $k ";
    else echo "<a href='" . $link . "page=" . $k . "'onmouseover=\"this.style.color='#EBA003'\" onmouseout=\"this.style.color='#FFFFFF'\" style=\"text-decoration:none\">" . $k . "</a>\n";

 

That $spages is what is defined in this printf( function which in turn displays the pages:

if(sizeof($results) > 0) {
	printf(" <tr><td align=\"left\" class=\"heading\" width=\"200px\"><a href=\"http://www.airplanephotozone.com/photos_main.php\" style=\"text-decoration:none\"><b>New Search</b></a></td>");
	printf(" <td align=\"center\" class=\"heading\">Displaying Photos %d to %d of %d</td>", $start, $end, sizeof($results));
	printf(" <td align=\"right\" class=\"heading\" width=\"200px\"> </td></tr>");	
	{
	if(sizeof($results) > 20)
		printf(" <tr><td align=\"center\" colspan=\"3\" class=\"heading\" align=\"right\">[color=red]Pages: %s   </td></tr>", $spages[/color]);
	else
		echo "";
	}
}
else
	echo " <td colspan=\"3\" class=\"heading\">No Photos Found</td>";
echo "</tr>";

echo "<tr>";

 

That's my problem. They somehow need to be made to work with eachother so the pages can be displayed in the same location as they are now.

 

Ideas?

 

Ben

 

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.