0

I'm very new to PHP and database queries.

I have a WordPress site with the Genesis framework installed and I'm creating a child theme from it and adding some template pages to insert data from my database. I'm creating a category page and a product page.

I'm having issues on some of the queried data on the category page, if there is anyone out there that can point me in the right direction, I'd be extremely grateful.

I have all the database rows outputting individually in a table format, which is exactly how I want it, but I can't get a heading to output above the displayed products, I've tried lots of solutions but none so far have done the job. Here is my code:

function child_product_data_loop() {

global $wpdb;
$category = $wpdb->get_results("SELECT * FROM product_data WHERE prod_cat_1='Retractable Plastic Pens';");

echo "<main class='content'>

    <article class='page type-page status-publish entry' itemscope='' itemtype='http://schema.org/CreativeWork'>

        <header class='entry-header'>

            <h1 class='entry-title' itemprop='headline'>".$category->prod_cat_1."</h1>

        </header>

        <div class='entry-content' itemprop='text'>

            <p>For more information on any product within our ".$category->prod_cat_1." range please click on &quot;details and prices&quot;.</p>";

            foreach($category as $product){

            echo "<table width='100%' border='0' cellpadding='2' cellspacing='0'>

                <tbody> 

                    <tr valign='top'>

                        <td><h3><a href='' style='color: rgb(51, 102, 153); text-decoration: none;'>".$product->prod_name."</a></h3></td>

                        <td style='color: #ff0000; text-align: right;'>From: &pound;".$product->prod_price_5."</td>

                    </tr>

                    <tr valign='top'>

                        <td colspan='2'><a href='' style='color: #CC6600; text-decoration: none;'>Click here for details and prices</a></td>

                    </tr>

                    <tr align='center' valign='middle'>

                        <td height='75' colspan='2'><a href='' title='".$product->prod_name."'><img width='100%' height='auto' src='http://thepensite.co.uk/wp-content/uploads/media-clic.jpg' class='prod-image' alt='".$product->prod_name."' srcset='http://thepensite.co.uk/wp-content/uploads/media-clic-300x33.jpg 300w, http://thepensite.co.uk/wp-content/uploads/media-clic.jpg 420w' sizes='(max-width: 420px) 100vw, 420px' /></a></td>

                    </tr>

                </tbody>

            </table>";

            }

        echo "</div>

    </article>

</main>";

}

My issue is within these lines of code:

echo "<main class='content'>

    <article class='page type-page status-publish entry' itemscope='' itemtype='http://schema.org/CreativeWork'>

        <header class='entry-header'>

            <h1 class='entry-title' itemprop='headline'>".$category->prod_cat_1."</h1>

        </header>

        <div class='entry-content' itemprop='text'>

            <p>For more information on any product within our ".$category->prod_cat_1." range please click on &quot;details and prices&quot;.</p>";

For some reason the category name is not been outputted, as I said before I have tried quite a few solutions I've found but they haven't worked, so I'm obviously doing some thing totally wrong.

Can any of you out there help?

Many thanks in advance.

2 Answers 2

2

Use $category[0]->prod_cat_1 you are not accessing the array value correctly. [0] is key here

EDIT:

Do print("<pre>".print_r($category,true)."</pre>"); after your query and look how are you getting the results.

Sign up to request clarification or add additional context in comments.

3 Comments

You are an absolute star, thank you very much, that works perfectly. Could you just explain what [0] is doing so I have an idea for future reference?
@SharpenedPixels Do print("<pre>".print_r($category,true)."</pre>"); after your query and look how are you getting the results. Try to understand how multidimensional arrays work. If you have any problems. I'll help you. :) if you find my answer helpful you can accept it.
Ok, I kind of see, that is saying take the info from stdClass Object [0], so wouldn't output anything if no records existed. It'll work for now as all categories will have at least one record. Thanks for the help.
0

In all likelihood, you are not accessing what you got back from the query properly. I have to guess a bit because you have not posted enough information to be certain.

Suggestion #1: Add your table definition to the question so I can see what columns you have.

Suggestion #2: dump the data

            foreach($category as $product){
                var_dump($category); // <--- add this

Edit your question and post the output.

I am assuming that you are looking for html structure that is a table with all your rows inside the table. The way you have made your loop, you will end up with a table for each product.

Is this what you want?

table
    product
    product
    product

This is what your code will produce:

table
    product
table
    product
table
    product

1 Comment

Thanks for the response, it wasn't an issue with the table format. That worked as I wanted. Showing each product within it own table so I can style each individual table. It was just the name of the category I wanted posted as a title and that wasn't working. @Dray answered with a solution that worked, but thank you for your time.

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.