PHP Increment/Decrement

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

PHP Increment/Decrement

Post by jadecook on Mon Nov 19, 2012 7:02 pm
([msg=71109]see PHP Increment/Decrement[/msg])

This might sound a bit noobish but I am having trouble creating a increment/decrement button. So basically, I created a website that uses Tumblr feed to view photos that I've tagged. For example, if I pass a variable called "nav" that equals california, it will show all of my photos from california by using this code.

Code: Select all
<?php
$location = $_GET['nav'];
echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["nav"] . "/js' type='text/javascript'></script>";
?>


But the problem I am having is that it only shows the first 10 photos and in order for me to view the next page I have to add this into the code.

Code: Select all
<?php
$location = $_GET['nav'];
echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["nav"] . "/page/2/js' type='text/javascript'></script>";
?>


And so on...

I want to be able to add a button to change the variable after "page/". And another thing is, the first page cannot read "page/1", because when I do it doesn't show anything. So the first page has to be the first script, and then after they press a "next" button, it has to go to the second code.

Any help would be amazing!
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 77
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by pretentious on Mon Nov 19, 2012 7:44 pm
([msg=71111]see Re: PHP Increment/Decrement[/msg])

only kinda half read the question. Methinks just put the second piece of code into a loop and increment the $pageNumber variable. If i'm totally off the mark, whoops
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
pretentious wrote:Welcome to bat country
User avatar
pretentious
Contributor
Contributor
 
Posts: 687
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by Shade_of_Gray on Mon Nov 19, 2012 8:28 pm
([msg=71113]see Re: PHP Increment/Decrement[/msg])

There are probably several ways to do this. This is probably the easiest way to understand conceptually:

Code: Select all
<?php
$location = $_REQUEST['nav'];
$page = $_REQUEST['page'];
if ($page <= 1)
{
   echo "<script src='http://myblog.tumblr.com/tagged/" . $location . "/js' type='text/javascript'></script>";
}
else
{
   echo "<script src='http://myblog.tumblr.com/tagged/" . $location . "page/" . $page . "/js' type='text/javascript'></script>";
}
?>


Of course, in order for this to work, you'll have to keep track of which page you're on. There are again several ways you could do this, but the easiest is probably to keep the current page number in a hidden form field. Then, just have a "next" button and a "previous" button, and check which one was clicked by testing to see if the appropriate $_POST index is set:

Code: Select all

$location = $_REQUEST['nav'];
$page = $_POST['page'];
if (isset($_POST['next']))
{
   $page++;
}
else if (isset($_POST['previous']))
{
   $page--;
}

if ($page <= 1)
{
   echo "<script src='http://myblog.tumblr.com/tagged/" . $location . "/js' type='text/javascript'></script>";
}
else
{
   echo "<script src='http://myblog.tumblr.com/tagged/" . $location . "page/" . $page . "/js' type='text/javascript'></script>";
}


That should be enough to get you started. I'd also suggest thinking about how you can disable the previous or next buttons when you are on the first or last page of their range - with the code above, what happens if you click the "previous" button while you're on page 1? Then what happens if you click the next button?

If you have any questions or if I wasn't clear about something, please let me know and I'll try to explain further. =)
Shade_of_Gray
Experienced User
Experienced User
 
Posts: 60
Joined: Mon Oct 22, 2012 11:04 pm
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by jadecook on Mon Nov 19, 2012 10:40 pm
([msg=71116]see Re: PHP Increment/Decrement[/msg])

Shade_of_Gray wrote:There are probably several ways to do this.


Just one question! How do I make the previous and next buttons? Would it just be:

Code: Select all
<input type="submit" value="previous"><input type="submit" value="next">


Thank you so much!
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 77
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by Shade_of_Gray on Tue Nov 20, 2012 10:44 am
([msg=71135]see Re: PHP Increment/Decrement[/msg])

Yup! Only use the "name" attribute rather than the "value" attribute:

Code: Select all
<input type="submit" name="previous" value="Previous"><input type="submit" name="next" value="Next">


The "name" attribute is what corresponds to the key in the $_POST array (so, in this case, $_POST['previous']). The "value" attribute corresponds to the value stored in $_POST['previous'] when the script is called.

Got it? =)
Shade_of_Gray
Experienced User
Experienced User
 
Posts: 60
Joined: Mon Oct 22, 2012 11:04 pm
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by jadecook on Tue Nov 20, 2012 11:53 am
([msg=71138]see Re: PHP Increment/Decrement[/msg])

When I press the buttons the page doesn't reload with the new variables /:
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 77
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by Shade_of_Gray on Tue Nov 20, 2012 6:43 pm
([msg=71151]see Re: PHP Increment/Decrement[/msg])

Could you post your current code?
Shade_of_Gray
Experienced User
Experienced User
 
Posts: 60
Joined: Mon Oct 22, 2012 11:04 pm
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by jadecook on Tue Nov 20, 2012 6:48 pm
([msg=71152]see Re: PHP Increment/Decrement[/msg])

Code: Select all

<?php
$location = $_REQUEST['nav'];
$page = $_POST['page'];
if (isset($_POST['next']))
{
   $page++;
}
else if (isset($_POST['previous']))
{
   $page--;
}

if ($page <= 1)
{
   echo "<script src='http://myblog.tumblr.com/tagged/" . $location . "/js' type='text/javascript'></script>";
}
else
{
   echo "<script src='http://myblog.tumblr.com/tagged/" . $location . "page/" . $page . "/js' type='text/javascript'></script>";
}
?>

<input type="submit" name="previous" value="Previous"><input type="submit" name="next" value="Next">

User avatar
jadecook
Experienced User
Experienced User
 
Posts: 77
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by Shade_of_Gray on Tue Nov 20, 2012 6:59 pm
([msg=71153]see Re: PHP Increment/Decrement[/msg])

Submit buttons need to be embedded in a form element:

http://www.w3schools.com/tags/tag_form.asp
Shade_of_Gray
Experienced User
Experienced User
 
Posts: 60
Joined: Mon Oct 22, 2012 11:04 pm
Blog: View Blog (0)


Re: PHP Increment/Decrement

Post by jadecook on Tue Nov 20, 2012 11:05 pm
([msg=71163]see Re: PHP Increment/Decrement[/msg])

Alright, still having a problem. I'm using this code:

Code: Select all
<?php
$location = $_REQUEST['nav'];
$page = $_POST['page'];
if (isset($_POST['next']))
{
   $page++;
}
else if (isset($_POST['previous']))
{
   $page--;
}

if ($page <= 1)
{
   echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["job"] . "/js' type='text/javascript'></script>";
}
else
{
   echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["job"] . "page/" . $_REQUEST["page"] . "/js' type='text/javascript'></script>";
}
?>

<form>
<input type="submit" name="previous" value="previous">
<input type="submit" name="next" value="next">
</form>

</center>


But now then I press "next" all I get is: http://mysite.com/preview/preview.php?next=next

And in the source it reads:

Code: Select all
<script src='http://myblog.tumblr.com/tagged//js' type='text/javascript'></script>


So it's not carrying over the "job" and it's not going to the next page.

Idk what to do!
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 77
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests