denniss Posted October 15, 2009 Share Posted October 15, 2009 How do I do this? This is what I have tried and it is not working. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="submit" name="set" id="set" value="Set Variables" /> <input name="storeArr" name="storeArr" type="hidden" value="<?php echo serialize(htmlentities($arrVars)); ?>" /> </form> and after the $_POST['set'] is set $arrVars = unserialize(html_entity_decode(stripslashes($_POST['storeArr']))); Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/ Share on other sites More sharing options...
trq Posted October 15, 2009 Share Posted October 15, 2009 There is probably no need to use htmenities unless your objects contain html, also, unless you have magic_quotes_gpc enabled there is no need to strip slashes either. When you say 'not working', what does that mean? What results are you getting? Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937228 Share on other sites More sharing options...
denniss Posted October 15, 2009 Author Share Posted October 15, 2009 well for example if i do this echo count($arrVars); i will get 1 when the content is more than 1 edit: printf($arrVars); will give me nothing even tho i have function __toString for my ADT inside the array. Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937231 Share on other sites More sharing options...
trq Posted October 15, 2009 Share Posted October 15, 2009 Where do you define $arrVars ? Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937270 Share on other sites More sharing options...
denniss Posted October 15, 2009 Author Share Posted October 15, 2009 hey thorpe, thank you for your reply so far. I am trying to avoid posting all the codes here because it's going to look messy, unless you want me to. However, here is the relevant codes for $arrVars. Note that $arrVars is defined inside the bracket of an if statement. It is still on the same file. I have tried initializing $arrVars outside of the if bracket but the same thing happens. $arrVars = acquireVars($varCount, $problem); and the function acquireVars() is given below function acquireVars(&$count, $text){ $token = strtok($text, " \n\t"); while($token) { if(substr($token,0,1) == '#'){ $count ++; } $token = strtok(" \n\t"); } $arrVars[$count]; $i = 0; $token = strtok($text, " \n\t"); while($token) { if(substr($token,0,1) == '#'){ $arrVars[$i] = new problemVar($token); $i++; } $token = strtok(" \n\t"); } return $arrVars; } and the problemVar ADT is <?php class problemVar { var $myVar; var $lowerRand; var $upperRand; var $myVal; function __construct($var){ $this->myVar = $var; $this->myVal = 0; $this->lowerRand = 0; $this->upperRand = 0; } function setUpper($n){ $this->upperRand = $n; } function setLower($n){ $this->lowerRand = $n; } function setVal(){ $this->myVal = rand($this->lowerRand, $this->upperRand); } function getUpper(){ return $this->upperRand; } function getLower(){ return $this->lowerRand; } function getVal(){ return $this->myVal; } function getVar(){ return $this->myVar; } function __toString(){ return $this->myVar; } } ?> Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937276 Share on other sites More sharing options...
denniss Posted October 15, 2009 Author Share Posted October 15, 2009 <head> <?php /* Abstract Data Structure import*/ require_once("problemVarADT.php"); /* Functions library*/ function acquireVars(&$count, $text){ $token = strtok($text, " \n\t"); while($token) { if(substr($token,0,1) == '#'){ $count ++; } $token = strtok(" \n\t"); } $arrVars[$count]; $i = 0; $token = strtok($text, " \n\t"); while($token) { if(substr($token,0,1) == '#'){ $arrVars[$i] = new problemVar($token); $i++; } $token = strtok(" \n\t"); } return $arrVars; } ?> <title>Problem Parser :: by Dennis Suratna</title> </head> <body> <?php $arrVars; if(!isset($_POST['submit']) && !isset($_POST['set'])){ ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label for="problem">Enter the problem here:</label> <br /> <textarea name="problem" id="problem" style="background:#E9E9E9; border:solid thin #000000; width:300px; height:100px;"></textarea> <br/> <br/> <input id="submit" name="submit" value="Parse problem" type="submit"/> </form> <?php }else if(!isset($_POST['set'])){ $varCount = 0; $problem = $_POST['problem']; $arrVars = acquireVars($varCount, $problem); $i = 0; ?> Please enter the minimum and maximum number for each variable declared. <br /> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <?php while($i < count($arrVars)){ echo "Variable $arrVars[$i] <br/>"; ?> <label for="<?php echo $arrVars[$i] . "lo"; ?>">Min: </label> <input name="<?php echo $arrVars[$i] . "lo"; ?>" id="<?php echo $arrVars[$i] . "lo"; ?>" /><br /> <label for="<?php echo $arrVars[$i] . "up"; ?>">Max: </label> <input name="<?php echo $arrVars[$i] . "up"; ?>" id="<?php echo $arrVars[$i] . "up"; ?>" /><br /> <br/> <?php $i++; } ?> <input type="submit" name="set" id="set" value="Set Variables" /> <input name="storeProb" name="storeProb" type="hidden" value="<?php echo $_POST['problem'];?>" /> <input name="vCount" name="vCount" type="hidden" value="<?php echo $varCount;?>" /> <input name="storeArr" name="storeArr" type="hidden" value="<?php echo serialize(htmlentities($arrVars)); ?>" /> </form> <?php }else{ $i = 0; $arrVars = unserialize(html_entity_decode(stripslashes($_POST['storeArr']))); echo count($arrVars); printf($arrVars); while($i < count($arrVars)){ /*$arrVars[$i]->setUpper($_POST[$arrVars[$i]->getVar() + "up"]); $arrVars[$i]->setLower($_POST[$arrVars[$i]->getVar() + "lo"]); $arrVars[$i]->setVal();*/ echo $arrVars[$i] . " "; //echo $arrVars[$i]->getVal(); $i++; } } ?> </body> </html> just realized I can do this Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937278 Share on other sites More sharing options...
denniss Posted October 15, 2009 Author Share Posted October 15, 2009 bump Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937431 Share on other sites More sharing options...
denniss Posted October 15, 2009 Author Share Posted October 15, 2009 may be someone knows an alternative way of doing this? basically i want my array of objects to be retained after forum submission.. thats all Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937554 Share on other sites More sharing options...
trq Posted October 15, 2009 Share Posted October 15, 2009 The easiest way is probably to simply save $arrVars into the $_SESSION array. It'll take care of serialization for you. Having said that however, there's no reason passing it through the form won't work. Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937695 Share on other sites More sharing options...
denniss Posted October 16, 2009 Author Share Posted October 16, 2009 I got it. this is kinda tedious though. I have to serialize each object inside the array and then serialize the array and then 64encode it =P Link to comment https://forums.phpfreaks.com/topic/177752-solved-passing-an-array-of-objects-after-submit/#findComment-937888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.