Page 1 of 1

Cant figure out multidimensional arrays

Posted: Mon Dec 12, 2016 1:49 pm
by wdarby
I should clarify, I am able to use them but I do not understand how to utilize them properly.

I have a code that takes contact form data on contactform.php and passes it to formprocessor.php

On formprocessor.php here is the code

Code: Select all


////form variables
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phoneNumber'];



//////SESSION arrays
if(isset($_SESSION['nameArray'])){
array_push($_SESSION['nameArray'],$name);
}
else{
$_SESSION['nameArray'] = array();
array_push($_SESSION['nameArray'],$name);
}

if(isset($_SESSION['emailArray'])){
array_push($_SESSION['emailArray'],$email);
}
else{
$_SESSION['emailArray'] = array();
array_push($_SESSION['emailArray'],$email);
}

if(isset($_SESSION['phoneArray'])){
array_push($_SESSION['phoneArray'],$phone);
}
else{
$_SESSION['phoneArray'] = array();
array_push($_SESSION['phoneArray'],$phone);
}

//////// Multidimensional array

$formsArray = array ($_SESSION['nameArray'],
					 $_SESSION['emailArray'],
					 $_SESSION['phoneArray']
					 );


///////The part I am having issues with

foreach($formsArray as $key){
  foreach($key as $key2 => $value){
  print_r($value);
  }}
I want it to display like this:

Code: Select all

form1: Name1 Email1 Phone1

form2: Name2 Email2 Phone2

form3: Name3 Email3 Phone3
But instead I get this:

Code: Select all

array1: Name1 Name2 Name3

array2: Email1 Email2 Email3

array3: Phone1 Phone2 Phone3




Re: Cant figure out multidimensional arrays

Posted: Mon Dec 12, 2016 2:02 pm
by Celauran
Arrays are just lists. You're storing all names in one list, all emails in another, and all phone numbers in yet another. Why? Since you're looking to display the data on a per-submission basis, why not also store it that way? You could have $_SESSION['submissions'] or some such and each time you receive a $_POST, you append that array to the $_SESSION['submissions'] array. When you're iterating over it, you'll have all your data already grouped the way you wanted it.

Code: Select all

<?php

// If $_SESSION['submissions'] doesn't already exist, initialize to an empty array
if (!array_key_exists('submissions', $_SESSION)) {
    $_SESSION['submissions'] = [];
}

// If $_POST contains data, we add the new entry to our submissions array
if (!empty($_POST)) {
    $_SESSION['submissions'][] = $_POST;
}

// Now when we iterate over $_SESSION['submissions'] we get one submission per
// iteration, rather than all the names, followed by all the emails, followed by
// all the phone numbers
foreach ($_SESSION['submissions'] as $submission) {
    print_r($submission);
}

Re: Cant figure out multidimensional arrays

Posted: Mon Dec 12, 2016 2:34 pm
by wdarby
Thanks, I figured I was doing it wrong one way or another.

Do you have any advice on how I can go about allowing clients to view/edit/delete a particular $submission before they make the final confirmation?

Re: Cant figure out multidimensional arrays

Posted: Mon Dec 12, 2016 2:47 pm
by Celauran
Arrays are indexed, so $_SESSION['submissions'][0] refers to the first entry, $_SESSION['submissions'][1] the second, and so on. That would be the easiest way to target a particular item.

Re: Cant figure out multidimensional arrays

Posted: Tue Dec 13, 2016 5:29 am
by wdarby
I have the data outputting to a table now but I am just having one last issue.

Code: Select all

<?php

$counter = 0;

    // Defining function

    function deleteEntry(){

       array_splice($_SESSION['submissions'], $counter, 1);
    

    }
    
    
    // Starting table

echo "<table Border=5 CELLSPACING=3>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>Remove Item</th>
</tr>";
foreach ($_SESSION['submissions'] as $submission){
    echo "<tr ALIGN=CENTER><td>".$submission['Name']."</td><td>".$submission['Email']."</td>
    <td>".$submission['Phone_Number']."</td>";
    
    /// I would like to add a button here (in the fourth column named 'Remove Item') to 
    ///call the deleteEntry() function I 
    ///defined above. I do not want to use javascript, how can I accomplish this with
    ///only PHP? I understand that the page would have to be refreshed which I do not mind.
    
    $counter++;
    
    echo "</tr>";
}
echo"</table>";

Re: Cant figure out multidimensional arrays

Posted: Tue Dec 13, 2016 8:04 am
by Celauran
You could iterate over the session using key => value rather than just value and set the value of the button to the index to be removed.