0

I have multiple quires retrieving data from mysql database (multiple tables)

SELECT description FROM table1 As descr WHERE type='MYTYPE'; //this shows description of each product type
SELECT count(id) FROM table2 As tp1 WHERE ACTIVE='Y' AND type=1"); //this is to count records that has this product type =1
SELECT count(id) FROM table2 As tp2 WHERE ACTIVE='Y' AND type=2");
SELECT count(id) FROM table2 As tp3 WHERE ACTIVE='Y' AND type=3");
SELECT count(id) FROM table2 As tp4 WHERE ACTIVE='Y' AND type=4");

What I'm trying to have is a table of each type description, along with count of active products has this type: ignoring the count if the count is 0

Here is modest try, I know it isn't perfect, therefore seeking help here, perhaps different approach will be also helpful, but this doesn't work as it have defining variable which has if statement and also logic is not correct?

$tbl_header = '<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>';

$tbl_data =     

if (tp1>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[0][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp1.'</td></font>
</tr>';}

if (tp2>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[1][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp2.'</td></font>
</tr>';}

;

echo "<table> $tbl_header $tbl_data </table>";

1 Answer 1

1

Try this method. It should work and you don't have to worry about if statements being in the middle of variable

<table>
  <tr>
    <th width=220px align=left>Type description</th>
    <th width=10px align=left>Active products of this type</th>
  </tr>
  
  <?php
    if ($tp1 > 0) {
  ?>
    <tr>
      <td width=2 20 px align=l eft>
        < font color=# 000000>
          <?php echo descr[0][0]; ?>
          </font>
      </td>
      <td width=10px align=left>
        <font color=#FF0000>
          <?php echo $tp1; ?>
        </font>
      </td>
    </tr>
    <?php
      }
    ?>

      <?php
        if ($tp2 > 0) {
      ?>
        <tr>
          <td width=2 20 px align=l eft>
            < font color=# 000000>
              <?php echo descr[1][0]; ?>
              </font>
          </td>
          <td width=10px align=left>
            <font color=#FF0000>
              <?php echo $tp2; ?>
            </font>
          </td>
        </tr>
       <?php
        }
       ?>
</table>

How about this?

<?php
  $table_head = "<tr>
    <th width=220px align=left>Type description</th>
    <th width=10px align=left>Active products of this type</th>
  </tr>";
        if ($tp1 > 0) {
        $table_data_1 = "<tr>
          <td width=2 20 px align=l eft>
            < font color=# 000000>". descr[0][0] . "</font>
          </td>
          <td width=10px align=left>
            <font color=#FF0000>". $tp1. "</font>
          </td>
        </tr>";
       }
       
       if ($tp2 > 0) {
        $table_data_2 = "<tr>
          <td width=2 20 px align=l eft>
            < font color=# 000000>". descr[1][0] . "</font>
          </td>
          <td width=10px align=left>
            <font color=#FF0000>". $tp2. "</font>
          </td>
        </tr>";
       }
       
   echo "<table> $table_ head $table_data_1 $table_data_2 </table>";
   ?>

<?php
$table_data = "";
      $table_head = "<tr>
        <th width=220px align=left>Type description</th>
        <th width=10px align=left>Active products of this type</th>
      </tr>";
            if ($tp1 > 0) {
            $table_data = "<tr>
              <td width=2 20 px align=l eft>
                < font color=# 000000>". descr[0][0] . "</font>
              </td>
              <td width=10px align=left>
                <font color=#FF0000>". $tp1. "</font>
              </td>
            </tr>";
           }
           
           if ($tp2 > 0) {
            $table_data .= "<tr>
              <td width=2 20 px align=l eft>
                < font color=# 000000>". descr[1][0] . "</font>
              </td>
              <td width=10px align=left>
                <font color=#FF0000>". $tp2. "</font>
              </td>
            </tr>";
           }
           
       echo "<table> $table_ head $table_data </table>";
       ?>

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

9 Comments

although, looks valid, but doesn't work with me, as mentioned above, i want to display them as $variables later will be pupulated in a field...
any other ideas here?
Have a look is the second snippet work for you. I'm assuming that the information are present and your SQL data is retrieved and stored correctly.
Thanks Praveen, we are a step forward, now this variable $table_data displays with the last value only, for example above if we have value resulted > 0 in both tp1 and tp2, then only tp2 is shown in the table... any clue?
@MikeDerik That's my fault; you can either use different variable names or append the first one on the second statement; at the moment I'm overriding it. I have corrected it now.
|

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.