Jump to content

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  } ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.