Pagination, PHP Search Results

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
drumking88
Forum Newbie
Posts: 9
Joined: Tue Apr 28, 2009 4:32 pm

Pagination, PHP Search Results

Post by drumking88 »

I am currently trying to fix a bug in a requests system which I have designed in PHP for my radio stations new website.

In a nutshell:

There is a text field (part of a form) which the user fills in. Upon pressing submit, the user is then presented with the search results (from an SQL Query, using a MYSQL database). This is a self driven PHP page.

Code: Select all

while($row = mysql_fetch_array($result))
  {
  echo '<form method = "POST" action = "check_request.php">';
  echo '<table border = "">';
  echo '<tr>';
  echo '<td width = "200">';
 // echo $row['title'];
    $item = $row['Title'];
        $artistFirstName = $row['Name'];
        $artistSurname = $row['Surname'];
        $artistFullName = $artistFirstName. " ".$artistSurname;
 
  echo '<input type = "hidden" name = "song_name" value = " '. $item.' ">';
        echo $item;
  echo '</td>';
  echo '<td>';
  echo '<input type = "hidden" name = "artist" value = " '.$artistFullName.'">';
  echo $artistFullName;
  echo '</td>';
  /*echo '<td>';
  echo $row['content'];
  echo '</td>';
  */
  echo '<td>';
 
  echo '<input type = "submit" name = "submit" value = "Request It Now ">';
  echo '</td>';
  echo '</tr>';
  echo '</table>';
 
My dilemna is that I would like to see only 5 results per page. I understand that we have to use something called Pagination, but have no idea how to go about it, and can't find any simple examples on the t'internet.

Thanks for your time!

Drumking88
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Pagination, PHP Search Results

Post by Mirge »

drumking88
Forum Newbie
Posts: 9
Joined: Tue Apr 28, 2009 4:32 pm

Re: Pagination, PHP Search Results

Post by drumking88 »

Mirge wrote:You might find this guide helpful:

http://www.webpronews.com/expertarticle ... with-mysql
Thank you Mirge for your reply.

I read the tutorial you linked me to, but alas have fallen into deep waters once again.

Code: Select all

<?php
//Database connection parameters
mysql_connect("localhost", "root", "")or die(mysql_error());
mysql_select_db("php_test") or die(mysql_error());
 
$result = mysql_query("SELECT COUNT(*) AS total_entries FROM products") or die(mysql_error()); $row = mysql_fetch_row($result); $total_entries = $row[0];
 
$entries_per_page = 3;
 
if(isset($_GET['page_number'])) { $page_number = $_GET['page_number']; } else { $page_number = 1; } 
 
$total_pages = ceil($total_entries / $entries_per_page);
 
 
 
$offset = ($page_number - 1) * $entries_per_page;
 
$result = mysql_query("SELECT * FROM products LIMIT $offset, $entries_per_page") or die(mysql_error()); while($obj = mysql_fetch_object($result)) { // Display the data however you want here. 
print <<id - $obj->name - $obj->description<br>
 
EOD; }
 
for($i = 1; $i <= $total_pages; $i++) { if($i == $page_number) { // This is the current page. Don't make it a link. 
print "$i "; } else 
{ 
// This is not the current page. Make it a link. 
print "<a href="products.php?page_number=$i">$i</a>"; 
}
}
 
?>
I don't know whats gone wrong, but the error message I am getting is:
Parse error: parse error in C:\xampp\htdocs\www\dev\php\index.php on line 28
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Pagination, PHP Search Results

Post by Mirge »

print "<a href="products.php?page_number=$i">$i</a>";

You have to escape your quotes :).

It should be:

print "<a href=\"products.php?page_number=$i\">$i</a>";

Glad you're taking the initiative and actually trying to figure this out.
drumking88
Forum Newbie
Posts: 9
Joined: Tue Apr 28, 2009 4:32 pm

Re: Pagination, PHP Search Results

Post by drumking88 »

Mirge wrote:print "<a href="products.php?page_number=$i">$i</a>";

You have to escape your quotes :).

It should be:

print "<a href=\"products.php?page_number=$i\">$i</a>";

Glad you're taking the initiative and actually trying to figure this out.
i Just figured that out as I opened up the thread again :o

Ive sorted that out, but its now throwing up a new error:

Code: Select all

print <<id - $obj->name - $obj->description<br> 
 
EOD; }
Parse error: parse error in C:\xampp\htdocs\www\dev\php\index.php on line 19
Line 19 being the print <<id line, so Ive now added in an die statement to make it look like:

Code: Select all

print <<id - $obj->name - $obj->description <br> EOD or die(mysql_error()); }
I'm sorry for being a pain at 2:30 am! Thanks a lot for your help.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Pagination, PHP Search Results

Post by Mirge »

It looks like you're trying to here a heredoc. The format for one is like follows (for example):

Code: Select all

 
<?php
 
print <<<EOT
This is a test.
Hello world!
EOT;
 
?>
 
My delimiter is "EOT" (end of text). Nothing (including whitespace) can come before OR after the ending delimiter (EOT).
Post Reply