Jump to content

Problem in If condition


newphpcoder

Recommended Posts

Hi..

 

I tried this code:

 

<?php
     error_reporting(0);
   date_default_timezone_set("Asia/Singapore"); //set the time zone  
$con = mysql_connect('localhost', 'root','');
  mysql_select_db("mes", $con); 
if (!$con) {
    echo 'failed';
    die();
}

$sql = "SELECT PCODE, total FROM kanban_checker ORDER BY PCODE";
$res = mysql_query($sql, $con) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
    $PCODE = $row['PCODE'];
    $total = $row['total'];
}
  $sql = "SELECT 
                P28, P28_max, P28_min,
                P30, P30_max, P30_min,
                P32, P32_max, P32_min,
                P33, P33_max, P33_min,
                P35, P35_max, P35_min,
                P35M, P35M_max, P35M_min,
                P35W, P35W_max, P35W_min,
                P38, P38_max, P38_min,
                P41, P41_max, P41_min,
                P42, P42_max, P42_min,
                P43, P43_max, P43_min,
                P46, P46_max, P46_min
                FROM parameter_settings";
        $res_pcode = mysql_query($sql, $con);
        
  $row = mysql_fetch_assoc($res_pcode);
            $P28 = $row['P28'];
            $P28_max = $row['P28_max'];
            $P28_min = $row['P28_min'];
        
            $P30 = $row['P30'];
            $P30_max = $row['P30_max'];
            $P30_min = $row['P30_min'];
            
            $P32 = $row['P32'];
            $P32_max = $row['P32_max'];
            $P32_min = $row['P32_min'];
            
            $P33 = $row['P33'];
            $P33_max = $row['P33_max'];
            $P33_min = $row['P33_min'];
            
            $P35 = $row['P35'];
            $P35_max = $row['P35_max'];
            $P35_min = $row['P35_min'];
           
            $P35M = $row['P35M'];
            $P35M_max = $row['P35M_max'];
            $P35M_min = $row['P35M_min'];
            
            $P35W = $row['P35W'];
            $P35W_max = $row['P35W_max'];
            $P35W_min = $row['P35W_min'];
            
            $P38 = $row['P38'];
            $P38_max = $row['P38_max'];
            $P38_min = $row['P38_min'];
            
            $P41 = $row['P41'];
            $P41_max = $row['P41_max'];
            $P41_min = $row['P41_min'];
            
            $P42 = $row['P42'];
            $P42_max = $row['P42_max'];
            $P42_min = $row['P42_min'];
            
            $P43 = $row['P43'];
            $P43_max = $row['P43_max'];
            $P43_min = $row['P43_min'];
            
            $P46 = $row['P46'];
            $P46_max = $row['P46_max'];
            $P46_min = $row['P46_min'];
            
            $P47 = $row['P47'];
            $P47_max = $row['P47_max'];
            $P47_min = $row['P47_min'];             
       if($PCODE = $P28 || $total = $P28_min){
            echo $P28_min;
        }
        elseif($PCODE = $P30 || $total = $P30_min){
            echo $P30_min;
        }
        elseif($PCODE = $P32 || $total = $P32_min){
            echo $P32_min;
        }
        elseif($PCODE = $P33 || $total = $P33_min){
            echo $P33_min;
        }
        elseif($PCODE = $P35 || $total = $P35_min){
            echo $P35_min;
        }
        elseif($PCODE = $P35M || $total = $P35M_min){
            echo $P35M_min;
        }
        elseif($PCODE = $P35W || $total = $P35W_min){
            echo $P35W_min;
        }
        elseif($PCODE = $P38 || $total = $P38_min){
            echo $P38_min;
        }
        elseif($PCODE = $P41 || $total = $P41_min){
            echo $P41_min;
        }
        elseif($PCODE = $P42 || $total = $P42_min){
            echo $P42_min;
        }
        elseif($PCODE = $P43 || $total = $P43_min){
            echo $P43_min;
        }
        elseif($PCODE = $P46 || $total = $P46_min){
            echo $P46_min;
        }
        elseif($PCODE = $P47 || $total = $P47_min){
            echo $P47_min;
        }
        else{
            echo '';
        }
