Jump to content

[SOLVED] Question


adam291086

Recommended Posts

Ahh beat me

OK option 2

$query=mysql_query("SELECT MAX(id) as maxID FROM `table` LIMIT 1");
$row=mysql_fetch_array($query);
echo $row['maxID'];

 

 

 

BUT i think you MAYBE doing something bad here

 

if you want the last ID entered after an insert use

mysql_insert_id()

 

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());

(assuming ID is an AUTO_INCREMENT )

 

as if 2 members insert at the same time your get a problem using the highest

Link to comment
Share on other sites

Ok guys i am really really stuck. I have a page displaying a photo albums content. I have a next button and all it does is take the Photo Id current displayed and +1. The problem is that it carrys on even when there is no photo. I wanted to find the max number and create an if statement saying If id exceeds the max ID then redirect to start of album. I have tried all method for days. Below is the basic code to display the gallery. Please can you point me in the right direction.

 

<?php
include("config.php");
if( !$_GET['photo_id'] ){
$msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view.";
// Display error message
displayPage($msg);
die();
}

// get picture details 

$con;
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = " . addslashes($_GET['photo_id']);
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
while($row = @mysql_fetch_array($result))
{
$photo_title = $row['photo_title'];
$photo_loc = $row['photo_location'];
$album = $row['album_id'];

}	
?>

<html>
<head>
<title>Gallery - <?php echo($photo_title); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Gallery - <?php echo($photo_title); ?></td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td>
<img src="<?php echo($photo_loc); ?>" />
</td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><a href="gallery.php">View Gallery</a></td>
</tr>
<tr>
<td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td>
</tr>
<tr>
<td><a href="view_photo.php?photo_id=<?php echo ($_GET['photo_id']+1); ?>">Next</a></td>
</tr>
<tr>
<td><a href="view_photo.php?photo_id=<?php echo ($_GET['photo_id']-1); ?>">Previous</a></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

Link to comment
Share on other sites

maybe

 

<?php
include("config.php");
if( !isset($_GET['photo_id']) ){
$msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view.";
// Display error message
displayPage($msg);
die();
}

// get picture details 

$con;
$id = (int)$_GET['photo_id'];
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $id");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
if($num_rows == 0)
{
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = 1");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
}

while($row = @mysql_fetch_array($result))
{
$photo_title = $row['photo_title'];
$photo_loc = $row['photo_location'];
$album = $row['album_id'];

}	
?>

Link to comment
Share on other sites

No the idea is that the html code is supposed to run, therefore nothing is displayed as that photo_id dosen't have a picture attached to it . I think i need to run a check to ensure the Photo_id is in the database and if it isn't then return something else. How can i go about this?

Link to comment
Share on other sites

i assumed ID 1 has an image..

 

quick update..

 

totally untested

 

<?php
include("config.php");

// get picture details 
$con;
$id = (int)$_GET['photo_id'];
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $id");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{
$row = @mysql_fetch_array($result);
$photo_title = $row['photo_title'];
$photo_loc = $row['photo_location'];
$album = $row['album_id'];


//check Next
$nid = $id+1;
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $nid");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
$Next = ($num_rows > 0);
//check Previous
$pid = $id-1;
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $pid");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
$Previous = ($num_rows > 0);
?>
<html>
<head>
<title>Gallery - <?php echo($photo_title); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Gallery - <?php echo($photo_title); ?></td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td>
<img src="<?php echo($photo_loc); ?>" />
</td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><a href="gallery.php">View Gallery</a></td>
</tr>
<tr>
<td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td>
</tr>
<tr>
<td>
<?php
echo ($Previous)?""<a href='view_photo.php?photo_id=$pid'>Previous</a>:"No more";
?>
</td>
</tr>
<tr>
<td>
<?php
echo ($Next)?""<a href='view_photo.php?photo_id=$nid'>Next</a>:"No more";
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

<?php
}else{
$msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view.";
// Display error message
displayPage($msg);
die();
}
?>

Link to comment
Share on other sites

updated

what () did i miss ?

 

<?php
include("config.php");

// get picture details 
$con;
$id = (int)$_GET['photo_id'];
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $id");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{
$row = @mysql_fetch_array($result);
$photo_title = $row['photo_title'];
$photo_loc = $row['photo_location'];
$album = $row['album_id'];


//check Next
$nid = $id+1;
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $nid");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
$Next = ($num_rows > 0):true:false;
//check Previous
$pid = $id-1;
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $pid");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
$Previous = ($num_rows > 0):true:false;
?>
<html>
<head>
<title>Gallery - <?php echo($photo_title); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Gallery - <?php echo($photo_title); ?></td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td>
<img src="<?php echo($photo_loc); ?>" />
</td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><a href="gallery.php">View Gallery</a></td>
</tr>
<tr>
<td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td>
</tr>
<tr>
<td>
<?php
echo ($Previous === true)?"<a href='view_photo.php?photo_id=$pid'>Previous</a>":"No more";
?>
</td>
</tr>
<tr>
<td>
<?php
echo ($Next === true)?"<a href='view_photo.php?photo_id=$nid'>Next</a>":"No more";
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

