0

a:1:{s:9:"toys_item";a:1:{s:16:"toys_item_ranger";a:11:{i:0;s:31:"2 x Seadoo GTX 230/IBR Jet-Skis";i:1;s:30:"1 x HD Runway 10′ Waterslide";i:2;s:14:"4 x F5 Seabobs";i:3;s:49:"3 x Water-Skis for children, teenagers and adults";i:4;s:43:"1 x 2 Person Nereus Kayak w/paddles & seats";i:5;s:14:"1 x Wake Board";i:6;s:35:"2 x Jobe Parana bamboo Paddleboards";i:7;s:44:"Seadoo Spark 900HO AXE 2UP/IBR Trixx Jet-Ski";i:8;s:31:"2 x Seadoo GTX 230/IBR Jet-Skis";i:9;s:14:"4 x F5 Seabobs";i:10;s:43:"1 x 2 Person Nereus Kayak w/paddles & seats";}}}

I want to show specific text from the above results, something like this:

2 x Seadoo GTX 230/IBR Jet-Skis
1 x HD Runway 10′ Waterslide
4 x F5 Seabobs
3 x Water-Skis for children, teenagers, and adults 1 x 2 Person Nereus Kayak w/paddles & seats
1 x Wake Board
2 x Jobe Parana bamboo Paddleboards
Seadoo Spark 900HO AXE 2UP/IBR Trixx Jet-Ski
2 x Seadoo GTX 230/IBR Jet-Skis
4 x F5 Seabobs
1 x 2 Person Nereus Kayak w/paddles & seats

here is my code

<?php
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM wp_postmeta where meta_key = 'fat-mb-boat-toys' AND post_id = $post_id"); // Query to fetch data from database table and storing in $results
if(!empty($results))
    // Checking if $results have some values or not
{    
        echo "<table>";
        echo "<tr>";
        echo "<th> Post ID <th>";
        echo "<th> Meta Key <th>";
        echo "<th> Meta Value <th>";
        echo "</tr>";

    foreach($results as $row){
       echo "<tr>";
       echo "<td>". $row->post_id ."<td>";
       echo "<td>". $row->meta_key ."<td>";
       echo "<td>". $row->meta_value ."<td>";
       echo "</tr>";
    }
        echo "<table>";


}
?> ```
2
  • You would need to first unserialize that data, then you can check the value using print_r to see what you type you are working with and what is available. Commented Feb 18, 2022 at 9:08
  • can you please provide some reference? Commented Feb 18, 2022 at 9:30

1 Answer 1

0

You should pretty much never need to use $wpdb to grab data. There are helper functions built into WordPress that will do what you need.

In this case, use get_post_meta().

Having used https://www.unserialize.com/ to unserialize your data, it looks like the item list in your question is in $meta['toys_item']['toys_item_ranger']. I'll use that in my code snippet to make a list like the one you're looking for.

$meta = get_post_meta( $post_id, 'fat-mb-boat-toys', true );
if (
    ! empty( $meta )
    && ! empty( $meta['toys_item'] )
    && ! empty( $meta['toys_item']['toys_item_ranger'] ) 
) {
    foreach ( $meta['toys_item']['toys_item_ranger'] as $toy ) {
        echo $toy . '<br />';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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