?>

 

for display data if total = min.

 

The problem is, in this condition he satisfied if condition even though $total is not equal to $P28_min.

 

Thank you

Link to comment
Share on other sites

did you change it to this?

 

if($PCODE == $P28 || $total == $P28_min){
            echo $P28_min;
        }
        elseif($PCODE == $P30 || $total == $P30_min){
            echo $P30_min;
        }
        elseif($PCODE == $P32 || $total == $P32_min){
            echo $P32_min;
        }
        elseif($PCODE == $P33 || $total == $P33_min){
            echo $P33_min;
        }
        elseif($PCODE == $P35 || $total == $P35_min){
            echo $P35_min;
        }
        elseif($PCODE == $P35M || $total == $P35M_min){
            echo $P35M_min;
        }
        elseif($PCODE == $P35W || $total == $P35W_min){
            echo $P35W_min;
        }
        elseif($PCODE == $P38 || $total == $P38_min){
            echo $P38_min;
        }
        elseif($PCODE == $P41 || $total == $P41_min){
            echo $P41_min;
        }
        elseif($PCODE == $P42 || $total == $P42_min){
            echo $P42_min;
        }
        elseif($PCODE == $P43 || $total == $P43_min){
            echo $P43_min;
        }
        elseif($PCODE == $P46 || $total == $P46_min){
            echo $P46_min;
        }
        elseif($PCODE == $P47 || $total == $P47_min){
            echo $P47_min;
        }

 

also bare in mind that || means OR so only 1 of the conditions needs to be true for it to satisfy your if statement, if you want them to both be true you need to change || to && which means AND, so both statements need to be true for it to pass

Link to comment
Share on other sites

Hi..

 

I resolved my issue when i revise my code:

 

$sql = "SELECT PCODE, total FROM kanban_checker ORDER BY PCODE";
$res = mysql_query($sql, $con) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
    $PCODE = $row['PCODE'];
    $total = $row['total'];
  $sql = "SELECT 
                P28, P28_max, P28_min,
                P30, P30_max, P30_min,
                P32, P32_max, P32_min,
                P33, P33_max, P33_min,
                P35, P35_max, P35_min,
                P35M, P35M_max, P35M_min,
                P35W, P35W_max, P35W_min,
                P38, P38_max, P38_min,
                P41, P41_max, P41_min,
                P42, P42_max, P42_min,
                P43, P43_max, P43_min,
                P46, P46_max, P46_min,
                P47, P47_max, P47_min
                FROM parameter_settings";
        $res_pcode = mysql_query($sql, $con);
        
  $row = mysql_fetch_assoc($res_pcode);
            $P28 = $row['P28'];
            $P28_max = $row['P28_max'];
            $P28_min = $row['P28_min'];
        
            $P30 = $row['P30'];
            $P30_max = $row['P30_max'];
            $P30_min = $row['P30_min'];
            
            $P32 = $row['P32'];
            $P32_max = $row['P32_max'];
            $P32_min = $row['P32_min'];
            
            $P33 = $row['P33'];
            $P33_max = $row['P33_max'];
            $P33_min = $row['P33_min'];
            
            $P35 = $row['P35'];
            $P35_max = $row['P35_max'];
            $P35_min = $row['P35_min'];
           
            $P35M = $row['P35M'];
            $P35M_max = $row['P35M_max'];
            $P35M_min = $row['P35M_min'];
            
            $P35W = $row['P35W'];
            $P35W_max = $row['P35W_max'];
            $P35W_min = $row['P35W_min'];
            
            $P38 = $row['P38'];
            $P38_max = $row['P38_max'];
            $P38_min = $row['P38_min'];
            
            $P41 = $row['P41'];
            $P41_max = $row['P41_max'];
            $P41_min = $row['P41_min'];
            
            $P42 = $row['P42'];
            $P42_max = $row['P42_max'];
            $P42_min = $row['P42_min'];
            
            $P43 = $row['P43'];
            $P43_max = $row['P43_max'];
            $P43_min = $row['P43_min'];
            
            $P46 = $row['P46'];
            $P46_max = $row['P46_max'];
            $P46_min = $row['P46_min'];
            
            $P47 = $row['P47'];
            $P47_max = $row['P47_max'];
            $P47_min = $row['P47_min'];             
       if($PCODE == $P28 && $total == $P28_min){
            echo $P28_min;
        }
        
       else{
           echo '';
       }
        
       if($PCODE == $P30 && $total == $P30_min){
            echo $P30_min;
        }
       else{
           echo '';
       }
       
       if($PCODE == $P32 && $total == $P32_min){
            echo $P32_min;
       }
       else{
           echo '';
       }
       
       if($PCODE == $P33 && $total == $P33_min){
            echo $P33_min;
       }
       else{
           echo '';
       }
       
       if($PCODE == $P35 && $total == $P35_min){
            echo $P35_min;
       }
       else{
           echo '';
       }
       
       if($PCODE == $P35M && $total == $P35M_min){
            echo $P35M_min;
       }
       else{
           echo '';
       }
       
       if($PCODE == $P35W && $total == $P35W_min){
            echo $P35W_min;
       }
       else{
           echo '';
       }
       
       if($PCODE == $P38 && $total == $P38_min){
            echo $P38_min;
       }
       else{
           echo '';
       }
       
       if($PCODE == $P41 && $total == $P41_min){
            echo $P41_min;
       }
       else{
           echo '';
       }
       
       if($PCODE == $P42 && $total == $P42_min){
            echo $P42_min;
       }
       else{
           echo '';
       }
       if($PCODE == $P43 && $total == $P43_min){
            echo $P43_min;
       }
       else{
           echo '';
       }
       if($PCODE == $P46 && $total == $P46_min){
            echo $P46_min;
       }
       else{
           echo '';
       }
       
       if($PCODE == $P47 && $total == $P47_min){
            echo $P47_min;
       }
        else{
            echo '';
        }
}

 

