Jump to content

trying to add 1 to a variable


AstrosMF

Recommended Posts

trying to do a simple photo gallery... when the page loads it looks for $pic in the address bar... if it's not there then it assigns $pic as 1... in a link i have for the "next" button i want it to add 1 to $pic so it will goto $pic = 2... see what i have and what i am doing wrong... thanks

<?php

$pic = ($_GET['pic']) ? $_GET['pic'] : 1;

echo '<a href="index.php?sub=sb_photo&gallery=01&album=01&start=yes&pic='.$pic++.'"><img width="111" height="20" src="images/next.gif"></a>';

?>

Link to comment
https://forums.phpfreaks.com/topic/43215-trying-to-add-1-to-a-variable/
Share on other sites

What is this?

 

$pic = ($_GET['pic']) ? $_GET['pic'] : 1;

 

Why would you not write it like this:

 

$pic = $_GET['pic'];

 

if($pic == "")

{

$pic == "1";

}

 

Actually I'm not sure if $pic == "1" needs the quotes or not.  I don't do much with numbers in php.  But anyways thats how I would write that part.  Then for the url string I always write them like this:

 

echo "<a href=\"index.php?sub=sb_photo&gallery=01&album=01&start=yes&pic=". $pic ."><img width=\"111\" height=\"20\" src=\"images/next.gif\"></a>";

 

This is completely untested code, but it should work.

What is this?

 

$pic = ($_GET['pic']) ? $_GET['pic'] : 1;

 

what the OP has written will work.

 

in my belief, $i++ and ++$i will both have the same effect. try this:

 

<?php

$pic = ($_GET['pic']) ? $_GET['pic'] : 1;

echo "<a href=\"index.php?sub=sb_photo&gallery=01&album=01&start=yes&pic=".($pic++)."\"><img width=\"111\" height=\"20\" src=\"images/next.gif\"></a>";

?>

 

good luck. :)

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.