PHP - Next/Previous picture link

Creating Next/Previous link with PHP

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 8307 Views - Last Post: 30 August 2009 - 10:50 PM Rate Topic: -----

#1 Chriszea101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-August 09

PHP - Next/Previous picture link

Posted 29 August 2009 - 12:28 AM

Hi all, as part of an assignment i need to make a next/previous button that will iterate back and forth between pictures.
It MUST be done in PHP no java etc to achieve the outcome.

The basics, we have several variables:

$id = 74;
$last = 76;
$first = 71;
$file = "../ass1_data_stage1/jpg/P$id.JPG";
$self = $_SERVER['PHP_SELF'];

basically i want to increment $id by 1 when i hit next and down when i hit previous but also reload the page using $self.

Here is my code so farrrrr ...


$id = 74;
$last = 76;
$first = 71;
$file = "../ass1_data_stage1/jpg/P$id.JPG";

echo "<p><img src=\"$file\"></p>";

if ($id = 71) {
echo "<a href=$self><h2>NEXT</h2></a>";
} 
elseif ($id = 76) { 
echo "<a href=$self><h2>PREVIOUS</h2></a>"; 
}
else {
echo "<a href=$self><h2>NEXT</h2></a><a href=$self><h2>PREVIOUS</h2></a>";
}




I didnt bother including the useless code, my IF statements dont seem to worth either :angry: .

ANY help would be appreciated!! Thank you in advance!

zea01

Is This A Good Question/Topic? 0
  • +

Replies To: PHP - Next/Previous picture link

#2 paperclipmuffin  Icon User is offline

  • Disassembling...
  • member icon

Reputation: 13
  • View blog
  • Posts: 944
  • Joined: 16-April 09

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 01:15 AM

So what other problems are you having?

P.S. Just a hunch, but try
$id = 74;
$last = 76;
$first = 71;
$file = "../ass1_data_stage1/jpg/P$id.JPG";

echo "<p><img src=\"$file"\></p>";

if ($id = 71) {
echo "<a href='$self'><h2>NEXT</h2></a>";
}
elseif ($id = 76) {
echo "<a href='$self'><h2>PREVIOUS</h2></a>";
}
else {
echo "<a href='$self'><h2>NEXT</h2></a><a href='$self'><h2>PREVIOUS</h2></a>";
}





(YAY! Another Auzzie!)

This post has been edited by paperclipmuffin: 29 August 2009 - 01:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 01:57 AM

Does it always start on image one or do they see thumbnails of the images first so the can pick where it starts?

tommyflint
Was This Post Helpful? 0
  • +
  • -

#4 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 02:11 AM

Does it always start on image one or do they see thumbnails of the images first so the can pick where it starts?

tommyflint

edit: opps didn't mean to double post.... my bad

This post has been edited by tommyflint: 29 August 2009 - 02:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 Chriszea101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-August 09

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 04:52 AM

Yes Tommy it will always begin with the first picture, number 71, and iterate through to 76.

I have two problems within my code at the moment, one is the 'IF' statment:
if ($id = 71) {
echo "<a href='$self'><h2>NEXT</h2></a>";
}
elseif ($id = 76) {
echo "<a href='$self'><h2>PREVIOUS</h2></a>";
}
else {
echo "<a href='$self'><h2>NEXT</h2></a><a href='$self'><h2>PREVIOUS</h2></a>";
}


Where no matter what the value of $id, the page always shows only NEXT instead of showing just PREVIOUS at img 76 and both NEXT and PREVOUS inbetween 71 and 76.

My second problem is the link:
<a href='$self'><h2>NEXT</h2></a>


I need this so that when NEXT is pressed $id is incremented by 1 so the next picture is shown, and the page reloaded (using $self which reloads the page).

THANK YOU ALL for your replys :rolleyes:

Any help is awesome!

(GO AUZZIES!!) :P
Was This Post Helpful? 0
  • +
  • -

#6 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 05:38 AM

Are the images, or relative links to them, stored in a database?

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,342
  • Joined: 08-August 08

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 06:34 AM

I would use an html form with submit buttons, then increment/decrement the $first and $last by the difference between them depending on which submit button was clicked.

if(isset($_POST['Previous']))
{
  $first -= 6; 
  if($first < 0) $first = 0;
  $last = $first + 5;
} elseif(isset($_POST['Next']))
{
  $last += 6;
  if($last > $number_of_items) $last = $number_of_items;
  $first = $last - 5;
}  


This post has been edited by CTphpnwb: 29 August 2009 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

#8 sfurnival  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 27-August 09

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 08:39 AM

This is untested but demonstrates what you need to do. Each time the page is called you need to update that $id otherwise your never going anywhere. Secondly you need to setting next & previous link urls based off of your current id.


if (isset($_REQUEST['id'])) {
	$id = $_REQUEST['id'];
} else {
	$id = 74; // Whatever the default starting value should be
}

$self = $_SERVER['PHP_SELF'] + "?id=";

$last = 76;
$first = 71;
$file = "../ass1_data_stage1/jpg/P$id.JPG";

echo "<p><img src=\"$file\"></p>";

if ($id < $last) {
	echo "<a href=".$self.($id + 1)."><h2>NEXT</h2></a>";
} 
if ($id > $first) { 
	echo "<a href=".$self.($id - 1)."><h2>PREVIOUS</h2></a>"; 
}


