Jump to content

Dynamically create 2 dimensional arrays using a loop


bern1o

Recommended Posts

Hi,

 

This problem has been driving me crazy all day.  I am relatively new to PHP.  I basically am trying to populate a database with data from site users.  I am using session variables to store their data temporarily as they navigate through the sign up process.  A user will input how many 'categories' they wish to populate on page 1.  Page 2 will then ask them to specify the details of each category.  Eg Category 1: Title, Description, Amount.  Category 2: Title, etc.

 

So far I have been able to do this, I now want to store what they have input in session variables.  My thoughts were to take the number of categories they have sepcified and create that number of arrays using a loop.  Each array will store the details on each category.  My code is as follows:

 

$count=$_POST['count']; //Get how many categories were added
//Create an array for each category
for ( $counter = 0; $counter <= $count; $counter++){
${'Categoryarr'.$counter} = array();
};

for ( $counter = 0; $counter <= $count; $counter++){
	$Categoryarr[$counter][1]=$_POST['amount_'.$count];
	$Categoryarr[$counter][2]=$_POST['desc_'.$count];
	$Categoryarr[$counter][3]=$_POST['title_'.$count];
};

 

When I output the code, I seems to have created the specified number of arrays, but has populated all of them with the same data from the last category.  Does anyone know where I am going wrong?

 

Thanks,

Bernard

For example, the html form will have a varying number of categories.  Eg. a user will decide on having 2 categories.  They will be displayed with a form consisting of 2 boxes that allows them to enter the details of their category.

Category 1
amount_0 = 50 
title_0       = "TestCategory 1"
desc_0      = "Test Description 1"

Category 2
amount_1 = 300 
title_1       = "TestCategory 2"
desc_1      = "Test Description 2"

 

From this example I would like 2 arrays created.  Categoryarr1 and Categoryarr2.  They should have the following data:

 

Categoryarr1[0] = 50
Categoryarr1[1] = "TestCategory 1"
Categoryarr1[2] = "Test Description 1"

Categoryarr2[0] = 300
Categoryarr2[1] = "TestCategory 2"
Categoryarr2[2] = "Test Description 2"

 

Does that help?

bern1o posted while I was working out an example. Mine is a little different so I'll just post it for comparison.

<?php
$inventory = array( array("itema", costa, quantitya),
               array("itemb", costb, quantityb),
               array("itemc", costc, quantityc) 
             ); 
?>

 

Then you retrive it like this:

<?php
echo $inventory [0][0]." costs ".$inventory [0][1]." and you get ".$inventory [0][2]."<br />";
echo $inventory [1][0]." costs ".$inventory [1][1]." and you get ".$inventory [1][2]."<br />";
echo $inventory [2][0]." costs ".$inventory [2][1]." and you get ".$inventory [2][2]."<br />";
?>

 

Do the insert loop in the first php section

I'm thinking this would be pretty easy to accomplish in the form itself, then just assign the values from the $_POST array to the $_SESSION array (if that's where you want them to end up). Does that sound like it would work for you?

Ok, I've just realised the problem and feel like a total idiot.  It was populating each array with the same data because I used the count variable when I should have been using the *counter* variable when iterating through the loop!  Thanks for the replys though!

No, I was referring to setting up the fields so they return arrays within the $_POST array using name="group1[name]", name="group1[description]", etc., but if you've gotten it worked out, I guess there's no need.

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.