Re: [PHP] multiple choice dropdown box puzzle

2009-02-23 Thread Andrew Ballard
On Mon, Feb 23, 2009 at 10:25 AM, PJ af.gour...@videotron.ca wrote:
 I think this is a tough one... and way above my head:
 PLEASE READ ALL OF THE ABOVE TO UNDERSTAND WHAT I AM TRYING TO DO.
 Having a bit of a rough time figuring out how to formulate php-mysql to 
 insert data into fields using a multiple dropdown box in a form.

 to post I am using the following:
 snip...
 $categoriesIN   = $_POST[categoriesIN];

 ...snip...

 select name=$categoriesIN[] multiple=multiple
OPTIONChoose Categories.../option
OPTION VALUE=? echo $categoriesIN; ?1
OPTION VALUE=? echo $categoriesIN; ?2
OPTION VALUE=? echo $categoriesIN; ?3
OPTION VALUE=? echo $categoriesIN; ?4
OPTION VALUE=? echo $categoriesIN; ?5
/SELECT

 ...snip...

 $sql4 = FOR ( $ii = 0 ; $ii  count($categoriesIN) ; $ii++ )
INSERT INTO temp (example) $categoriesIN[$ii] ;
$result4 = mysql_query($sql4, $db);
 ...snip

 this does not work! The other posts work like a charm... but this...

You are confusing the PHP and MySQL.

?php

$count = count($categoriesIN);
for ($ii = 0; $ii  $count; ++$ii) {
$sql = INSERT INTO temp (example) VALUES (' .
mysql_real_escape_string($categoriesIN[$ii]) . ');
$result = mysql_query($sql, $db);
}

?

MySQL also has an alternative syntax that you can use to do this in a
single statement:

?php

$count = count($categoriesIN);
if ($count  0) {
$sql = INSERT INTO temp (example) VALUES ;
for ($ii = 0; $ii  $count; ++$ii) {
if ($ii  0) $sql .= ', ';
$sql .=  (' . mysql_real_escape_string($categoriesIN[$ii]) . ');
}
}

$result = mysql_query($sql, $db);

?



Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] multiple choice dropdown box puzzle

2009-02-23 Thread Afan Pasalic



PJ wrote:

I think this is a tough one... and way above my head:
PLEASE READ ALL OF THE ABOVE TO UNDERSTAND WHAT I AM TRYING TO DO.
Having a bit of a rough time figuring out how to formulate php-mysql to insert 
data into fields using a multiple dropdown box in a form.

to post I am using the following:
snip...
$categoriesIN   = $_POST[categoriesIN];

...snip...

select name=$categoriesIN[] multiple=multiple
OPTIONChoose Categories.../option
OPTION VALUE=? echo $categoriesIN; ?1
OPTION VALUE=? echo $categoriesIN; ?2
OPTION VALUE=? echo $categoriesIN; ?3
OPTION VALUE=? echo $categoriesIN; ?4
OPTION VALUE=? echo $categoriesIN; ?5
	/SELECT 


...snip...

$sql4 = FOR ( $ii = 0 ; $ii  count($categoriesIN) ; $ii++ )
INSERT INTO temp (example) $categoriesIN[$ii] ;
   
$result4 = mysql_query($sql4, $db); 
...snip

this does not work! The other posts work like a charm... but this...

I cannot figure out what I should be entering where... I have tried several 
different configurations, but nothing seems to work...

I found this as a model for entering the selections but can't figure out how to 
modify it for my needs:

select name=branch_no[] multiple=multiple size=5
option  Choose your location(s) /option
option value=31003100/option
option value=31053105/option
option value=3503 3503/option
option value=3504 3504/option
/select

What I would like to do is something like the following:
select name=$categoriesIN[] multiple=multiple
OPTIONChoose Categories.../option
OPTION VALUE=1History
OPTION VALUE=2Temples
OPTION VALUE=2Pharaohs and Queens
OPTION VALUE=4Cleopatra
OPTION VALUE=4Mummies
/SELECT
and going further, I would like to be able to use a table that actually holds 
these values to feed them to the code above. I am sure this is possible but it 
must take some huge knowledge and experience to do it.

