Solved

Help with If statements

Posted on 2001-09-18
5
187 Views
Last Modified: 2008-03-06
Okay, I created a form to search for makes and models of cars.  When I submit that form, I created a php page that will process it and show the results.

Here is what I have done so far and I get a parse error :(

<?
if ($submit == "Search Inventory")
{
/* This is where I start to have problems...The user
    can either select a Make or leave it at the default
    -- select all --.  They can also select a Make and
    leave the model at the default --select all--.
    I was trying to see if both were set then use the
    values from those select boxes in the query.  If one
    had a value and the other one had the select all  
    option then the query is as follows.  */

if (isset($Make) && isset($Model))
{
$query = "select * from tblcarinventory where Make='$Make' and Model='$Model' order by Make asc, Model asc ";
}

if (isset($Make) && $Model ="-- select all -- "))
{
$query = "select * from tblcarinventory where Make='$Make' order by Make asc, Model asc ";
}


$result = safe_query($query);
if ($result)
{
     while ($row = mysql_fetch_array($result))

     {
     print "<table border=2 width=55% bordercolor=black>";
     print "<tr>";
     print "<td width=40%><b>New?:</b></td>";
     print "<td width=60%>";
     print $row["NeworUsed"];
     print "</td>";
      print "</tr>";
     print "<tr>";
     print "<td width=40%><b>VIN #:</b></td>";
     print "<td width=60%>";
     print $row["VIN_Number"];
     print "</td>";
     print "</tr>";
     print "<tr>";
     print "<td width=40%><b>Year:</b></td>";
     print "<td width=60%>";
     print $row["CarYear"];
     print "</td>";
     print "</tr>";
     print "<tr>";
     print "<td width=40%><b>Make:</b></td>";
     print "<td width=60%>";
     print $row["Make"];
     print "</td>";
     print "</tr>";
     print "<tr>";
     print "<td width=40%><b>Model</b></td>";
     print "<td width=60%>";
     print $row["Model"];
     print "</td>";
     print "</tr>";
     print "<tr>";
     print "<td width=40%><b>Exterior Color:</b></td>";
     print "<td width=60%>";
     print $row["ExtColor"];
     print "</td>";
     print "</tr>";
     print "<tr>";
     print "<td width=40%><b>Interior Color:</b></td>";
     print "<td width=60%>";
     print $row["IntColor"];
     print "</td>";
     print "</tr>";
     print "<tr>";
     print "<td width=40%><b>Miles:</b></td>";
     print "<td width=60%>";
     print number_format($row["Miles"]);
     print "</td>";
     print "</tr>";
     print "<tr>";
     print "<td width=40%><b>Price:</b></td>";
     print "<td width=60%>";
     print "$";
     print number_format($row["Price"]);
     print "</td>";
     print "</tr>";
     print "<tr>";
     print "<td width=40%><b>Equipment:</b></td>";
     print "<td width=60%>";
     print $row["Equipment"];
     print "</td>";
     print "</tr>";
     print "<br>\n";
     print "<br>\n";
     print "<br>\n";
     print "</table>";

     }
     mysql_free_result($result);
}


}
?>


Any help would be greatly appreciated.

Zippy21
0
Comment
Question by:zippy21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Expert Comment

by:heddesheimer
ID: 6492562
you forgot an opening brace in an if statement in line 21 (just check your error messages :-) )
it should read:

if (isset($Make) && ($Model ="-- select all -- "))

hope that helps

Marian
0
 
LVL 8

Expert Comment

by:us111
ID: 6492647
I would suggest to convert

while ($row = mysql_fetch_array($result))
 {
    print "<table border=2 width=55% bordercolor=black>";
    print "<tr>";
    print "<td width=40%><b>New?:</b></td>";
    print "<td width=60%>";

into

while ($row = mysql_fetch_array($result))
 {
?>
 <table border=2 width=55% bordercolor=black>
  <tr>
   <td width=40%><b>New?:</b></td>
   <td width=60%><?print $row["NeworUsed"];?>
...
...
...

it's more convenient....and it will avoid (a little bit) to get some parse errors
0
 
LVL 8

Expert Comment

by:us111
ID: 6492651
and you forgot a =:

if (isset($Make) && ($Model = = "-- select all -- "))
0
 
LVL 8

Accepted Solution

by:
us111 earned 50 total points
ID: 6492655
an advice:
if (isset($Make) && $Model ="-- select all -- "))
{
  $query = "select * from tblcarinventory where Make='$Make' order by Make asc, Model asc ";
}

if you have  only one line after the if, don't use { and }

if (isset($Make) && $Model ="-- select all -- "))
       $query = "select * from tblcarinventory where Make='$Make' order by Make asc, Model asc ";

is the same

The less you have some {}, the less you will have some parse errors.
0
 
LVL 1

Expert Comment

by:sweatje
ID: 6496390
I have seen it recommented elsewhere that you do all of your if comparisons as "-- selectt all --" == $Model.  That way if you accidentally forget the second =, it will result in a script error rather than the unintended consequence of a variable assignment.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question