How to add multiple $id in one row ?
04-03-2008, 12:43 PM
|
How to add multiple $id in one row ?
|
Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
|
Dear Friends,
I want to save multiple data ($id) in one row on MySql,
For example: from authorization Combo Box (SELECT) I would like to choose multiple authorizations By using (Ctrl + Click) to save it in one row.
Then how could I output this data from (mysql_query) like "SELECT * FROM `table` WHERE ????? 'row' ????? " ?
What should I replace ????? with to check the Authorization ?
Like:
PHP Code:
<select size='10' name='auth'> <option value='1'>Add New Posts</option> <option value='2'>Edit Posts</option> <option value='3'>Delete Posts</option> <option value='4'>Add New Users</option> <option value='5'>Edit Users</option> <option value='6'>Delete Users</option> </select>
Please Advise,
Many Thanks in Advance,
A. Mansouri
|
|
|
|
04-04-2008, 03:53 AM
|
Re: How to add multiple $id in one row ?
|
Posts: 1,044
Name: Mike
Location: Mataro, Spain
|
1. <select name="auth[]">
2. $ids = $_REQUEST['auth']
3. $in = implode(',', $ids)
4. select * from table where id in ($in)
|
|
|
|
04-04-2008, 04:30 AM
|
Re: How to add multiple $id in one row ?
|
Posts: 128
Name: irimia octavian
Location: Romania
|
but the value must be a number... else, you must add some quotes there
|
|
|
|
04-04-2008, 06:27 AM
|
Re: How to add multiple $id in one row ?
|
Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
|
Thanks Very much for helping...
Quote:
Originally Posted by vectorialpx
but the value must be a number... else, you must add some quotes there
|
What "there" refer to ?
Do you mean separite data like:
PHP Code:
(select * from table where `id` in ('1','3','7','10') )
Or You Mean all in one quote, like:
PHP Code:
(select * from table where `id` in ('1,3,7,10') )
Or you mean other ??
Many Thanks in Advance...
A. Mansouri
|
|
|
|
04-04-2008, 08:22 AM
|
Re: How to add multiple $id in one row ?
|
Posts: 70
Name: Martin
Location: Fife, Scotland
|
Hi,
To get this to work what you need to do is add [] at the end of the name in your select tag, this will create an array of the data outputed.
Then you need to use the foreach loop and extract() to take the individual elements in the array and put them into usable variables.
Once you've done that you need to run a for or do while loop to run through each of the variables to determine which is which and what was selected.
I have put together a page of code which does all this then puts the information into a database.
The code for which is:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Multiple List Selection in PHP</title> </head> <body> <?php $action=$_REQUEST['action']; if ($action=='process') { // Include your database settings here or link to a file with them in it. include('functions.php'); dbconnect(); // get form variables $output=$_REQUEST['userrank']; if($output) { foreach($output as $output_value) { extract($output, EXTR_PREFIX_INVALID, "user"); } } $num_keys=count($output); $i=0; do { $var="user_{$i}"; if("".${$var}.""=='Admin') { $admin="".${$var}.""; } if("".${$var}.""=='Member') { $member="".${$var}.""; } if("".${$var}.""=='Client') { $client="".${$var}.""; } $i=$i + 1; } while ($i<=$num_keys); // check to see if variables contain data if ($admin=='') { // admin wasn't selected $admin=0; } else { // set admin to 1 $admin=1; } if ($member=='') { //member wasn't selected $member=0; } else { // set member to 1 $member=1; } if ($client=='') { // client wasn't selected $client=0; } else { // set client to 1 $client=1; } // input variables to the database $addinfo=mysql_query("INSERT INTO auth_test VALUES (NULL, '$admin', '$member', '$client')")or die(mysql_error()); if ($addinfo==0) { echo "There was a problem!"; } else { echo "The information was added successfully, the user now has the following settings: <br />"; echo "Admin: ".$admin."<br />"; echo "Member: ".$member."<br />"; echo "Client: ".$client."<br />"; echo "<br />Were the 1 means they have that authoriazation the 0 means they don't."; }
} else { ?> <form action="multiple-list.php?action=process" method="post" enctype="multipart/form-data" name="multiple" id="multiple"> <p> <select name="userrank[]" size="5" MULTIPLE id="userrank"> <option value="Admin">Admin</option> <option value="Member">Member</option> <option value="Client">Client</option> </select> </p> <p> <input type="submit" name="Submit" value="Submit" /> </p> </form> <?php } ?> </body> </html>
If you want to see what the running example looks like go to http://www.labs.tazzyserver.co.uk/mu...tiple-list.php
Hope this helps.
If you need any further help just let me know.
Regards,
Martin
|
|
|
|
04-13-2008, 02:23 PM
|
Re: How to add multiple $id in one row ?
|
Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
|
Hi,
In Edit page...
How could I Add "selected" on OPTION who was in row table ?
In this code:
PHP Code:
$sql=mysql_query("SELECT * FROM `company` ORDER BY `id` ASC") or die(mysql_error()); while($cc=mysql_fetch_array($sql)){ echo "<option value='".$cc["id"]."'>".$cc["name"]."</option> "; }
Many Thanks in Advance,
A. Mansouri
Last edited by ISSC : 04-14-2008 at 04:29 AM.
|
|
|
|
04-14-2008, 04:28 AM
|
Re: How to add multiple $id in one row ?
|
Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
|
Quote:
Originally Posted by ISSC
Hi,
In Edit page...
How could I Add "selected" on OPTION who was in row table ?
In this code:
PHP Code:
$sql=mysql_query("SELECT * FROM `company` ORDER BY `id` ASC") or die(mysql_error()); while($cc=mysql_fetch_array($sql)){ echo "<option value='".$cc["id"]."'>".$cc["name"]."</option> "; }
Many Thanks in Advance,
A. Mansouri
|
Any Update ?
|
|
|
|
04-25-2008, 06:53 PM
|
Re: How to add multiple $id in one row ?
|
Posts: 70
Name: Martin
Location: Fife, Scotland
|
Quote:
Originally Posted by ISSC
Hi,
In Edit page...
How could I Add "selected" on OPTION who was in row table ?
In this code:
PHP Code:
$sql=mysql_query("SELECT * FROM `company` ORDER BY `id` ASC") or die(mysql_error()); while($cc=mysql_fetch_array($sql)){ echo "<option value='".$cc["id"]."'>".$cc["name"]."</option> "; }
Many Thanks in Advance,
A. Mansouri
|
First of all i don't know if i'm understanding what you want correctly but i think what you mean is to get it to set the user as selected if the user matches some other variable.
I would do it this way:
PHP Code:
$sql=mysql_query("SELECT * FROM `company` ORDER BY `id` ASC") or die(mysql_error()); while($cc=mysql_fetch_array($sql)){ if($userid==$cc['id']) { echo "<option value='".$cc["id"]."' selected>".$cc["name"]."</option>"; } else { echo "<option value='".$cc["id"]."'>".$cc["name"]."</option> "; } }
Set the $userid variable to whatever variable you want to match with the id variable and it will set that one dbrow output as the selected option and the rest will be listed as normal.
Hope that helps.
Regards,
Martin
|
|
|
|
04-26-2008, 05:52 AM
|
Re: How to add multiple $id in one row ?
|
Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
|
Hi,
But if $userid = "4,8,12";
that's not work with "=="
Regards,
A. Mansouri
|
|
|
|
04-27-2008, 08:34 AM
|
Re: How to add multiple $id in one row ?
|
Posts: 70
Name: Martin
Location: Fife, Scotland
|
Quote:
Originally Posted by ISSC
Hi,
But if $userid = "4,8,12";
that's not work with "=="
Regards,
A. Mansouri
|
In other words your $userid variable holds an array, so you would need to use the explode() function to extract each individual one then use a foreach loop to check each against the other variable the == approach.
Hope that helps
Regards,
Martin
|
|
|
|
|
« Reply to How to add multiple $id in one row ?
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|