Jump to content

Multidimensional Array


totalx

Recommended Posts

Hi everybody,

 

I generally find any assistance that I need on various sites, but this one has me stumped. I'm not overly advanced with my use of arrays, so I'd like some help here if anyone knows what I am looking for.

 

I have a form that I would like to submit to a MySQL database. In that form, there is the ability to add up to 3 harddrives:

			Brand: <input type="text" name="hdds[0][hddbrand]" id="hddbrand"><br/>
			Model and/or size,type: <input type="text" name="hdds[0][hddtype]" id="hddtype"><br/>
			SN: <input type="text" name="hdds[0][hddsn]" id="hddsn"><br/>
			Notes: <input type="text" name="hdds[0][hddnotes]" id="hddnotes" size="50">

 

Obviously the next harddrive would be hdds[1][hddbrand], etc.

 

I am having an awful time looping through this to get more than 1 harddrive's information, however. I've tried foreach embedded in foreach and been messing around with this for a good 3-4 hours now and I feel like I am just missing something.

 

I've got this right now to debug:

foreach($_POST['hdds'] as $key => $a) {
	$hddbrand = $a['hddbrand'];
}

 

my print_r($a) comes up with the following:

Array ( [hddbrand] => Toshiba [hddtype] => Shaba 500gb [hddsn] => 5fu8bvw4 [hddnotes] =>none ) 1

 

Good. That's what I want. But I need some help constructing my array/loop to get the values of more than 1 drive should I have to enter information for more than 1 drive.

 

I feel like I am close, but I am just not getting what I need. I have not had to work with multidimensional arrays before and they are proving to be more trickier than I expected. Any help would be enormously appreciated!

 

Patrick

Link to comment
Share on other sites

Try removing the actual numbers out of the html array.  PHP will assign the numeric indexes for you appropriately. 

 

<input type="text" name="hdds[][hddbrand]" id="hddbrand"><br/>
Model and/or size,type: <input type="text" name="hdds[][hddtype]" id="hddtype"><br/>
SN: <input type="text" name="hdds[][hddsn]" id="hddsn"><br/>
Notes: <input type="text" name="hdds[][hddnotes]" id="hddnotes" size="50">

Link to comment
Share on other sites

beegro: Thank you. I will try that and see what results I receive.

 

Laffin: I see that I am getting the correct data outputted when I only have ONE harddrive. When there are two harddrives, it does not seem to iterate through the first and instead only returns the second. I will keep toying with it and let you know if I come to a conclusion. I just want to make sure that I am not missing something. Like I said, I am not overly experienced with multidimensional arrays, and I am having trouble returning values from two different harddrives.

 

I suppose I failed to mention that I am trying to submit this into a MySQL database, too. I have been programming for several years now and have strangely never come across a scenario like this and I just feel like I'm missing something very minute!

 

Thanks again.

Link to comment
Share on other sites

Laffin: I see that I am getting the correct data outputted when I only have ONE harddrive. When there are two harddrives, it does not seem to iterate through the first and instead only returns the second. I will keep toying with it and let you know if I come to a conclusion. I just want to make sure that I am not missing something. Like I said, I am not overly experienced with multidimensional arrays, and I am having trouble returning values from two different harddrives.

I suspect you are running the print_r() AFTER the foreach loop. So the variable you are assigning the data to on each iteration is being overwritten. Then, when the loop completes, the variable only holds the data from the last iteration.

 

OK, I assume that there would always be three sets of fields and in some cases you won't have data for some, correct? Then just run some validation on each iteration of the loop to figure out if some records should be excluded

 

A rough example:

$inputValues = array();
foreach($_POST['hdds'] as $hdd_data)
{
    $brand  = trim($hdd_data['hddbrand']);
    $type   = trim($hdd_data['hddtype']);
    $serial = trim($hdd_data['hddsn']);
    $notes  = trim($hdd_data['hddnotes']);

    ## PERFORM VALIDATIONS - Add What you need, these are examples

    //Check req'd fields
    if(empty($brand) || empty($type)  || empty($serial))
    {
        continue; //Skip this record
    }

    //Format data into a SQL insert value
    $inputValues[] = sprint_f("('%s', '%s', '%s', '%s')",
            mysql_real_escape_string($brand),
            mysql_real_escape_string($type),
            mysql_real_escape_string($serial),
            mysql_real_escape_string($notes)
            );
}

//Prepare complete INSERT statement
$query = "INSERT INTO table_name (brand, type, serial_no, notes)
          VALUES " . implode(', ', $inputValues);

Link to comment
Share on other sites

Thank you, Psycho. I've made a few changes, but still needs some work.

 

Here's what I have - I originally only have the option for 1 harddrive to be added. If another one is needed, the user can press a button  and a javascript function makes an additional set appear. Where I am having trouble is if I have more than 1 harddrive trying to be added. I need it to distinguish between 2 different drives. They each have their own column in the database. So hdd[0] cannot be mixed with hdd[1], and so forth. I'm sorry if this is confusing and I'm not explaining it well. Somehow in my foreach loop I need to include the drive number (for inserting into the table) and looping through ALL of the inputs, not just the last that I submitted. Right now, I am only getting the 2nd or 3rd harddrive's info even if the 1st form is filled out.

Link to comment
Share on other sites

Adding fields via JavaScript is probably your issue. Instead of dynamically adding the fields, I would suggest just creating the three sets of fields by default and then hiding the last two sets until the user clicks the button. That way all three sets of fields are passed in the POST data (just ignore the empty ones) and you won't have nay problems with the JavaScript corrupting your form.

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.