Jump to content

[SOLVED] passing an array of objects after submit


denniss

Recommended Posts

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'])));

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;

}

}

?>

 

 

<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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.