Jump to content

Archived

This topic is now archived and is closed to further replies.

AdRock

will this if/elseif/else statement work? [RESOLVED]

Recommended Posts

I am trying to make 3 comparisons and depending on what text fields have in them dpenedswhich SQL update to use.  But will this code work?

//if $event and $row['eventdate'] are the same and $pic and $row['photo'] are the same don't update the database and change their values but update the rest

    if (strcmp( $event,$row['eventdate'] ) =0) && (strcmp( $pic,$row['photo'] ) =0) {
        $query="UPDATE events SET title='$name', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

//if $event and $row['eventdate'] don't match and $pic and $row['photo'] are the same don't update $row['photo'] but update the rest

    elseif (strcmp( $event,$row['eventdate'] ) !=0) && (strcmp( $pic,$row['photo'] ) =0) {
        $query="UPDATE events SET title='$name', eventdate='$event', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

//if $event and $row['eventdate'] are the same and $pic and $row['photo'] don't match don't update $row['eventdate'] but update the rest

    else (strcmp( $event,$row['eventdate'] ) =0) && (strcmp( $pic,$row['photo'] ) !=0) {
        $query="UPDATE events SET title='$name', photo='$pic', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

Share this post


Link to post
Share on other sites
the else at the end cant have a condiition because it is used for any other condition, the guys will help u more so keep visiting ur post

Share this post


Link to post
Share on other sites
$event,$row this dosent look right what are you trying to do is event an object in for its $event->row

Share this post


Link to post
Share on other sites
example 1
elseif is the same as else if ok.
[code]
<?php

if(condition){

elseif(condition){

else if(condition){

}else{
(condition);
}
?>
[/code]

example 2
[code]
<?php

if(condition){

do somethink

}else{

not done

}
?>
[/code]


example 3

[code]
<?php

if(conditon){

if(condition){

do some think if the two condition true

}
}else{

if failed

}

?>
[/code]

Share this post


Link to post
Share on other sites
try this

[code]<?php
    if (strcmp($event,$row['eventdate']) == 0 && strcmp($pic,$row['photo']) == 0) {
        $query="UPDATE events SET title='$name', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

    else if (strcmp($event,$row['eventdate']) !=0 && strcmp($pic,$row['photo']) == 0) {
        $query="UPDATE events SET title='$name', eventdate='$event', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

    else if(strcmp($event,$row['eventdate']) == 0 && strcmp($pic,$row['photo']) !=0) {
        $query="UPDATE events SET title='$name', photo='$pic', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }
else (){
}
?>[/code]

Share this post


Link to post
Share on other sites
please also read this ok.

[code]

It's definitely worth noting that the return-values of strcmp() when used for i.e. password-checking is the oposite of that of the ==-operator.
<?php

// I.e.:

$pw1 = "yeah";
$pw2 = "yeah";

if (strcmp($pw1, $pw2)) {  // This returns false.
  // $pw1 and $pw2 are NOT the same.
} else {
  // $pw1 and $pw2 are the same.
}

//Where the use of the == operator would give us.:
if ($pw1==$pw2) {    // This returns true.
  // $pw1 and $pw2 are the same.
} else {
  // $pw1 and $pw2 are NOT the same.
}

//Additionally, to check if $pw1 and $pw2 are of the same type you can use the === operator.

<?

[/code]

Share this post


Link to post
Share on other sites
my version
[code]
<?php
    if ((strcmp($event,$row['eventdate']=='0'))&&(strcmp($pic,$row['photo']) == '0'))) {
        $query="UPDATE events SET title='$name', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

    else if ((strcmp($event,$row['eventdate'] !='0')) && (strcmp($pic,$row['photo']) == '0'))) {
        $query="UPDATE events SET title='$name', eventdate='$event', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

    else if((strcmp($event,$row['eventdate'] == '0' ))&& (strcmp($pic,$row['photo']) !='0'))) {
        $query="UPDATE events SET title='$name', photo='$pic', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }
else (){
}
?>

[/code]

Share this post


Link to post
Share on other sites
Thanks guys I managed to get it all working exactly how I want.  it's nice to see people help so quickly and I do try and help others when i can  :D

Share this post


Link to post
Share on other sites

×

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.