4

I have several tables in a database. In table products_info one of the column is named short_description (Short description of a product) and has some values

I need the value present in the short_description

In table order-items I need to add the short_description column with the data from short_description present in Table products_info.

enter image description here

Any suggestions? Thank you.

4
  • Instead of the horrendous duplication of data (which some would call de-normalization), why not join the two tables using product_id to get all the information that you need? Commented Oct 12, 2018 at 4:02
  • So I will instruct the php page to get the short description of the related product id. It seems appropriate. I try like this echo $item['product_id', 'short_description']; but did not work Commented Oct 13, 2018 at 5:53
  • Can you add the PHP and SQL that was used to the question? Commented Oct 13, 2018 at 16:43
  • Yes: this is the php that will "print" (echo) the information I need: <?php foreach ($order['items'] as $item) { ?> <tr> <td><?php echo (float)$item['quantity']; ?></td> <td><?php echo $item['sku']; ?></td> <td style="white-space: normal;"><?php echo $item['name']; ?> <?php if (!empty($item['options'])) { foreach ($item['options'] as $key => $value) { echo '<br />- '.$key .': '. $value; } } ?> and the information are taken from the table order_item in the above picture. Commented Oct 14, 2018 at 9:34

3 Answers 3

3
+25

Instead of the unnecessary copy of the column, you can combine products_info table and products_info table with JOIN statement. For example:

SELECT order_items.*,
       products_info.short_description
FROM order_items
LEFT JOIN products_info ON order_items.product_id = products_info.product_id

You shouldn't create any table in a database, you'll get data from this query in your PHP script.

You can use a result of the query in the following way:

<?php 
$sql = "SELECT lcqu_orders_items.*, lcqu_products_info.short_description FROM lcqu_orders_items LEFT JOIN lcqu_products_info ON lcqu_orders_items.product_id = lcqu_products_info.product_id" 
// run query 
?>

<?php foreach ($order['items'] as $item) { ?> 
    <tr> 
        <td><?php echo (float)$item['quantity']; ?></td> 
        <td><?php echo $item['sku']; ?></td> 
        <td><?php echo $item['sku']; ?></td> 
        <--! showing short description -->
        <td><?php echo $item['lcqu_products_info.short_description']; ?></td> 
        <td style="white-space: normal;"><?php echo $item['name']; ?> 
        <?php if (!empty($item['options'])) { 
           foreach ($item['options'] as $key => $value) {
               echo '<br />- '.$key .': '. $value;
          } 
        }?>
     <!-- another code -->
}?>
Sign up to request clarification or add additional context in comments.

10 Comments

I run it but did not create any new table. Should I write it in the php?
@MassimilianoRubino, You shouldn't create any table in a database, you should get data from this query in your PHP script
OK. So when I run it for a test in phpmyadmin (Run SQL query) in the results I get the column I desire. But I could not get a way of print it: <?php $sql = "SELECT lcqu_orders_items.*, lcqu_products_info.short_description FROM lcqu_orders_items LEFT JOIN lcqu_products_info ON lcqu_orders_items.product_id = lcqu_products_info.product_id" ?> <?php echo $sql['lcqu_products_info.short_description']; ?><br><?php echo $item['sku']; ?> where the single final part <?php echo $item['sku']; ?> it does print correctly the sku. How to get printed the short_description value?
@MassimilianoRubino, You must use $item['lcqu_products_info.short_description'] instead of $sql['lcqu_products_info.short_description']
Please note the line between 129 and 131: txt file
|
1

You have two tables in the question, with different details on the same products. One thing you never want to do is duplicate data across tables. It is wasteful of resources and risks the integrity of the data. Ex. if the name of a product changes, you will have to modify it in many tables. If you miss one, the application will have issues since it will not always receive the same name. You can read about data normalization.

To perform queries on two tables, you use a JOIN operation. One value that is unique to each product, and appears in both tables is product_id.

Ex, get the products name from lcqu_products_info

SELECT name
FROM lcqu_products_info

Ex, get the price from lcqu_orders_items

SELECT price
FROM lcqu_orders_items

Now if you need the name AND the price in a single query, join the tables with the product_id:

SELECT pi.name, oi.price
FROM lcqu_products_info AS pi
LEFT JOIN lcqu_orders_items AS oi ON pi.product_id = oi.product_id

Based on that, you should remove the "name" column from the lcqu_orders_items. The name should be extracted from the lcqu_products_info, using the same methos showed above.


Now about your PHP code:

<?php 
    foreach ($order['items'] as $item) 
    { 
?> 
    <tr>
        <td>
            <?php echo (float)$item['quantity']; ?>
        </td> 
        <td>
            <?php echo $item['sku']; ?>
        </td> 
        <td style="white-space: normal;">
            <?php echo $item['name']; ?> 
            <?php 
                if (!empty($item['options'])) 
                { 
                    foreach ($item['options'] as $key => $value) 
                    { 
                        echo '<br />- '.$key .': '. $value; 
                    } 
                } 
            ?>

In this code you require these values from lcqu_orders_items

  • quantity: in the table
  • sku: in the table
  • name: in the table. Should not be. It should be taken from lcqu_products_info
  • options: in the table.

Therefore the query should be:

SELECT oi.quantity, oi.sku, pi.name, oi.options
FORM lcqu_orders_items AS oi
LEFT JOIN lcqu_products_info AS pi ON oi.product_id = pi.product_id

the results of this query will end up into the $orders array, on which you can loop and print the data.


I understand this is not a direct answer to your question, but such an answer would send you down a path that is not best practice, and cause you problems later.

Comments

-1

First Create a column named short_description in the table you want enter image description here

After that write a small php code for getting the "product_id" and the "short_description" from the "icqu_products_info table" and use the "product_id" to update the values in the "icu_orders_items" table

1 Comment

like this one: INSERT INTO lcqu_orders_items (short_description) VALUES (SELECT short_description FROM lcqu_products_info WHERE product_id = product_id) ?

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.