<?php
}else{
$msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view.";
// Display error message
displayPage($msg);
die();
}
?>

Link to comment
Share on other sites

ok heres a brief description:

 

When you click on view gallery you can a list of thumbnails representing all the different photo albums. If you click on one a load of thumbnails appear showing all the pictures within that album. Then if you click on an individual picture the full image is displayed. Below the full images is the next button. This button should only take you through the pictures within that album. At present the next button takes you through every single photo in the database regardless of the album.

Link to comment
Share on other sites

try this, first

 

<?php
$con;
$id = (int)$_GET['photo_id'];
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos LIMIT $id, 1");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
?>

 

if thats fine then

try this

 

<?php
$con;
$id = (int)$_GET['photo_id'];
$aid = (int)($_GET['album_id']);
$where = ($aid>0)?" WHERE album_id = $aid":"";
$sql = "SELECT photo_title, photo_id, photo_location, album_id FROM photos $where LIMIT $id, 1");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
?>

 

Links

echo ($Previous === true)?"<a href='view_photo.php?photo_id=$pid&album _id=$album'>Previous</a>":"No more";

Link to comment
Share on other sites

<?php
include("config.php");

// get picture details 
$con;
$id = (int)$_GET['photo_id'];
$sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $id");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 

$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{
$row = @mysql_fetch_array($result);
$photo_title = $row['photo_title'];
$photo_loc = $row['photo_location'];
$album = $row['album_id'];


//check Next
$nid = $id+1;
$sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $nid");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
$Next = ($num_rows > 0);


//check Previous
$pid = $id-1;
$sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos WHERE photo_id = $pid");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
$Previous = ($num_rows > 0);
?>
<html>
<head>
<title>Gallery - <?php echo($photo_title); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Gallery - <?php echo($photo_title); ?></td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td>
<img src="<?php echo($photo_loc); ?>" />
</td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><a href="gallery.php">View Gallery</a></td>
</tr>
<tr>
<td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td>
</tr>
<tr>
<td>
<?php
echo ($Previous === true)?"<a href='view_photo.php?photo_id=$pid'>Previous</a>":"No more";
?>
</td>
</tr>
<tr>
<td>
<?php
echo ($Next === true)?"<a href='view_photo.php?photo_id=$nid'>Next</a>":"No more";
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

<?php
}else{
$msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view.";
// Display error message
displayPage($msg);
die();
}
?>

Link to comment
Share on other sites

this should work!

(kinida hard to check while at work ;))

<?php
include("config.php");

// get picture details 
$con;
$id = (int)$_GET['photo_id'];
$aid = (int)($_GET['album_id']);
$where = ($aid>0)?" WHERE album_id = $aid":"";

$sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos $where LIMIT $id, 1");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 

$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{
$row = @mysql_fetch_array($result);
$photo_title = $row['photo_title'];
$photo_loc = $row['photo_location'];
$album = $row['album_id'];


//check Next
$nid = $id+1;
$sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos $where LIMIT $nid, 1");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
$Next = ($num_rows > 0);


//check Previous
$pid = $id-1;
$sql = ("SELECT photo_title, photo_id, photo_location, album_id FROM photos $where LIMIT $pid, 1");
$result = @mysql_query($sql) or die("Error retrieving record: " . mysql_error()); 
$num_rows = mysql_num_rows($result);
$Previous = ($num_rows > 0);
?>
<html>
<head>
<title>Gallery - <?php echo($photo_title); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Gallery - <?php echo($photo_title); ?></td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td>
<img src="<?php echo($photo_loc); ?>" />
</td>
</tr>
</table>
<table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><a href="gallery.php">View Gallery</a></td>
</tr>
<tr>
<td><a href="display_album.php?album_id=<?php echo ($album); ?>">Return to Photo Album</a></td>
</tr>
<tr>
<td>
<?php
echo ($Previous === true)?"<a href='view_photo.php?photo_id=$pid&album _id=$album'>Previous</a>":"No more";
?>
</td>
</tr>
<tr>
<td>
<?php
echo ($Next === true)?"<a href='view_photo.php?photo_id=$nid&album _id=$album'>Next</a>":"No more";
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

<?php
}else{
$msg .= "No photo selected! Please <a href=\"gallery.php\">choose an album</a> you would like to view.";
// Display error message
displayPage($msg);
die();
}
?>

Link to comment
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.