Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
php database paging problem

 
Post new topic   Reply to topic    dna88 Forum Index -> Web scripting language Discussion Forum
Author Message
tanveer
User
User


Joined: 21 Jun 2004
Posts: 85
Location: Dhaka,Bangladesh

Post Post subject: php database paging problem Reply with quote

Dear friends,
I got stuck in a problem which is, I want to show a query result page by page and default records per page is 10 (let).
I searched many sites and found this code which in my case shows the links without any problem and first 10 records from db but the problem occurs when you click on any links. They simply dont respond.
I think the query is not passing in url.
I will be greatful if u provide me with any suggestions or advices.

Here is the code:
Code:

<?php

 include 'dbconnect.php'; // include 2 lines for connecting to the database.

 if(!($limit)) { $limit=10;} // default 10 records per page
 if(!($page)) { $page=0;} // default page value

 $results=mysql_query("select * from reg_alumni where std_id like'%".$query."%'");
 $rows=mysql_num_rows($results);
 //echo $rows;

 if( $rows==0) print " No result found";

$pages=intval($rows/$limit); // number of results page

if($rows%$limit) {$pages++;} // if remaider then 1 more page

$current= ($page/$limit)+1;  // current page number

if(($pages<1) || ($pages==0)) { $total=1;} // if page is less than or equal to 1 then total page=1
else { $total=$pages; } //else total page is $pages value

 $first=$page+1;// the first result

if(!((($page+$limit)/$limit)>=$pages) && $pages!=1) // if no last result page, last result equals                                                                //    $page+$limit
{
  $last=$page+$limit;
}
else  $last=$rows; // if last results page, last result equals total number of results.

?>

<html>
<title>Search result for<?=$query?></title>
<body>
<table border="1"><tr><td width="50%">results<b><?=$first?></b> -
<b><?=$last?></b>  of  <b><?=$rows?></b></td>

<td width="50%">Pages = <b><?=$current?></b>  of  <?=$total?></td>

</table>

<?php
$results1=mysql_query("SELECT * From reg_alumni limit $page,$limit");
  while($data=mysql_fetch_array($results1))
   {
 ?>
    <p>      <?=$data["std_id"]?>   <?=$data["passwd"]?>     </p>
<?php
  }
