Home > Mobile >  Explode the values in the database
Explode the values in the database

Time:11-09

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

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"

CodePudding user response:

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>
  • Related