Solved

Help with If statements

Posted on 2001-09-18
5
181 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
  • 3
5 Comments
 
LVL 4

Expert Comment

by:heddesheimer
Comment Utility
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
Comment Utility
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
Comment Utility
and you forgot a =:

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

Accepted Solution

by:
us111 earned 50 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now