Andy17 Posted September 7, 2008 Share Posted September 7, 2008 Hey there! I got my script working where you can browse content from my MySQL database by pressing a previous/next button (previous/next row). The thing is that I used POST with my buttons so I am wondering how I can make each MySQL row have its own individual link (so that you can enter it in the address bar and you will see that row's content displayed). For instance, I would like my link to look like this: http://www.myhost.com/mypage.php?id=17 ...where 17 is the row in my database with id = 17. I'm not sure if it's possible to involve a hidden field or something. Anyways, here is some code if it helps you guys (only the relevant part): <?php // MySQL connection + database selection is done here // Checking which button the user pressed if (isset($_POST['next'])) { $number++; // Checking if the $number contains a valid id number ($maxrows is found with a code that is not shown here) if ($number >= 0 && $number <= $maxrows) { button1($number); } } elseif (isset($_POST['previous'])) { $number--; // Same as before if ($number > 0 && $number <= $maxrows) { button1($number); } } else { // If the $number variable has not been set, it's set to 0 here if(!isset($number)) { $number = 0; } } function button1($number) { // MySQL data is withdrawn here } <form action="<?= $php_SELF ?>" method="post"> // $number and $maxrows are stored in hidden fields for later use (when a button is clicked) <input type="hidden" name="number" value="<?php echo $number; ?>"/> <input type="hidden" name="rows" value="<?php echo $maxrows; ?>"/> // My previous button <input type="submit" name="previous" value="Previous"> // My next button <input type="submit" name="next" value="Next"> </form> ?> As mentioned, this is just the important parts of my code that I think are relevant to my question. Quickly summarized, I want to make the url id (page.php?id=number) the same as the $number variable. In other words, if a user is viewing row 7 from the MySQL database, the link should be: http://www.myhost.com/mypage.php?id=7 ...and when someone enters that link into his/her browser's address bar, he/she should view the content from row 7 in the MySQL database. I hope I made myself clear and someone has an idea how to do this. Thanks a lot in advance! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 7, 2008 Share Posted September 7, 2008 You just need to retrieve the variable from the GET array. For example, if you're URL is: http://www.example.com/page.php?id=1&foo=bar then you can do this: $id = $_GET['id']; echo $id;//echos 1 $foo = $_GET['foo']; echo $foo;//echos bar Of course, you'll need to sanitize the information you get from the URL, as it's provided by the user. Quote Link to comment Share on other sites More sharing options...
Andy17 Posted September 7, 2008 Author Share Posted September 7, 2008 You just need to retrieve the variable from the GET array. For example, if you're URL is: http://www.example.com/page.php?id=1&foo=bar then you can do this: $id = $_GET['id']; echo $id;//echos 1 $foo = $_GET['foo']; echo $foo;//echos bar Of course, you'll need to sanitize the information you get from the URL, as it's provided by the user. The thing is that I have the variable I want in the URL in my PHP code, but I don't know how to get it up there. Obviously I could do it by using GET, but is that possible when I am already using POST for my buttons? I mean I'm not sure how to do that without having to make another button. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 7, 2008 Share Posted September 7, 2008 I'm not sure I entirely follow your question, but yes, it is possible to have information passed with both GET and POST in the same request. For example, it's entirely reasonable to have the action of a form with the POST method contain variables. That is, file.php?id=4 is a valid action, regardless of the method. Quote Link to comment Share on other sites More sharing options...
Andy17 Posted September 7, 2008 Author Share Posted September 7, 2008 I'm not sure I entirely follow your question If you look at my code, you will see that I use the variable $number to browse through my MySQL rows ($number++; & $number--. This means that when $number is 7 for example, row 7 from my MySQL table is displayed on my website. This all works out smoothly. However, I would like to have an individual link for all the MySQL rows that are displayed on the website. For example, if you have browsed your way to row 7 on my page (http://menu.jokeheaven.eu/randomjoke.php), I would want the link to look like this: http://menu.jokeheaven.eu/randomjoke.php?id=7 This URL should be displayed in the browser's address bar and you should be able to send this to another person. When that person clicks the link, he or she should see row 7 from my MySQL table displayed. So, the id in the URL should increment/decrement along with the $number. What I need to know is how to get the "?id=X" up into the browser's address bar and make sure that the id in the URL is the same as $number. I am not sure how to do this, since the $number is incremented/decremented by using the POST method. I hope I made the situation clearer. Quote Link to comment Share on other sites More sharing options...
Infinitive Posted September 8, 2008 Share Posted September 8, 2008 Why not approach it from a different angle? You're currently saying "next" or "previous" and then then the new page calculates the new number and retrieves that record. Why not just pass the next number? Then you wouldn't even need a form at all if you wanted it. You could just put the proper number in the URL or trigger with a javascript:window.location; You also wouldn't have to pass the "maxrows" variable that way, because you would pre-determine if you were at the last in the list. Quote Link to comment Share on other sites More sharing options...
Andy17 Posted September 8, 2008 Author Share Posted September 8, 2008 Why not approach it from a different angle? You're currently saying "next" or "previous" and then then the new page calculates the new number and retrieves that record. Why not just pass the next number? Then you wouldn't even need a form at all if you wanted it. You could just put the proper number in the URL or trigger with a javascript:window.location; You also wouldn't have to pass the "maxrows" variable that way, because you would pre-determine if you were at the last in the list. I use a form to make my buttons work by using the POST method. I just need to find a way to get $number up in the URL. :/ Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 8, 2008 Share Posted September 8, 2008 Still slightly lost - cant you just set the action to "yourpage.php?number=<?php echo $number; ?>" ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.