TeamCIT Posted February 5, 2010 Share Posted February 5, 2010 Hey guys, I'm trying to look for a way to create a variable that increments every time a submit button is pressed. What I have is a basic web page with a form that contains 3 drop-down lists. The user is to click an item in the list, then click the submit button at the bottom of the page to add it to a list. This list is created by a "selected" column in my database, that represents a numeric value. I have been using a MySQL query to update the "selected" column (starts as default "0") to a "1" if the item was selected, then another MySQL query to pull all the items in the database with a "1". What I would like to do instead is create a counter variable that updates the "selected" column in the database to an incrementing varaible instead so that I may list the items in the order chosen by the user, not in the order they appear in my database. tl;dr - I would like to update a column in my database with a counter variable that increases each time my submit button is pressed Here is the part of my code that is not working as desired, if this will help: $mainCounter = $mainCounter + $subCounter; if($_POST['submitted'] == true) { $subCounter++; $destChose = $_POST['destList']; $memChose = $_POST['memList']; $restChose = $_POST['restList']; //Destination If Statement if($destChose >= 1) { mysql_query("update combineddests set selected={$mainCounter} where id={$destChose}"); }//closes if destchose //Memorial If Statement if($memChose >= 1) { mysql_query("update combineddests set selected={$mainCounter} where id={$memChose}"); }//closes if memchose //Restaurant If Statement if($restChose >= 1) { mysql_query("update combineddests set selected={$mainCounter} where id={$restChose}"); }//closes if restchose $result = mysql_query("SELECT place_name, place_addr FROM combineddests WHERE selected >= '1'"); while ($row = mysql_fetch_array($result)) { echo "<li>"; echo $row["place_name"]; echo "</li>"; echo $mainCounter; }//closes while echo "<br />"; echo "<br />"; echo "Please select a destination."; }//closes main If submit Any input / tips are greatly appreciated, thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/ Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2010 Share Posted February 5, 2010 Would be easy just to store in a session: session_start(); $_SESSION['counter'] = (!$_SESSION['counter']) ? 0 : $_SESSION['counter']; if($_POST['submit']) { $_SESSION['counter']++; } Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007438 Share on other sites More sharing options...
TeamCIT Posted February 5, 2010 Author Share Posted February 5, 2010 Would be easy just to store in a session: session_start(); $_SESSION['counter'] = (!$_SESSION['counter']) ? 0 : $_SESSION['counter']; if($_POST['submit']) { $_SESSION['counter']++; } This definitely looks like it could work. Could you help me out a little by explaining this code in minor detail to me? I'm still somewhat new to PHP and MySQL. Does the $_SESSION['counter'] piece create a counter that will not re-initialize each time the submit button is pressed? Also, what exactly is the question mark there for, and what does the "0 : " do? $_SESSION['counter'] = (!$_SESSION['counter']) ? 0 : $_SESSION['counter']; Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007442 Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2010 Share Posted February 5, 2010 $_SESSION['counter'] = (!$_SESSION['counter']) ? 0 : $_SESSION['counter']; Its just a condensed if else condition. Really it should be $_SESSION['counter'] = isset($_SESSION['counter']) ? $_SESSION['counter'] : 0; Basically, if $_SESSION['counter'] is set and has a value, then keep its value. If not set it's value to 0. When the submit button is pressed increment its value. However you must remember that you will need to destroy the session value somewhere. If a user left the page and then returned later the session value would still persist. Using a session is just one option. You could just use a hidden field within the form to keep the counters value persistent Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007444 Share on other sites More sharing options...
TeamCIT Posted February 5, 2010 Author Share Posted February 5, 2010 Its just a condensed if else condition. Really it should be $_SESSION['counter'] = isset($_SESSION['counter']) ? $_SESSION['counter'] : 0; Basically, if $_SESSION['counter'] is set and has a value, then keep its value. If not set it's value to 0. When the submit button is pressed increment its value. However you must remember that you will need to destroy the session value somewhere. If a user left the page and then returned later the session value would still persist. Using a session is just one option. You could just use a hidden field within the form to keep the counters value persistent How would I destroy the session value? Would I put session_destroy(); at the end of my PHP code, before my form? Will this cause the session to be destroyed when the submit button is pressed? Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007446 Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2010 Share Posted February 5, 2010 no, you just unset it. unset($_SESSION['counter']); Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007448 Share on other sites More sharing options...
oni-kun Posted February 5, 2010 Share Posted February 5, 2010 Ternary and tertiary condensed operators are slow, I wouldn't recommend using them. Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007449 Share on other sites More sharing options...
TeamCIT Posted February 5, 2010 Author Share Posted February 5, 2010 no, you just unset it. unset($_SESSION['counter']); Oh, ok. So does this make it so that I don't need to add "session_destroy();"? Also where would I put the unset($_SESSION['counter']); to make sure it doesn't reset the counter when the submit button is pressed? Should I open a new PHP tag at the bottom of my code, under my form? Or could I put it in the PHP tag at the top of my code before the HTML tags and have it not reset when the button is pressed? Ternary and tertiary condensed operators are slow, I wouldn't recommend using them. Would you not recommend using them solely on the fact that they are slow? Speed is not really an issue in my case, I just need it to generate a list in order using a database. Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007457 Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2010 Share Posted February 5, 2010 Oh, ok. So does this make it so that I don't need to add "session_destroy();"? Also where would I put the unset($_SESSION['counter']); to make sure it doesn't reset the counter when the submit button is pressed? Should I open a new PHP tag at the bottom of my code, under my form? Or could I put it in the PHP tag at the top of my code before the HTML tags and have it not reset when the button is pressed? You dont want to destroy the session value on the same page as you would just reset the counter. You could add a global function to all your other pages that will clean out session data that is no longer required. This is why I suggested to just use a hidden field in your form to hold the value of the counter if you don't want to work with sessions and just increment the $_POST['counter'] value. Forget about using session_destroy(). Also in your script speed is not a factor. You are talking microseconds. Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007463 Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2010 Share Posted February 5, 2010 i.e <?php $_POST['counter'] = isset($_POST['counter']) ? $_POST['counter'] : 0; if($_POST['submit']) { $_POST['counter']++ } ?> <!-- form --> <input type="hidden" name="counter" value="<?php print $_POST['counter']; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007464 Share on other sites More sharing options...
TeamCIT Posted February 5, 2010 Author Share Posted February 5, 2010 i.e <?php $_POST['counter'] = isset($_POST['counter']) ? $_POST['counter'] : 0; if($_POST['submit']) { $_POST['counter']++ } <!-- form --> <input type="hidden" name="counter" value="<?php print $_POST['counter']; ?>" /> ?> Ok thank you this looks like it will be a lot easier to understand. How would I reference "$_POST['counter']"? Would it be "$counter" or do I assign a value to it here like this? if($_POST['submitted'] == true) { $counter = $_POST['counter']; $destChose = $_POST['destList']; $memChose = $_POST['memList']; $restChose = $_POST['restList']; $_POST['counter']++; Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007470 Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2010 Share Posted February 5, 2010 yeah, or to simplify: <?php // set counters initial value or use the value in the superglobal $counter = isset($_POST['counter']) ? $_POST['counter'] : 0; if($_POST['submitted']) { $counter++; $destChose = $_POST['destList']; $memChose = $_POST['memList']; $restChose = $_POST['restList']; } ?> <!-- form --> <?php print "You have submitted the form: ".$counter." times.<br />"; ?> <input type="hidden" name="counter" value="<?php print $counter; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007474 Share on other sites More sharing options...
TeamCIT Posted February 5, 2010 Author Share Posted February 5, 2010 yeah, or to simplify: <?php // set counters initial value or use the value in the superglobal $counter = isset($_POST['counter']) ? $_POST['counter'] : 0; if($_POST['submitted']) { $counter++; $destChose = $_POST['destList']; $memChose = $_POST['memList']; $restChose = $_POST['restList']; } ?> <!-- form --> <?php print "You have submitted the form: ".$counter." times.<br />"; ?> <input type="hidden" name="counter" value="<?php print $counter; ?>" /> I tried this, and $counter is staying equal to 1 every time the submit button is pressed. I'm not too sure how to go about fixing this because I have never used a hidden field in a form before. Do you have any ideas why it would not be incrementing? Here is my code: $counter = isset($_POST['counter']) ? $_POST['counter'] : 0; if($_POST['submitted'] == true) { $counter++; $destChose = $_POST['destList']; $memChose = $_POST['memList']; $restChose = $_POST['restList']; //Destination If Statement if($destChose >= 1) { mysql_query("update combineddests set selected={$counter} where id={$destChose}"); }//closes if destchose //Memorial If Statement if($memChose >= 1) { mysql_query("update combineddests set selected={$counter} where id={$memChose}"); }//closes if memchose //Restaurant If Statement if($restChose >= 1) { mysql_query("update combineddests set selected={$counter} where id={$restChose}"); }//closes if restchose $result = mysql_query("SELECT place_name, place_addr FROM combineddests WHERE selected >= '1'"); while ($row = mysql_fetch_array($result)) { echo "<li>"; echo $row["place_name"]; echo "</li>"; echo $mainCounter; }//closes while echo "<br />"; echo "<br />"; echo "Please select a destination."; }//closes main If submit //Code for Reset Button if($_POST['reset'] == true) { //resets selected places mysql_query("update combineddests set selected=0 where selected >='1'"); } //Code to generate Link if($_POST['linkgen'] == true) { //gets destination stuff into address $placeResult = mysql_query("SELECT place_addr FROM combineddests WHERE selected = '1'"); $placeCounter = 1; echo '<a href="http://maps.google.com/maps?f=d&source=s_d'; while ($placeRow = mysql_fetch_assoc($placeResult)) { switch($placeCounter) { case 1: echo "&saddr="; break; case 2: echo "&daddr="; break; default: echo "+to:"; break; } echo $placeRow['place_addr']; $placeCounter++; } echo "&pw=2"; echo "\">" . "Directions" . "</a>"; } ?> <html> <head> <title>Create a Tour</title> </head> <body> <br /> <br /> <form name="tourCreation" action="CombinedDB_CreateATour1.php" method="post"> Please complete this form to create your tour: <br /> <br /> Hot Spots:<br /> <select name="destList" size="10"> <option value="1">National Gallery of Art</option> <option value="2">National Archives & Records Administration</option> <option value="3">National Law Enforcement Officers Memorial</option> <option value="4">Kenilworth Aquatic Gardens</option> <option value="5">White House Park</option> <option value="6">Washington Photo Safari</option> <option value="7">Federal Bureau of Investigation</option> <option value="8">Franciscan Monastery - Commissariat of the Holy Land in America</option> <option value="9">National Portrait Gallery Special Events Department</option> <option value="10">Rock Creek Park</option> <option value="11">Anacostia Community Museum</option> <option value="12">National Shrine of the Immaculate Conception </option> <option value="13">U.S. National Arboretum</option> <option value="14">Union Station</option> </select> <br /> <br /> <br /> Memorials:<br /> <select name="memList" size="10"> <option value="18">Thomas Jefferson Memorial</option> <option value="19">Korean War Veterans Memorial</option> <option value="20">National World War II Memorial</option> <option value="21">African American Civil War Memorial</option> <option value="22">Franklin Delano Roosevelt Memorial</option> <option value="23">Marine Corps War Memorial</option> <option value="24">Women in Military Service for America Memorial</option> <option value="25">Air Force Memorial</option> <option value="26">Pentagon Memorial</option> <option value="27">Martin Luther King, Jr. National Memorial</option> </select> <br /> <br /> <br /> Restaurants:<br /> <select name="restList" size="10"> <option value="28">Old Ebbitt Grill</option> <option value="29">Song Que</option> <option value="30">Komi</option> <option value="31">Citronelle</option> <option value="32">Café Atlantico</option> <option value="33">CityZen</option> <option value="34">Palena Café</option> <option value="35">Vidalia</option> <option value="36">The Oval Room</option> <option value="37">The Source</option> <option value="38">BLT Steak</option> <option value="39">Kinkead’s</option> <option value="40">Teatro Goldoni</option> <option value="41">Mio</option> <option value="42">Cafe Du Parc</option> <option value="43">Tosca</option> </select> <input type="hidden" name="counter" value="<?php print $_POST['counter']; ?>" /> <br /> <br /> <br /> <input type="submit" name="submitted" value="Select Destination" /> <input type="submit" name="reset" value="Reset" /> <input type="submit" name="linkgen" value="Get Link" /> </form> <br /> <br /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007478 Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2010 Share Posted February 5, 2010 Change this: <input type="hidden" name="counter" value="<?php print $_POST['counter']; ?>" /> to <input type="hidden" name="counter" value="<?php print $counter; ?>" /> As you have assigned the post value to $counter you are incrementing this variable. Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007481 Share on other sites More sharing options...
TeamCIT Posted February 5, 2010 Author Share Posted February 5, 2010 Change this: <input type="hidden" name="counter" value="<?php print $_POST['counter']; ?>" /> to <input type="hidden" name="counter" value="<?php print $counter; ?>" /> As you have assigned the post value to $counter you are incrementing this variable. Alright everything is working as desired now. Thank you very much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007484 Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2010 Share Posted February 5, 2010 No worries. Hopefully you can see how data is made persistent by using PHP's superglobals. In your case POST ($_POST). You can also pass data through urls and access it via $_GET. Session data is stored in $_SESSION and cookie data is held in $_COOKIE. Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007486 Share on other sites More sharing options...
gwolgamott Posted February 5, 2010 Share Posted February 5, 2010 Cool, Dummy me didn't even think of suggesting cookies... doh. Ha but glad you got it working. Quote Link to comment https://forums.phpfreaks.com/topic/191062-create-a-counter-variable-that-increments-when-submit-button-is-pressed/#findComment-1007490 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.