0

I did a custom JOIN query to my db and returned an array of all the info I need but I can't display it. If i var_dump($query); I can see all the info but when I do this I get nothing?

orders = $wpdb->get_results($query);
<?php foreach ($orders as $order) { ?>
    <tr>
        <td><?php echo $date; ?></td>
        <td><?php echo $order['order_first_name']; ?></td>
        <td><?php echo $order['order_last_name']; ?></td>
        <td><?php echo $order['contact_email']; ?></td>
        <td><?php echo $order['contact_phone']; ?></td>
        <td><?php echo $order['order_address']; ?></td>
        <td><?php echo $order['order_state']; ?></td>
        <td><?php echo $order['order_zip']; ?></td>
    </tr>
<?php } ?>

If I dump one order like <?php foreach ($orders as $order) { var_dump($order)}?> I get this

object(stdClass)#2300 (11) { ["order_contact_id"]=> string(5) "67378" ["order_date"]=> string(17) "20160129T15:23:20" ["order_contact"]=> string(13) "XXXXXX" ["order_first_name"]=> string(6) "XXXXXX" ["order_last_name"]=> string(6) "XXXXXX" ["contact_email"]=> string(23) "XXXXXX" ["contact_phone"]=> string(17) "XXXXXX" ["order_address"]=> string(24) "XXXXXX" ["order_city"]=> string(8) "XXXXXX" ["order_state"]=> string(2) "CO" ["order_zip"]=> string(5) "80241" }
1
  • Any PHP object can be converted to an array by 'cast'ing it. i.e. <?php foreach ( (array) $orders as $order) ... Commented Mar 6, 2019 at 11:36

1 Answer 1

1

You are trying to access a object like an array. Try this:

<?php
$orders = $wpdb->get_results($query);
foreach ($orders as $order) { ?>
    <tr>
        <td><?php echo $date; ?></td>
        <td><?php echo $order->order_first_name; ?></td>
        <td><?php echo $order->order_last_name; ?></td>
        <td><?php echo $order->contact_email; ?></td>
        <td><?php echo $order->contact_phone; ?></td>
        <td><?php echo $order->order_address; ?></td>
        <td><?php echo $order->order_state; ?></td>
        <td><?php echo $order->order_zip; ?></td>
    </tr>
<?php } ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Good call. I didnt see that it was dumping an object. Thanks!
Lol thanks, sometimes it is the simplest things that trip us up. Even the most experienced devs fall victim to that.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.