AstrosMF Posted March 18, 2007 Share Posted March 18, 2007 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 More sharing options...
jokur Posted March 18, 2007 Share Posted March 18, 2007 Try ++$pic. Link to comment https://forums.phpfreaks.com/topic/43215-trying-to-add-1-to-a-variable/#findComment-209813 Share on other sites More sharing options...
s0c0 Posted March 18, 2007 Share Posted March 18, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43215-trying-to-add-1-to-a-variable/#findComment-209815 Share on other sites More sharing options...
Stopofeger Posted March 18, 2007 Share Posted March 18, 2007 yap. try ++$pic instead of $pic++. the reason is, $pic++ returns the variable and then incriments. while ++$pic increments the value and then returns. Link to comment https://forums.phpfreaks.com/topic/43215-trying-to-add-1-to-a-variable/#findComment-209818 Share on other sites More sharing options...
JasonLewis Posted March 18, 2007 Share Posted March 18, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43215-trying-to-add-1-to-a-variable/#findComment-209826 Share on other sites More sharing options...
Barand Posted March 18, 2007 Share Posted March 18, 2007 To avoid error messages $pic = ( isset($_GET['pic']) ) ? $_GET['pic'] : 1; Link to comment https://forums.phpfreaks.com/topic/43215-trying-to-add-1-to-a-variable/#findComment-209842 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.