Reply
How to add multiple $id in one row ?
Old 04-03-2008, 12:43 PM How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

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
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
When You Register, These Ads Go Away!
Old 04-04-2008, 03:53 AM Re: How to add multiple $id in one row ?
mtishetsky's Avatar
King Spam Talker

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)
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 04-04-2008, 04:30 AM Re: How to add multiple $id in one row ?
vectorialpx's Avatar
Super Talker

Posts: 128
Name: irimia octavian
Location: Romania
but the value must be a number... else, you must add some quotes there
__________________
my photos, my website, creation lab
vectorial pixel web services :: and, sorry for my English !
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 04-04-2008, 06:27 AM Re: How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
Thanks Very much for helping...

Quote:
Originally Posted by vectorialpx View Post
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 `idin ('1','3','7','10') ) 
Or You Mean all in one quote, like:
PHP Code:
(select from table where `idin ('1,3,7,10') ) 
Or you mean other ??

Many Thanks in Advance...
A. Mansouri
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-04-2008, 08:22 AM Re: How to add multiple $id in one row ?
digitalfusion's Avatar
Skilled Talker

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($outputEXTR_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
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-13-2008, 02:23 PM Re: How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

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.
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-14-2008, 04:28 AM Re: How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
Quote:
Originally Posted by ISSC View Post
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 ?
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-25-2008, 06:53 PM Re: How to add multiple $id in one row ?
digitalfusion's Avatar
Skilled Talker

Posts: 70
Name: Martin
Location: Fife, Scotland
Quote:
Originally Posted by ISSC View Post
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
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-26-2008, 05:52 AM Re: How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

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
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-27-2008, 08:34 AM Re: How to add multiple $id in one row ?
digitalfusion's Avatar
Skilled Talker

Posts: 70
Name: Martin
Location: Fife, Scotland
Quote:
Originally Posted by ISSC View Post
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
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Reply     « Reply to How to add multiple $id in one row ?
 

Thread Tools

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.19237 seconds with 12 queries