Jump to content

[SOLVED] need help query data separte with comma.


salman1

Recommended Posts

dear friends,

 

i wrote a mysql query code with php. when i use the (while) loop for data, there i want to separate data with comma. but i dont want to show the comma at the last. my query resylt like this...

abc, def, ghi, jkl, mno,

 

but my expected result is...

abc, def, ghi, jkl, mno

 

whats the solution? please help me asap.

 

regards

 

salman

your post didnt make much sense... You also didnt post any code.. next time you want help with some particular code I suggest you post It.

 

But im nice so i'll try to help you =)

 

Since you didnt post anything, Im going to assume that in your while loop, you do something like

while(blah blah){
$echo $row['column'].",";
}

Am I right? Well instead of doing that, you can just add each result into an array, like so

$i = 0;
$array = array();
while (blah blah){
array[$i] = $row['column'];
}

and then once your loop is done you can implode the array into a string with the delimiter as a comma. IE

$output = implode(',', $array);
echo $output;

 

I think thats along the lines of what you want. I hope that helps! If not please post your code and explain what you want a little better

my written code as mikesta707 says:

		       <?php
					$query    	= "Select * from tbl_dhads order by id";
				$result   	= mysql_query($query);
				$a=array(0);
	while ($data = mysql_fetch_array($result))

			  { ?> '<?php echo $data["dh_img"]; ?>': { caption: '<?php echo $data["dh_img_name"]; ?>' } <? echo implode(',',$a); ?>
			  
			  <?php		  }		  ?>

but its not working.

 

my current result:

'1240412456bart-01.gif': { caption: 'image 1' }0 '1240412899bart-10.gif': { caption: 'image 2' }0 '1240427843bugs-03.gif': { caption: 'image 3' }0 '1240500312Jaiviru.jpg': { caption: 'jaiviru' }0

 

my expecting result:

'1240412456bart-01.gif': { caption: 'image 1' }, '1240412899bart-10.gif': { caption: 'image 2' }, '1240427843bugs-03.gif': { caption: 'image 3' }, '1240500312Jaiviru.jpg': { caption: 'jaiviru' }

<?php
	$query    	= "Select * from tbl_dhads order by id";
$result   	= mysql_query($query);
//$a=array(0);
        $comma = '';
while ($data = mysql_fetch_array($result)){ 
?> '
<?php echo $comma. $data["dh_img"]; $comma = ', ' ?>': { caption: '<?php echo $data["dh_img_name"]; ?>' }
<?php  } ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.