By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

merge pages into one page

100+
P: 123
i have 3 pages,home.php, next.php page and other.php.in the home.php page
i selected name from the database and i gave it a limit and also
an if statement to echo next.for the next.php page and other.php i did the same
thing but i indicate (where comp_id >3 limit 3) for next page (where comp_id >6 limit 3)
for other page so as to display the next 3 name repectively and also echo next and back
to go the previous page.how can i put these pages into one so that when i click next it will
take me to the same pages but display the next name.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. homepage
  4.  
  5.     $max = $_GET['mino'];
  6.     $query = "SELECT name FROM hotel LIMIT 3";
  7.     $result = mysql_query ($query) or die('query error');
  8.     $count = 0;
  9.     while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
  10.     $present = $line[name];
  11.     echo "$present ";
  12.     $count++;
  13.     }
  14.  
  15.  
  16.     $query1 = "SELECT comp_id FROM hotel LIMIT 3";
  17.     $result1 = mysql_query ($query1) or die('query error2');
  18.     $count = 0;
  19.     while ($fine = mysql_fetch_array($result1, MYSQL_ASSOC)){
  20.     foreach ($fine as $value){
  21.     }
  22.     $count++;
  23.     }
  24.     if($value == 3){
  25.        echo "<a href='next.php?min=3'>NEXT</a>";
  26.    }
  27.  
  28. ?>
  29.  
  30.  
  31. <?php
  32.  
  33. nextpage    
  34.     $min = $_GET['min'];
  35.     if($min ){
  36.  
  37.     $query1 = "SELECT name FROM hotel WHERE comp_id >3 LIMIT 3";
  38.     $result1 = mysql_query ($query1) or die('query error');
  39.     $count = 0;
  40.     while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)){
  41.     $sente = $line[name];
  42.     echo "$sente ";
  43.     $count++;
  44.     }
  45.  
  46.     }
  47.  
  48.     $query1 = "SELECT comp_id FROM hotel WHERE comp_id >3 LIMIT 3";
  49.     $result1 = mysql_query ($query1) or die('query error2');
  50.     $count = 0;
  51.     while ($fine = mysql_fetch_array($result1, MYSQL_ASSOC)){
  52.     foreach ($fine as $value){
  53.     }
  54.     $count++;
  55.     }
  56.     if($value == 6){
  57.        echo "<a href='home.php'>BACK</a>";
  58.     echo "<a href='other.php?mine=6'>NEXT</a>";
  59.    }
  60.  
  61. ?>
  62.  
  63.  
  64.  
Jan 23 '09 #1
Share this Question
Share on Google+
11 Replies


Atli
Expert 5K+
P: 5,058
Hi.

What you are talking about (or at least, what I think you are talking about) is called "pagination". Try Googling that term an you should get a bunch of detailed tutorials.

But, essentially, what you do is you place a page number into the URL:
Expand|Select|Wrap|Line Numbers
  1. www.example.com/page.php?page=1
And then you use that number to select a range of rows from your table:
Expand|Select|Wrap|Line Numbers
  1. // Ge the page variable from the URL
  2. ($page = @$_GET['page']) or $page = 0;
  3. if(!is_numeric($page) || $page < 0) {
  4.   // The number is invalid. It's either not a number or a negative number.
  5.   $page = 0;
  6. }
  7.  
  8. // Calculate the LIMIT clause for the SQL
  9. $rowsPerPage = 5;
  10. $startLimit = $rowsPerPage * $page;
  11.  
  12. // Create the query, only fetching the data for the current page.
  13. $sql = "SELECT * FROM myTable LIMIT $startLimit, $rowsPerPage";
  14. $result = mysql_fetch($sql) or die("Query failed: ". mysql_error());
  15.  
  16. // Display the data.
  17.  
And then just create next and prev buttons with links to the same page, only using +-1 for the page number.

Keep in mind that the page number starts with 0, not 1. So the first page would be 0, the second 1 and so forth.
Jan 23 '09 #2

100+
P: 123
i am having a problem with my if statement i want
NEXT(passing variable througth url) to display when the first 3
image in the first page is complete and it should not display
BACK(passing variable through url) yet. when the NEXT is display
and is click on to pass a value into page if the image display is
one or two it should display BACK a alone but if it is three it should
display BACK and NEXT
Expand|Select|Wrap|Line Numbers
  1.     $page = @$_GET['page']) or $page = 0; 
  2.     if(!is_numeric($page) || $page < 0) { 
  3.       // The number is invalid. It's either not a number or a negative number. 
  4.       $page = 0; 
  5.     } 
  6.  
  7.  
  8.     // Calculate the LIMIT clause for the SQL 
  9.     $rowsPerPage = 3; 
  10.     $startLimit = $rowsPerPage * $page;
  11.  
  12.     $newpage = $page + 1;
  13.     $ne = $page - 1;
  14.  
  15.     // Create the query, only fetching the data for the current page. 
  16.     $sql = "SELECT * FROM hotel LIMIT $startLimit, $rowsPerPage";
  17.     $result = mysql_query ($sql) or die('query error'); 
  18.     $count = 0;
  19.     while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
  20.     $web = $line[web];
  21.     $present = $line[picname];
  22.     $value = $line[comp_id];
  23.     //echo "$value<br>";
  24.     echo "<img src='company/$present'/> ";
  25.       $count++;
  26.     }
  27.  
  28.  
  29.     if($present <= 3){
  30.        echo "<br><a href='ineedit.php?page=$newpage'>NEXT</a>";
  31.     }elseif($present >= 3){
  32.     echo "<br><a href='ineedit.php?page=$ne'>Back</a>";
  33.     }
  34.  
