Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    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 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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Your first order of business should be to add error handling to _all_ of your queries. If one of those queries fails, your code does nothing about it. This is especially important when you build queries using variables, especially external variables (such as $_POST variables).

    For example: You are assigning values to $input and $output only if the queries succeed, but then you use these variables in your $CLT_yield calculation, even if the query fails. At the very least, assign a value of "0" to the variable if the query fails. But better than that, abort the script and report the problem.

    Your second order of business should be to use mysql_real_escape_string() to take care of those SQL injection vulnerabilities.

  • Users who have thanked Fumigator for this post:

    newphpcoder (01-02-2011)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    I try new code like this:



    PHP Code:

    $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

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    That's because 1) you haven't fetched any data from your resultset yet and 2) $result will only be false if the query fails. It will be true if the query returns 0 rows. You need to use mysql_fetch_array() to fetch a row from your resultset, and/or check mysql_num_rows() to find out if your query has anything in it. Look those functions up on php.net for usage examples.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    I have new code in my new code only the if condition was work even the cloth type is DDD.

    here is my new code:
    PHP Code:
      
                           $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;
                            } 

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    That's because 1) you haven't fetched any data from your resultset yet and 2) $result will only be false if the query fails. It will be true if the query returns 0 rows. You need to use mysql_fetch_array() to fetch a row from your resultset, and/or check mysql_num_rows() to find out if your query has anything in it. Look those functions up on php.net for usage examples.
    I tried to add code mysql_fetch_array

    here is my code:

    PHP Code:
    $query  "SELECT cloth_type FROM clt_transact WHERE cloth_type IN ('AAA', 'BBB', 'CCC')";
                            
    $result mysql_query($query) or die(mysql_error());
                            
    $row mysql_fetch_array($result) or die(mysql_error());
                            
    $cloth_type $row['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;
                            } 
    And the result is only the if condition work even the cloth type is DDD the formula was used is the formula in if condition.
    I don't know what is the missing in my codes.

    Thank you

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    $row mysql_fetch_array($result) or die(mysql_error()); 
    The "or die()" logic can't apply to mysql_fetch_array(), since it will return false if there are no more rows to fetch in the resultset. You don't want to abort your script just because you reached "end-of-results". But, you DO want to handle end-of-results, because there will be nothing in the $row variable to process.

    In addition, if your resultset contains more than one row, are you going to ignore all but the first row? If you need to process all rows in the resultset, you need to put mysql_fetch_array() in a loop of some kind. Most people use "while" loops but I like "for" loops better:

    PHP Code:
    for ($i 0$i mysql_num_rows($result); $i++) {
        
    $rows[$i] = mysql_fetch_array($result);

    Notice we are only going to fetch the number of rows in the resultset and no more. And now you have a nice little array, $rows, with all your query results in it. You can now loop through this array and do whatever you want with the data.

    If you're only interested in the first row, go ahead and call mysql_fetch_array() once, but make sure you allow for an empty resultset. Something like this would do nicely:

    PHP Code:
    if (mysql_num_rows($result) > 0) {
        
    $row fetch_row_array($result);
        
    $CLT_yield = @($clicking_output / ($spreading_input 8.8)) * 100;
    } else {
        
    $CLT_yield = @($clicking_output / ($spreading_input 9)) * 100;

    But it seems you are not even using any data from the query; it appears you are only interested in finding out if there are any cloth_types of AAA, BBB, or CCC in your table. In that case, you don't even need to fetch anything; just look at mysql_num_rows() and it returns more than 0 then you know there's at least one AAA/BBB/CCC cloth type in your table.

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    I tried this code:

    PHP Code:

    $query  
    "SELECT cloth_type FROM clt_transact WHERE cloth_type IN ('AAA', 'BBB', 'CCC')";
      
    $result mysql_query($query);
                            if (
    mysql_num_rows($result) > 0) { 
                            
    $row fetch_row_array($result); 
                            
    $CLT_yield = @($clicking_output / ($spreading_input 8.8)) * 100
                            } else { 
                             
    $CLT_yield = @($clicking_output / ($spreading_input 9)) * 100
                             } 
    only the else was work in AAA and DDD cltoh type and i got warning:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\xampp\htdocs\INTRANET\plt_report.php on line 533

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\xampp\htdocs\INTRANET\plt_report.php on line 533


    I dont know where i can put this code that you gave

    Code:
    for ($i = 0; $i < mysql_num_rows($result); $i++) { 
        $rows[$i] = mysql_fetch_array($result); 
    }

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I feel like I'm going in circles with you. If you read the 2nd post in this thread, I caution you to add error checking to all of your queries. You haven't learned that lesson yet I guess.

  • #10
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    i tried it but no error appear.

    Thanks

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You are trying to use a variable as if it were a query resource (as in, the value that mysql_query() returns) but it's not a query resource, which means the query failed. What does your latest code look like?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •