Page 1 of 1

update multiple text field

Posted: Wed Jan 06, 2010 3:30 am
by nga
i have a form consists of multiple text fields. the purpose of the form is for admin to update the order of multiple products (pulling from database). Single submit button should submit the form and update the order of respective products.

The problem here is, i dont know the number of the text fields will be displayed so i used textfield[] for each text field and for loop to loop through everysingle text field
-> i dont even know which field belong to which product T.T

I'm thinking of using the same sql select statement to select all product and just update the product order like (not writing in php, just the logic)
$i=0;
while($row['order']=...)
{
update $row['order']=textfield[$i]
$i++;
}

is there any smarter way to go? If no, is there any problem using the above logic?

Re: update multiple text field

Posted: Wed Jan 06, 2010 3:40 am
by requinix
You can put keys in there too, like textfield[123] where 123 is the (unique) product ID.

Re: update multiple text field

Posted: Wed Jan 06, 2010 3:43 am
by nga
hmm, if i put key in like that, how can i access the key?

Re: update multiple text field

Posted: Wed Jan 06, 2010 4:04 am
by requinix
$_POST["textfield"] will be just like any other array.

Code: Select all

foreach ($_POST["textfield"] as $key => $value) {
    echo "key=$key, value=$value<br>\n";
}
Also, array_keys() will give you an array of just the keys, if that's easier for you to deal with.

Re: update multiple text field

Posted: Wed Jan 06, 2010 9:00 am
by nga
thank you very much! I've seen people using key and value that way but didnt understand. Can you explain the concept?

Re: update multiple text field

Posted: Wed Jan 06, 2010 9:19 am
by jason
If you're referring to:

Code: Select all

foreach ($_POST["textfield"] as $key => $value) {
It's actually quite easy.

Code: Select all

 
$array = array('title'=>'Yo Dawg Wuz up!', 'content'=>'Yo dawg, wut be happunin rite yo?  Kool, talk to ya latah, dawg!');
foreach ($array as $value) {
Okay, so, you have an array. This array consists of key=>value pairs. Basically, to access a key, you'd do:

Code: Select all

echo $array['title'];
This would output:

Code: Select all

Yo Dawg Wuz up!
Of course, that's clearly just the title. If you wanted to access the content, you'd write:

Code: Select all

echo $array['content'];
This would output:

Code: Select all

Yo dawg, wut be happunin rite yo?  Kool, talk to ya latah, dawg!
Fo shizzle my nizzle? Good. Of course, let's say you want to do something else with that array. Let's say you want to bold the title. Let's also assume you are like most programmers, and are lazy and you want to do this easily. Let's also say that the array has an email address as well, like so:

Code: Select all

$array = array('title'=>'Yo Dawg Wuz up!', 'content'=>'Yo dawg, wut be happunin rite yo?  Kool, talk to ya latah, dawg!','email'=>'foshizzle@mynizzle.cuz');
And let's say you want to make that email address a link. Now, you can hard code everything, and copy/paste echo statements and all that, but there is an easier way.

Code: Select all

<?php
 
$array = array('title'=>'Yo Dawg Wuz up!', 'content'=>'Yo dawg, wut be happunin rite yo?  Kool, talk to ya latah, dawg!','email'=>'foshizzle@mynizzle.cuz');
 
foreach ( $array as $name => $value ) {
    switch ( $name ) {
        case 'title':
            $value = '<b>'.$value.'</b>'; // Using the bold tag cuz I'm hardcore!
            break;
        case 'email':
            $value = '<a href="mailto:'.$value.'">Email me, good sir</a>';
            break;
    }
    
    echo $value."<br />";
}
?>
Basically, the $name=>$value pairing allows us to access the $name, or the value of the array's current index. This is the index associated with the value. Generally, this makes things easier for us programmers to do nifty things.

Fo shizzle?

Re: update multiple text field

Posted: Wed Jan 06, 2010 9:31 am
by nga
god! understand this makes my life so much easier! so it's a paring of key and value in much simpler way than i always did. Dont know if it works well with array in array?

Re: update multiple text field

Posted: Wed Jan 06, 2010 10:26 am
by jason
You can handle sub-arrays easily enough. Something like this:

Code: Select all

 
// Aliases?
$aliases = array(
    'real_name' => 'Jason Lotito',
    'aliases' => array(
        'King of the World',
        'Master of All Creation',
        'Your Lord and Master',
        'Some Guy on the Interwebz',
        'Loser On The Internet Too Often'
    );
    
);
 
foreach ( $aliases as $key => $value ) {
    if ( is_array($value) ) {
        $aliasStr = "<ul>";
        foreach ( $value as $alias ) {
            $aliasStr .= "<li>$alias</li>";
        }
        $aliasStr .= "</ul>";
    } else {
        $nameStr = "<strong>Name: $value</strong>";
    }
}
 
echo $nameStr.$aliasStr;
You should be able to follow the code there.