Feb 2 '09 #3

Atli
Expert 5K+
P: 5,058
I don't really understand what you are saying. Could you try explaining a bit more clearly?

Are just trying to show/hide the Back/Next buttons on the First/Last pages?

If so, then the Back button is no problem. Any page with a page ID of 1 or higher should include it. The one with page ID 0 should not.

The Next button is trickier. You would have to find out if the current page is in fact the last page, which would require you to fetch the total amount of rows in the table, divide them by the number of rows per page and compare that with the current page number. (Note; the number not the ID. The page number is the ID + 1, remember.)

P.S.
When you fetch array elements, you should use strings as the element names. All strings should be quoted, or they will be considered constants.
The only reason why the arrays in your code are working for you right now is because PHP is assuming you don't know this and is converting them for you. If you had debug messages enabled, you would see that.
Expand|Select|Wrap|Line Numbers
  1. $array[element]; // INCORRECT! Generates warnings.
  2. $array['element']; // Correct. Like it should be done.
Unless your element names are stored in constants? (Which I don't see in your code.)
Feb 3 '09 #4

TheServant
Expert 100+
P: 1,168
Best walk through for pagination I know:
www.tonymarston.net/php-mysql/pagination.html
I advise you to just run through that with your code and see if you forget things like Atli suggested (check if it's last page etc...).
Feb 3 '09 #5

100+
P: 123
yes I just want to show/hide the Back/Next buttons on the First/Last pages,
and aslo i did want you said i fetch the total amount of rows and i divide
it by the number of rows per page which given my a decimal no and i now compare
it with the current page number but this time it did not show the next button.


Expand|Select|Wrap|Line Numbers
  1. <?
  2. $result = "SELECT MAX(comp_id) FROM hotel";
  3. $product = mysql_query ($result) or die('query error3');
  4. $count = 0;
  5. while ($nine = mysql_fetch_array($product, MYSQL_ASSOC)){
  6. foreach ($nine as $totalno){
  7. echo "<br>$totalno<br>";//total number of row
  8. }
  9. $count++;
  10. }
  11.  
  12. $divide = $totalno/$rowsPerPage;
  13. echo "<br>$divide";
  14. if($divide == $page){
  15. echo "<br><a href='ineedit.php?page=$newpage'>NEXT</a>";
  16. }
  17.  
  18. ?>
Feb 7 '09 #6

TheServant
Expert 100+
P: 1,168
Did you read that tutorial I suggested? It uses the ceil() function so you should not have decimals in your page number.
Feb 8 '09 #7

100+
P: 123
yes i visit the url (http://www.tonymarston.net/php-mysql/pagination.html) you suggested but is not coming up
Feb 9 '09 #8

TheServant
Expert 100+
P: 1,168
Can you post your code where you think it might be going wrong?
Feb 9 '09 #9

100+
P: 123
here is the code

Expand|Select|Wrap|Line Numbers
  1. <? 
  2. $result = "SELECT MAX(comp_id) FROM hotel"; 
  3. $product = mysql_query ($result) or die('query error3'); 
  4. $count = 0; 
  5. while ($nine = mysql_fetch_array($product, MYSQL_ASSOC)){ 
  6. foreach ($nine as $totalno){ 
  7. echo "<br>$totalno<br>";//total number of row 
  8. $count++; 
  9.  
  10. $divide = $totalno/$rowsPerPage; 
  11. echo "<br>$divide"; 
  12. if($divide == $page){ 
  13. echo "<br><a href='ineedit.php?page=$newpage'>NEXT</a>"; 
  14.  
  15. ?>
Feb 11 '09 #10

TheServant
Expert 100+
P: 1,168
OK, well I'm not sure what all your variables are, but just going by names, shouldn't it be:
Expand|Select|Wrap|Line Numbers
  1. if($divide != $page) { show next button
?
Isn't divide the last page number???
Feb 11 '09 #11

Markus
Expert 5K+
P: 6,050
You're giving us little information to help you with.
  • What is the problem?
  • What does happen?
  • What doesn't happen?
  • Do you get any errors?
Feb 11 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.