0

I am trying to write a check for Vendors and models in a form I have put together

Here is the relavent part of the form

<table  width="75%">
<form action="?" method="post">
<tr>
<td colspan="6" align="center">
<font size="5"><b>Add new record</b></font>
</td>
</tr>
<tr>
<td align="right">
<b>Vendor:</b>
</td>
<td>
<select name="vendor">
<option value="ClearAccess">ClearAccess</option>
<option value="VisionNet">VisionNet</option>
<option value="Other">Other</option>
</select>
</td>
<td align="right">
<b>Model:</b>
</td>
<td>
<select name="model">
<option value="NotModel">--ClearAccess--</option>
<option value="AG10W">AG10W</option>
<option value="SR100G">SR100G</option>
<option value="SR300N">SR300N</option>
<option value="SR350N">SR350N</option>
<option value="SR500N">SR500N</option>
<option value="NotModel">--VisionNet--</option>
<option value="M404">M404</option>
<option value="M405">M405</option>
<option value="M505">M505</option>
<option value="M505n">M505N</option>
<option value="Legacy">Legacy</option>
</select>
</td>

my form is good. my issue is when I am building my if loop. Here is what I have so far

if ($_POST['vendor']='ClearAccess') && ($_POST['model']='M404') || ($_POST['model']='M405') || ($_POST['model']='M505') || (_$POST['model']='M505N') || ($_POST['model']='Legacy')
echo "You have not selected a valid vendor/model combination"

I am having an issue with where and how to place my parenthesis.

I am also going to create an if statement similar to this

if ($_POST['vendor']='VisionNet') && ($_POST['model']='AG10W') || ($_POST['model']='SR100G') || ($_POST['model']='SR300N') || (_$POST['model']='SR350N') || ($_POST['model']='SR500N')|| ($_POST['model']='Legacy')
echo "You have not selected a valid vendor/model combination"

I am completely open to doing it different ways. Eventually I will turn this into java script where you pick a vendor and get only the relavant models for that vendor. Put as I am still learning PHP and have not learned even the basics of Java yet I want to try it this way.

Alan Haggai Alavi
  • 67,398
  • 19
  • 96
  • 124
radikaos
  • 1
  • 1
  • 2

7 Answers7

4

Reduce your logic. Start with what you want to do in general statements.

A = "Vendor is VisionNet"
B = "Model is AG10W"
C = "Model is SR100G"
etc.

Then write up your logic as

if (A and (B or C))

OR

if ((A and B) or C)

etc.

Then replace your place-holders with the PHP equivalent.

In PHP, if you're trying to see if x is one of many values, you can use the in_array() method:

$models = array('AG10W', 'SR100G', 'SR300N', 'SR350N', 'SR500N', 'Legacy');

// if the vendor is vision net OR the model is in the list of models defined in $models, do something
if ($_POST['vendor'] == 'VisionNet' || in_array($_POST['model'], $models)) {
    // do something.
}

Take operators into consideration as well. Assignment is different than comparison.

Remember, logic is just another subset of mathematics. Parentheses are important, so if you close your if statement early, that's the end of it. Everything after will become a syntax error.

I mention that because you wrote this:

if ($_POST['vendor'] = 'VisionNet') || ...

Here's a link to a tutorial on discrete mathematics which is a fancy way of saying "the mathematics of logic".

Also, for all those interested, MIT (the Massachusetts Institute of Technology) offers open courses online for free, one of which is the mathematics of computer science which discusses discrete math in-depth.

Matt
  • 6,745
  • 4
  • 24
  • 49
  • 1
    Couldn't write better than this. – Oussama Jilal Aug 03 '12 at 18:15
  • @Yazmat I tried making it as explicit as possible. The issue was not necessarily PHP, but a lack of knowledge about how logic works. OP needs a discrete mathematics course. – Matt Aug 03 '12 at 18:29
1

Here are your problems :

  1. Using = instead of ==
  2. Not placing () in the if statement

change from

if ($_POST['vendor']='ClearAccess') && ($_POST['model']='M404') || ($_POST['model']='M405') || ($_POST['model']='M505') || (_$POST['model']='M505N') || ($_POST['model']='Legacy')

to :

if (($_POST['vendor']=='ClearAccess') && ($_POST['model']=='M404') || ($_POST['model']=='M405') || ($_POST['model']=='M505') || (_$POST['model']=='M505N') || ($_POST['model']=='Legacy')) 
mlishn
  • 1,665
  • 14
  • 19
