2

I have created dynamic add/remove fields in frontend post submission page. I use wp_json_encode and $wpdb->insert to insert data into my custom WordPress database table. the system is working perfectly.

The data is stored in “reward_details” column of my custom table.. It looks like :

{"reward_amount":["500","250","20000","3000"],"reward_title":["Horse","Cat","Tiger","Monkey"],"reward_description":["Horse Home","Cat Home","Tiger Home","Monkey Home"]}

But however I am not able to retrieve and display the data using json_decode. Following is my code:

<?php $project_id = $_SESSION['project_id']; global $wpdb; $string = $wpdb->get_results( "SELECT reward_details FROM wpxa_rewards WHERE project_id = $project_id" );

  $someArray = json_decode($string, true);

    $count = count( $someArray );
    for ( $i = 0; $i < $count; $i++ ) { ?>

<div class="panel panel-default">
  <div class="panel-body">
     <?php echo $someArray[$i]["reward_title"]; ?>
  </div>
</div>

 <?php } ?>

I want to retrieve and display data dynamically… Plz help…

  • Can you update question with your result from database query? – Malay Solanki Feb 1 '18 at 6:39
  • @MalaySolanki var_dump is NULL – Minesh Feb 1 '18 at 6:46
  • please var_dump($wpdb); you will get error code / error in your database query – Malay Solanki Feb 1 '18 at 6:51
  • 1
    @MalaySolanki... Thanks... Actually var_dump($sameArray); is NULL means json_decode is not working... – Minesh Feb 1 '18 at 6:58
2

$wpdb->get_results() returns an Array of rows representing the results. The rows themselves can be represented as objects or arrays depending on the second argument.

See the documentation:

output_type
One of four pre-defined constants. Defaults to OBJECT. See SELECT a Row and its examples for more information.

  • OBJECT - result will be output as a numerically indexed array of row objects.
  • OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
  • ARRAY_A - result will be output as a numerically indexed array of associative arrays, using column names as keys.
  • ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays.

So when you run your query, you're going to get an array, with one element in it that is an object with a property reward_details that contains the JSON.

So without changing your query, you'd access the JSON with:

$someArray = json_decode( $string[0]->reward_details, true);

The key bit being $string[0]->reward_details. That's where the value is.

wpdb has a better method for getting a single cell like this though, called get_var():

$query = $wpdb->prepare( 
    "SELECT
        reward_details
    FROM 
        wpxa_rewards 
    WHERE 
        project_id = %d
    ", 
    $project_id
);

$string = $wpdb->get_var( $query );

$someArray = json_decode( $string );

get_var() basically does the [0]->reward_details part for you.

Also note that I have used $wpdb->prepare for inserting the project ID into the query. You shouldn't put user data directly into a query like you did in the original query.

  • I have updated the code. The system is working but there is one problem. Only 3 values are displaying.<?php $project_id = $_SESSION['project_id']; $query = $wpdb->prepare( "SELECT reward_details FROM wpxa_rewards WHERE project_id = %d", $project_id ); $string = $wpdb->get_var( $query ); $someArray = json_decode( $string, true ); $count = count( $someArray ); for ( $i = 0; $i < $count; $i++ ) { ?> <div class="panel panel-default"> <div class="panel-body"> <?php echo $someArray['reward_title'][$i]; ?> </div> </div> <?php } ?> – Minesh Feb 1 '18 at 7:56
  • This is correct. Based on your example, there's only 3 elements in the top-level of the array: reward_amount, reward_title, and reward_description. Then each has an array of values. If there's supposed to be 4 pieces of data with an amount, title and description each, then you're storing the data wrong. – Jacob Peattie Feb 1 '18 at 8:43
  • Plz tell me what changes to be made.. – Minesh Feb 1 '18 at 8:47
  • I don't know enough about your project and am not about to teach programming in the comments. Start buy dumping the decoded JSON and you'll see the structure of the data, and the problem. You're grouping all values for a particular key, rather than grouping the actual rewards data together as a single item. – Jacob Peattie Feb 1 '18 at 8:49
  • I'm guessing that you're storing values from a form with fields like description[]. The problem is that forms don't understand the concept of 'rows', so it returns arrays of values for each field name, but that's not what you want. You want rows with one value for each field name. You'll need to reorganise the submitted values before saving to the database. See this question and the accepted answer for a demonstration. – Jacob Peattie Feb 1 '18 at 9:02
2

Hi thanks for ur valuable support... I have found the solution. See below:

<?php $project_id = $_SESSION['project_id']; $query = $wpdb->prepare( "SELECT reward_details FROM wpxa_orocox_rewards WHERE project_id = %d", $project_id );

$string = $wpdb->get_var( $query );

$someArray = json_decode( $string, true );

    $count = count( $someArray['reward_title'] );
    for ( $i = 0; $i < $count; $i++ ) { ?>

<div class="panel panel-default">
  <div class="panel-body">
     <?php echo $someArray["reward_amount"][$i]; ?>
     <?php echo $someArray["reward_title"][$i]; ?>
     <?php echo $someArray["reward_description"][$i]; ?>
  </div>
</div>

<?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.