Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php + gallery navigation help

Options
  • 07-08-2008 11:30am
    #1
    Registered Users Posts: 788 ✭✭✭


    Looking for advice on how to have gallery navigation with php.
    Currently I have the gallery set up dynamically so that each gallery reloads on the same page but with a different id (which i get from the mysql database) for example, gallery.html?id=5. At the end of each page i'd like a gallery navigation system with previous ( for ex. gallery.html?id=4) and next (for ex. gallery.html?id=6) but im not sure how to set it up dynamically :confused:

    If anyone has any advice i'd really appreciate it!


Comments

  • Registered Users Posts: 2,119 ✭✭✭p


    My php is a bit rusy, but at it's most basic you just need to add or take one away from your id variable. Something like this would roughly do. It's not perfect, but will give you a starting point.

    [php]<?
    echo('<a href="gallery.html?id=' . $id-1 . '">previous</a>');
    echo('<a href="gallery.html?id=' . $id+1 . '">next</a>');
    ?>[/php]


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    p wrote: »
    My php is a bit rusy, but at it's most basic you just need to add or take one away from your id variable. Something like this would roughly do. It's not perfect, but will give you a starting point.

    [php]<?
    echo('<a href="gallery.html?id=' . $id-1 . '">previous</a>');
    echo('<a href="gallery.html?id=' . $id+1 . '">next</a>');
    ?>[/php]

    Looks about right; just remember to wrap the both the echos in their own if statements, so that if you're on the first or last pages there's no link.

    [php]<?
    if ($id>1) echo('<a href="gallery.html?id=' . $id-1 . '">previous</a>');
    if ($id<$totalPages) echo('<a href="gallery.html?id=' . $id+1 . '">next</a>');
    ?>[/php]

    N.B. The code above assumes the indexing is from 1 - $totalPages; subtract one from both if it's a zero-indexed list or array.


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    Thanks very much for your help. I have one more query :)

    I am not going by totalPages but rather the id in the database that makes up the page.

    so far my total count of id's in my db is 7, but currently my actual id's range from 2-8 so saying ($id<$totalNum) is not quite working for me. I've messed about with it but its just not working right :confused: thanks again for your help!


  • Registered Users Posts: 2,793 ✭✭✭oeb


    This is a quick hack.
    [PHP]
    $id = intval($_GET);
    $dir = $_GET; // 'next' or 'last'

    $sql = "SELECT * FROM `photos` WHERE `id`=$id";
    $result = mysql_query($sql);

    if(!mysql_num_rows($result)) {
    if($dir == 'up') {
    $sql_op = "WHERE `id` > $id ORDER BY `id` ASC LIMIT 1";
    } else {
    $sql_op = "WHERE `id` < $id ORDER BY `id` DESC LIMIT 1";
    }

    $sql = "SELECT * FROM `photos` $sql_op";
    $result = mysql_query($sql);
    }

    $photoArray = mysql_fetch_array($result);
    [/PHP]

    Now, you use p's code at the bottom, with one minor change.

    [PHP]
    echo '<a href="gallery.html?id', $id-1, '&dir=down">Prev</a>';
    echo '<a href="gallery.html?id', $id+1, '&dir=up">Next</a>';
    [/PHP]

    What this does is if the image with the id $id does not exist, it gets the next lowest if you are going down, or highest if you are going up. This is not tested, and is a filthy hack. But it should work.


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    I tried the above but i cant get it to work correctly. Here is my code
    <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>
    

    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. I cant get it to do this please help :(


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    [php]
    $result2=mysql("$DBName","SELECT COUNT(id) FROM project");
    while($row=mysql_fetch_array($result2)) {
    $totalNum = $row;
    [/php]

    Can you actually reference the column 'Count(id)? - I've a feeling it not possible but then again I've never tried it. Better off using the as thingy and referencing that instead.

    [php]
    $result2=mysql("$DBName","SELECT COUNT(id) as numOfProjects FROM project");
    while($row=mysql_fetch_array($result2)) {
    $totalNum = $row;
    [/php]

    Now I would scrap all before this and go with whats below. To overcome problem of next ID not being $current+-1, try this.

    Assuming your primary key column is named id... this will do the job for you.
    [php]
    mysql_connect("$DBHost","$DBUser","$DBPass");
    $result = mysql($DBName, "(SELECT * FROM project where id <= '$projid' ORDER BY id DESC LIMIT 2) UNION (SELECT * FROM project where id > '$projid' ORDER BY id ASC LIMIT 1) ;");

    $nextID = null;
    $previousID = null;

    while($row=mysql_fetch_assoc($result))
    {
    if ($row < $projid)
    $previousID = $row;
    else if ($row > $projid)
    $nextID = $row;
    }

    print ($previousID != null ? '<a href="project.html?id='.$previousID.'">< Prev Project</a>' : 'None Previous')
    . " | " .
    ($nextID != null ? '<a href="project.html?id='.$nextID.'">Next Project ></a>' : 'None Next');

    [/php]

    This should solve your problems :)


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    Thanks a million, didn't quite work though (maybe its my fault heh), however I've tried something else which seems to work perfectly apart from one tiny problem:
    <div class="scroll-gallery">
                        <?
                        mysql_connect("$DBHost","$DBUser","$DBPass");
                    $result=mysql("$DBName","SELECT MAX(id) FROM project where id < '$projid' LIMIT 0,1");
    			  while($row=mysql_fetch_row($result)) {
    					$previous = $row[0];
    			}
    
    
    		$result2=mysql("$DBName","SELECT MIN(id) FROM project where id > '$projid' LIMIT 0,1");
    			  while($row2=mysql_fetch_row($result2)) {
    					$next=$row2[0];
    				}
    
    $result3=mysql("$DBName","SELECT COUNT(id) FROM project");
           while($row3=mysql_fetch_array($result3)) {
           $totalNum = $row3['COUNT(id)'];
           }
    
    
    
      if ($previous>1){echo"<a href=\"project.html?id=$previous\"
        class=\"prev\">Prev Project</a><span class=\"separ\">"; }
    
       if ($next<$totalNum) { echo "<span class=\"separ\"><a
        href=\"project.html?id=$next\" class=\"next\">Next Project</a><span
        class=\"scr-numb\">"; } ;
    
    
                    ?>
    

    I think my problem here is
    if ($previous>1){echo"<a href=\"project.html?id=$previous\"
        class=\"prev\">Prev Project</a><span class=\"separ\">"; }
    


    It works fine for all id's but as the first id in database is 1, when it moves to the next id (3), it doesnt give a "previous" link on this page back to the project id 1. I've it set to if ($previous>1), could this be why? :confused:


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Having 3 queries to MySQL for such a simple thing is a bit messey.
    What didn't work with my one? - I tested it there and it works perfectly there. It probably something small.

    Your way should work too - but you are placing the variables inside the while loop statements so their scope is only visible there. As well since each query is returning a single row there is no need for the while loops.

    [php]$result=mysql("$DBName","SELECT MAX(id) FROM project where id < '$projid' LIMIT");
    $row=mysql_fetch_row($result));
    $previous = $row[0];



    $result2=mysql("$DBName","SELECT MIN(id) FROM project where id > '$projid' LIMIT 0,1");
    $row2=mysql_fetch_row($result2);
    $next=$row2[0];


    $result3=mysql("$DBName","SELECT COUNT(id) FROM project");
    $row3=mysql_fetch_array($result3);
    $totalNum = $row3;




    if ($previous>1){echo"<a href=\"project.html?id=$previous\"
    class=\"prev\">Prev Project</a><span class=\"separ\">"; }

    if ($next<$totalNum) { echo "<span class=\"separ\"><a
    href=\"project.html?id=$next\" class=\"next\">Next Project</a><span
    class=\"scr-numb\">"; } ;
    [/php]

    Id still prefer my solution as it much cleaner and no need for the 3rd count.
    I've edited my code above to make it use functions you prefer to use. I've never seen mysql($DB....) before though, always used mysql_query. :confused:


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    Your code in post #7 gives me a blank screen but I cant check what the error is as I am not at work. My code still doesnt say "previous page" for the id 1 when the current page is project.html?id=3 :(


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    Oh now it works :D Cheers!!!


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Which works, yours? - Good stuff :)


Advertisement