How do I enable arrow key navigation on a site with PHP generated links?

Hi Everyone… I would like to enable arrow key navigation. I found this bit of code doing a webs search…I do not know anything about JavaScript. How do I implement this code in my site. How do I make this work ? My links are dynamically generated PHP links. Here is one of my link sample…

rajeevthomas.com/viewgallery.php?cname=Colorado-Fall&pcaption=Light-On-Fall-Colors

In the code below, I see that there are areas to place my code in. But I am not sure what to do here. Can you help me please…?
Thank you everyone…

$(document).keydown(function(e){
    switch(e.which) {
        case $.ui.keyCode.LEFT:
        // your code here
        break;

        case $.ui.keyCode.UP:
        // your code here
        break;

        case $.ui.keyCode.RIGHT:
        // your code here
        break;

        case $.ui.keyCode.DOWN:
        // your code here
        break;

        default: return; // allow other keys to be handled
    }

    // prevent default action (eg. page moving up/down)
    // but consider accessibility (eg. user may want to use keys to choose a radio button)
    e.preventDefault();
 

Hi,

What do you mean by “enable arrow key navigation”
Do you want to let your users navigate through your photos by pressing the left and right arrow keys?

Pullo…thank you for replying. Yes that’s what I want to do. Sorry if I was not clear with my question…

No probs :slight_smile:

You seem to have a different page for each photo (nice photos by the way).

My first question is: could you not use a slider?
There are many free ones and these have this functionality built in.
You would have all of the images (or sets of images anyway) on the same page, so it would make for a more seamless experience for your users.

My current favourite is Slick: http://kenwheeler.github.io/slick/

If sliders/carousels are not an option, just let me know and we’ll work out where to go from there.

Pullo, thank you. I looked in to sliders at first but they seemed to be sluggish when a large amount of photos uploaded with it. I will have 600+ images uploaded there on to this site. Right now it is a PHP generated page. There is only one page and the image is pulled from the database. So far I think this is the fastest method. Then again I am not the expert. So I prefer the existing method but I have seen some sites with arrow key navigation which use JavaScript/JQuery for it. I am not sure how to implement it… And here is a site that uses navigation like I would like to have… http://www.kenkoskela.com/photo/blue-angel/

Oh, ok.
In that case what about this:

You create your back/forward arrows, then when a user clicks on them or presses left/right arrow on their keyboard, you send an ajax request to a PHP script on your server. You tell the PHP script what the current photo is and whether the user pressed/clicked forward or back. The PHP script will then answer with the url of the previous/next image, as well as the image’s meta data (title, alt etc). Back in your JS script you then load the image by swapping out the current image’s src attribute with the one returned by the server. You can also alter the address shown in the address bar and update the meta data.

Sounds complicated Pullo especially for a newbie like me… ! :slight_smile:

Yeah, the main problem lies in knowing which picture to display next.
As far as I can see, you’ll need to ask the server for that information.

I could help you implement it if you fancy.

That would be awesome… what do I need to do? I have to warn you though I am a child in this area so you might need to walk me through…

Hi,

Sorry I just dropped off - had to work :frowning:

Anyways, could you post the PHP code used to fetch the images (or image paths) etc. from the database?
I’m still thinking about the best way to do this.

Pullo…Thank you for helping me even when you are busy working… I really appreciate your help…

$result = mysql_query( "SELECT photo_caption, photo_description, photo_filename,photo_keywords FROM gallery_photos WHERE photo_caption='".addslashes($pcaption)."'" ); 

list($photo_caption, $photo_description, $photo_filename, $photo_keywords) = mysql_fetch_array( $result ); 

$nr = mysql_num_rows( $result ); 
mysql_free_result( $result );     

$p_caption = $photo_caption;
$p_description = $photo_description;
$p_keywords = $photo_keywords;

//fill caption_array with sorted pids in current category 

$caption_array = array();
$result = mysql_query( "SELECT photo_caption FROM gallery_photos WHERE category_name='".addslashes($cname)."' " ); 
$ct = mysql_num_rows( $result ); 

while ($row = mysql_fetch_array($result)) { 

$row[0]= trim($row[0]);
$row[0] = str_replace(" ","-",$row[0]);
$row[0] = str_replace("'","%27",$row[0]);
$caption_array[] = trim($row[0]); 
} 

mysql_free_result( $result ); 

if( empty($nr ) ) 
{ 
$result_final = "\	<tr><td>***No Photo found*******</td></tr>\
"; 
} 
else 
{ 
$category_name = $cname; 
$cname = str_replace(" ", "-", $cname); 
$result_final = "
<div class=limagePage>
<div class=llink><a href=/viewgallery.php>ALBUMS</a><span class=arrow>>&gt</span><a href=/viewgallery.php?cname=$cname>$category_name</a></div>
";

// display previous and next links if more than one photo 
$next=0;
$prev=0;

if ($ct > 1) 
{ 
$pcaption = trim($pcaption);
$pcaption = str_replace(" ","-",$pcaption);
$pcaption = str_replace("'","%27",$pcaption);
$key = array_search($pcaption , $caption_array); 
$prev = $key - 1; 
if ($prev < 0) $prev = $ct - 1; 
$next = $key + 1; 
if ($next == $ct) $next = 0; 
$total_count= count($caption_array);
$result_final .= "<div class='prevnext'>"; 
$result_final .= "<span class='prev'><a href=/viewgallery.php?cname=$cname&pcaption=".$caption_array[$next]."><img src=/photos/assets/left.png  border=0 ></a></span>"; 
$result_final .= "<span class='next'><a href=/viewgallery.php?cname=$cname&pcaption=".$caption_array[$prev].">
<img src=/photos/assets/right.png  border=0 ></a></span>"; 
$result_final .= "</div>";  


}            
}
$cname = str_replace(" ", "-", $cname);
$images_dir =str_replace(".","",$images_dir);
#$result_final .= "<div class=limage><table><tr><td><table class=image><tr>\
\	<td><a href=/viewgallery.php?cname=$cname&pcaption=".$caption_array[$next].">
#<img 	src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_keywords."' /></a>
#<div class=caption>".$photo_caption."</div> 
#<div class='excerpt'>".$photo_description."</div> 
#</td>                    
#</tr></table></td></tr></table><div class=underline></div></div>
#<!-- .limagePage --></div>	";  

$result_final .= "<div class=limage><table><tr><td><table class=image><tr>\
\	<td><a href=/viewgallery.php?cname=$cname&pcaption=".$caption_array[$next].">
<img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_keywords."'/></a>
<div class=caption>".$photo_caption."</div> 
<div class='excerpt'>".$photo_description."</div> 
</td>                    
</tr></table></td></tr></table><div class=underline></div></div>
<!-- .limagePage --></div>	";  

Hi,

So, I’ve been playing with this and have taken the following approach:

I created a table called images with a few entries.
Each row contains the file path, a caption and a slug.

When my page loads, it fires off one AJAX request (you could do this on a per gallery basis) and fetches all of the picture details in JSON format.
It then inserts the image dynamically (changing the src attribute), as well as the caption and appends the slug to the url.

Here’s a first demo.
Navigation works both by clicking “Prev” and “Next”, as well as with the arrow keys.

Does that seem to be what you’re after?

Pullo…did you just create a whole gallery code !!! :slight_smile: Wow… yes I was just looking for a way to get my gallery do arrow key navigation. But of course that’s what I was looking for …and does this handle large number of images well?

I guess so :slight_smile:

I’m not saying that this is the only way.
I’m not even sure that this is the best way.
For example, another idea I had, was that you could have your PHP page echo out the respective links to the “Previous” and “Next” pages, then when a user presses a left/right arrow key, you could simulate a click on said links.
I discounted this originally, as it means loading a whole new page for each image, which might make for a slower, clunkier experience for your users.
Nonetheless, if you are considering more of a PHP solution, then it might be worth asking in the PHP forum.

It will handle them, but I dunno how well.
How large is large?

Pullo…Thank you for being so helpful.I was thinking about this. I think your discounted idea is more appealing to me ( I could be wrong ). And I think if we go with the demonstrated method above we also need to work on creating thumbnails. Since I would like to have thumbnails displayed in a gallery.

If it is not too much can we try the second idea echoing out the links? I am sorry if I am asking too much. Thank you so much for taking your time helping me…

Hi there,

Sure. I see from your site you have a “Previous” and “Next” button already.

<span class="prev"><a href="/viewgallery.php?cname=Colorado-Fall&amp;pcaption=Gold"><img src="/photos/assets/left.png" border="0"></a></span>
<span class="next"><a href="/viewgallery.php?cname=Colorado-Fall&amp;pcaption=Golden-Light"><img src="/photos/assets/right.png" border="0"></a></span>

Try adding the following code to the bottom of your page (I’m guessing it’s a template), just before the closing <body> tag:

$(document).keydown(function(e){
  if (e.keyCode == 37) { 
    e.preventDefault();
    window.location.href = $(".prev > a").attr("href");
  } else if (e.keyCode == 39) { 
    e.preventDefault();    
    window.location.href = $(".next > a").attr("href");
  }
});

This doesn’t include any error checking, but will get the ball rolling in the right direction.
Let me know if that works.

Yes they are the ‘previous’ and ‘next’ buttons.

Try adding the following code to the bottom of your page (I’m guessing it’s a template), just before the closing <body> tag:

$(document).keydown(function(e){
  if (e.keyCode == 37) { 
    e.preventDefault();
    window.location.href = $(".prev > a").attr("href");
  } else if (e.keyCode == 39) { 
    e.preventDefault();    
    window.location.href = $(".next > a").attr("href");
  }
});

This doesn’t include any error checking, but will get the ball rolling in the right direction.
Let me know if that works.

Pullo… I did add that and for some reason that did not work. I am adding my template below…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="/viewgallery.php/"/>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<!-- <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/> -->
<title>$p_caption - Rajeev Thomas Photography </title>
<link rel="stylesheet" type="text/css" href="/styles.css"/>
<meta name="author" content="by Rajeev Thomas.">
<meta name="Copyright" content="All rights reserved by Rajeev Thomas, Rajeev Thomas Photography 2011.">  
<meta name="description" content= "$gallery_description","Rajeev Thomas Photography.">
<meta name="keywords" content="$p_keywords,Photography by Rajeev Thomas.">
<link rel="shortcut icon" type="image/x-icon" href="/favicon1.0.ico"/>
</head>
<body>
<div class="wrapper">
<h1 id="header1"><a href="/viewgallery.php"><span></span></a></h1>
<div class="main">
<ul class="nav">
<li><a href="/index.php">HOME&nbsp;&nbsp;|</a></li>
<li><a href="/viewgallery.php">MY GALLERIES&nbsp;&nbsp;|</a></li>
<li><a href="/journal">NOTES&nbsp;&nbsp;|</a></li>
<li><a href="/searchmyway.php">SEARCH&nbsp;&nbsp;|</a></li>
<li><a href="/aboutme.php">ABOUT ME</a></li>
</ul>

$result_final

</div>
</div>
<div class="footer">
<div class="facebook"><ul><li><a href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.rajeevthomas.com%2F&t=Rajeev%20Thomas" class=facebook><img src="http://www.rajeevthomas.com/photos/assets/facebook.png"></a></li></ul></div>
<div class="footinner">
<form name="seeker" id="seeker" action="searchmyway.php" method="get"><input type="text" name="q" id="search"/>
<input type="submit"  id="find" value="Search" />
<input type="hidden" name="form_id:search" value="1" />
</form></div><!--footinner-->
<p class="footnote">All content and website by Rajeev Thomas. All rights reserved. All &copy; by Rajeev Thomas. </p>
</div>
$(document).keydown(function(e){
  if (e.keyCode == 37) { 
    e.preventDefault();
    window.location.href = $(".prev > a").attr("href");
  } else if (e.keyCode == 39) { 
    e.preventDefault();    
    window.location.href = $(".next > a").attr("href");
  }
});

</body>
</html>

Oh sorry, you need to contain them in <script> tags:

<script>
$(document).keydown(function(e){
  if (e.keyCode == 37) { 
    e.preventDefault();
    window.location.href = $(".prev > a").attr("href");
  } else if (e.keyCode == 39) { 
    e.preventDefault();    
    window.location.href = $(".next > a").attr("href");
  }
});
</script>

Pullo,

I tried the script with those tags as well yesterday but that did not work also…

Man, I’m sorry. It was late when I posted that. You have to include the jQuery library, too:

[B]<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>[/B]
<script>
$(document).keydown(function(e){
  if (e.keyCode == 37) { 
    e.preventDefault();
    window.location.href = $(".prev > a").attr("href");
  } else if (e.keyCode == 39) { 
    e.preventDefault();    
    window.location.href = $(".next > a").attr("href");
  }
});
</script>