Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Explode the values in the database

I want to implode my id values that I keep from database.

My data in database
wp_badges

id    badges        value_id
----  ------        --------
1     Outloot       2719,3314 

My id values that I saved as value is actually become my product id

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Following my code :PHP

 $result = $wpdb->get_results( "SELECT DISTINCT values_id FROM wp_badges");
    foreach($result as $value){

     $value->values_id;
    }
    $arr = array($value->values_id);
    echo '"'.implode('","',$arr).'"';

Here is the output:"2719,3314"

But the output I wanted was to get each element with double quotes

The output I want:"2719","3314"

>Solution :

You are doing things in the wrong place, the explode and echo has to be inside the loop or you will only ever do anything with the last occurance in the $result array.

$result = $wpdb->get_results( "SELECT DISTINCT values_id FROM wp_badges");
foreach($result as $value){
    $bits = explode(',',$value->values_id);
    echo '"' . implode('","', $bits) . '"<br>';
}

RESULT

"123","456"<br>
"923","956"<br>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading