PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] Explode the values in the database

 January 17, 2022     implode, php, wordpress     No comments   

Issue

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"


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>


Answered By - RiggsFolly
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing