homepage Welcome to WebmasterWorld Guest from 54.146.190.193
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
remove last seperator from array while loop
Chunter



 
Msg#: 4437919 posted 11:31 am on Apr 6, 2012 (gmt 0)

Hi,

I'm having an issue, I'm trying to display mysql data with a php query in some javascript to display a chart.

this is my php:


$query = "SELECT * FROM stats";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo $row['values'] . ", ";
}

The return generates the following:

10000, 20000, 15000, 5000, 20000, 23000, 10000,

That result has to remove the last ", " so the new result is:

10000, 20000, 15000, 5000, 20000, 23000, 10000

Can someone plz help?
Thanks

 

Frank_Rizzo

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4437919 posted 12:57 pm on Apr 6, 2012 (gmt 0)

Two ways to do this.

a) Echo the comma before except on the first instance
b) Build the echo string then strip the last comma

a)

$query = "SELECT * FROM stats";
$result = mysql_query($query) or die(mysql_error());

$row_count = 0;
while($row = mysql_fetch_array($result)){
if($row_count > 0) {
echo ", ";
}
$row_count++;
echo $row['values'];
}

This will not echo the first comma because it is the the first row, then echo the data, then for each subsequent row a comma and data.

b)

$query = "SELECT * FROM stats";
$result = mysql_query($query) or die(mysql_error());
$data = '';

while($row = mysql_fetch_array($result)){
$data .= $row['values'] . ", ";
}
$data = substr(trim($data), 0, -1);
echo $data;

This builds the data string and then before printing it trims (to remove the blank space at the end), and then chops off the last character (the comma)

Chunter



 
Msg#: 4437919 posted 1:51 pm on Apr 6, 2012 (gmt 0)

Thanks for the quick reply, however I think something is going wrong. I took over both methods and both generate this error:

Notice: Undefined index: values in H:\xampp\htdocs\stats\highcharts.php on line 121

What am I doing wrong plz?
thx

Frank_Rizzo

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4437919 posted 3:37 pm on Apr 6, 2012 (gmt 0)

What is in line 121

?

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4437919 posted 4:34 pm on Apr 6, 2012 (gmt 0)

I like to use option b with preg_replace. :-) It's less lines of code (no counter and if's) and executes a mod on the output once.

$data = preg_replace('/,\s+$/','',$data);

Chunter



 
Msg#: 4437919 posted 5:31 pm on Apr 6, 2012 (gmt 0)

Thanks for all the help it works perfect now

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved