Hello! I’m completely new to this forum, so forgive me if I’m missing some sort of etiquette rule, but I am exhausted of endless Googling that is getting me nowhere. I’m hoping a website like phphelp.com can help me.
So I have a resource website that I learned PHP for. I’m still a super beginner, but I made the content management system myself, so I am rather pleased. Unfortunately, I’m stuck when it comes to Pagination under the Search/Filter by __ feature on my website.
Case in point: http://lovespire.org/resources/icons/
Filtering to “Sailor Moon” works fine, but when I try to go to the 2nd page, it gives me errors. =[
Warning: include(search?page=2.php) [function.include]: failed to open stream: No such file or directory in /hermes/bosweb/web106/b1064/ipg.lovespireorg/resources/icons/index.php on line 15Warning: include() [function.include]: Failed opening ‘search?page=2.php’ for inclusion (include_path=’.:/usr/local/lib/php-5.2.17/lib/php’) in /hermes/bosweb/web106/b1064/ipg.lovespireorg/resources/icons/index.php on line 15
Here is the backbone of the icons/ folder. It contains index.php, main.php, and search.php.
Index.php dynamically includes main.php
[code]<?php
include(’…/…/header.php’);
?>
<?php if (isset($_GET['x'])) { if (strpos($_GET['x'], "/")) { $dir = substr(str_replace('..', '', $_GET['x']), 0, strpos($_GET['x'], "/")) . "/"; $file = substr(strrchr($_GET['x'], "/"), 1); include($dir.$file.".php"); } else { include(basename($_GET['x']).".php"); } } else { include("main.php"); } ?> <?php include('../../footer.php'); ?>[/code]
Main.php contains the database for all of the icons, including a pagination script which works.
[php]<?
$link = mysql_connect(’ ', ’ ', ’ ');
if (!$link) {
die('Could not connect: ’ . mysql_error());
}
mysql_select_db(“resources”, $link);
$sql=“SELECT DISTINCT icon.series FROM icon”;
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$series=$row["series"];
$options.="<OPTION VALUE=\"$series\">".$series;
}
?>
// find out how many rows are in the table
$sql = “SELECT COUNT(*) FROM icon”;
$result = mysql_query($sql, $conn) or trigger_error(“SQL”, E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 20;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET[‘page’]) && is_numeric($_GET[‘page’])) {
// cast var as int
$page = (int) $_GET[‘page’];
} else {
// default page num
$page = 1;
} // end if
// if current page is greater than total pages…
if ($page > $totalpages) {
// set current page to last page
$page = $totalpages;
} // end if
// if current page is less than first page…
if ($page < 1) {
// set current page to first page
$page = 1;
} // end if
// the offset of the list, based on current page
$offset = ($page - 1) * $rowsperpage;
// get the info from the db
$sql = “SELECT * FROM icon ORDER BY id DESC LIMIT $offset, $rowsperpage”;
$result = mysql_query($sql, $conn) or trigger_error(“SQL”, E_USER_ERROR);
echo " ";
// while there are rows to be fetched…
while ($row = mysql_fetch_array($result)) {
// echo data
echo “<img src=’” . $row[‘image’] . “’”;
echo “title=’” . $row[‘id’] . " | " . $row[‘series’] . “’ />”;
} // end while
/****** build the pagination links ******/
// range of num links to show
$range = 3;
echo “
”;
// if not on page 1, don’t show back links
if ($page > 1) {
// show << link to go back to page 1
echo "
<a href=’{$_SERVER[‘PHP_SELF’]}?page=1’>«« ";
// get previous page num
$prevpage = $page - 1;
// show < link to go back to 1 page
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?page=$prevpage’ class=‘prev’>« Previous ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
// if it’s a valid page number…
if (($x > 0) && ($x <= $totalpages)) {
// if we’re on current page…
if ($x == $page) {
// ‘highlight’ it but don’t make a link
echo " $x ";
// if not current page…
} else {
// make it a link
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?page=$x’>$x ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($page != $totalpages) {
// get next page
$nextpage = $page + 1;
// echo forward link for next page
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?page=$nextpage’ class=‘next’>Next » ";
// echo forward link for lastpage
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?page=$totalpages’>»» ";
} // end if
/****** end build pagination links ******/
echo “
?>[/php]
I got the pagination script here: http://www.phpfreaks.com/tutorial/basic-pagination
And then search.php, which is almost exactly like main.php, but filters by WHERE $series=___ via a $_post command on main.php.
[php]
Go back to alter your search.
// number of rows to show per page
$rowsperpage = 20;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET[‘page’]) && is_numeric($_GET[‘page’])) {
// cast var as int
$page = (int) $_GET[‘page’];
} else {
// default page num
$page = 1;
} // end if
// if current page is greater than total pages…
if ($page > $totalpages) {
// set current page to last page
$page = $totalpages;
} // end if
// if current page is less than first page…
if ($page < 1) {
// set current page to first page
$page = 1;
} // end if
// the offset of the list, based on current page
$offset = ($page - 1) * $rowsperpage;
// get info
$sql = mysql_query(“select * from icon where series like ‘$series’ ORDER BY id DESC LIMIT $offset, $rowsperpage”);
while ($row = mysql_fetch_array($sql)){
echo "<img src='" . $row['image'] . "'> ";
}
/****** build the pagination links ******/
// range of num links to show
$range = 3;
echo “
”;
// if not on page 1, don’t show back links
if ($page > 1) {
// show << link to go back to page 1
echo "
<a href=’{$_SERVER[‘PHP_SELF’]}?x=search?page=1’>«« ";
// get previous page num
$prevpage = $page - 1;
// show < link to go back to 1 page
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?page=$prevpage’ class=‘prev’>« Previous ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
// if it’s a valid page number…
if (($x > 0) && ($x <= $totalpages)) {
// if we’re on current page…
if ($x == $page) {
// ‘highlight’ it but don’t make a link
echo " $x ";
// if not current page…
} else {
// make it a link
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?x=search?page=$x’>$x ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($page != $totalpages) {
// get next page
$nextpage = $page + 1;
// echo forward link for next page
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?x=search?page=$nextpage’ class=‘next’>Next » ";
// echo forward link for lastpage
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?x=search?page=$totalpages’>»» ";
} // end if
/****** end build pagination links ******/
echo “
?>
[/php]
As for the aforementioned error, line 15 on index.php is
[php]else {
include(basename($_GET[‘x’]).".php");[/php]
Which, I’m assuming, just basically means… icons/index.php?x=search?page=2 just doesn’t exist. Which makes sense. It should be icons/index.php?x=SERIES?page=2 for it to make sense. But I don’t know how to get to that point. I tried changing the $_post in the dropdown search menu to a $_get, which changed the URL, but pagination still didn’t work. It recognizes that there are 2 pages, but when I click page 2, it doesn’t yield any results. Its blank. It even says “Now showing only icons.” instead of “Now showing only something icons.”
I’d love some help with this. Even if it involves creating a completely new search/filter feature. As long as pagination works ;D