Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2003, 04:17 PM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default From db to an array to db

I am trying to write to an array from a group of list boxes that all have the same name. They were written to an html page from a database using a loop. Does anyone have any idea how this could be done? I was thinking I could declare an array using the name of the list box and loop through the array the same way it was written, but I am new to php and I am not sure. Any help would be much appreciated. Thank you for listening.
 
Old July 22nd, 2003, 06:15 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Actually its not too complicated!

Assume that we have the following:

In this example we'll query a database table called 'database_table' for the row_id. And place the row_id into an html form element array.

Code:
<?php

// form action, headers, etc. here

     $data_result = mysql_query("SELECT row_id FROM database_table WHERE condition = 'condition'");

     $i = 0;

     while ($data = mysql_fetch_array($data_result)) {

       echo "<input type=\"checkbox\" name=\"row_id[$i]\" value=\"{$data["row_id"]}\" /><br />";

          $i++;
 
     }

// other form inputs, buttons, etc. here

?>
This query is pretty simple, it creates an array of checkbox elements based on the result of the mysql query. Notice that the element's name value is set up to create an array.

Now to access that same array we'll build a very simple loop. Suppose that the form has been submitted, and the user has checked some elements. To ensure that only the checked elements are used we'll break apart the array into key value pairs so that we are working with only those that were checked. Assuming that our form was submitted using the 'post' method we would do the following:

Assuming that the same table has a lastaccesstime field, which will record the current time stamp, this is how we would update the table but only where a checkbox has been checked by the user. The key refers to the $i variable that we set to keep count in the array.

Code:
<?php

     while(list($key, $value)=each($_POST["row_id"])) {

       $update_row = mysql_query("UPDATE database_table SET lastaccesstime = null WHERE row_id = '{$value}'");

     }

     // You can just as easily do this as well and it will produce the same result:

     while(list($key, $value)=each($_POST["row_id"])) {

       $update_row = mysql_query("UPDATE database_table SET lastaccesstime = null WHERE row_id = '{$_POST["row_id"][$key]}'");

     }

?>
See there, very simple!

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 22nd, 2003, 11:23 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

BTW:
I didn't notice that you were working with <select> fields, silly me, (that is what you meant by 'listboxes', right?) so often I miss these little important details in the posts. Ah, you probably realize that working with <select> fields is very similar, you have a select field where multiple values may be selected, the submitted data is automatically formatted in a numbered array corresponding to each selected value, in which case, the example that I provided would work identically as far as the update database example is concerned.

Code:
<?php

//form stuff

    echo "<select name=\"row_id\" size=\"3\" multiple=\"multiple\">";

    while ($data=mysql_fetch_array($data_result)) {

         echo "<option value=\"{$data["row_id"]}\">{$data["row_id"]}</option>";

    }

     echo "</select>";

// more form stuff
multiple=\"multiple\" is BTW the XHTML compliant method of writing that attribute.

Upon submission, the user has clicked option 1 and option 3
The corresponding array should look like this:

$_POST["row_id"][0] = (value data for option 1)
$_POST["row_id"][1] = (value data for option 3)

Though I say this in theory only - I've never worked with multiple select boxes, but its an educated guess that they would output in this manner. If you want to be absolutely sure what the key value is, just echo it out when you're running through the loop. Which doesn't really matter anyway, the list() and each() functions will keep it all straight!

And you said they all have the same name? That you are going to have to change! If you have more than one form element with the exact same name the data from each new element will overwrite the last. If you are looping to recreate the <select> fields like this...

Lets say we have a database table of poems, each user may have multiple poems, so we'll generate a table of poems for each user and define a loop to go through the selected ones and set permission to yes.

The database table looks like this:

submission_id
user_id
poem_title
poem_body
lastaccesstime
permission

Code:
//form stuff

$users_result = mysql_query("SELECT * FROM poems");


while ($data = mysql_fetch_array($users_result)) {
 
 echo "<select name=\"users[{$data["user_id"]}]\">";


    $poems_result = mysql_query("SELECT * FROM poems WHERE user_id = '{$data["user_id"]}'");

    while ($poems_data = mysql_fetch_array($poems_result)) {

        echo "<option value=\"{$poems_data["submission_id"]}\">{$poems_data["poem_title"]}</option>";

    }

  echo "</select>";

}

// more form stuff


Now each users field is really a multi-dimensional array.

Each would look like this:

$_POST["users"][234][0] corresponds to option 1 for user '234'
$_POST["users"][234][1] corresponds to option 2 for user '234'

Two submissions were selected by user 234.


So to access this mess we're going to need a double loop:

while(list($user_key, $user_value) = each($_POST["users"])) {

    if (is_array($_POST["users"][$user_key])) {


        while(list($key, $value) = each($_POST["users"][$user_key])) {


            $update = mysql_query("UPDATE poems SET permission = 'Y' WHERE submission_id = '{$value}' AND user_id = '{$user_key}'");

                if (!$update) {

                    echo mysql_error();            
                    echo mysql_errno();

                } else {

                    echo "Record: #{$value} updated successfully!<br />";

                }

        }


    }


}
I haven't tested this code but I think its theory is pretty sound. There may be some parse errors in there but I think it does the trick.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 23rd, 2003, 10:42 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the timely reply to my problem. This code is pure gold and much appreciated!!
 
Old November 21st, 2005, 11:31 AM
Authorized User
 
Join Date: Nov 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this example just shows one array form input. How can I populate multiple array form inputs with just one query?
Thank you in advance.






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to transform an MDF db into a permanent db? hertendreef SQL Server 2005 2 April 10th, 2007 03:57 PM
Finding the DB Sailor.mdb of Beginning ASP DB book anna Classic ASP Databases 2 August 5th, 2006 01:13 PM
access db to sql server db mikersantiago Classic ASP Basics 4 November 16th, 2004 03:33 AM
Synchronizing web db with our master db sunny25 Classic ASP Basics 0 October 17th, 2003 09:16 AM
From db to an array to db wfrisch PHP Databases 1 July 22nd, 2003 09:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.