Jump to content

Recommended Posts

Hey guys,
I have a gallery for a website: http://www.vivagaete.com/gallery.php
If you click on the thumbnail it will bring up viewgallery.php - which has the larger image on it.

What I want, is for viewgallery.php to have the options for "PREVIOUS and NEXT" images.  So they don't have to click "Click here to Go Back" every time, they can browse by clicking the NEXT link or PREVIOUS.

How would I go about doing this to pull the next image ID from the database?
Here is the code for viewgallery.php:

[code]
<style type="text/css">
.greentext {
color: #2a8f3d;
margin: 4px;
font-family: Tahoma;
font-size: 19px;
}
.blacktext {
color: #000000;
margin: 4px;
font-family: Tahoma;
font-size: 11px;
}
</style>

<?
include 'db.php';
$getid = $_GET['id'];
$sql = "SELECT * FROM gallery WHERE id = $getid";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$picture = $row['picture'];
$heading = $row['heading'];
$description = $row['description'];
}

$sql2 = "SELECT id FROM gallery WHERE id > $id LIMIT 1";
$result2 = mysql_query($sql2);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
}

if ($getid == "$id") {

echo "<center>
<FORM>
$id
<INPUT type='button' value='Click here to go back' onClick='history.back()'>
</FORM>
<div class=\"greentext\"><b>$heading</b></div>
<div class=\"blacktext\">$description</div><br />
<img src=\"images/$picture\" style=\"border: 2px solid #d4cccc;\"><br /><br />

<FORM>
<INPUT type='button' value='Click here to go back' onClick='history.back()'>
</FORM></center>";

} else {

echo "No image verfied";

}

?>
[/code]

Thanks a lot!!
Link to comment
https://forums.phpfreaks.com/topic/28449-gallery-help/
Share on other sites

This should work (it has been a while since I've done some coding, so let me know if I made a mistake somewhere)

[code]
<style type="text/css">
.greentext {
color: #2a8f3d;
margin: 4px;
font-family: Tahoma;
font-size: 19px;
}
.blacktext {
color: #000000;
margin: 4px;
font-family: Tahoma;
font-size: 11px;
}
</style>

<?php
include 'db.php';
$getid = $_GET['id'];
$sql = "SELECT * FROM gallery WHERE id = $getid";
$result = mysql_query($sql);
$total_rows = mysql_num_rows($sql); // find out how many rows are in that table, see what the max number is
while ($row = mysql_fetch_array($result)) {
$picture = $row['picture'];
$heading = $row['heading'];
$description = $row['description'];
}

$sql2 = "SELECT id FROM gallery WHERE id > $id LIMIT 1";
$result2 = mysql_query($sql2);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
}

if ($getid == "$id") {
echo "<div align='center'>
  <div class=\"greentext\"><b>$heading</b></div>
  <div class=\"blacktext\">$description</div><br />
  <img src=\"images/$picture\" style=\"border: 2px solid #d4cccc;\"><br /><br />";
  if(($getid-1) <= 0) { // if id in link is less than or equal to 0
  echo "Previous Image"; // show just text
  } else {
  $id_prev = $getid-1; //otherwise, set variable
  echo "<a href='/viewgallery.php?id=".$id_prev."'>Previous Image</a>"; // and show a link to previous image
  }
  echo " - "; // spacer between links
  if(($getid+1) > $total_rows) { //if the id in link is more than total rows
  echo "Next Image"; //show just normal text.. no link
  } else {
  $id_next = $getid+1; // otherwise, set variable
  echo "<a href='/viewgallery.php?id=".$id_next."'>Next Image</a>"; //and provide a link to next image
  }
  echo "</div>"; // end center
} else {
  echo "No image verfied";
}
?>[/code]

[i]edited to make it easier to read the code - ACK I forgot a semicolon.. sorry![/i]
Link to comment
https://forums.phpfreaks.com/topic/28449-gallery-help/#findComment-130223
Share on other sites

Hey there,
Thakns for taking the time to supply that code!

I get: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /hsphere/local/home2/andres30/vivagaete.com/viewgallery.php on line 21

Line 21: $total_rows = mysql_num_rows($sql); // find out how many rows are in that table, see what the max number is

http://www.vivagaete.com/viewgallery.php?id=5
Link to comment
https://forums.phpfreaks.com/topic/28449-gallery-help/#findComment-130239
Share on other sites

Try:

[code]
<style type="text/css">
.greentext {
color: #2a8f3d;
margin: 4px;
font-family: Tahoma;
font-size: 19px;
}
.blacktext {
color: #000000;
margin: 4px;
font-family: Tahoma;
font-size: 11px;
}
</style>

<?php
include 'db.php';
$getid = $_GET['id'];
$sql = "SELECT * FROM gallery WHERE id = $getid";
$result = mysql_query($sql);
$total_rows = mysql_num_rows(mysql_query("SELECT * FROM gallery")); // find out how many rows are in that table, see what the max number is
while ($row = mysql_fetch_array($result)) {
$picture = $row['picture'];
$heading = $row['heading'];
$description = $row['description'];
}

$sql2 = "SELECT id FROM gallery WHERE id > $id LIMIT 1";
$result2 = mysql_query($sql2);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
}

if ($getid == $id) {
echo "<div align='center'>
  <div class=\"greentext\"><b>$heading</b></div>
  <div class=\"blacktext\">$description</div><br />
  <img src=\"images/$picture\" style=\"border: 2px solid #d4cccc;\"><br /><br />";
  if(($getid-1) <= 0) { // if id in link is less than or equal to 0
  echo "Previous Image"; // show just text
  } else {
  $id_prev = $getid-1; //otherwise, set variable
  echo "<a href='/viewgallery.php?id=".$id_prev."'>Previous Image</a>"; // and show a link to previous image
  }
  echo " - "; // spacer between links
  if(($getid+1) == $total_rows) { //if the id in link is more than total rows
  echo "Next Image"; //show just normal text.. no link
  } else {
  $id_next = $getid+1; // otherwise, set variable
  echo "<a href='/viewgallery.php?id=".$id_next."'>Next Image</a>"; //and provide a link to next image
  }
  echo "</div>"; // end center
} else {
  echo "No image verfied";
}
?>[/code]

Also, I just noticed you're not always having a numerical order - like you start at 5, there isn't a 15, etc.

Which, my code will work, but you're going to be cutting off a few images at the end and it will show the images that aren't there -- I recommend just making your table be numerical order.

Or, if somebody else would like to post some code showing how to do it another way ;)
Link to comment
https://forums.phpfreaks.com/topic/28449-gallery-help/#findComment-130245
Share on other sites

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.