Now my problem is..

 

How and where i can add this code in my form to display minimum value per Items which Items = PCODE.

 

here is my code where I need to add the above code:

 

<?php                                                                          
   error_reporting(0);
   date_default_timezone_set("Asia/Singapore"); //set the time zone  
$con = mysql_connect('localhost', 'root','');

if (!$con) {
    echo 'failed';
    die();
}

mysql_select_db("mes", $con);
$sr_date =date('Y-m-d H:i:s');

$sql = "SELECT sr_number FROM stock_requisition ORDER BY sr_date DESC LIMIT 1";
        $result = mysql_query($sql, $con);
        if (!$result) {
            echo 'failed'; 
            die();
        }
        $total = mysql_num_rows($result);
        if ($total <= 0) {
            $currentSRNum = 1;
        } 
        else {
//------------------------------------------------------------------------------------------------------------------
            // Stock Number iteration.... 
            $row = mysql_fetch_assoc($result);
            
            $currentSRNum = (int)(substr($row['sr_num'],0,3));
            
            $currentSRYear  = (int)(substr($row['sr_num'],2,2));
            $currentSRMonth = (int)(substr($row['sr_num'],0,2));
            
            $currentYear  = (int)(date('y'));
            $currentMonth = (int)(date('m'));
            $currentDay = (int)(date('d'));
            
            
            if ($currentYear == $currentSRYear) {
                if ($currentMonth == $currentSRMonth) {
                    $currentSRNum = $currentSRNum + 1;
                }
                if ($currentMonth > $currentSRMonth) {
                    $currentSRNum = 1;
                }
                if ($currentDay > $currentSRDay) {
                    $currentSRNum = 1;
                }
            }  
            if ($currentYear > $currentRefYear) {      
                $currentSRNum = 1;                    
            }                                          
        }
//------------------------------------------------------------------------------------------------------------------         
        $yearMonth = date('ymd');    
        $currentSR = $yearMonth . sprintf("%03d", $currentSRNum); 