BUT ...
as I look at things, I am wondering if the FOR statement in the above should be 
used to do several INSERTs, that is, one $sql(number) per selected category... 
now, would that require many $sqls or many INSERTs within the $sql ?


  


first, I think, $categoriesIN is string, but in the form you made it  as 
an array $categoriesIN[]. I think you have to modify it a little bit, 
something like {$categoriesIN}.'[]'


second, I think the php part FOR ( $ii = 0 ; $ii  count($categoriesIN) 
; $ii++ ) can't be part of the mysql statement, it should be outside 
the statement


FOR ( $ii = 0 ; $ii  count($categoriesIN) ; $ii++ )
{
$sql4 = INSERT INTO temp (example) $categoriesIN[$ii] ;   
  
$result4 = mysql_query($sql4, $db); 
}


afan




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] multiple choice dropdown box puzzle

2009-02-23 Thread Curtis Maurand


You're looking for something like:

This gets called 10 times from another function, but this is sort of 
what you're looking for. This gives me a combo-box.


function qselect($mysql_link, $i)
  {
 $driverquery = select car_no, drv_name from cars order by car_no 
+ 0;

 $driverresult = mysql_query($driverquery, $mysql_link);
 print(select name='pick$i'\n);
 while ($driverrows = mysql_fetch_array($driverresult))
  {
print(  option value = 
'$driverrows[0]'$driverrows[1]/option\n);

  }
 print(   /select\n);
  }

HTH
Curtis

Afan Pasalic wrote:



PJ wrote:

I think this is a tough one... and way above my head:
PLEASE READ ALL OF THE ABOVE TO UNDERSTAND WHAT I AM TRYING TO DO.
Having a bit of a rough time figuring out how to formulate php-mysql 
to insert data into fields using a multiple dropdown box in a form.


to post I am using the following:
snip...
$categoriesIN= $_POST[categoriesIN];

...snip...

select name=$categoriesIN[] multiple=multiple
OPTIONChoose Categories.../option
OPTION VALUE=? echo $categoriesIN; ?1
OPTION VALUE=? echo $categoriesIN; ?2
OPTION VALUE=? echo $categoriesIN; ?3
OPTION VALUE=? echo $categoriesIN; ?4
OPTION VALUE=? echo $categoriesIN; ?5
/SELECT
...snip...

$sql4 = FOR ( $ii = 0 ; $ii  count($categoriesIN) ; $ii++ )
INSERT INTO temp (example) $categoriesIN[$ii] ;   
$result4 = mysql_query($sql4, $db);   
...snip


this does not work! The other posts work like a charm... but this...

I cannot figure out what I should be entering where... I have tried 
several different configurations, but nothing seems to work...


I found this as a model for entering the selections but can't figure 
out how to modify it for my needs:


select name=branch_no[] multiple=multiple size=5
option  Choose your location(s) /option
option value=31003100/option
option value=31053105/option
option value=3503 3503/option
option value=3504 3504/option
/select

What I would like to do is something like the following:
select name=$categoriesIN[] multiple=multiple
OPTIONChoose Categories.../option
OPTION VALUE=1History
OPTION VALUE=2Temples
OPTION VALUE=2Pharaohs and Queens
OPTION VALUE=4Cleopatra
OPTION VALUE=4Mummies
/SELECT
and going further, I would like to be able to use a table that 
actually holds these values to feed them to the code above. I am sure 
this is possible but it must take some huge knowledge and experience 
to do it.


BUT ...
as I look at things, I am wondering if the FOR statement in the above 
should be used to do several INSERTs, that is, one $sql(number) per 
selected category... now, would that require many $sqls or many 
INSERTs within the $sql ?



  


first, I think, $categoriesIN is string, but in the form you made it  
as an array $categoriesIN[]. I think you have to modify it a little 
bit, something like {$categoriesIN}.'[]'


second, I think the php part FOR ( $ii = 0 ; $ii  
count($categoriesIN) ; $ii++ ) can't be part of the mysql statement, 
it should be outside the statement


FOR ( $ii = 0 ; $ii  count($categoriesIN) ; $ii++ )
{
$sql4 = INSERT INTO temp (example) $categoriesIN[$ii] 
;   
$result4 = mysql_query($sql4, $db);   
}



afan







--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php