Log in

View Full Version : Problem IF Statements



NXArmada
04-27-2006, 05:46 PM
I have 2 IF statements and they dont seem to work as they are told.



<?
if ($myrow["svc"] = "%RMA-%") {
echo '<div class="tmodule">';
echo '<h3>Reason for return:</h3>';
echo '<div>';
echo '',$myrowr["reason"],'';
echo '</div>';
echo '</div>';
echo '<div class="tmodule">';
echo '<h3>Special Instructions or Notes:</h3>';
echo '<div>';
echo '',$myrowr["notes"],'';
echo '</div>';
echo '</div>';
}
else {}
?>
<?
if ($myrow["svc"] = "%T-%") {
echo '<div class="tmodule">';
echo '<h3>Special Instructions or Notes:</h3>';
echo '<div>';
echo '',$myrowt["notes"],'';
echo '</div>';
echo '</div>';
}
else {}
?>


When an RMA record is selected it should only show the RMA statement and not the trade and same with a trade record. But when i selected an RMA record the Trade statement shows up also same with a trade record the rma show up also. No data is in the divs but the div box's show up.

Twey
04-27-2006, 05:56 PM
= is an assignment operator, and will always return the value on its right-hand side. == is a comparison operator, and returns either true or false depending on whether or not the left- and right-hand operands are equal. Do not confuse them. If you are prone to doing so, put the absolute value (where possible) on the left-hand side, so that PHP throws an "operation not supported" error rather than continuing to execute your code in a manner you didn't intend.


echo '',$myrowt["notes"],''; What's the point of those empty quotes? There's no need for them to be there.

NXArmada
04-27-2006, 06:01 PM
Habbit lol and replaced = with == and now nothing shows up.

Twey
04-27-2006, 06:04 PM
Because, I daresay, neither of your conditions were met.

Oh, the empty else clauses are rather pointless too.

NXArmada
04-27-2006, 06:17 PM
I know the conditions should be met theres tons of RMA and T records in there. I just want it to check and see of the SVC is an RMA or a T and display the correct DIV's



<?
if ($myrow["svc"] == "RMA-%") {
echo '<div class="tmodule">';
echo '<h3>Reason for return:</h3>';
echo '<div>';
echo $myrowr["reason"];
echo '</div>';
echo '</div>';
echo '<div class="tmodule">';
echo '<h3>Special Instructions or Notes:</h3>';
echo '<div>';
echo $myrowr["notes"];
echo '</div>';
echo '</div>';
}
?>
<?
if ($myrow["svc"] == "T-%") {
echo '<div class="tmodule">';
echo '<h3>Special Instructions or Notes:</h3>';
echo '<div>';
echo $myrowt["notes"];
echo '</div>';
echo '</div>';
}
?>

mwinter
04-27-2006, 06:17 PM
if ($myrow["svc"] = "%RMA-%") {To continue what Twey wrote, you seem to be trying to apply pattern matching in SQL to PHP, which doesn't work.

If you want to check if a substring exists within some other string, use the strpos (http://uk.php.net/strpos) function:



if (strpos($myrow['svc'], 'RMA-') != -1) {
/* 'RMA-' exists somewhere within the string. */
}
If you want to check that the string starts with a particular substring, you can use the following function.



function beginsWith($string, $prefix) {
return $prefix === substr($string, 0, strlen($prefix));
}

if (beginsWith($myrow['svc'], 'RMA-')) {
/* ... */
}
Mike

NXArmada
04-27-2006, 06:24 PM
the strpos bring me back to my original problem

and the beginsWith give me function errors

NXArmada
04-27-2006, 06:30 PM
I got the Begins with to work stupid me lol thanks for the awsome help guys.