...

View Full Version : Logical Operator Problem (if... elseif... else...)



Deacon Frost
03-15-2008, 11:18 AM
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
{

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.

InsaneRhino
03-15-2008, 12:30 PM
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 :(

Deacon Frost
03-15-2008, 12:36 PM
Here's the forms: (I use tables, shush)



<table width="75&#37;">
<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:



$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.

abduraooft
03-15-2008, 12:46 PM
if newimage{
update it
show the message
}

if about{
update it
show the message
} I think that should work.

Deacon Frost
03-15-2008, 12:52 PM
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:



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. :p.


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


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.

InsaneRhino
03-15-2008, 01:02 PM
Try changing the condition on your ifs to use != "". So:


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.


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

Deacon Frost
03-15-2008, 01:22 PM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum