Jump to content

Array Outputs only last entry


shotos

Recommended Posts

hi,

i designed a form which allows a user to enter how many entries he wld want to make

then the form is displayed for him to make the first entry, after the data entered

is stored in an array, the form is re-displayed again until all entries have been entered.

 

problem is when i use print_r() to output, only the last entry is outputted.

<?php
session_start();
ob_start();

if(!isset($_POST['submit'] ))
{
?>
<head>
<link rel="stylesheet" type="text/css" href="new.css" />
<title>Checklist System</title>
</head>
<body>
<div id="container">

<div id="banner">
<h1>Checklist System</h1> 
</div>

<div id="nav"color="backgrounds/blue01">
Welcome <?php echo $_SESSION['user'] ?><br><br><br>
<a href="view_users.php">View Users</a><br><br><a href="create_User.php">Create User Accounts</a><br><br><a href="edit_delete_user.php">Edit/Delete User</a><br><br><a href="end.php">Generate Checklist</a><br><br><a href="login_success.php">View all Checklists</a><br><br><br><a href="log_out.php">Sign Out</a>
</div>

<div id="content">
<h2>Enter Check Item and its Properties </h2>
<form method="POST" action="<?php $_SERVER['PHP_SELF']?>">
<table border="0" align="center" cellpadding="10" cellspacing="10">
<tr>
<td>Check Item: <input type="text" name="checkItem"></td></tr>
<tr><td><h3>Select Check Item's Corresponding Checklist Element</h3></td></tr>
<tr>
<td><input type="radio" name="element" value="radio">Radio Button<br></td>
</tr>
<tr>
<td><input type="radio" name="element" value="checkbox">Checkbox<br></td>
</tr>
<tr>
<td><input type="radio" name="element" value="text">Textbox<br></td>
</tr>
<tr><td>&nbsp</td></tr>
<tr><td><h3>Enter Labels for the chosen checklist element</h3></td></tr>
<td><FONT color="red" size="1">***enter labels seperated by commas***</FONT></td></tr>
<tr>
<td>Label : <input type="text" name="label" size="50"></td></tr>

<tr><td>&nbsp</td></tr>

<tr>
<td ><input type="hidden" id="num" name="num" value="<?php echo $_SESSION['count'];?>">
</td></tr>
<tr>
<td align="center"><input type="submit" name="submit" value="Submit">
</td></tr>
</table>
</form>
</div>


</div>
</body>
<?php
}
else
{

$num = $_POST['num'];
$check_item[$num]['checkitem'] = $_POST['checkItem'];
$check_item[$num]['element'] = $_POST['element'];
$check_item[$num]['label'] = $_POST['label'];

/*foreach($_POST as $var)
	{
	 	$check_item[] = $var; 
	}*/

//unset($_POST);
$_SESSION['count'] = ++$num ; 
if($num == $_SESSION['num_Items'])
	{
	$_SESSION['checks'] = $check_item ;
	header("location:checking1.php?item=$check_item");
	} 
else
	{	
	header("location:checking.php");
	}
}
ob_flush();
?>

Link to comment
Share on other sites

This most likely means you keep overwriting the array.  You should just be able to do something like:

 

$array[]=$new_value;

 

This will keep incrementing the index by one each time you add something to the array.

Link to comment
Share on other sites

array's don't persist from page to page and you can only transmit serialized data (text) through a get or post request.  You can't pass an array like you can with a function call, so

 

header("location:checking1.php?item=$check_item");

 

Isn't doing what you think it is.

 

Ideally, this is performed with a database, which stores the user's entry on every submit... or by allowing the user to input all info at once.

 

If you insist on this format, I supposed you could try to serialize() the array first, pass it through $_GET and then unserialize() it on the other side, but that would get messy in a hurry

Link to comment
Share on other sites

Or store it in the session or even in a cookie if its small enough.  There is a class out there that stores arrays in cookies by serializing them.  Just look in PHPs documentation on cookies, it was posted there by a user a while back.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.