?>
<html>
<title>Stock Requisition</title>
<head>
</head>
<body>
<div id="ddcolortabs">
<ul>
<li> <a href="ParameterSettings.php" title="Parameter Settings"><span>Parameter Settings</span></a></li>
<li id="current"><a href="StockRequisition.php" title="Stock Requisition"><span>Stock Requisition</span></a></li>
<li style="margin-left: 1px"><a href="kanban_report.php" title="WIP Report"><span>Wip Report</span></a></li>
</ul>
</div>
<div id="SR_date">
<label>Date :</label>
<input type="text" name="sr_date" value="<?php echo $sr_date; ?>" size="16" readonly="readonly">    
</div>
<div id="SR_number">
<label>SR# :</label>
<input type="text" name="sr_number" value="<?php echo $currentSR; ?>" size="8" readonly="readonly" style="font-weight: bold;">
<br/> 
</div> 
<div>
<table>
<thead>
<th>Items</th>
<th>Sub Items</th>
<th>Item Code</th>
<th>Demanded Qty</th>
<th>UoM</th>
<th>Class</th>
<th>Description</th>
<th>BIN Location</th>
</thead>
<?php
$sql = "SELECT DISTINCT Items FROM bom_subitems ORDER BY Items";
$res_bom = mysql_query($sql, $con);

while($row = mysql_fetch_assoc($res_bom)){
    
    $Items = $row['Items'];
echo "<tr>
        <td style='border: none;font-weight: bold;'> $row[items]</td>
        </tr>";       

$sql = "SELECT SubItems, ItemCode, UoM, Class, Description, BINLocation FROM bom_subitems WHERE Items = '$row[items]' ORDER BY Items"or die(mysql_error());
$res_sub = mysql_query($sql, $con);

while($row_sub = mysql_fetch_assoc($res_sub)){

    echo "<tr>
        <td style='border: none;'> </td>
        <td style='border: none;'>$row_sub[subItems]</td>
        <td style='border: none;'> $row_sub[itemCode]</td>
        <td style='border: none;'><center><input type='text' name='DemandedQty' id='DemandedQty' value='' size='7'></center></td>
        <td style='border: none;' size='3'> $row_sub[uoM]</td>
        <td style='border: none;'> $row_sub[Class]</td>
        <td style='border: none;'> $row_sub[Description]</td>
        <td style='border: none;'> $row_sub[bINLocation]</td>     
        </tr>";

}
}   
               
?>
</table>
</div>

</body>
</html>

 

In Demanded Qty i need to display the Minimum if the total = minimum per PCODE/Item per Item.

 

for example in P28 the total = P28_min then It will display on the 3 textboxes which belong to P28

I attach my sample image.

 

Thank you..

 

post-101569-13482403355804_thumb.jpg

Link to comment
Share on other sites

Sorry to jump in here, but you are still writing (and testing and changing and testing) far too many lines of repetitious hard-coded logic. In at least one of your previous threads, someone suggested fixing your database table designs and to NOT use a series of named variables where the variable name indicates the data in the variable (the variable name should indicate the purpose of the data in the variable - $PCODE, $total, $min, $max.) Since your kanban_checker table is apparently laid out correctly, I'm not sure why you haven't corrected your parameter_settings table.

 

