salman1 Posted April 27, 2009 Share Posted April 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155784-solved-need-help-query-data-separte-with-comma/ Share on other sites More sharing options...
mikesta707 Posted April 27, 2009 Share Posted April 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155784-solved-need-help-query-data-separte-with-comma/#findComment-820073 Share on other sites More sharing options...
sasa Posted April 27, 2009 Share Posted April 27, 2009 or $comma = ''; while(blah blah){ $echo $comma.$row['column']; $comma = ', '; } Quote Link to comment https://forums.phpfreaks.com/topic/155784-solved-need-help-query-data-separte-with-comma/#findComment-820110 Share on other sites More sharing options...
salman1 Posted April 27, 2009 Author Share Posted April 27, 2009 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' } Quote Link to comment https://forums.phpfreaks.com/topic/155784-solved-need-help-query-data-separte-with-comma/#findComment-820168 Share on other sites More sharing options...
salman1 Posted April 27, 2009 Author Share Posted April 27, 2009 please any one help me................... Quote Link to comment https://forums.phpfreaks.com/topic/155784-solved-need-help-query-data-separte-with-comma/#findComment-820256 Share on other sites More sharing options...
sasa Posted April 27, 2009 Share Posted April 27, 2009 <?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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155784-solved-need-help-query-data-separte-with-comma/#findComment-820437 Share on other sites More sharing options...
salman1 Posted April 28, 2009 Author Share Posted April 28, 2009 guru thank u....... i've solved my prob now. thanks a lot... guru... Quote Link to comment https://forums.phpfreaks.com/topic/155784-solved-need-help-query-data-separte-with-comma/#findComment-821126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.