Forum Moderators: coopster
1. id must be entered, otherwise they are told to enter an id
2. session id must match the user id associated with the db entry
3. 'status' column in the db must have a certain text.
All need to be matched or they will not be able to view the page but instead shown an error page informing them how to correct it based on the problem.
Even while I admit using them in my PHP code, it is my opinion that it's bad form. My best excuse is that I have not mastered the semantics of PHP. I've seen nested if conditions bring perfectly good programmers to their knees; ie., I once had the pleasure of re-writing a large program that consisted of an if-else condition, then each of those conditions were followed with hundreds of if-if-else-if-else-while-if type tests. It was a nightmare to maintain, and not much better to have to re-write.
<My 2 cents>
Use them sparingly and only as a last recourse. Your code will be much better for it, then one day you'll be happier, make more money, and live your retirement fantasy.
So, here you go...
1. id must be entered, otherwise they are told to enter an id
2. session id must match the user id associated with the db entry
3. 'status' column in the db must have a certain text.
if ($id!= NULL)
{
if ($sessid == $dbsess)
{
if ($status == $db[column])
{
// all three conditions are met
// show them the screen
} //endif $status
else
{
// no match in the db for $status
}
} // endif $sessid
else
{
// did not find the right session id
}
} // endif $id
else
{
echo "Gimme your ID";
}
I think that's right. I added the else conditions.
1. id must be entered, otherwise they are told to enter an id
2. session id must match the user id associated with the db entry
3. 'status' column in the db must have a certain text.
if(!$id){$err[]="Er, id?";}
if(!$session_id==$user_id){$err[]="Tut, tut, session?";}
if(!$status!="fish and chips"){$err[]="I'm hungry";}
if(sizeof($err)){
// do error stuff.. print_r($err)
}
else{
// away you go
}
if (Condition1 && Condition2 && Condition3)
{
execute your code
}
else //This would be the error handling
{
if (Condition1 == false)
{
echo"There was an error with condition 1";
}
if (Condition2 == false)
{
echo"There was an error with condition 2";
}
if (Condition3 == false)
{
echo"There was an error with condition 3";
}
}
Here's the code currently
echo "$rbname<br>$rbimage<br>$rbdescription<br>$rbpoints<br>";
if($rab > $rb) {
echo "You have enough";
} else {
echo "You dont have enough";
}
==========================================
What I would like to do if put it inside of this
if($earmark > 0) {
echo "You have enough points to get this item";
} else {
echo "You still need to earn $rbtotal points to get this item";
}
==========================================
under the "if($earmark > 0) {" section
if $rab is greater than $rb then they have enough points
if $rab is not greater than $rb but $earmark is greater than 0 they also have enough points
if none of the above case are met then they do not have enough points
is that the logic you are looking for?
What I want to do is have it like this
if($earmark = 0) {
echo "<br>";
} else {
then it needs to show a table which is coded as follows
echo "$rbname<br>$rbimage<br>$rbdescription<br>$rbpoints<br>";
if($rab > $rb) {
echo "You have enough";
} else {
echo "You dont have enough";
}
I just am befuddled on how I could include the second if (if($rab > $rb) { ) without confusing it with the first.
the way I often do it is to write it out first, then code it later. I know, pencil and paper
Luddite!
Grandpa, there's nothing wrong with nesting if statements and it's usually necessary to do anything at all. Sometimes it makes things clearer than having a lengthy list of conditions in if-elseif-else blocks.
One little trick to keep in mind though, is that if your ifs are in function...
instead of
if (conditiona){
do a
}
elseif (conditionb) {
do b
}
else (conditionc) {
do c
}
it is sometimes clearer to do
if (conditiona){
return a
}
if (conditionb) {
return b
}
return c