Confused in using if condition

Good day!

I have a problem in if and else condition to know if the cloth type is NW (AAA or BBB or CCC) and W the rest cloth type that did not listed in NW. My code did not read the code for NW, only W was read so that even the cloth type is NW the formula was used is the formula of W. I don’t know what is wrong in my code so that if the cloth type is NW the formula is $CLT_yield = @($output / ($input * 8.8)) * 100; and if W the formula is $CLT_yield = @($output / ($input * 9)) * 100;

In my code only the code $CLT_yield = @($output / ($input * 9)) * 100; was run even the cloth type is CCC.

Here is my code:


<?php
  <?php
  <?php
include 'config.php';
if($_POST["clt_date"])
        {
        $query = "SELECT clt_no FROM clt_transact WHERE clt_date = '" . $_POST["clt_date"] . "'";
        $result_loop = mysql_query($query);
        
        $date = $_POST["clt_date"];
                
        if($result_loop) if(mysql_num_rows($result_loop) > 0)
            {
            $totalloop = mysql_num_rows($result_loop);
            
            $clt_no = mysql_result($result_loop,0,"clt_no");
            $query = "SELECT d.operation_name, SUM(d.input_qty) AS inputqty, d.input_unit, SUM(d.output_qty) AS outputqty, d.output_unit FROM clt_traceability d, clt_transact t WHERE t.clt_no = '$clt_no' AND d.clt_transact_id = t.clt_transact_id AND d.operation_name IN ('Total1', 'Total2', 'Operation3', 'Operation4', 'Operation5')GROUP BY d.operation_name, d.output_unit, d.input_unit ORDER BY d.operation_name";
            $result = mysql_query($query);
            if($result)
                {
                echo "<table cellspacing='2' style='font-family: arial narrow; font-size: 12px; border-width: 2px 2px 2px 2px; border-style: solid;'>";
                
                echo "<tr>";
                echo "<tr><b> Date: &nbsp;  " . $date . "</b></tr>";
                echo "<th class='tdclass'>CLT #</th>";
                $total_row = mysql_num_rows($result);
                for($ctr=0; $ctr < $total_row; $ctr++)
                    {
                    $opname = mysql_result($result,$ctr,"operation_name");
                    $i = strpos($opname," ",0);
                    $opname = substr($opname,$i);
                    echo "<th colspan='2' class='tdclass'>" . $opname . "<br />(" . mysql_result($result,$ctr,"output_unit") . ")</th>";
                    }
                echo "<th class='tdclass'>CLT Yield</th>";
                echo "</tr>";
                
                echo "<tr>";
                echo "<td class='tdclass'></td>";
                
                for($ctr=0; $ctr < $total_row; $ctr++)
                    {
                    
                    echo "<td class='tdclass'>Input</td>";
                    echo "<td class='tdclass'>Output</td>";
                    
                    }
                
                echo "<td class='tdclass'>";
                echo "</td>";
                    
                echo "</tr>";
                        
                }
            }
        
        $query = "SELECT clt_no FROM clt_transact WHERE clt_date = '" . $_POST["clt_date"] . "' ORDER BY clt_no ASC";
        $result_loop = mysql_query($query);               
        while($row = mysql_fetch_array($result_loop))
            {
            $loopctr += 1;
            $clt_no = $row["clt_no"];
            $query = "SELECT * FROM clt_transact WHERE clt_no = '$clt_no'";
            $result_no = mysql_query($query);
            if($result_no)
                {
                if(mysql_num_rows($result_no) > 0)
                    {
                    
                    $input = 0;
                    $output = 0;
                    
                    ///Non-W////
                    
                    $query  = "SELECT SUM(t.input_qty) AS input FROM clt_traceability t, clt_transact p WHERE t.clt_transact_id = p.clt_transact_id AND t.operation_name='Total1' AND p.cloth_type IN ('AAA', 'BBB', 'CCC') AND p.clt_no = '$clt_no'";
                    $resultyield = mysql_query($query);
                    if($resultyield)
                        {
                        if(mysql_num_rows($resultyield) > 0) $input = mysql_result($resultyield,0,"input");
                        }
                    
                    $query  = "SELECT SUM(t.output_qty) AS output FROM clt_traceability t, clt_transact p WHERE t.clt_transact_id = p.clt_transact_id AND t.operation_name='Total2' AND p.cloth_type IN ('AAA', 'BBB', 'CCC') AND p.clt_no = '$clt_no'";
                    $resultyield = mysql_query($query);
                    if($resultyield)
                        {
                        if(mysql_num_rows($resultyield) > 0) $output = mysql_result($resultyield,0,"output");
                        }
                        
                    $CLT_yield = @($output / ($input * 8.8)) * 100;
                    
                    ///W////
                    
                    $query  = "SELECT SUM(t.input_qty) AS input FROM clt_traceability t, clt_transact p WHERE t.clt_transact_id = p.clt_transact_id AND t.operation_name='Total1' AND p.clt_no = '$clt_no'";
                    $resultyield = mysql_query($query);
                    if($resultyield)
                        {
                        if(mysql_num_rows($resultyield) > 0) $input = mysql_result($resultyield,0,"input");
                        }
                    
                    $query  = "SELECT SUM(t.output_qty) AS output FROM clt_traceability t, clt_transact p WHERE t.clt_transact_id = p.clt_transact_id AND t.operation_name='Total2' AND p.clt_no = '$clt_no'";
                    $resultyield = mysql_query($query);
                    if($resultyield)
                        {
                        if(mysql_num_rows($resultyield) > 0) $output = mysql_result($resultyield,0,"output");
                        }
                        
                    $CLT_yield = @($output / ($input * 9)) * 100;
                     
                   $query = "SELECT d.operation_name, SUM(d.input_qty) AS inputqty, d.input_unit, SUM(d.output_qty) AS outputqty, d.output_unit FROM clt_traceability d, clt_transact t WHERE t.clt_no = '$clt_no' AND d.clt_transact_id = t.clt_transact_id AND d.operation_name IN ('Total1', 'Total2', 'Operation3', 'Operation4', 'Operation5') GROUP BY d.operation_name, d.output_unit, d.input_unit ORDER BY d.operation_name";
                    $result = mysql_query($query);
                    if($result)
                        {
                       
                        
                        echo "<tr>";
                        echo "<td><strong>$clt_no</strong></td>";
                        
                        for($ctr=0; $ctr < $total_row; $ctr++)
                          {
                            echo "<td class='tdclass'>";
                            echo number_format((mysql_result($result,$ctr,"inputqty")),2);  
                            
                            echo "</td>";
                            echo "<td class='tdclass'>";
                            echo number_format((mysql_result($result,$ctr,"outputqty")),2); 
                            
                            echo "</td>";
                          } 
                            
                        echo "<td class='tdclass'><strong>";
                        printf ("%01.2f", $CLT_yield);
                         echo "%</strong></td>";
                     
                        echo "</tr>";
                        
                    
                        }
                    }
                }
            }
            echo "</table>";              
            }
