peter-cooper
asked on
Convert php array to comma seperated list
Hi
I have an array that I need to convert to comma seperated for inclusion in mysql. I have posted my code and would appreciate any help or guidance. Thanks
output:
php
results page
This is what I have tried in results page
I have an array that I need to convert to comma seperated for inclusion in mysql. I have posted my code and would appreciate any help or guidance. Thanks
output:
[boxdest] => Array
(
[0] => WD1
[1] => WD10
[2] => WD11
)
php
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("sample",$conn);
$result = mysql_query("SELECT * FROM boxes where department = '{$_GET['dept']}' ORDER BY custref ASC");
?>
<select name="boxdest[]" id="boxdest" size="7" multiple="multiple">
<?php
$i=0;
while($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row["custref"];?>"><?php echo $row["custref"];?></option>
<?php
$i++;
}
?>
</select>
<input type="button" id="submit2" name="submit2" value=">" />
<input type="button" id="submit3" name="submit3" value="<" />
<select name="boxdest2[]" id="boxdest2" size="7" multiple="multiple"></select>
<script type="text/javascript">
$('#submit2').click(function () {
var selected = $('#boxdest > option:selected');
$('#boxdest2').append(selected.clone());
selected.hide();
});
$('#submit3').click(function () {
$('#boxdest').append($('#boxdest2 > option').clone());
$('#boxdest2').empty();
});
</script>
results page
This is what I have tried in results page
if (isset($_POST['boxdest']))
{
if (!empty($_POST['boxdest']))
{
$destroydata = split(',', $_POST['boxdest'][0]);
$result = array();
foreach ($destroydata as $val4)
{
if ( $val4 != "" )
{
$boxdest[] = $val4;
$result[] = $boxdest;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It will convert the given array into the comma separated string, as like below.
And if you want again to convert comma separated string into the Array you can achieve that as follows.
$arr = array ( 0 => "lorem",1 => "ipsum", 2 => "dolor");
$str = implode (", ", $arr);
echo $str; //it will output result - 'lorem, ipsum, dolor'
And if you want again to convert comma separated string into the Array you can achieve that as follows.
$str = "lorem, ipsum, dolor";
$arr= explode(", ",$str);
print_r (explode(", ",$arr)); //it will ouput result as - Array ( [0] => lorem[1] => ipsum [2] =>dolor )
ASKER
Thanks Banshi. There is one problem. I am trying to count the number of items in the array and it only shows one not 3. Do i need to run the $str through foreach loop to count the items. Thanks
If you want to count the number of item in an array, you can use the Count() method.
$arr = array ( 0 => "lorem",1 => "ipsum", 2 => "dolor");
$total = count($arr);
echo $total; //it will output 3
ASKER
So in my case would that be like the following fro your code example. thanks
$total = count($str);
More on implode here
Here is an extension to Banshi's solution - which is the right one
$total = count($str);
Won't work - $str is now a string - so the result will be $total = 1Here is an extension to Banshi's solution - which is the right one
$data = is_array($__POST['boxdest']) ? $__POST['boxdest'] : array();
// The above guarantees that $data is set AND is an array. If the $_POST['boxdest']
// does not exist it fails over to an empty array
// Now we can simply do this
$str = implode(',', $data);
$total = count($data);
ASKER
Thanks for that Julian. Shall I start a new question for the count because Banshi answered my original question. thanks
What do you still need with the count - I thought that was solved in my last post.
count on the $data array (extracted from $_POST) will give you the number of items in the original array.
$str contains the converted array - you should have everything you need.
count on the $data array (extracted from $_POST) will give you the number of items in the original array.
$str contains the converted array - you should have everything you need.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Some good learning resources for PHP are available here:
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
ASKER
Julian. To award you answer for the count question.
To award you answer for the count question.When you assign points you can choose more than one answer - you chose the one that best answers your question as "Best Solution" and any others become "Assisted Solutions". You can choose in the wizard how to apportion points.
This is the standard practice at EE - we don't open new questions just for the purpose of assigning points.
ASKER
I only joined today after being away from EE for some time. Totally different from when I used it. If you want to bring to attention of moderator, I will be only too willing to awatd points again. Thanks
I only joined today after being away from EE for some time.Welcome back - yes they have made quite a few changes.
I have re-opened the question for you to regrade
ASKER
Thanks for your help.
You are welcome.
ASKER
Julian. I have got it working. One think, initially i have the submit disabled so users cannot submit an empty list. Is there way with your code to detect if a list has items and do something like the code below. However if there are items then make it false. Thanks
$("#submit").prop( "disabled", true);
ASKER
Julian. Sorry. Didn't mean to post that here. Thanks
ASKER