Multi dimension array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BrettCarr
Forum Newbie
Posts: 22
Joined: Fri Feb 12, 2010 6:45 pm

Multi dimension array problem

Post by BrettCarr »

Hi guys, I hope the day is going well for everyone.
At the moment when I post I can only get 1 record to go into my array, I need to be able to put 35 in and have them echo out to the page before i decide what to do. with them, which will be putting them in my mysql database.
I want the array to be to be able to hold all the feild's in the post and each time the user post i want to see the record being added to the array.
Can someone tell me what is wrong here?

Thanks in Advance

Code: Select all

<?php
/*//check to see if the form has been submitted
if(isset($_POST['fname']))
{
$post_array = array();//initalize array
foreach ($_POST as $key => $value) {//loop through post values
$post_array[$key] = $value;//add post value to array
}
 
//loop through post_array and echo values
foreach($post_array as $key => $value)
{
echo $key .": " .$value."<br/>";
}
}*/
 
if (isset($_POST['fname'])) 
{
$post_array = array( array( ));
 
    foreach ($_POST as $key[] => $value)
    {
    
    $post_array[] = $value;
    
    }
    foreach($post_array as $key => $value)
    {
    echo $key .": " .$value."<br/>";
    }
    
}
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<form name="info" action="" method="post">
first name: <input type="text" name="fname" value="" /><br/>
last name:<input type="text" name="lname" /><br />
phone number:<input type="text" name="phone" /><br />
<input type="submit" />
</form>
</body>
</html>
 
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Multi dimension array problem

Post by Weiry »

The problem your facing here is that the data on each page only exists while that page is open.

So when you submit 1 form's data, you will receive that data on the submission page and it will be added to your array.
However the moment you submit a new form, the data you previously submitted will cease to exist because that data is not being passed along to the new page as well.

If you really need to create 35 form's worth of data before you submit it, you can either store the information in an array located in a session variable, or create your 35 sets of data entry on a single page.

The session variable is possibly the easiest way that i can think of.
So you may need to do something like this:

Code: Select all

<php
session_start();
if(isset($_POST['mySubmitButton'])){
    if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['phone'])){
        $_SESSION['formArray'][] = array("fname" => $_POST['fname'], "lname" => $_POST['lname'], "phone" => $_POST['phone']);
    }else{
        print "Error: Not enough form data";
    }
}
/* You would probably also add in some sort of code to view the current data or a way to edit it */
?>
 
<!-- Your html form data -->
 
This way you would be able to access all the data for each form through a simple for loop.

Code: Select all

for($i=0;$i<=count($_SESSION['formArray']);$i++){
    print $_SESSION['formArray'][$i]['fname']." ".$_SESSION['formArray'][$i]['lname']." ".$_SESSION['formArray'][$i]['phone'];
}
I hope that helps :D


Last bumped by BrettCarr on Fri Feb 19, 2010 3:14 pm.
Post Reply