Current location: Hot Scripts Forums » Programming Languages » PHP » Pulling data from multiple select list


Pulling data from multiple select list

Reply
  #1 (permalink)  
Old 12-08-04, 08:46 PM
bayside2004 bayside2004 is offline
Newbie Coder
 
Join Date: Dec 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Pulling data from multiple select list

Hello,
I am looking for code that will accept data from a select list set to multiple. PRefer PHP but CGI in perl would be ok to.
Thanks,
ARt
Reply With Quote
  #2 (permalink)  
Old 12-08-04, 09:03 PM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Here you go, free to use, modify etc, not to sell, Released under the ACL (Still writing it).
PHP Code:

<?php

// Copyright Acecool @ Acecoolco.com
if (isset($_POST['submit']))
{
    foreach (
$_POST as $x => $y)
    {
        if (
is_array($_POST[$x]))
        {
            foreach (
$_POST[$x] as $v => $c)
            {
                echo 
"$x - $v - $c<br />";
            }
        }
        else
        {
            echo 
"$x - $y<br />";
        }
    }
}
?>
<br />
<form method="POST" method="zselect.php">
<select name="test[]" size="10" MULTIPLE>
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
<option value="test4">test4</option>
<option value="test5">test5</option>
<option value="test6">test6</option>
<option value="test7">test7</option>
<option value="test8">test8</option>
<option value="test9">test9</option>
<option value="test0">test0</option>
</select>
<br />
<input type="submit" name="submit">
</form>
Now to explain:
If the form was submitted
Go through every single thing submitted

if form_name is an array (since we used [] for multi select support)
go through each key, and print out as form_name, key, value

else
Output as form_name, value

$x = Form_Name
$y = Form_Value
$v = Form_Key (For arrayed form types)
$c = Form_Key_Value, for arrayed form types.
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions

Last edited by Acecool; 12-08-04 at 09:06 PM.
Reply With Quote
  #3 (permalink)  
Old 12-09-04, 12:19 AM
bayside2004 bayside2004 is offline
Newbie Coder
 
Join Date: Dec 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the quick responce,
This code looks good, but I need to send the data to a seperate php script which will in a loop save each data value into a MYSQL table. If you could elaborate on additional steps I may need, it would be greatly appreciated.
Thanks,
Art
Reply With Quote
  #4 (permalink)  
Old 12-09-04, 05:22 AM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
You could edit $x, $y, $c, $v dynamically, then generate a query to insert it.

You could also take this part:
PHP Code:

        if (is_array($_POST[$x])) 

        { 
            foreach (
$_POST[$x] as $v => $c
            { 
                echo 
"$x - $v - $c<br />"
            } 
        } 
and change the $x part to the multi select field, so when its submitted, it will go through each value submitted, then you could do something like..

$Output = FALSE;

REPLACE:
foreach ($_POST[$x] as $v => $c)
with
$i = FALSE;
foreach ($_POST[$x] as $v => $c)

Replace echo "$x - $v - $c<br />";
with
$Output .= $c;
$i++;
if ($i < count($_POST[$x] - 1))
{
// Insert comma space if not at the end of the thing..
$Output .= ', ';
}

..

How exactly is this multi select setup?
Do you want to insert it into 1 column (above code)
or multiple rows, muliple columns?
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #5 (permalink)  
Old 12-09-04, 07:06 AM
bayside2004 bayside2004 is offline
Newbie Coder
 
Join Date: Dec 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
I have 4 selection lists, I was planning on using individual rows for each list, but if I can find a split function like Perl, I like to place them in one row with the multiple values in each cell. I will research it more, but do you know the split fnction in php?
Thanks,
art
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why won't select list item insert into database? gavacho PHP 2 12-05-08 06:38 PM
Formmail Script Fix - Cant display multiple list selections.. zamen PHP 3 05-02-04 09:59 AM
need help with retaining select list values sky4est PHP 1 03-25-04 05:00 PM
Mail Script sending multiple times when Table has a lot of data dsumpter PHP 12 07-16-03 05:17 PM


All times are GMT -5. The time now is 05:18 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.