1

First off, you need to use === instead of =. The single equals means assignment, not comparing.

Secondly, what I think you're trying to do is say "if it's this vendor AND one of these models, do something. If that's the case, try this:

   if($_POST['vendor'] === 'ClearAccess' && (*various models, using ||*))
       //dostuff

That way, it will equate to true if the vendor is equal as well as at least one of the models.

A better way to do this would be to build up an array of the various models and then use PHP's in_array()

SomeKittens
  • 35,809
  • 19
  • 104
  • 135
  • It's a best practice. `==` will do type conversion, which can cause weird errors. http://stackoverflow.com/questions/80646/how-do-the-equality-double-equals-and-identity-triple-equals-comparis – SomeKittens Aug 03 '12 at 18:17
  • @mlishn: Because some people don't like PHP's type juggling. They contend that it's good practice to always use `===` unless you need PHP's type coercion magic (though they also like to think you *never* need it, and thus that you should never be using `==`). – cHao Aug 03 '12 at 18:19
  • @cHao so when using `===` do the two variables need to explicity defined with a variable type, or does PHP assume based on format? So something like `(string) $string="string";` vs. `$string="string";` – mlishn Aug 03 '12 at 18:24
  • 1
    @mlishn: They don't have to be defined with a type; in PHP, the type is on the value, not the variable, and the type of a literal is pretty obvious. But if two values are not the same type, `===` says they're never equal. So if you expect them to be different types, you'd either cast them to the same type or use `==`. – cHao Aug 03 '12 at 18:28
0

You must use == instead = in your if conditions.

So instead of:

if ($_POST['vendor']='ClearAccess') && ...

use

if ($_POST['vendor'] == 'ClearAccess') && ...

Better to have your if condition like this:

if ($_POST['vendor'] == 'ClearAccess') &&
    str_replace(array('M404', 'M405', 'M505', 'M505N', 'Legacy'), '', $_POST['model']) 
         != $_POST['model']
   )
   echo "You have not selected a valid vendor/model combination"
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • That's true, but the least of OP's problem. – Matt Aug 03 '12 at 18:10
  • 1
    @Matt: I dunno about "the least of OP's problem"; it's a pretty big reason for the failure. The "conditions" would always return true, no matter what the original values were, because everything gets set to a truthy value (which gets returned as the value of that expression). So even if the ||s and &&s and parentheses were right, the condition would still be very broken. – cHao Aug 03 '12 at 18:14
0

You should look at the php syntax for if then statements http://php.net/manual/en/control-structures.if.php because your syntax is wrong. You are assigning your post variables to values other than using ==

if ($_POST['vendor']=='VisionNet')
Also, you should rethink your $$ and || usage and decide what that is actually saying in your program.

jawerty
  • 145
  • 2
  • 9
0

Wrap your && within its own set of parenthesis. So the example below says if POST[vendor] is Clear Acces and POST[model] is M404, or POST[model] is M405, M505, M505N, or Legacy.

if (($_POST['vendor']=='ClearAccess') && ($_POST['model']=='M404')) || ($_POST['model']=='M405') || ($_POST['model']=='M505') || (_$POST['model']=='M505N') || ($_POST['model']=='Legacy') 
echo "You have not selected a valid vendor/model combination"
WhoaItsAFactorial
  • 3,468
  • 4
  • 26
  • 44
0

You need to put your ()'s right...

Just to make you aware of the syntax, here is the code with nice line breaks :)

if (
($_POST['vendor'] == 'ClearAccess' && $_POST['model'] = 'M404') || 
$_POST['model'] == 'M405' || 
$_POST['model'] == 'M505' || 
$_POST['model'] == 'M505N' ||
$_POST['model'] == 'Legacy'
) {
echo "You have not selected a valid vendor/model combination"
}

And without the linebreaks:

if (($_POST['vendor'] == 'ClearAccess' && $_POST['model'] == 'M404') || $_POST['model'] == 'M405' ||  $_POST['model'] == 'M505' || $_POST['model'] == 'M505N' || $_POST['model'] == 'Legacy') {
echo "You have not selected a valid vendor/model combination"
}

Your other statement goes the same way.

EibergDK
  • 534
  • 1
  • 6
  • 19