Jump to content

[SOLVED] Next / Previous Links


Recommended Posts

OK, I have a page that displays 1 image, I am trying to make next / previous links, they will look somthing like this:

<a class="bodyLink" href="enlargeImage?userID=1&id=13">Next</a>

 

Then when the user clicks on next, and the new page displays, the Next link could look something like this:

<a class="bodyLink" href="enlargeImage?userID=1&id=25">Next</a>

 

Notice the bold words in each, the next/previous id variable may not be in 1,2,3 order, but in 1,5,10 order.

 

So... how do I make next/previous links like this?

Link to comment
https://forums.phpfreaks.com/topic/49689-solved-next-previous-links/
Share on other sites

have three javascript variables and write a small javascript function, something like

var prev = "";
var now = ""
var next = "";
function prevNext()
{
prev = now;
now = next;
next = however you get the next image number;
}

then use the variables prev and next

<a class="bodyLink" href="enlargeImage?userID=1&id=" + prev>Previous[/url]
<a class="bodyLink" href="enlargeImage?userID=1&id=" + next>Next[/url]

 

then run the function onclick of an image link

then no function is required

<?php
$sql = mysqli_query($db,"SELECT * FROM default_images WHERE userID = '$userID' ORDER BY id ASC")or die(mysqli_error($db));
$count = mysqli_num_rows($sql);
while($row = mysqli_fetch_array($sql))
{
$picArray[] = $row['picture number']; //place all picture codes into an array
}
?>

 

then for your next and previous links

<a class="bodyLink" href="enlargeImage?userID=1&id="<?php echo prev($picArray); ?> >Previous</a>
<a class="bodyLink" href="enlargeImage?userID=1&id="<?php echo next($picArray); ?> >Next</a>

 

 

the code has been edited from its origional, if you are looking at it again I have made correct the next / prev code

 

 

This doesn't work:

 

<?php
$sql = mysqli_query($db,"SELECT * FROM default_images WHERE userID = '$userID' ORDER BY id ASC")or die(mysqli_error($db));
while($row = mysqli_fetch_array($sql)){
$picArray[] = $row['id']; //place all picture codes into an array
}
echo'
<p style="text-align:center">
<a class="bodyLink" href="enlargeImage?userID='.$userID.'&id='.prev($picArray).'" >Previous</a> |
<a class="bodyLink" href="enlargeImage?userID='.$userID.'&id='.next($picArray).'" >Next</a>
</p>';
?>

 

If it helps, here is the page:

http://auto.tzfiles.com/enlargeImage?userID=1&id=5

the next link should go to this one:

http://auto.tzfiles.com/enlargeImage?userID=1&id=12

I have had a go at something else, as the page is refreshed when the image changes try this

<?php
$sql = mysqli_query($db,"SELECT * FROM default_images WHERE userID = '$userID' ORDER BY id ASC")or die(mysqli_error($db));
while($row = mysqli_fetch_array($sql)){
$picArray[] = $row['id']; //place all picture codes into an array
}
$count = count($picArray);
if (isset($_GET['arrid']))
{
	$arrid = $_GET['arrid'];
	if($arrid == "")
	{
	$arrid = 0;
}
	if ($arrid < $count - 1)
	{
	$previd = $arrid-1;
	$nexid = $arrid+1;
$prev = $picArray[$previd];
$next = $picArray[$nexid];
}
}
?>

 

then

 

<p style="text-align:center">
<a class="bodyLink" href='enlargeImage?userID=<?php echo $userID; ?>&arrid=<?php echo $previd; ?>&id=<?php echo $prev; ?>'>Previous</a>
<a class="bodyLink" href='enlargeImage?userID=<?php echo $userID; ?>&arrid=<?php echo $nexid; ?>&id=<?php echo $next; ?>'>Next</a>
</p>

 

I have sent across with the id a value called arrid, which is the array key variable, the php function then assigns all the variables that are needed for the next and previous

 

OK... I got it, and if anyone wants to know how... Here it is:

 

<?php
$sql = mysqli_query($db,"SELECT * FROM default_images WHERE userID = '$userID' ORDER BY id ASC")or die(mysqli_error($db));
while($row = mysqli_fetch_array($sql)){
$idArray[] = $row['id'];
}
$key = array_search($id,$idArray);
$mode = current($idArray);
for($i=0;$i<$key;$i++){
$mode = next($idArray);
}
$mode = prev($idArray);
if($mode == ""){
$mode = end($idArray);
}
echo'
<p style="text-align:center">
« <a class="bodyLink" href="enlargeImage?userID='.$userID.'&id='.$mode.'" >Previous</a> |';

$mode = reset($idArray);
for($i=0;$i<$key;$i++){
$mode = next($idArray);
}
$mode = next($idArray);
if($mode == ""){
$mode = reset($idArray);
}
echo'
<a class="bodyLink" href="enlargeImage?userID='.$userID.'&id='.$mode.'" >Next</a> »
</p>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.