form values into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bluesman333
Forum Commoner
Posts: 52
Joined: Wed Dec 31, 2003 9:47 am

form values into an array

Post by bluesman333 »

i have a form, with text fields storing data in an array. each will be a row of data in the db.


<input name="value[]" type="text">
<input name="id[]" type="hidden" value="<?php print $id; ?>">


i have two questions.

how do i get these value into the db? i've been accessing each element by it's number in a while loop. ex: insert $value[1], id[1], insert $value[2]...

i'm sure this isn't the best way to do this.

also, i'm not requiring that all the fields in the form have data and am getting zero values. how do i make it so these values are never stored in the array? my idea was to only insert, into the db, the values that were not zero. is there a more efficient way?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

To avoid storing empty values use

Code: Select all

if (!empty($var)) {
    //Do whatever
}
<input name="value[]" type="text">
This makes no sense, where's the value attribute? As far as storing data from multiple fields goes then yes you can use a loop, that's efficient enough. Everything should be in $_POST or $_GET depending upon how you send the data however, so you should be accessing it via this method.
User avatar
bluesman333
Forum Commoner
Posts: 52
Joined: Wed Dec 31, 2003 9:47 am

Post by bluesman333 »

As far as storing data from multiple fields goes then yes you can use a loop, that's efficient enough.
but is there a better way? here's what i've been doing.

Code: Select all

$x = 0;
$num = count(the number of input fields in the form);
 
while ($num > $x) {
             
            INSERT INTO TABLE VALUES ($id[$x], $value[$x])
             $x++;
}

but, this will insert the values from every field in the form, even the ones with no value input.  


are you suggesting this?  

while ($num > $x) {
            if (!empty($value[$x]) {
            INSERT INTO TABLE VALUES ($id[$x], $value[$x])
             $x++;
             }
}

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes. Although perhaps a for() loop will be more suitable? Personal preference I guess though.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

d11wtq wrote:Yes. Although perhaps a for() loop will be more suitable? Personal preference I guess though.
better yet, a foreach()
Post Reply