bern1o Posted February 12, 2011 Share Posted February 12, 2011 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 Can you post an example of the data in it's current form, and an example of how you'd like it to end up? Quote Link to comment Share on other sites More sharing options...
bern1o Posted February 12, 2011 Author Share Posted February 12, 2011 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? Quote Link to comment Share on other sites More sharing options...
sunfighter Posted February 12, 2011 Share Posted February 12, 2011 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 Quote Link to comment Share on other sites More sharing options...
bern1o Posted February 12, 2011 Author Share Posted February 12, 2011 Yes but in that example, it seems you have hard coded 3 sets of items. If you don't know how many sets of items you will be presented with, how do you deal with that? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 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? Quote Link to comment Share on other sites More sharing options...
bern1o Posted February 12, 2011 Author Share Posted February 12, 2011 Do you mean somehow put these values together into an array before the form is submitted? Would this be done using javascript? Quote Link to comment Share on other sites More sharing options...
bern1o Posted February 12, 2011 Author Share Posted February 12, 2011 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! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 12, 2011 Share Posted February 12, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.