Jump to content

Recommended Posts

Hi everyone

 

Firstly I hope I've put this in the correct forum.

 

Looking for advice on how to have page navigation with php.

Currently I have the page set up dynamically so that each page reloads on the same page but with a different id (which i get from the mysql database) for example, project.html?id=5. At the end of each page i'd like a project navigation system with previous ( for ex. project.html?id=4) and next (for ex. project.html?id=6).

 

This is my code for this piece:

 

<div class="scroll-gallery">
		 <?
		 mysql_connect("$DBHost","$DBUser","$DBPass");
			$result=mysql("$DBName","SELECT * FROM project where id = '$projid'");
   while($row=mysql_fetch_row($result)) {
		   $pid=$row[0];
		   $pid1 = $pid+1;
		   $pid2 = $pid-1;
   $result2=mysql("$DBName","SELECT COUNT(id) FROM project");
   while($row=mysql_fetch_array($result2)) {
   $totalNum = $row['COUNT(id)'];
   }
   }


	  if ($pid>1){ echo"<a href=\"project.html?id=$pid2\"
class=\"prev\">Prev Project</a><span class=\"separ\">"; }
	  if ($pid<$totalNum){ echo"<span class=\"separ\"><a
href=\"project.html?id=$pid1\" class=\"next\">Next Project</a><span
class=\"scr-numb\">"; }


		?>


   </div>

 

 

But as its going by the id in the database, if a record is erased from the database i need it to skip to the next available id number otherwise a blank page will show. I cant get it to do this please help :( *beginner*

Link to comment
https://forums.phpfreaks.com/topic/120892-phpmysql-page-navigation-help/
Share on other sites

I think what I would do is initially pull out all the ids from the database and put it in a session array and then base your GET var off the array position, and use the value of that position in your query.  That way you are working with a list of numbers without any gaps. 

 

pseudo code:

 

<?php
   session_start();

   if (!$_SESSION['id_list']) {
      $sql = "select id from table";
      $result = mysql_query($sql);
      while ($n = mysql_fetch_row($result)) {
         $_SESSION['id_list'][]= $n[0];
      } // end while $n
   } // end if !$_SESSION['id_list']

   $id = $_SESSION['id_list'][{$_GET['id']}];
   // use $id in your query   

Thanks you gave me an idea and I tried doing it with an array like this, would this be correct?

 

$numbers = array();
       <div class="scroll-gallery">
                    <?
                    mysql_connect("$DBHost","$DBUser","$DBPass");
                $result=mysql("$DBName","SELECT * FROM project where id = '$projid'");
                 while($row=mysql_fetch_row($result)) {
                 
                 array_push($numbers,$row[0]); 
                 
                 }
                 
                 sort($numbers, [sORT_NUMERIC]);
                 array_count_values($numbers);
                 $total = array_count_values($numbers);
                 
    foreach ($numbers as $number) {
    
              if ($number>1){ echo"<a href=\"project.html?id=$number\"
    class=\"prev\">Prev Project</a><span class=\"separ\">"; }
              if ($number<$total){ echo"<span class=\"separ\"><a
    href=\"project.html?id=$number\" class=\"next\">Next Project</a><span
    class=\"scr-numb\">"; }
    
    
                ?>
    
    
       </div>

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.