?>

I hope somebody can help me.

Any help is highly appreciated.

Thank you

You have syntax errors in your code.

You also have logic errors in your code.

Have a look at what [fphp]mysql_query[/fphp] returns.

I’m sorry I’m not good in mysql query

$query = “SELECT clt_no FROM clt_transact WHERE clt_date =” . $_POST[‘clt_date’] ;

is enough

I tried to put the formula inside the if condition but still nothing change in the output.

Then did you write all that posted code? It seems like you got it from somewhere else and now just want someone to fix it for you. If you genuinely want to learn, then perhaps read through the link I posted and it should help you fix at least 1 logic error in your code.

<?php
include ‘config.php’;
if($_POST[‘clt_date’])
{
$query = “SELECT clt_no FROM clt_transact WHERE clt_date =” . $_POST[‘clt_date’];
$result_loop = mysql_query($query);

    $date = $_POST["clt_date"]; 
             
    if($result_loop) if(mysql_num_rows($result_loop) &gt; 0) 
        { 
        $totalloop = mysql_num_rows($result_loop); 
         
        $clt_no = mysql_result($result_loop,0,"clt_no"); 
        [COLOR="red"]$query = "SELECT d.operation_name, SUM(d.input_qty) AS inputqty, d.input_unit, SUM(d.output_qty) AS outputqty, d.output_unit FROM clt_traceability d, clt_transact t WHERE t.clt_no =".$clt_no." AND d.clt_transact_id = t.clt_transact_id AND d.operation_name IN ('Total1', 'Total2', 'Operation3', 'Operation4', 'Operation5')GROUP BY d.operation_name, d.output_unit, d.input_unit ORDER BY d.operation_name";[/COLOR] 
        $result = mysql_query($query); 
        if($result) 
            { 
            echo "&lt;table cellspacing='2' style='font-family: arial narrow; font-size: 12px; border-width: 2px 2px 2px 2px; border-style: solid;'&gt;"; 
             
            echo "&lt;tr&gt;"; 
            echo "&lt;tr&gt;&lt;b&gt; Date: &nbsp;  " . $date . "&lt;/b&gt;&lt;/tr&gt;"; 
            echo "&lt;th class='tdclass'&gt;CLT #&lt;/th&gt;"; 
            $total_row = mysql_num_rows($result); 
            for($ctr=0; $ctr &lt; $total_row; $ctr++) 
                { 
                $opname = mysql_result($result,$ctr,"operation_name"); 
                $i = strpos($opname," ",0); 
                $opname = substr($opname,$i); 
                echo "&lt;th colspan='2' class='tdclass'&gt;" . $opname . "&lt;br /&gt;(" . mysql_result($result,$ctr,"output_unit") . ")&lt;/th&gt;"; 
                } 
            echo "&lt;th class='tdclass'&gt;CLT Yield&lt;/th&gt;"; 
            echo "&lt;/tr&gt;"; 
             
            echo "&lt;tr&gt;"; 
            echo "&lt;td class='tdclass'&gt;&lt;/td&gt;"; 
             
            for($ctr=0; $ctr &lt; $total_row; $ctr++) 
                { 
                 
                echo "&lt;td class='tdclass'&gt;Input&lt;/td&gt;"; 
                echo "&lt;td class='tdclass'&gt;Output&lt;/td&gt;"; 
                 
                } 
             
            echo "&lt;td class='tdclass'&gt;"; 
            echo "&lt;/td&gt;"; 
                 
            echo "&lt;/tr&gt;"; 
                     
            } 
        } 
     
    [COLOR="red"]$query = "SELECT clt_no FROM clt_transact WHERE clt_date =" . $_POST['clt_date'] . " ORDER BY clt_no ASC"; [/COLOR]
    $result_loop = mysql_query($query);                
    while($row = mysql_fetch_array($result_loop)) 
        { 
        $loopctr += 1; 
        $clt_no = $row["clt_no"]; 
        $query = "SELECT * FROM clt_transact WHERE clt_no =".$clt_no; 

I have changed some lines here and i guess u can do more on this by looking at it i guess

I try new code:


<?php
  
                    $input = 0;
                    $output = 0;
                    
                    $query  = "SELECT SUM(t.input_qty) AS input FROM clt_traceability t, clt_transact p WHERE t.clt_transact_id = p.clt_transact_id AND t.operation_name='Total1' AND p.clt_no = '$clt_no'";
                    $resultyield = mysql_query($query);
                    if($resultyield)
                        {
                        if(mysql_num_rows($resultyield) > 0) $input = mysql_result($resultyield,0,"input");
                        }
                    
                    $query  = "SELECT SUM(t.output_qty) AS output FROM clt_traceability t, clt_transact p WHERE t.clt_transact_id = p.clt_transact_id AND t.operation_name='Total2' AND  p.clt_no = '$clt_no'";
                    $resultyield = mysql_query($query);
                    if($resultyield)
                        {
                        if(mysql_num_rows($resultyield) > 0) $output = mysql_result($resultyield,0,"output");
                        }
                    $query = "SELECT cloth_type FROM clt_transact";
                    $result = mysql_query($query);
                    if(cloth_type == ('3392' || 'Aldifa' || 'G3')){
                    $CLT_yield = @($output / ($input * 8.8)) * 100;
                    }
                    else{
                    $CLT_yield = @($output / ($input * 9)) * 100;
                    }
?>

at my previous code my problem is this only formula was used:$CLT_yield = @($output / ($input * 9)) * 100; but now in my new code the formula:$CLT_yield = @($output / ($input * 8.8)) * 100; was only works. I don’t know what is wrong in my code. because in my sample I have two different cloth type one is NW and one is W and i noticed that only the formula for NW was work now.

Thank you

I try new code like this:



$query = "SELECT cloth_type FROM clt_transact WHERE cloth_type IN('AAA', 'BBB', 'CCC')";
                      $result = mysql_query($query);
                      //$cloth_type = $result['cloth_type'];
                      if($result){
                            $CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100;
                                 }
                      else{
                            $CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100;
                          }


To test if the two formula was work i have data that the cloth type is AAA and I have data that the cloth type is DDD and when I run my code only the first formula was work even the cloth type is DDD. I don’t know why the else condition did not work in cloth type DDD.why the formula in AAA was work in the DDD cloth type. I mean only this formula:$CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100; was work in both AAA and DDD.

Thank you

Try like this::
if (mysql_num_rows($result)!=0)
$CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100;
else
$CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100;

i tried the code that you suggested but nothing change in the result.


 $query = "SELECT cloth_type FROM clt_transact WHERE cloth_type IN('AAA', 'BBB', 'CCC')";
                      $result = mysql_query($query);
                      if (mysql_num_rows($result)!=0){
                        $CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100;
                         }
                      else{
                           $CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100;
                           }

I tried everything that on my mind but still only the first formula in all cloth type.

Thank you

Then you modify the query like this;

$query = “SELECT cloth_type FROM clt_transact”;
$result = mysql_query($query);
while($data= mysql_fetch_array( $result ))
{
if($data[0]==‘AAA’ || $data[0]==‘BBB’ || $data[0]==‘CCC’)
$CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100;
else
$CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100;
}
You can get the desired result.

Thank you for trying to help me. Now I tried the code you given and the result is only the second formula was used even my 1st data is AAA the formula that was used is the formula in the else statement. I want is if the cloth type is AAA the first formula will used and if the formula was not listed in the if condition it falls on the else statement where the 2nd form,ula should be used. On our 1st try the 1st formula was work and now the 2nd was work.

Thank you very much…

I tried to add curly bracket in if and else condition buyt still only the second formula was work in all cloth type.

Thank you

The code i given is for all the records in the database, means it will iterate the whole data it fetches…
I want to know, how you get the cloth_type, is there any list box or text box to execute a query?
or you want to update for all records or any one specific record?

cloth type is in input in the textbox and it will save in the database and the code that i have a problem is for the report.

means based on the selection of cloth_type?
then you get the value of select box and execute a query based on the selection…

You will get more reliable result.

I have a form and the user put what the cloth type and the input and output and it will save in the database and now I need to ahve the reports so that the code that i ahve problem is for the reports and it is based on the database which the user inserted in the form.

ok.
You trim the user data entered data before submitting into the database.
Because the spaces before first char also may fails the condition.