I am trying to show images from database with next previous button.
here’s my code:
<?PHP
session_start();
$page = "voting.php";
include ('dbconnect.php');
$q = "SELECT * FROM vote_frames";
$r = mysql_query($q);
while($row = mysql_fetch_array($r)){
$i_array[] = $row['frame_pic'];
$i_array1[] = $row['frame_pic1'];
$i_array2[] = $row['frame_pic2'];
$id_array[] = $row['frame_id'];
}
$fpp = 1;
$num_images = count($i_array);
$image_path = "frames/";
$y = $fpp;
if(!isset($_GET['first'])){
$first = 0;
}else{
$first = (int) $_GET['first'];
}
$x = $num_images - 1;
$z = $x - $y;
if($first>$z) {
$first = $z;
}
$last = $first + $fpp;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
</head>
<body>
<div>
<?PHP
$i = $first;
$prev = $first-1;
$next = $first +1;
if($prev<0){ $prev = 0; }
?><a href="<?PHP echo $page; ?>?first=<?PHP echo $prev; ?>">Previous</a><?PHP
while($i<$last) {
$f = $image_path . $i_array[$i];
$f1 = $image_path . $i_array1[$i];
$f2 = $image_path . $i_array2[$i];
?>
<img src="<?PHP echo $f; ?>">
<img src="<?PHP echo $f1; ?>">
<img src="<?PHP echo $f2; ?>">
<?PHP
$i++;
}
?><a href="<?PHP echo $page; ?>?first=<?PHP echo $next; ?>"><Next</a><?PHP
?>
</div>
</body>
</html>
Images are displaying but with a strange behavior that last record is not showing. Actually for testing when I echoed id of current showing record, it shows next’s id so record is having id of next’s.
I know I can do it with jquery w/o any issue but I must use a way to store current showing pic’s id in session so I can show current showing and its next picture’s vote count in the graph below.
Any take on it?
This part here is VERY cryptic, why you have written it that way? I assume you have tested several ways to do it, but … clean up code, while you correct or test it. That way it’s not too cryptic to read.
And there is your problem too
$xis number of images minus one, and$zis$xminus$y(which is one, cryptically yes). Which means$zisnumber of images minus 2. Thats why last record is not showing. Clean up that code, and it starts working, it would be easy to fix, but I’ll leave it to you as practise.