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 <?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
From what I can see your using if($result_loop) if(mysql_num_rows($result_loop) > 0) which you should not do instead use ||(OR), &&(AND) for multiple conditions in your if statements example: PHP: <?php $myVar = 1; $gt = 0; if ($myVar != "" AND $myVar > $gt) { echo "my variable is not blank and is greater than 0"; } else { // both conditions were not met trigger error echo "variable is either blank or is less than 0"; } ?> The above would say "my variable is not blank and is greater than 0". The reason is anytime you use either && or AND it will check to make sure both conditions are met and if they are not it will execute the code in the else or else if statement and if you have neither the code will terminate because not other conditions were added. If you use || or OR it will check to see if one of the conditions are met and if so execute the code but if none of the conditions are met it will move to the else if or else statements or terminate the process.
I try new code like this: PHP: $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
Why are you using the @ symbol that is for error suppression. Where are the variables $clicking_output and $spreading_input? Your trying to multiple values that do not exist. Try this. Also what is the math supposed to be doing? normally math is left to right with anything inside () being done first. PHP: <?php function querySuccessful($query) { if(!$query) { return false; } else { return true; } } $Q = "SELECT DISTINCT cloth_type AS type FROM clt_transact"; // we only want the three values or how ever many distinct values exist $runQ = @mysql_query($Q); if(querySuccessful($runQ))// query ran ok check if anything has been returned now { if(@mysql_num_rows($runQ) > 0) // make cloth types an associative array { $cloth = @mysql_fetch_assoc($runQ); // creates an associative array foreach($cloth AS $clothTypes) { if($clothTypes == "AAA") { // math here //insert into db code here } else if($cloth[type] == "DDD") { //math here //insert into db code here } } } } else { // query did not run show error } ?>
this is my full code for getting the CLT Yield: PHP: <?phpinclude 'config.php';$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) { $spreading_input = 0; $clicking_output = 0; $query = "SELECT SUM(t.input_qty) AS spreading_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) $spreading_input = mysql_result($resultyield,0,"spreading_input"); } $query = "SELECT SUM(t.output_qty) AS clicking_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) $clicking_output = mysql_result($resultyield,0,"clicking_output"); } $query = "SELECT cloth_type FROM clt_transact WHERE cloth_type IN ('AAA', 'BBB', 'CCC')"; $record = mysql_query($query); $result = mysql_fetch_assoc($record); $cloth_type = $result['cloth_type']; if ($cloth_type == 'AAA' || $cloth_type == 'BBB' || $cloth_type == 'CCC') { $CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100; } else { $CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100; }?>
I'll brb I have to go to the store could you post your mysql table so I can test the code to make sure what I give you works. I spotted an error really fast what your using a variable in a query you need to add it like so $Q = "SELECT * FROM clt_transact WHERE cloth_type = '".$cloth_type."'"; its single quote, double quote, period, variable, period, double quote, single quote or ' " . $myVar . " ' (minus the spaces).
What do you mean mysql table? here is my full code: PHP: <?phpif($_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', 'Pack', 'Brush', 'Dip')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) { $spreading_input = 0; $clicking_output = 0; $query = "SELECT SUM(t.input_qty) AS spreading_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) $spreading_input = mysql_result($resultyield,0,"spreading_input"); } $query = "SELECT SUM(t.output_qty) AS clicking_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) $clicking_output = mysql_result($resultyield,0,"clicking_output"); } $query = "SELECT cloth_type FROM clt_transact WHERE cloth_type IN ('AAA', 'BBB', 'CCC')"; $record = mysql_query($query); $result = mysql_fetch_assoc($record); $cloth_type = $result['cloth_type']; if ($cloth_type == 'AAA' || $cloth_type == 'BBB' || $cloth_type == 'CCC') { $CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100; } else { $CLT_yield = @($clicking_output / ($spreading_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', 'Pack', 'Brush', 'Dip') 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 have inserted 2 data in my database I have AAA and DDD cloth type to test the if and else condition. Thank you
Ok let me ask what are you trying to do code wise? Your getting data from the database but not using that data at all in your first post. Your also using PHP: include'config.php'; This is the wrong way to do it because include is a built in function you need the (). the correct way is PHP: include('config.php'); // include back end I suggest that you comment your code to so you know what your trying to do and also use a IDE so you have syntax highlighting so you know if your making a mistake in your code. Code Lobster is a free IDE that is pretty good http://www.codelobster.com/ Also your first script will not work because your missing the last two }}, if you add them it will close off the two starting if statements your using. Why are you using the SUM aggregate function for in your queries? You use this to get the total of all specified columns(adds the values of all of the together). Only use this if your trying to figure out your total amount earned or something along those lines plus your using the old join method to when your only selection one value you should not use a join either old fashioned or new fashions(declare it as a join) This query does not make sense to me because its only selecting one value from one table but joins it with another table which has nothing being selected for return Code: SELECT SUM(t.input_qty) AS spreading_input FROM clt_traceability AS t, clt_transact AS p WHERE t.clt_transact_id = p.clt_transact_id AND t.operation_name='Total1' AND p.clt_no = '".$clt_no."' The above should just be Code: SELECT SUM(t.input_qty) AS spreading_input FROM clt_traceability AS t The reason is that your only selecting this value from this table and no data is selected from the joined table which is a waste of resources because the data is not being used. If you want to get the total number of rows use the COUNT function instead Code: SELECT COUNT(*) FROM clt_transact Are you trying to get the percentage on this or something?Use a function instead of rewriting the code over and over again. percentage function excepts additional parameter for rounding and by default rounds PHP: function percentage($origVal,$newVal,$round="true"){$percent = ($newVal / $origVal) * 100;// check if round is false dont roundif($round == "true"){return round($percent);}else{return $percent;}} This is the fixed script so that the syntax is correct but I did not adjust the code so that it works because I would like to know what your trying to get done code wise so I can help you get to that point once you tell me what it is your trying to do. PHP: <?php/* include is a function so you need to use include('page.php'); and not include'page.php'; */include('config.php');// include backend for database connection$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) // rows were returned from database { $spreading_input = 0; $clicking_output = 0; $query = "SELECT SUM(t.input_qty) AS spreading_input FROM clt_traceability AS t, clt_transact AS 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) { $spreading_input = mysql_result($resultyield,0,"spreading_input"); } } $query = "SELECT SUM(t.output_qty) AS clicking_output FROM clt_traceability AS t, clt_transact AS 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) { $clicking_output = mysql_result($resultyield,0,"clicking_output"); } } $query = "SELECT cloth_type FROM clt_transact WHERE cloth_type IN ('AAA', 'BBB', 'CCC')"; $record = mysql_query($query); $result = mysql_fetch_assoc($record); $cloth_type = $result['cloth_type']; if ($cloth_type == 'AAA' || $cloth_type == 'BBB' || $cloth_type == 'CCC') { $CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100; } else { $CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100; } } }?> Post what it is your trying to do so I can help you from there so your code will be cleaner with less lines of code.
PHP: <?phpinclude 'config.php';$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) { $spreading_input = 0; $clicking_output = 0; $query = "SELECT SUM(t.input_qty) AS spreading_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) $spreading_input = mysql_result($resultyield,0,"spreading_input"); } $query = "SELECT SUM(t.output_qty) AS clicking_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) $clicking_output = mysql_result($resultyield,0,"clicking_output"); } $query = "SELECT cloth_type FROM clt_transact WHERE cloth_type IN ('AAA', 'BBB', 'CCC')"; $record = mysql_query($query); $result = mysql_fetch_assoc($record); $cloth_type = $result['cloth_type']; if ($cloth_type == 'AAA' || $cloth_type == 'BBB' || $cloth_type == 'CCC') { $CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100; } else { $CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100; }?>
I tried the code that you gave and nothing has changed. I don't know why the if condition only that was work. Thank you
Ok let me ask you this, what do you want the program to do? What is the end result, once you tell me I can help you rewrite this code so it works how you want it to work.Will get you two include pages config.php and functions.php and then get this code to work properly.
I want that the program to do is if the cloth type is AAA or BBB or CCC the if statement work which has a formula and if the cloth type is not mention like DDD the else statement which also a different formula. Now the if condition only works even the cloth type is DDD. I already include the config.php but i don't have functions.php Thank you
Ok simple compare the value of what ever is being used as the cloth type source. If your using data retrieved from a database table wrap the if statement in there so that it does it for each instance of the data or else just us a plan old if statement. The functions.php is so you can add most of your logic in functions so you dont have to recode everything every time you want to use it. Lets do a hypothetical situation. General if statement checking if value is one or the other. You need to specify each time what variable and value pairs are being evaluated. I noticed you used if($clothType == "AAA" || "BBB" || "CCC") which explains why its not working. It would be nice if it did work that way but it does not see below how it should be done. PHP: <?php$clothType = $_POST['cloth_type'];if($clothType == "AAA" || $clothType == "BBB" || $clothType == "CCC"){// code to execute}else{//value is not one for the three do another calculation here}?> You can make a function for calculating the total cost easy PHP: <?phpfunction calculateCost($itemAmt,$Cost,$Tax,$preTax="false",$round="true"){//make sure the values are numericif(!is_numeric($itemAmt) && !is_numeric($Cost) && !is_numeric($Tax)){// all items need to be numeric of else this will not workreturn "NaN";}else{// everything is a number lets roll$subTotal = $itemAmt * $Cost;$totalAmt = $subTotal * $Tax;if($preTax == "true"){return $subTotal;}else{if($round == "true"){return round($totalAmt);}else{return $totalAmt;}}}?> This function will make doing total calculations simple just call the function and fill in the params. I think I already posted a percentage function but yeah anything that is not fixed you should turn into a function to save coding time and file bloat.
in PHP or many other language if condition run on boolen expression ... like if the condition or result would be true or 1 then your inner condition will be run else if it return 0 or false then the condtion would not be satisfy like in php when we check if($_POST['abc']) this will check is any value of $_POST['abc'] if it occure either null it could be run else it will return false