/    Sign up×
Community /Pin to ProfileBookmark

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:

[code=php]
<?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>”;
}
?>
[/code]

I hope somebody can help me.

Any help is highly appreciated.

Thank you

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@DasherDec 29.2010 — Not sure why you are doing loops in a mysql query when the query can sort all the data for you.

You need a if ($something == $row['something']{ $CLT_yield = @($output / ($input * 9)) * 100; } Also if you test for $input==0 you can avoid the @ in front of the calculation. Better to not have an error instead of just trying to ignore them.

$loopctr += 1; counts but is not used anywhere.
Copy linkTweet thisAlerts:
@rhodaroseauthorDec 30.2010 — The loop is for the operation_name. I tried to put the formula before the close curly bracket still only the last formula was used.

i think i need to have the if and else statement, my problem is I have no idea where i can put it.

Thank you
Copy linkTweet thisAlerts:
@rhodaroseauthorDec 30.2010 — I try new code:

[code=php]
<?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;
}
?>
[/code]


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
Copy linkTweet thisAlerts:
@rhodaroseauthorJan 03.2011 — I try new code like this:



[code=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;
}

[/code]




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
Copy linkTweet thisAlerts:
@thraddashJan 03.2011 — You were not extracting any information from the recordset, currently the if statement is passing because you are giving it a resource.

Try something like this...

[code=php]$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']; //SHOULD WORK NOW

if ($cloth_type == 'AAA') {
$CLT_yield = @($clicking_output / ($spreading_input * 8.8)) * 100;
} else {
$CLT_yield = @($clicking_output / ($spreading_input * 9)) * 100;
}[/code]
Copy linkTweet thisAlerts:
@rhodaroseauthorJan 03.2011 — I tried the code that you suggested and still only the formula in if statement was work even the cloth_type is DDD the 1st formula was used.

Thank you
Copy linkTweet thisAlerts:
@thraddashJan 03.2011 — I did this test, I even set up my database the same way so I know this works...

[code=php]$db_conn = mysql_connect('localhost', 'root', '');
mysql_select_db('database', $db_conn);

$query = 'SELECT cloth_type FROM clt_transact WHERE cloth_type IN ("AAA", "BBB", "CCC")';
$recordset = mysql_query($query, $db_conn);
while ($result = mysql_fetch_assoc($recordset)) {
switch ($result['cloth_type']) {

case 'AAA':
echo 'APPLE<br />';
break;
case 'BBB':
echo 'BANANA<br />';
break;
case 'CCC':
echo 'CAT<br />';
break;
case 'DDD':
echo 'DUCK<br />';
break;

}
}
mysql_free_result($recordset);[/code]


If you still have problems, try removing the @ characters before your statements, you might just be suppressing errors you need to fix.
Copy linkTweet thisAlerts:
@rhodaroseauthorJan 04.2011 — I want is no need to put the other cloth type because NW cloth are AAA, BBB, and CCC and the rest cloth is W which I don't want to put it in my code so that i used if and else, else is for W . Because I have a lot of cloth type in W.

Thank you...
×

Success!

Help @rhodarose spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...