sharpenedpixels
sharpenedpixels

Reputation: 55

Database select and query issues - PHP

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.

Upvotes: 0

Views: 73

Answers (2)

Dray
Dray

Reputation: 887

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.

Upvotes: 2

ryantxr
ryantxr

Reputation: 4217

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

Upvotes: 0

Related Questions