Discussion:
set array
John Taylor-Johnston
2013-11-26 02:53:44 UTC
Permalink
I have a SET field in MYSQL.
'1. Type One','2. Type Two','3. Type Three'

The user can multi-select
<select name="DPRtype[]" multiple>
<option value="1. Type One">
<option value="2. Type Two">
<option value="3. Type Three">
</select>

I just dont know how to express $_POST[DPRtype] to be able to build the
$SQL to express the separate possibilities.

$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";


Thanks. Not a newbie. I should know how to do this. I just don't code
every day.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-11-26 03:08:37 UTC
Permalink
Post by John Taylor-Johnston
I have a SET field in MYSQL.
'1. Type One','2. Type Two','3. Type Three'
The user can multi-select
<select name="DPRtype[]" multiple>
<option value="1. Type One">
<option value="2. Type Two">
<option value="3. Type Three">
</select>
I just dont know how to express $_POST[DPRtype] to be able to build the
$SQL to express the separate possibilities.
$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";
Thanks. Not a newbie. I should know how to do this. I just don't code
every day.
One google search later I found this:

For example, suppose that a column is specified as SET('a','b','c','d'):
mysql> CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));


If you insert the values 'a,d', 'd,a', 'a,d,d', 'a,d,a', and 'd,a,d':
mysql> INSERT INTO myset (col) VALUES
-> ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0


It would appear that you need to built a comma-separated string :
$setvals='';
foreach($_POST['DPRtype'] as $item)
if ($setvals<>'')
$setvals .= ",".$item;
else
$setvals .= $item;
and then do:

set DPRtype=('$setvals')

in your query.

here's the link to this info:
http://dev.mysql.com/doc/refman/5.0/en/set.html
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Taylor-Johnston
2013-11-26 04:08:55 UTC
Permalink
I have a SET field in MYSQL.
Post by John Taylor-Johnston
'1. Type One','2. Type Two','3. Type Three'
The user can multi-select
<select name="DPRtype[]" multiple>
<option value="1. Type One">
<option value="2. Type Two">
<option value="3. Type Three">
</select>
I just dont know how to express $_POST[DPRtype] to be able to build
the $SQL to express the separate possibilities.
$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";
If I use a foreach like this, it will be very clunky. And I'll have an
extra comma at the end. What is a cleaner way?

$MyString = express_value_select ($_POST['DPRtype'],"1. Type One");
$MyString .= express_value_select ($_POST['DPRtype'],"2. Type Two");
$MyString .= express_value_select ($_POST['DPRtype'],"3. Type Three");