Was This Post Helpful? 1
  • +
  • -

#9 feLix_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-August 09

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 02:57 PM

btw. You have to use (=) double in if statements


$id = 74;
$last = 76;
$first = 71;
$file = "../ass1_data_stage1/jpg/P$id.JPG";

echo "<p><img src=\"$file\"></p>";

if ($id == 71) {
echo "<a href=$self><h2>NEXT</h2></a>";
}
elseif ($id == 76) {
echo "<a href=$self><h2>PREVIOUS</h2></a>";
}
else {
echo "<a href=$self><h2>NEXT</h2></a><a href=$self><h2>PREVIOUS</h2></a>";
}
 

This post has been edited by feLix_: 29 August 2009 - 02:58 PM

Was This Post Helpful? 0
  • +
  • -

#10 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 03:01 PM

View PostChriszea101, on 29 Aug, 2009 - 05:52 AM, said:

I have two problems within my code at the moment, one is the 'IF' statment:
if ($id = 71) {
echo "<a href='$self'><h2>NEXT</h2></a>";
}
elseif ($id = 76) {
echo "<a href='$self'><h2>PREVIOUS</h2></a>";
}
else {
echo "<a href='$self'><h2>NEXT</h2></a><a href='$self'><h2>PREVIOUS</h2></a>";
}


Where no matter what the value of $id, the page always shows only NEXT instead of showing just PREVIOUS at img 76 and both NEXT and PREVOUS inbetween 71 and 76.


The problem with the if statements is that you are using the assignment operator (=) instead of the comparison operator (==). It hits if ($id = 71), assigns 71 to $id and, since it succeeded in doing that, the statement is interpreted as true so it creates the "NEXT" link and skips over the elseif and else clauses.

Edit: I see feLix posted basically the same thing while I was busy writing. Sorry for the repeated information.

This post has been edited by smacdav: 29 August 2009 - 03:03 PM

Was This Post Helpful? 1
  • +
  • -

#11 Chriszea101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-August 09

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 11:29 PM

AWESOME!!! Thank you all very much, i just woke up so im going to try all the suggestions now.

Also my bad with the id = 71 instead of id ==71, thank you, its always something so very simple.

I shall give you all my thanks and post my success!!

Zea01
Was This Post Helpful? 0
  • +
  • -

#12 Chriszea101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-August 09

Re: PHP - Next/Previous picture link

Posted 29 August 2009 - 11:56 PM

Quoting sfurnival :^:

View Postsfurnival, on 29 Aug, 2009 - 07:39 AM, said:

This is untested but demonstrates what you need to do. Each time the page is called you need to update that $id otherwise your never going anywhere. Secondly you need to setting next & previous link urls based off of your current id.


if (isset($_REQUEST['id'])) {
	$id = $_REQUEST['id'];
} else {
	$id = 74; // Whatever the default starting value should be
}

$self = $_SERVER['PHP_SELF'] + "?id=";

$last = 76;
$first = 71;
$file = "../ass1_data_stage1/jpg/P$id.JPG";

echo "<p><img src=\"$file\"></p>";

if ($id < $last) {
	echo "<a href=".$self.($id + 1)."><h2>NEXT</h2></a>";
} 
if ($id > $first) { 
	echo "<a href=".$self.($id - 1)."><h2>PREVIOUS</h2></a>"; 
}




I understood this code and how it works the best, the IF statement now works perfectly (thank you). Only thing now is the link ...

  $self = $_SERVER['PHP_SELF'] + "?id=";
if ($id < $last) {
	echo "<a href=".$self.($id + 1)"><h2>NEXT</h2></a>";
}



When i try this code, i get an error that the path "http://localhost/assignment_1/stage1/.0.(71.." Could not be found. This means that $self is equal to 0 instead of equaling the file path, and that ($id + 1) is being used as part of the path .. hmmm .. i tried many other different methods to try and get it to work i also tried just putting the $id + 1 as code instead of in the path name, no go.

Any help?

:pirate:
Was This Post Helpful? 0
  • +
  • -

#13 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: PHP - Next/Previous picture link

Posted 30 August 2009 - 12:10 AM

The + sign is not a concatenation operator in PHP. $self should be assigned as follows:

$self = $_SERVER['PHP_SELF'] . "?id=";


This post has been edited by smacdav: 30 August 2009 - 12:10 AM

Was This Post Helpful? 0
  • +
  • -

#14 Chriszea101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-August 09

Re: PHP - Next/Previous picture link

Posted 30 August 2009 - 02:13 AM

View PostShaneK, on 29 Aug, 2009 - 04:38 AM, said:

Are the images, or relative links to them, stored in a database?

Yours,
Shane~


Nope, they are merely in folders, there is no database just folders.

Thx smacdav, but still im getting $self = to 0! ... BAH!!
Was This Post Helpful? 0
  • +
  • -

#15 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP - Next/Previous picture link

Posted 30 August 2009 - 02:41 AM

does what smacdav suggested work now as you were using the wrong concatenation sign?

tommyflint

edit opps juse saw what you wrote?

This post has been edited by tommyflint: 30 August 2009 - 02:51 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2