?>
<p align="center">
<?php
         if($page!=0) // dont show back link if page is first page
     {
      $back_page=$page - $limit;
      echo ("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a> \n");
     }

  for($i=1;$i<=$pages;$i++) // loop through pages and give link to it
   {
    $ppage=$limit*($i-1);

    if($ppage==$page)
     {
      echo ("<b>$i</b>\n"); // current page dont give link just text
     }
     else
     {
       echo ("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
  }

  if(!((($page+$limit)/$limit)>=$pages) && $pages !=1) // if last page then dont give next link
   {
    $next_page=$page+$limit;
    echo ("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a> \n");
   }
?>
</p>
</body>
</html>

Thanks in advance.
Fri Sep 17, 04 10:00 am
Back to top
tanveer View user's profile Send private message
hasnut
Expert User
Expert User


Joined: 28 Aug 2004
Posts: 201

Post Post subject: Reply with quote

Give me your url please so I can see it live.
_________________
Sarder Hasnut
MCSD, CIW A

Need Low Cost Prefessional Hosting Contact me
Sat Sep 18, 04 1:30 am
Back to top
hasnut View user's profile Send private message Visit poster's website MSN Messenger
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Php mysql pagination, multipage navigation Reply with quote

There is no code in that snippet for showing the link. It looks pretty messy too.

Below is the code that I used for This thread list page. This is much more compact, legible and most importantly it works.

Code:
function list_records() {
         global $records_per_page, $cur_page;
         global $php_self;
         $records_per_page = 50;
         $query = "SELECT count(*) FROM mytopics ORDER BY topic_time desc";
         $result = mysql_query($query);
         $query_data = mysql_fetch_row($result);
         $total = $query_data[0];
         $total_show = $total;
         
         if (empty($cur_page)) { $cur_page = 0; }
         $page_num = $cur_page + 1;
         $total = $last_page = ceil($total/$records_per_page);         
         
         $limit_str = "LIMIT " . $cur_page * $records_per_page . ", $records_per_page";
         $query = "SELECT title, id FROM mytopics ORDER BY topic_time desc $limit_str";
         $result = mysql_query($query);
         while($row=  mysql_fetch_assoc($result))
         {
            echo "<a href='forum-article.html".$row["topic_id"]."'>".$row["title"]."</a><br>";
           }
         
         echo "<table width = 98% border=0><tr align=center><td bgcolor=#eeeeee width = 25%>";         
         if ($page_num>1) {
         $prev_page = $cur_page -1;
         echo "<a href = \"$php_self?action=list_records&cur_page=0\" class=gensmall>Top </a>";
         echo "</td><td bgcolor=#eeeeee width = 25%>";
         echo "<a href = \"$php_self?action=list_records&cur_page=$prev_page\">Previous</a>";
         }
         echo "</td><td bgcolor=#eeeeee width = 25%>";
         if ($page_num < $total) {
         $nxt_page = $cur_page + 1;
         $last_page = $total - 1;
         echo "<a href = \"$php_self?action=list_records&cur_page=$nxt_page\">Next </a>";
         echo "</td><td bgcolor=#eeeeee width = 25%>";
         echo "<a href = \"$php_self?action=list_records&cur_page=$last_page\> Bottom</a>";
         }         
         echo "</td></tr></table>";
         
         }
         list_records();


I am sure you can customize it very easily for your purpose.
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Sat Sep 18, 04 11:07 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
tanveer
User
User


Joined: 21 Jun 2004
Posts: 85
Location: Dhaka,Bangladesh

Post Post subject: Reply with quote

Thanks a lot for ur reply. It solved with a very llittle modification.
Thanks a lot.
Let me ask a very novice question. Since the day I installed PHP in linux I always post my data in a php page using this sysntax

<form method ="post" action="http://localhost/hello.php">

If I put only the name of the php page in action then it shows

[file:///var/www/html/hello.php] in the url and prints the php code in that page.

Do U know why its happening?
Sun Sep 19, 04 12:36 pm
Back to top
tanveer View user's profile Send private message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Problem with php file path? Reply with quote

That's very odd tanveer. I do not see why this would happen. In my windows apache environment, just directing to the file name as post action works fine. Your problem may appear only if something is changed in the apache cnf file or in the php.ini file. What version of php you are using? There is a remote possibility that if cgi redirect is turned off this could happen. Does not make much sense to me though.

Or, let me see...is the php file and the form file in the same directory? Or the php file is one directory level up?
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Mon Sep 20, 04 3:09 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
tanveer
User
User


Joined: 21 Jun 2004
Posts: 85
Location: Dhaka,Bangladesh

Post Post subject: Reply with quote

The form and php page are on the same directory which is /var/www/html.
My php version is 4.3.3-6 .
Where to check in php.ini or apache.conf file ?
Tue Sep 21, 04 3:18 am
Back to top
tanveer View user's profile Send private message
hasnut
Expert User
Expert User


Joined: 28 Aug 2004
Posts: 201

Post Post subject: Reply with quote

tanveer wrote:

Where to check in php.ini or apache.conf file ?

in run type php.ini
same to other one
_________________
Sarder Hasnut
MCSD, CIW A

Need Low Cost Prefessional Hosting Contact me
Tue Sep 21, 04 12:45 pm
Back to top
hasnut View user's profile Send private message Visit poster's website MSN Messenger
tanveer
User
User


Joined: 21 Jun 2004
Posts: 85
Location: Dhaka,Bangladesh

Post Post subject: Reply with quote

sorry hasnat ,
Didn't understand what u mean by
Quote:
in run type php.ini
Wed Sep 22, 04 8:28 am
Back to top
tanveer View user's profile Send private message
dinangkur
Super Moderator
Super Moderator


Joined: 24 Mar 2004
Posts: 491
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Hi,

If you're using redhat linux you will find your apache configuration file in the following location /etc/httpd/conf/httpd.conf

Your PHP configuration file is in /etc/php.ini

I hope this will help.

Happy Hacking.

-DK
_________________
...we too are stardust...
Wed Sep 22, 04 9:29 pm
Back to top
dinangkur View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
tanveer
User
User


Joined: 21 Jun 2004
Posts: 85
Location: Dhaka,Bangladesh

Post Post subject: Reply with quote

dear dinangkur thanks for ur info but I know where to find those files, what I wanted to know is where to edit in those files to solve this problem?
Thu Sep 23, 04 3:32 am
Back to top
tanveer View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Web scripting language Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group