function express_value_select($tofilter,$tofind) {
foreach($tofilter as $value){
if ($value == $tofind) echo $tofind.",";
}
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ken Robinson
2013-11-26 04:27:50 UTC
Permalink
Post by John Taylor-Johnston
I have a SET field in MYSQL.
Post by John Taylor-Johnston
'1. Type One','2. Type Two','3. Type Three'
The user can multi-select
<select name="DPRtype[]" multiple>
<option value="1. Type One">
<option value="2. Type Two">
<option value="3. Type Three">
</select>
I just dont know how to express $_POST[DPRtype] to be able to build
the $SQL to express the separate possibilities.
$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";
If I use a foreach like this, it will be very clunky. And I'll have
an extra comma at the end. What is a cleaner way?
$MyString = express_value_select ($_POST['DPRtype'],"1. Type One");
$MyString .= express_value_select ($_POST['DPRtype'],"2. Type Two");
$MyString .= express_value_select ($_POST['DPRtype'],"3. Type Three");
function express_value_select($tofilter,$tofind) {
foreach($tofilter as $value){
if ($value == $tofind) echo $tofind.",";
}
}
You don't want to use "echo" in a function, you want to return the
value. Store the values returned in a temporary array, then implode
the array to get the comma separated string

<?php
function express_value_select($tofilter,$tofind) {
foreach($tofilter as $value){
if ($value == $tofind) return $tofind;
}
}

$tmp = array();

$tmp[] = express_value_select ($_POST['DPRtype'],"1. Type One");
$tmp[] = express_value_select ($_POST['DPRtype'],"2. Type Two");
$tmp[] = express_value_select ($_POST['DPRtype'],"3. Type Three");

$MyString = implode(", ",$tmp);

?>

Ken
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Taylor-Johnston
2013-11-26 04:40:37 UTC
Permalink
Post by John Taylor-Johnston
I have a SET field in MYSQL.
Post by John Taylor-Johnston
'1. Type One','2. Type Two','3. Type Three'
The user can multi-select
<select name="DPRtype[]" multiple>
<option value="1. Type One">
<option value="2. Type Two">
<option value="3. Type Three">
</select>
I just dont know how to express $_POST[DPRtype] to be able to build
the $SQL to express the separate possibilities.
$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";
If I use a foreach like this, it will be very clunky. And I'll have an
extra comma at the end. What is a cleaner way?
$MyString = express_value_select ($_POST['DPRtype'],"1. Type One");
$MyString .= express_value_select ($_POST['DPRtype'],"2. Type Two");
$MyString .= express_value_select ($_POST['DPRtype'],"3. Type Three");
function express_value_select($tofilter,$tofind) {
foreach($tofilter as $value){
if ($value == $tofind) echo $tofind.",";
}
}
foreach($_POST['DPRtype'] as $row){
echo $row. ",";
}

Then trim a comma?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Taylor-Johnston
2013-11-26 05:13:47 UTC
Permalink
Post by John Taylor-Johnston
I have a SET field in MYSQL.
Post by John Taylor-Johnston
'1. Type One','2. Type Two','3. Type Three'
The user can multi-select
<select name="DPRtype[]" multiple>
<option value="1. Type One">
<option value="2. Type Two">
<option value="3. Type Three">
</select>
I just dont know how to express $_POST[DPRtype] to be able to build
the $SQL to express the separate possibilities.
$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";
PHP created rtrim, but did not make something like this? Ouf, I feel
like I just smashed a fly with a nuke.

function express_value_select($tofilter) {
$start = 0;
$ToFilterSQL ="";
foreach($tofilter as $row){
$ToFilterSQL .= $row. ",";
$start++;
}
if ($start > 0)
$ToFilterSQL = rtrim($ToFilterSQL, ",");

#echo $ToFilterSQL;
return $ToFilterSQL;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Taylor-Johnston
2013-11-26 11:41:18 UTC
Permalink
I just dont know how to express $_POST[DPRtype] to be able to build the $SQL to express the separate possibilities.
$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";
PHP created rtrim, but did not make something like this? Ouf, I feel like I just smashed a fly with a nuke.
function express_value_select($tofilter) {
$start = 0;
$ToFilterSQL ="";
foreach($tofilter as $row){
$ToFilterSQL .= $row. ",";
$start++;
}
if ($start > 0)
$ToFilterSQL = rtrim($ToFilterSQL, ",");
#echo $ToFilterSQL;
return $ToFilterSQL;
}
as David Robley wrote, implode does it. It creates a string from an array using one parameter as a „glue“ character
in any case, you would not need $start in your function. better use is_array before using your function.
for information on implode see http://us1.php.net/function.implode
function express_value_select($tofilter) {
$ToFilterSQL=implode(“,“,$tofilter);
return $ToFilterSQL;
}
Chris
Thanks. I have received no other posts. I will look this over.
:) John
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sachin Raut
2013-11-26 14:30:03 UTC
Permalink
implode() eg.

<?php

$names=array("Apple","Microsoft","Google","Facebook");

$longString=implode(",", $names);

echo "<br />".$longString; //$longString=Apple,Microsoft,Google,Facebook

?>


regards

Sachin Raut


On Tue, Nov 26, 2013 at 5:11 PM, John Taylor-Johnston <
Post by John Taylor-Johnston
I just dont know how to express $_POST[DPRtype] to be able to build the
Post by John Taylor-Johnston
$SQL to express the separate possibilities.
$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";
PHP created rtrim, but did not make something like this? Ouf, I feel
like I just smashed a fly with a nuke.
function express_value_select($tofilter) {
$start = 0;
$ToFilterSQL ="";
foreach($tofilter as $row){
$ToFilterSQL .= $row. ",";
$start++;
}
if ($start > 0)
$ToFilterSQL = rtrim($ToFilterSQL, ",");
#echo $ToFilterSQL;
return $ToFilterSQL;
}
as David Robley wrote, implode does it. It creates a string from an array
using one parameter as a „glue“ character
in any case, you would not need $start in your function. better use
is_array before using your function.
for information on implode see http://us1.php.net/function.implode
function express_value_select($tofilter) {
$ToFilterSQL=implode(“,“,$tofilter);
return $ToFilterSQL;
}
Chris
Thanks. I have received no other posts. I will look this over.
:) John
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Robley
2013-11-26 03:21:11 UTC
Permalink
Post by John Taylor-Johnston
I have a SET field in MYSQL.
'1. Type One','2. Type Two','3. Type Three'
The user can multi-select
<select name="DPRtype[]" multiple>
<option value="1. Type One">
<option value="2. Type Two">
<option value="3. Type Three">
</select>
I just dont know how to express $_POST[DPRtype] to be able to build the
$SQL to express the separate possibilities.
$sql = "UPDATE `taylorjo`.`CRTP_CGA`
SET
`DPRtype` = '$_POST[DPRtype]'
";
Thanks. Not a newbie. I should know how to do this. I just don't code
every day.
Use implode() to create a comma separated string for insertion into your
table
--
Cheers
David Robley

Why are Chinese fortune cookies written in English?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...