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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

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.