lon22 Posted January 19, 2008 Share Posted January 19, 2008 Hello, I having a problem with losing data in an array variable. The problem is that after I submit a query to a DB I fill an array with some values. This works fine, but after I do another submit with a different button, the values in the array seem to disappear. Any help will be appreciated. I placed comments in the code. I running php 5.2.5. Thanks. <?php /*****************8 Connect to database ******************/ mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("pluto_media") or die(mysql_error()); /****************** Set Title ****************************/ Echo "<html>"; Echo "<title>HTML with PHP</title>"; /***************** Variables ****************************/ $i = 0; $querStr = $_POST['querStr']; $mediaSubTypeEnum = array (1 => "TV Shows",2 => "Movies",3 => "Home Videos", 4 => "Sports Events", 5 => "Music Videos", 6 => "Alternative", 7 => "Popular Music", 8 => "Classical Music"); $fileFormatEnum = array (1 => "Low Res",2 => "DVD",3 => "Standard Def", 4 => "HD 720", 5 => "HD 1080", 6 => "Low Quality", 7 => "MP3", 8 => "CD Quality", 9 => "High-def audio"); /******************* Create main body of html page **********************************************/ Echo " <body bgcolor='Green'> <form action=".$_SERVER['PHP_SELF']." method='post'> Search For:<input name='querStr' type='text' size='10'/> <input type='submit' name='submit' value='submit'>"; /**************** perform action after button is clicked ***************************************/ if(isset($_POST['submit'])) { //If the initial button displayed is clicked $result = mysql_query("SELECT * FROM File Where Filename LIKE '%".$querStr."%'"); Echo " <input type='submit' name='submit2' value='submit2'> // output results of query in html table - this works fine <table border='1'> <tr> <th>Primary File</th> <th>Update File</th> <th>Filename</th> <th>Path</th> <th>MediaSubType</th> <th>FileFormat</th> <th>PK_File</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><input type='radio' name='primary[]' value=$i/></td>"; echo "<td><input type='checkbox' name='CopyFile[]' value='$i'/></td>"; echo "<td>".$row['Filename']."</td>"; echo "<td>".$row['Path']."</td>"; echo "<td>".$mediaSubTypeEnum[$row['FK_MediaSubType']]."</td>"; echo "<td>".$fileFormatEnum[$row['FK_FileFormat']]."</td>"; $myArray[$i++] = $row['PK_File']; echo "<td>".$myArray[$i-1]." -".$i."</td>"; echo "</tr>"; } echo "</table>"; } /**************** perform action after second button is clicked ***************************************/ if(isset($_POST['submit2'])) { foreach($_POST['primary'] as $value) { // determine which radio button is selected $value = $value + 1; echo "the primary is".$value."\n"; } for ($i = 0; $i < 10; $i++) { /* simple test to see if anything is in myArray. the array appears to be empty. This is the problem area. Before the second button was clicked the array had verified values in it.*/ echo $_REQUEST["myArray[$i]"]; echo " in loop".$i; } $result = mysql_query("SELECT * FROM Picture_File Where FK_File = '".$myArray[$value]."'"); $row = mysql_fetch_array($result); $FK_Picture = $row['FK_Picture']; $priVal = $value; echo "prival and fk_pic = ".$priVal. " - ".$FK_Picture."\n"; foreach($_POST['CopyFile'] as $value) { $value = $value + 1; mysql_query("INSERT INTO `Picture_File` VALUES ($FK_Picture,$myArray[$value],NULL,NULL,NULL,0,NULL,NULL)"); mysql_query("UPDATE File SET mediaSubTypeEnum='.mediaSubTypeEnum[$priVal].' fileFormatEnum='.$fileFormatEnum[$priVal].' WHERE FK_File='.$myArray[$value].'"); } } Echo " </form> </body> </html>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/ Share on other sites More sharing options...
Ninjakreborn Posted January 19, 2008 Share Posted January 19, 2008 * Save the array in a session. * Save the array in a cookie * Save the array itself in a db (seperate table) and retrieve it with the ip. The problem is when you submit it completely erases all memory that was on the page. If you submit something ti's submittted. If you submit something else it submits something else (not naturally maintaining what was originally submitted). Thus another problem that requires saving state. As explained above. Quote Link to comment https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/#findComment-443771 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 one more option. similar to the cookie. save it to a hidden form element. use implode and explode to flaten and then put it back into array. Quote Link to comment https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/#findComment-443775 Share on other sites More sharing options...
lon22 Posted January 19, 2008 Author Share Posted January 19, 2008 Thanks for the reply businessman332211: I tried to save the array as a session variable and I also tried to serialize it - separate attempts. I attempted to create the session variable by adding session_start(); to top of page. Then I did $_SESSION["myArray[$i++]] = $val; I'm sure that I just don't understand the syntax. Can you tell me how to do a session variable for an array? Thanks lon22 Quote Link to comment https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/#findComment-443781 Share on other sites More sharing options...
Ninjakreborn Posted January 19, 2008 Share Posted January 19, 2008 $_SESSION = array(whatever in the array); then you can access it the same as any array. $_SESSION['arraykeyhere']; So basically save the entire array into an array. Then merge that array into session like. $myarray = array(); then fill my array with whatever you want and then $_SESSION = $myarray; That is one method. The other is to use $_SESSION['subkey'] as your array. (makes it cleaner). $_SESSION['my_array'] will be your top level array. Then put your other keys and values underneath the my_array key. This is so if you needed more than 1 seperate array in session you can do $_SESSION['my_array'] $_SESSION['user_data'] $_SESSION['system_data'] and each one can be a seperate array or a multi array. Quote Link to comment https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/#findComment-443799 Share on other sites More sharing options...
lon22 Posted January 19, 2008 Author Share Posted January 19, 2008 Thanks for the replies. I decided to go with the hidden field. To save data: $saveStr = implode(",",$myArray); echo $saveStr; echo "<input type='hidden' id='arry' name='myArray' value='".$saveStr."'/>"; To retrieve data: $saveStr = $_POST['myArray']; echo $saveStr; $myArray = explode(",",$saveStr); Worked like a charm resago. Quote Link to comment https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/#findComment-443814 Share on other sites More sharing options...
pkSML Posted January 20, 2008 Share Posted January 20, 2008 Glad you got it working. When working with more complex data (ie binary data or non-standard characters), serialize the array and make it a hidden form element. Then unserialize it and you'll have the original array in its perfect form. PS You may want to base64 the serialized data to make sure nothing is lost in the POST operation. Quote Link to comment https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/#findComment-443926 Share on other sites More sharing options...
Fyorl Posted January 20, 2008 Share Posted January 20, 2008 Serialize is the best thing since foreach. Quote Link to comment https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/#findComment-443957 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.