update multiple text field

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
nga
Forum Commoner
Posts: 46
Joined: Mon Aug 17, 2009 3:05 am

update multiple text field

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: update multiple text field

Post by requinix »

You can put keys in there too, like textfield[123] where 123 is the (unique) product ID.
nga
Forum Commoner
Posts: 46
Joined: Mon Aug 17, 2009 3:05 am

Re: update multiple text field

Post by nga »

hmm, if i put key in like that, how can i access the key?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: update multiple text field

Post 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.
nga
Forum Commoner
Posts: 46
Joined: Mon Aug 17, 2009 3:05 am

Re: update multiple text field

Post by nga »

thank you very much! I've seen people using key and value that way but didnt understand. Can you explain the concept?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: update multiple text field

Post 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?
nga
Forum Commoner
Posts: 46
Joined: Mon Aug 17, 2009 3:05 am

Re: update multiple text field

Post 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?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: update multiple text field

Post 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.
Post Reply