If you do this, it will greatly simplify your code, which will shorten the time it takes you to write, test, and change your code and it will also increase the chance of someone helping with your code problems (if we cannot figure out what your code is trying to do, there's little chance of us helping with it.)

 

For example, if your parameter_settings table was corrected so that it had one row per PCODE, all that logic you have written and rewritten can be reduced to the following -

 

<?php

// select all the min/max settings (NOTE: you should have one row per PCODE with columns for the PCODE, min, max, and other settings for this PCODE...)
// you would then let php make an array (instead of you typing out a long list of named variables that you must change every time you add a pcode name) where the array index name is the pcode (so you can directly access the corresponding values) and you have an array with the min/max settings.
$sql = "SELECT PCODE, min, max FROM parameter_settings"
$result = mysql_query($sql,$con);
$settings = array();
while($row = mysql_fetch_assoc($result)){
$settings[$row['PCODE']] = $row; // an array with the SELECT'ed pcode, min, and max values for this pcode
}

// select all PCODE's and their total
$sql = "SELECT PCODE, total FROM kanban_checker ORDER BY PCODE";
$res = mysql_query($sql, $con) or die(mysql_error());
while($row = mysql_fetch_assoc($res)){
// get a PCODE/total
    $PCODE = $row['PCODE'];
    $total = $row['total'];
    
        // if the total for this PCODE is the same as its minimum, echo the minimum
if($settings[$PCODE]['min'] == $total){
	echo $settings[$PCODE]['min'];
}
}

Link to comment
Share on other sites

for example in P28 the total = P28_min then It will display on the 3 textboxes which belong to P28

I attach my sample image.

 

Now that I have studied what it is you are trying to do, based on my post above (of fixing the settings table design so that you can easily retrieve the data into an array), to conditionally output the subitems when the total for the PCODE/Items is == to the minimum, you would do something like the following -

 

------ <snip> code above this point not shown <snip> ------
<table>
<thead>
<th>Items</th>
<th>Sub Items</th>
<th>Item Code</th>
<th>Demanded Qty</th>
<th>UoM</th>
<th>Class</th>
<th>Description</th>
<th>BIN Location</th>
</thead>
<?php
// select all the min/max settings (NOTE: you should have one row per PCODE with columns for the PCODE, min, max, and other settings for this PCODE...)
// you would then let php make an array (instead of you typing out a long list of named variables that you must change every time you add a pcode name) where the array index name is the pcode (so you can directly access the corresponding values) and you have an array with the min/max settings.
$sql = "SELECT PCODE, min, max FROM parameter_settings";
$result = mysql_query($sql,$con) or die(mysql_error());
$settings = array();
while($row = mysql_fetch_assoc($result)){
$settings[$row['PCODE']] = $row; // an array with the SELECT'ed pcode, min, and max values for this pcode
}

// select all PCODE's and their total
$sql = "SELECT PCODE, total FROM kanban_checker ORDER BY PCODE";
$res = mysql_query($sql, $con) or die(mysql_error());
$totals = array();
while($row = mysql_fetch_assoc($res)){
// get a PCODE/total
    $totals[$PCODE] = $row['total'];
}

// get a list of Items/PCODEs appearing in the bom_subites table
$sql = "SELECT DISTINCT Items FROM bom_subitems ORDER BY Items";
$res_bom = mysql_query($sql, $con);
while($row = mysql_fetch_assoc($res_bom)){
$Items = $row['Items'];

echo "<tr>
	<td style='border: none;font-weight: bold;'> $Items</td>
</tr>";

// conditionally get and output the subitems 
if($totals[$Items] == $settings[$Items]['min']){
	// the following code gets the subitems for the current item
	$sql = "SELECT SubItems, ItemCode, UoM, Class, Description, BINLocation
		FROM bom_subitems WHERE Items = '$Items' ORDER BY Items";
	$res_sub = mysql_query($sql, $con);
	while($row_sub = mysql_fetch_assoc($res_sub)){
		echo "<tr>
			<td style='border: none;'> </td>
			<td style='border: none;'>$row_sub[subItems]</td>
			<td style='border: none;'> $row_sub[itemCode]</td>
			<td style='border: none;'><center><input type='text' name='DemandedQty' id='DemandedQty' value='' size='7'></center></td>
			<td style='border: none;' size='3'> $row_sub[uoM]</td>
			<td style='border: none;'> $row_sub[Class]</td>
			<td style='border: none;'> $row_sub[Description]</td>
			<td style='border: none;'> $row_sub[bINLocation]</td>
		</tr>";
	}
}
}
?>
</table>
</div>
</body>
</html>

 

Note: If all you are doing on this page is making the form, you can combine and reduce the three queries and the looped query using the parameter_settings, kanban_checker, and bom_subitems tables into one joined query statement, but that is beyond the scope of this thread (or the amount of time I would be willing to spend learning your table structures.)

Link to comment
Share on other sites

the fields of parameter settings is:

 

P28----------P28

P28_max---6

P28_min----5

P30----------P30

P30_max---10

P30_min----5

P32----------P32

P32_max---15

P32_min----10

P33----------P33

P33_max---20

P33_min----10

P35----------P35

P35_max---25

P35_min----15

and so on...

 

Link to comment
Share on other sites

Note: There's an error in the code I posted above for the $totals array. It should be -

$totals[$row['PCODE']] = $row['total'];

 

the fields of parameter settings is:

 

...

 

We know what your table looks like. It is designed incorrectly. That's why different people keep telling you in your threads that you need to straighten out your design so that you don't need to keep writing hundreds of lines of unnecessary code. You have over 100 lines of code that I was able to replace with 12 lines of code. Writing block after block of code with variables like $P28, $P28_max, $P28_min, $P30, $P30_max, $P30_min, ... is not effective use of variables and is not effective programming.

 

Also, putting a fixed/static query (that returns the same result set every time it is executed) inside of a loop, is a complete waste of processing time.

 

The following code will (untested) retrieve your parameter settings into the $settings array that I show in the code I have been posting -

<?php
$sql = "SELECT 
	P28, P28_max, P28_min,
	P30, P30_max, P30_min,
	P32, P32_max, P32_min,
	P33, P33_max, P33_min,
	P35, P35_max, P35_min,
	P35M, P35M_max, P35M_min,
	P35W, P35W_max, P35W_min,
	P38, P38_max, P38_min,
	P41, P41_max, P41_min,
	P42, P42_max, P42_min,
	P43, P43_max, P43_min,
	P46, P46_max, P46_min,
	P47, P47_max, P47_min
	FROM parameter_settings";
$res_pcode = mysql_query($sql, $con);
$settings = array();
$row = mysql_fetch_assoc($res_pcode);

$settings[$row['P28']]['max'] = $row['P28_max'];
$settings[$row['P28']]['min'] = $row['P28_min'];

$settings[$row['P30']]['max'] = $row['P30_max'];
$settings[$row['P30']]['min'] = $row['P30_min'];

$settings[$row['P32']]['max'] = $row['P32_max'];
$settings[$row['P32']]['min'] = $row['P32_min'];

$settings[$row['P33']]['max'] = $row['P33_max'];
$settings[$row['P33']]['min'] = $row['P33_min'];

$settings[$row['P35']]['max'] = $row['P35_max'];
$settings[$row['P35']]['min'] = $row['P35_min'];

$settings[$row['P35M']]['max'] = $row['P35M_max'];
$settings[$row['P35M']]['min'] = $row['P35M_min'];

$settings[$row['P35W']]['max'] = $row['P35W_max'];
$settings[$row['P35W']]['min'] = $row['P35W_min'];

$settings[$row['P38']]['max'] = $row['P38_max'];
$settings[$row['P38']]['min'] = $row['P38_min'];

$settings[$row['P41']]['max'] = $row['P41_max'];
$settings[$row['P41']]['min'] = $row['P41_min'];

$settings[$row['P42']]['max'] = $row['P42_max'];
$settings[$row['P42']]['min'] = $row['P42_min'];

$settings[$row['P43']]['max'] = $row['P43_max'];
$settings[$row['P43']]['min'] = $row['P43_min'];

$settings[$row['P46']]['max'] = $row['P46_max'];
$settings[$row['P46']]['min'] = $row['P46_min'];

$settings[$row['P47']]['max'] = $row['P47_max'];
$settings[$row['P47']]['min'] = $row['P47_min'];

$settings[$row['P28']]['max'] = $row['P28_max'];
$settings[$row['P28']]['min'] = $row['P28_min'];

$settings[$row['P28']]['max'] = $row['P28_max'];
$settings[$row['P28']]['min'] = $row['P28_min'];

$settings[$row['P28']]['max'] = $row['P28_max'];
$settings[$row['P28']]['min'] = $row['P28_min'];

$settings[$row['P28']]['max'] = $row['P28_max'];
$settings[$row['P28']]['min'] = $row['P28_min'];  

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.