newphpcoder Posted December 29, 2010 Share Posted December 29, 2010 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 <?phpinclude '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: " . $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 Link to comment Share on other sites More sharing options...
boen_robot Posted December 29, 2010 Share Posted December 29, 2010 Have you ran the queries separately (say, in MySQL Workbench or phpMyAdmin)? Are you sure they return what you think they return?Also, you have no condition in the area with the two forumlas... that is, no condition that would stop one of the queries and formula execution in favor of the other... you're unconditionally starting the first and continue with the second... in the end, the W forumla will always be applied.Somewhere in this area: ///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; Perhaps in the two places where you have comments, you should be having an if and an else, respectively, but I'm not sure I understand your code's flow to tell you what the condition itself should be. Link to comment Share on other sites More sharing options...
newphpcoder Posted December 30, 2010 Author Share Posted December 30, 2010 Have you ran the queries separately (say, in MySQL Workbench or phpMyAdmin)? Are you sure they return what you think they return?Also, you have no condition in the area with the two forumlas... that is, no condition that would stop one of the queries and formula execution in favor of the other... you're unconditionally starting the first and continue with the second... in the end, the W forumla will always be applied.Somewhere in this area: ///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; Perhaps in the two places where you have comments, you should be having an if and an else, respectively, but I'm not sure I understand your code's flow to tell you what the condition itself should be. Yes, I also think that I need to have the if and else condition to check if the cloth type is NW (AAA, BBB, or CCC) or W the rest cloth type that did not listed in NW. As you can see in my NW i put in my SELECT Statement the NW and in the SELECT statement for W i do'nt have code for the cloth type. and as you can see also they have different formula. My problem is my code for NW did not satisfied so that even the cloth type is NW like CCC the formula that was used is the formula of W.Thank you Link to comment Share on other sites More sharing options...
newphpcoder Posted December 30, 2010 Author Share Posted December 30, 2010 Yes, I also think that I need to have the if and else condition to check if the cloth type is NW (AAA, BBB, or CCC) or W the rest cloth type that did not listed in NW. As you can see in my NW i put in my SELECT Statement the NW and in the SELECT statement for W i do'nt have code for the cloth type. and as you can see also they have different formula. My problem is my code for NW did not satisfied so that even the cloth type is NW like CCC the formula that was used is the formula of W.Thank youI 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 Link to comment Share on other sites More sharing options...
boen_robot Posted December 30, 2010 Share Posted December 30, 2010 The condition is nonsence: if(cloth_type == ('3392' || 'Aldifa' || 'G3')){ This checks if a constant named cloth_type equals '3392'. Disregarding the fact you're not even analyzing the correct thing, writing out the condition in this way is never going to check for the other values, because the right side of "||" is applied only if the left has a falsy value (false, null, 0, '').You probably want something more like this: $result = mysql_query($query);$cloth_type = mysql_fetch_row($result);$cloth_type = $cloth_type[0]; if(in_array($cloth_type, array('3392', 'Aldifa', 'G3')){ BTW, have you considered embedding the formula in the SQL query itself? If the output format is the same for both types, this is certainly going to make things a lot more easier, and in your case, more efficient too. Link to comment Share on other sites More sharing options...
newphpcoder Posted January 3, 2011 Author Share Posted January 3, 2011 I try the code that you suggested: $query = "SELECT cloth_type FROM clt_transact"; $result = mysql_query($query); $cloth_type = mysql_fetch_row($result); $cloth_type = $cloth_type[0]; if(in_array ($cloth_type,array('3392','Aldifa','G3'))){ } else{ $CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100; } the result is 0% Link to comment Share on other sites More sharing options...
newphpcoder Posted January 3, 2011 Author Share Posted January 3, 2011 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 Link to comment Share on other sites More sharing options...
birbal Posted January 3, 2011 Share Posted January 3, 2011 else part will be only work when $result is false...and it will return false only when the query will fail..in your case it is returng a empty result set when you are doing DDD..i think..you have to do something like.. if(mysql_num_rows($result)>0)//do some workelse//result is empty do other work Link to comment Share on other sites More sharing options...
newphpcoder Posted January 3, 2011 Author Share Posted January 3, 2011 I tried it and still only the fisrt formula was used for both datra that was different cloth type. it means that even the 2nd data was fails it did not go to the else statewment to used the other formula.Thank you Link to comment Share on other sites More sharing options...
birbal Posted January 3, 2011 Share Posted January 3, 2011 2nd data was failswhich one is second data?where is your DDD? where you are putting it. i cant see any DDDI 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.