Solved

Help with If statements

Posted on 2001-09-18
5
185 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

739 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