Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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!
 
 
Register now for full access!
Old 04-04-2008, 04:53 AM Re: How to add multiple $id in one row ?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
1. <select name="auth[]">
2. $ids = $_REQUEST['auth']
3. $in = implode(',', $ids)
4. select * from table where id in ($in)
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

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, 05:30 AM Re: How to add multiple $id in one row ?
vectorialpx's Avatar
Extreme Talker

Posts: 241
Name: octavian
Location: Bucharest
Trades: 0
but the value must be a number... else, you must add some quotes there
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 04-04-2008, 07:27 AM Re: How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 09:22 AM Re: How to add multiple $id in one row ?
digitalfusion's Avatar
Skilled Talker

Posts: 73
Name: Martin
Location: Fife, Scotland
Trades: 0
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
__________________
---------------------------------------------------

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-13-2008, 03:23 PM Re: How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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 05:29 AM..
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-14-2008, 05:28 AM Re: How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 07:53 PM Re: How to add multiple $id in one row ?
digitalfusion's Avatar
Skilled Talker

Posts: 73
Name: Martin
Location: Fife, Scotland
Trades: 0
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
__________________
---------------------------------------------------

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-26-2008, 06:52 AM Re: How to add multiple $id in one row ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 09:34 AM Re: How to add multiple $id in one row ?
digitalfusion's Avatar
Skilled Talker

Posts: 73
Name: Martin
Location: Fife, Scotland
Trades: 0
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
__________________
---------------------------------------------------

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
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 Search this Thread
Search this Thread:

Advanced Search

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 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 1.16443 seconds with 12 queries