-1

I have a table on a MySQL database called quotes. This table contains a column called quote_id, I use a select query to retrieve an array where the quote_id matches the id of the quote being edited.

"SELECT * FROM quotes WHERE quote_id = '$quote_id'";

The remaining columns in the table contain the various products labeled by their product code.

enter image description here

The other table called items contain each product with its product code as model, description and price.

enter image description here

Each time a new quote is created it is inserted into the table with a unique quote ID. The value below each column is the quantity required for the quote.

What I am trying to achieve is using a query to retrieve a specific row from the database, and only display the value greater than 0.

As an example: enter image description here

As you can see I am able to achieve this but the table rows that are not showing affect the spacing between rows.

The if statement I used for each table row is as follows:

    <tr>
        <td>
             <?php if ($kx_t7765x >0 ){ echo "KX-T7765";} ?>
        </td>
        <td>
             <?php if ($kx_t7765x >0 ){ echo "Doorphone";} ?>
        </td>
        <td>
             <?php if ($kx_t7765x >0 ){ echo $kx_t7765x;} ?>
        </td>
    </tr>

I am sure there must be an easier way to go about doing this instead of coding this for each and every table row required, and Im sure using a while statement would be the correct way to go about this.

miken32
  • 42,008
  • 16
  • 111
  • 154
WWessels
  • 27
  • 6
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 16 '19 at 18:34
  • 2
    Why not simply restrict the SQL query? – Dharman Nov 16 '19 at 18:35
  • @Dharman I am looking into restricting the SQL query. Its easy enough to retrieve the row I need. But I am battling to understand how I can retrieve only the specific column with a value > 0. I have looked into selecting each column in the query AS product, Then use HAVING value > 0. I will be honest this is all very new to me, but I don't see this working. I also looked at using AND/OR but logically thinking I don't see this working either.. All my search results are referring to queries returning rows with values > 0. Not columns. Please could you steer me in the right direction. – WWessels Nov 16 '19 at 21:43
  • Which columns must be greater than 0? Just one of them or all? – Dharman Nov 16 '19 at 21:45
  • @Dharman, I am trying to retrieve the value from each column, excluding the quote_id column with a value greater than 0. This is to use in a Quote Table in the quantity column (quote to be provided to a prospective client for example). So showing line items with value equal to 0 is pointless. – WWessels Nov 16 '19 at 21:49
  • WHERE a+b+c >0 maybe – Dharman Nov 16 '19 at 22:04
  • @Dharman I will give this a try and let you know if I come right. – WWessels Nov 16 '19 at 22:06
  • @Dharman, looking at your last suggestion. The first thing I need to do is SELECT all the columns WHERE the quote_id = (a specific row in the table). For example. SELECT * FROM quotes WHERE quote_id = 5. The quote_id of 5 refers to the unique key of the quote that I am busy with. This unique key refers to a few other tables containing all the clients details, the salesman that did the quote, the dealership that the salesman works for etc. So I should always only be able to return one row. Your suggestion returned multiple rows. – WWessels Nov 16 '19 at 22:17
  • @Dharman, the query I tried was: SELECT `t7730`, `t7740`, `ts520`,`tgc210`,`tg1711`,`ts500`,`a405`,`t7765`,`ts580`,`slt_no_phone` FROM `quotes` WHERE `t7730`+`t7740`+`ts520`+`tgc210`+`tg1711`+`ts500`+`a405`+`t7765`+`slt_no_phone`+`ts580` > 0 this returned multiple rows. – WWessels Nov 16 '19 at 22:18
  • `WHERE .... +a405+t7765+slt_no_phone+ts580 > 0 AND quote_id = ?` – Dharman Nov 16 '19 at 22:41
  • @Dharman SELECT `t7730`, `t7740`, `ts520`,`tgc210`,`tg1711`,`ts500`,`a405`,`t7765`,`ts580`,`slt_no_phone` FROM `panasonic_slt` WHERE `t7730`+`t7740`+`ts520`+`tgc210`+`tg1711`+`ts500`+`a405`+`t7765`+`slt_no_phone`+`ts580` > 0 AND `quote_id` = 5. Returns the correct row, includes all the columns and not only the columns with a value > 0 – WWessels Nov 16 '19 at 22:48

1 Answers1

1

First off, this database is a mess. With an hour's work you could make it far more reasonable. There should be a pivot table linking quotes to items, not a separate column for each item. You have to rebuild the entire table every time you decide to sell a new item? That's not normal.

The simple answer to your immediate problem is to make the <tr> element conditional, not the contents of the <td> elements.

<?php if ($kx_t7765x > 0): ?>
<tr>
    <td>KX-T7765</td>
    <td>Doorphone</td>
    <td><?=$kx_t7765x?></td>
</tr>
<?php endif; ?>

Really, all those table cells should be dynamically entered from the items table as well.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • thank you. I feared that the structure of the database was an issue. I will investigate how to correctly structure the database and make use of the pivot table. – WWessels Nov 16 '19 at 22:57
  • If this is a work in progress, you really want to start over with a modern framework like Laravel. It's worth the time to learn. If it's legacy code, you should really look at hiring someone to modernize it (or start reading and do it yourself!) – miken32 Nov 16 '19 at 23:00
  • thank you for the advise. this is literally my own project and I started from scratch. I have no previous experience in development and what I know is self taught, so I will start looking into Laravel right away. – WWessels Nov 16 '19 at 23:05