Andy17 Posted September 6, 2008 Share Posted September 6, 2008 Hey there! I am not 100% sure, but I believe my problem is that my variable $number does not get passed correctly by my hidden field. My problem is that after clicking the Next button, the number does not increment like I want it to (notice my check below the button). Here is the script in action: http://menu.jokeheaven.eu/randomjoke.php And here is the code: <?php // Establishing MySQL connection mysql_connect("db_server", "username", "password") or die(mysql_error()); // Selecting database mysql_select_db("db_name") or die(mysql_error()); // Finding max number of rows $sql2 = "SELECT submitter FROM jokes"; $result2 = mysql_query($sql2); if ($result2) { $maxrows = mysql_num_rows($result2); } function next_button() { $number++; // Finding the row with the number as id - if it exists $sql1 = "SELECT * FROM jokes WHERE id = '$number'"; $result = mysql_query($sql1); if ($result) { $count = mysql_num_rows($result); if ($count == 1) { $row = mysql_fetch_array($result); $submitter = $row['submitter']; $title = $row['title']; $joke = $row['joke']; echo "<p><b>Submitter:</b> " . $submitter . "<br><br><b>Title:</b> " . $title . "<br><br>" . $joke . "</p>"; echo "<br><br>" . $number; } } else { echo "error"; } } function previous_button() { $number--; // Finding the row with the number as id - if it exists $sql1 = "SELECT * FROM jokes WHERE id = '$number'"; $result = mysql_query($sql1); if ($result) { $count = mysql_num_rows($result); if ($count == 1) { $row = mysql_fetch_array($result); $submitter = $row['submitter']; $title = $row['title']; $joke = $row['joke']; echo "<b>Submitter:</b> " . $submitter . "<br><br><b>Title:</b> " . $title . "<br><br>" . $joke; echo "<br><br>" . $number; } } else { echo "error"; } } if (isset($_POST['next'])) { next_button(); } elseif (isset($_POST['previous'])) { previous_button(); } else { if(isset($number)) { echo "this is just a test"; } else { // If the variable has not been set yet, it's set to 0 and some text is displayed $number = 0; echo "<b>Submitter:</b> Zeppy<br><br><b>Title:</b> Blonde in a Boat<br><br><br>There was a blonde driving down the road one day. She glanced to her right and noticed another blonde sitting in a nearby field, rowing a boat with no water in sight. The blonde angrily pulled her car over and yelled at the rowing blonde, \"What do you think you're doing? It's things like this that give us blondes a bad name. If I could swim, I'd come out there and kick your butt!\"<br><br>"; } } ?> <form action="<?= $php_SELF ?>" method="post"> <?php if ($number > 0) { ?> <input type="submit" name="previous" value="Previous"> <?php } if ($number < $maxrows) { ?> <input type="submit" name="next" value="Next"> <input type="hidden" name="number" value="<?php echo $number; ?>"/> <input type="hidden" name="rows" value="<?php echo $maxrows; ?>"/> </form> <?php } echo "<br><br>Number: " . $number . "<br> Rows: " . $maxrows; ?> Basically, what is happening is that when clicking a button, it calls either one of the functions. In those functions, $number will be incremented or decremented, but I believe the problem is that the updated variable is not passed on when clicking either one of the buttons - or my placement of the $number++ and $number-- is wrong. Any ideas? Link to comment https://forums.phpfreaks.com/topic/123064-solved-hidden-field-problem/ Share on other sites More sharing options...
steveclondon Posted September 6, 2008 Share Posted September 6, 2008 god that looks like one complex bit of code to do a really simple thing Link to comment https://forums.phpfreaks.com/topic/123064-solved-hidden-field-problem/#findComment-635518 Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 Why not use a LIMIT on your query instead of trying to find the next "row" through a query let the LIMITS do it Link to comment https://forums.phpfreaks.com/topic/123064-solved-hidden-field-problem/#findComment-635527 Share on other sites More sharing options...
nuttycoder Posted September 7, 2008 Share Posted September 7, 2008 in your functions you need to pass the number var so they can access it try using previous_button($number); next_button($number); Link to comment https://forums.phpfreaks.com/topic/123064-solved-hidden-field-problem/#findComment-635551 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 That code made my eyes hurt just a bit. You should realllly clean it up. Link to comment https://forums.phpfreaks.com/topic/123064-solved-hidden-field-problem/#findComment-635560 Share on other sites More sharing options...
Andy17 Posted September 7, 2008 Author Share Posted September 7, 2008 Yeah sorry about the messy code, I'm still new to PHP. I cleaned up the code a bit and put $number into the function parameter. Now it will let me go forward by pressing the next button, but when I try to go back by pressing the previous button, it cannot find the variable $number anymore. <?php // Establishing MySQL connection mysql_connect("db_server", "username", "password") or die(mysql_error()); // Selecting database mysql_select_db("db_name") or die(mysql_error()); // Finding all rows $sql2 = "SELECT submitter FROM jokes"; $result2 = mysql_query($sql2); if ($result2) { $maxrows = mysql_num_rows($result2); } function button1($number) { // Finding the row with the number as id - if it exists $sql1 = "SELECT * FROM jokes WHERE id = '$number'"; $result = mysql_query($sql1); if ($result) { $count = mysql_num_rows($result); if ($count == 1) { $row = mysql_fetch_array($result); $submitter = $row['submitter']; $title = $row['title']; $joke = $row['joke']; echo "<b>Submitter:</b> " . $submitter . "<br><br><b>Title:</b> " . $title . "<br><br>" . $joke . "<br><br>" . $number; } } else { echo "error"; } } // Checking which button the user pressed if (isset($_POST['next'])) { $number++; button1($number); } elseif (isset($_POST['previous'])) { $number--; button1($number); } else { if(isset($number)) { echo "just testing!"; } else { $number = 0; echo "<b>Submitter:</b> Zeppy<br><br><b>Title:</b> Blonde in a Boat<br><br><br>There was a blonde driving down the road one day. She glanced to her right and noticed another blonde sitting in a nearby field, rowing a boat with no water in sight. The blonde angrily pulled her car over and yelled at the rowing blonde, \"What do you think you're doing? It's things like this that give us blondes a bad name. If I could swim, I'd come out there and kick your butt!\"<br><br>"; } } ?> <form action="<?= $php_SELF ?>" method="post"> <?php if ($number > 0) { ?> <input type="submit" name="previous" value="Previous"> <?php } if ($number < $maxrows) { ?> <input type="submit" name="next" value="Next"> <input type="hidden" name="number" value="<?php echo $number; ?>"/> <input type="hidden" name="rows" value="<?php echo $maxrows; ?>"/> </form> <?php } echo "<br><br>Number: " . $number . "<br> Rows: " . $maxrows; ?> Code in action: http://menu.jokeheaven.eu/randomjoke.php Any ideas what I did wrong / what I should do? Thanks for the help so far! Why not use a LIMIT on your query instead of trying to find the next "row" through a query let the LIMITS do it I'm new to PHP and MySQL so I didn't even know of that yet. Anyways, it's quite simple, but the reason why I used "WHERE id = '$number'" is that it seemed easier to go to the next/previous row when clicking a button. Any thought of how you would do that when using LIMIT? Link to comment https://forums.phpfreaks.com/topic/123064-solved-hidden-field-problem/#findComment-635711 Share on other sites More sharing options...
Andy17 Posted September 7, 2008 Author Share Posted September 7, 2008 Actually the problem only occurs when you try to go back from the last row in the database. (Sorry, I am not able to edit my posts). Link to comment https://forums.phpfreaks.com/topic/123064-solved-hidden-field-problem/#findComment-635730 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.