Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert array into comma separated string 1

Status
Not open for further replies.

iainm2

IS-IT--Management
Jun 13, 2002
38
GB
Hi
- Apology, I posted this into someone else's question, sorry for any confusion caused. -

I have a problem getting something probably very simple to work but cannot see how to achieve it. The following works perfectly:
<?php

$db = mysql_connect("localhost", "username", "password");

mysql_select_db("helpdesk_calls",$db);

$query = ("select calls from yr2004");

$result = mysql_query($query);

while ($array = mysql_fetch_array($result, MYSQL_NUM)){

foreach ($array as $value) {

echo "$value, ";

?>

However what I want to do is put the array into a comma separated string instead of printing it to screen. I have tried using implode unsuccessfuly. The string is going into a script I have downloaded which places a graph into the browser.
The graph script works when the values are manually inserted and what I need to do is tie the array to the graph and I am done.

Code I used for implode is:
$csv = implode(", ", $array);


Many thanks
 
then simply use this
Code:
$string_for_record = "";
foreach ($array as $value) {
$string_for_record .= (strlen($string_for_record) > 0 ? ", " : "") . $value;
}
 
What you want to look for is where the formatting of the resulting string fails. What is it that the graphing program expects? What does the string look like which is created by implode().
By comparing the specification with the result from implode() you can find the discrepancy. Then we can go and look for a solution.
 
Does this code do what you want?
Code:
<?php
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("helpdesk_calls",$db);
$query = ("select calls from yr2004");
$result = mysql_query($query);
$my_array=array();
while ($row = mysql_fetch_assoc($result)){
     $my_array[]=$row['calls'];
}
?>
$comma_string=impolde(",",$my_array);
 
Dear Dustbuster
Sorry for the delay in responding - this code does what it says on the tin!
Very many thanks


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top