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 7 of 7
  1. #1
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts

    Logical Operator Problem (if... elseif... else...)

    Ok, my problem is that the way I have it checking to display makes it really difficult to understand. I have a series of elseifs to update where needed.

    What I need to do:

    1. Successfully use || to do 1 if without interfering with the other elseifs, ifs, or elses.
    2. Not lose anything XD. (Done twice now).


    PHP Code:
    <?php
    {
     
    if(
    $newimage " " && !$about && !$interests && !$services && !portfolio
     {
    mysql_query$update1 );
    echo 
    "You have edited your profile picture. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }



     
    elseif(
    $about " "&& !$newimage && !$interests && !$services && !portfolio)
     {
    mysql_query$update2 );
    echo 
    "You have edited your 'About Me' section. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }



     
    elseif(
    $interests " " && !$newimage && !$about && !$services && !portfolio)
     {
    mysql_query$update3 );
    echo 
    "You have edited your 'My Interests' section. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }




     
    elseif(
    $services " " && !$newimage && !$interests && !$about && !portfolio)
     {
    mysql_query$update4 );
    echo 
    "You have edited your 'My Services' section. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }


     
    elseif(
    $portfolio  " "&& !$newimage && !$interests && !$services && !$about)
     {
    mysql_query$update5 );
    echo 
    "You have edited your 'My Portfolio' section. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }



     
    elseif(!
    $id)
     {
    echo 
    "Do not try that! Please follow the procedure to properly edit your profile. If you feel you have found this page in error, please let us know using the contact form.";
     }
    };
    ?>
    Each if, and elseif (except the last with !$id), are its seperate form on the update page.

    Basically when you change newimage, you click a button to change just that, which sends that data to this page. if you change about, it just updates that, and you click the button for that form. I have 5 different forms on the previous page.

    My issue is, if the field is blank, and I put if($newimage || $newimage = " ") then if the field isn't filled when you submit something in the 'about' section, then it will update $newimage, and not $about.


    Not Relevant: I'm trying to figure out how to mass change everything, but I'd actually rather do it this way, cause soon I'm moving it to javascript to do side server script editing for each section, instead of having new pages. (Much further down the road, but having it ready in sections like this will make it quicker to switch.)


    Thanks in advance if you can help.


    Edit: It really is a logical issue. Not much of a problem with the PHP, just a "How would I work this" situation.
    Last edited by Deacon Frost; 03-15-2008 at 12:33 PM.

  • #2
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    First I think you may have problems with using = instead of == on your if statements, that has really screwed me up in the past, so check that out first.

    As you are using a form it maybe easier to use a case statement. What you ideally need is some sort of ID that tells your submit page which section to process.

    Without seeing the rest of your code, I cant help you much further

  • #3
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Here's the forms: (I use tables, shush)

    PHP Code:
    <table width="75%">
    <tr>
    <td>
    <h2>Current Image:</h2>
    <tr>
    <td>
    <form action="http://www.chrysys.net/confirmupdate.php?id=<? echo $id?>" method="post">
    <input type="text" size="22" name="newimage" maxlength="255" value="<? echo $face ?>" />
    <tr><td>
    <img src="<? echo $face ?>" width="150" height="200" align="left" />
    <tr><td>
    <input type="submit" value="Change Image" />
    </form>
    </td>
    </tr>
    </table>


    <table width="75%">
    <tr>
    <td>
    <h2>About Me</h2>
    <form action="http://www.chrysys.net/confirmupdate.php?id=<? echo $id?>" method="post">
    <textarea name="about" maxlength="1000" rows="10" cols="50">
    <? echo $about?>
    </textarea>
    <input type="submit" value="Update 'About Me'" />
    </form>
    </td>
    </tr>
    </table>

    <br /><br />

    <table width="75%">
    <tr>
    <td>
    <h2>My Interests</h2>
    <form action="http://www.chrysys.net/confirmupdate.php?id=<? echo $id?>" method="post">
    <textarea name="interests" maxlength="1000" rows="10" cols="50">
    <?php echo $interests?>
    </textarea>
    <input type="submit" value="Update 'My Interests'" />
    </form>

    </td>
    </tr>
    </table>


    <br /><br />

    <table width="75%">
    <tr>
    <td>
    <h2>My Services</h2>
    <form action="http://www.chrysys.net/confirmupdate.php?id=<? echo $id?>" method="post">
    <textarea name="services" maxlength="1000" rows="10" cols="50">
    <?php echo $services?>
    </textarea>
    <input type="submit" value="Update 'My Services'" />
    </form>

    </td>
    </tr>
    </table>


    <br /><br />

    <table width="75%">
    <tr>
    <td>
    <h2>My Portfolio</h2>
    <form action="http://www.chrysys.net/confirmupdate.php?id=<? echo $id?>" method="post">
    <textarea name="portfolio" maxlength="1000" rows="10" cols="50">
    <?php echo $portfolio?>
    </textarea>
    <input type="submit" value="Update 'My Portfolio'" />
    </form>

    </td>
    </tr>
    </table>
    and here's the pull on the recieving page:

    PHP Code:
    $id $_GET['id'];
    $newimage $_POST['newimage'];
    $about $_POST['about'];
    $interests $_POST['interests'];
    $services $_POST['services'];
    $portfolio $_POST['portfolio'];

    $update1 "UPDATE `deacon_profiles`.`profile` SET `Picture` = '".$newimage."' WHERE `profile`.`ID` = '".$id."' LIMIT 1";
    $update2 "UPDATE `deacon_profiles`.`profile` SET `About` ='".$about."' WHERE `profile`.`ID` ='".$id."' LIMIT 1";
    $update3 "UPDATE `deacon_profiles`.`profile` SET `Interests` ='".$interests."' WHERE `profile`.`ID` ='".$id."' LIMIT 1";
    $update4 "UPDATE `deacon_profiles`.`profile` SET `Services` ='".$services."' WHERE `profile`.`ID` ='".$id."' LIMIT 1";
    $update5 "UPDATE `deacon_profiles`.`profile` SET `Portfolio` ='".$portfolio."' WHERE `profile`.`ID` ='".$id."' LIMIT 1";



    ?> 

    I tried changing between = and == and ===, but none worked properly.


    Like I said, the main problem here is how to work it XD. I'll look into learning the case statements, because from every angle I've looked, it seems impossible to do it this way.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code:
    if newimage{
      update it
      show the message
    }
    
    if about{
     update it 
     show the message
    }
    I think that should work.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Yes, that works, but my problem being, I need to have a way that if it's empty, it doesn't update unless you TELL it to update.

    If I did:

    PHP Code:
    if ($newimage || $newimage =" ")
    {
     
    // update it
     // show the message
    }

    if (
    $about || $about " ")
    {
     
    // update it 
     // show the message

    The problem with that would be, that say you have an empty newimage, and then you click to edit about, what's going to happen, is it's going to update newimage, and not about. .


    The problem with that is, if you do the way I did it, it'll delete data because it reads it as not existing because of the ! operators I have installed to try and stop it from doing that code if the other codes are being submitted.


    EDIT:

    So I tried it, and it deleted all the data I had EXCEPT for on the form which I clicked.

    PHP Code:
    <?php

     
    if($newimage || $newimage " "
     {
    mysql_query$update1 );
    echo 
    "You have edited your profile picture. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }



     
    if(
    $about || $about " ")
     {
    mysql_query$update2 );
    echo 
    "You have edited your 'About Me' section. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }



     
    if(
    $interests || $interests " ")
     {
    mysql_query$update3 );
    echo 
    "You have edited your 'My Interests' section. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }




     
    if(
    $services || $services " ")
     {
    mysql_query$update4 );
    echo 
    "You have edited your 'My Services' section. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }


     
    if(
    $portfolio || $portfolio " ")
     {
    mysql_query$update5 );
    echo 
    "You have edited your 'My Portfolio' section. <br /> Return to your <a href='http://www.chrysys.net/portfolios.php?id=".$id."'>profile</a>.";
     }



     
    if(!
    $id)
     {
    echo 
    "Do not try that! Please follow the procedure to properly edit your profile. If you feel you have found this page in error, please let us know using the contact form.";
     }
    ?>



    So what I did is edited the = to ==, and it kind of worked, except it put me back at square one. What happens now, is when I delete data, it won't update it if I empty the field.
    Last edited by Deacon Frost; 03-15-2008 at 02:00 PM.

  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    Try changing the condition on your ifs to use != "". So:

    Code:
    if($newimage != "" && !$about && !$interests && !$services && !portfolio)
    That would mean that if $newimage is NOT empty, which is what you want I presume. At the moment you have your code to if the $newimage is equal to " " which it will never be if somebody has filled the form in.

    Also you may want to consider having just 1 update button, and 1 form that updates the entire page rather than 1 form for each update. Alternatively you could get up another $_GET and set the link to something like.

    Code:
    <form action="http://www.chrysys.net/confirmupdate.php?id=<? echo $id; ?>&update=picture" method="post">

  • #7
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Well, what the problem would have been was that if it was empty, what it would do is delete everything else, and update that field.

    It's a profile. so people edit when they choose, and can have empty fields, the problem with php verification of empty fields is, if they have an empty field, normally you don't want them, so there's either a really long process, or not a way to allow it at all.

    I'd want it to be if it is this, or this, then it'll change (if it has data, or if it is empty).

    The problem with that though, if it is empty, and another field has data that you want to save, what'll happen is it'll catch that empty field if, because it is empty, and it will update that only.


    Anyways, no worries, I just switched to one button for overall editing :P. Much quicker and easier for now.



    (if anyone CAN solve what I'm meaning, then I ask you to share, it still racks my brain).


  •  

    Posting Permissions

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