943,913 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 623
  • PHP RSS
May 23rd, 2009
0

if statement /mysql help please

Expand Post »
ok so this is what im trying to do. this is a products page and the products diplay but i want to create a filter so if they want to view only the in this case tvs that are sony they use the from that is in the coding to select what brand and then press submit and the form will send to the same page and the if statment will look for the one that states if variable(brand) > 0 then use the following if statment but i keep getting this problem

Parse error: syntax error, unexpected T_VARIABLE in /home/content/u/v/c/uvc2008/html/tv.php on line 21

help me please i'm confused

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $dbHost = "UVCvoldb.db.3892101.hostedresource.com";
  3. $dbUser = "UVCvoldb";
  4. $dbPass = "UVCvol2008";
  5. $dbDatabase = "UVCvoldb";
  6.  
  7. $Type=($_GET['Type']);
  8. $Brand=($_GET['Brand']);
  9. $Size=($_GET['Size']);
  10. $Price=($_GET['Price']);
  11. $Model=($_GET['Model']);
  12.  
  13. $db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
  14.  
  15.  
  16.  
  17. mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
  18.  
  19. if ( (strlen($Type) > 0) && (strlen($Brand) > 0 ) && (strlen($Size) > 0 ) && (strlen($Price) > 0 ) )
  20. {
  21. $sql= "select * from PRODUCTS where Type="$Type" and Brand="$Brand" and Size="$Size" and Price="$Price" order by ProductID");
  22. }
  23.  
  24. elseif ( (strlen($Type) > 0) && (strlen($Brand) > 0 ) && (strlen($Size) > 0 ) )
  25. {
  26. $sql= "select * from PRODUCTS where Type="$Type" and Brand="$Brand" and Size="$Size" order by ProductID");
  27. }
  28.  
  29. elseif ( (strlen($Type) > 0) && (strlen($Brand) > 0 ) )
  30. {
  31. $sql= "select * from PRODUCTS where Type="$Type" and Brand="$Brand" order by ProductID");
  32. }
  33.  
  34. elseif ( (strlen($Type) > 0) )
  35. {
  36. $sql= "select * from PRODUCTS where Type="$Type" order by ProductID");
  37. }
  38.  
  39. elseif ( (strlen($Brand) > 0) )
  40. {
  41. $sql= "select * from PRODUCTS where Brand="$Brand" order by ProductID");
  42. }
  43.  
  44. elseif ( (strlen($Size) > 0) )
  45. {
  46. $sql= "select * from PRODUCTS where Size="$Size" order by ProductID ");
  47. }
  48.  
  49. elseif ( (strlen($Price) > 0) )
  50. {
  51. $sql= "select * from PRODUCTS where Price="$Price" order by ProductID");
  52. }
  53.  
  54. elseif ( (strlen($Model) > 0) )
  55. {
  56. $sql= "select * from PRODUCTS where Model="$Model" order by ProductID");
  57. }
  58.  
  59. else
  60. {
  61. $sql= "select * from PRODUCTS";
  62. }
  63.  
  64.  
  65. $result=mysql_query("$sql");
  66.  
  67. //filter table start
  68. echo <<<END
  69.  
  70. <form action="products.php?Category=TV" meathod="GET">
  71.  
  72. <input type="hidden" value="TV" Name="Category">
  73.  
  74. <table width="969" border="1">
  75.  
  76. <tr>
  77.  
  78. <td>Type</td>
  79.  
  80. <td><select name="Type"><option selected="selected" value="">None</option>
  81.  
  82. <option Value="Flat Screen Plasma">Flat Screen Plasma</option>
  83.  
  84. <option Value="Flat Screen LCD"> Flat Screen LCD</option>
  85.  
  86. </select>
  87.  
  88. </td>
  89.  
  90. <td>Brand</td>
  91.  
  92. <td><select name="Brand"><option selected="selected" value="">None</option>
  93.  
  94. <option Value="Sony">Sony</option>
  95.  
  96. <option Value="LG"> LG</option>
  97.  
  98. </select>
  99.  
  100. </td>
  101.  
  102. <td>Size</td>
  103.  
  104. <td><select name="Size"><option selected="selected" value="">None</option>
  105.  
  106. <option Value="26">26</option>
  107.  
  108. <option Value="34"> 34</option>
  109.  
  110. </select>
  111.  
  112. </td>
  113.  
  114. <td>Price</td>
  115.  
  116. <td><select name="Price"><option selected="selected" value="">None</option>
  117.  
  118. <option Value="$200-$300">$200-$300</option>
  119.  
  120. <option Value="$300-$500"> $300-$500</option>
  121.  
  122. <option Value="$500-$1500"> $500-$1500</option>
  123.  
  124. </select>
  125.  
  126. </td>
  127.  
  128. <td>Model</td>
  129.  
  130. <td><input type="text" Name="Model"></td>
  131.  
  132. <td><input type="Submit" name="submit" value="submit"><input type="reset" name="reset" value="Clear"></td>
  133.  
  134. </tr>
  135.  
  136. </table>
  137.  
  138. </form>
  139.  
  140. END;
  141.  
  142. // filter table end
  143.  
  144.  
  145.  
  146. while($row = mysql_fetch_array( $result )) {
  147.  
  148. // displaying products
  149.  
  150. $Image=$row['Image'];
  151.  
  152. $Name=$row['Name'];
  153.  
  154. $ModelNum =$row['Model#'];
  155.  
  156. $ProductID=$row['ProductID'];
  157.  
  158. echo "<table width=969>";
  159.  
  160. echo "<tr><td rowspan=4 width=201>";
  161.  
  162. echo "<form target=_blank action=viewproduct.php method=post><input type=hidden value=$ProductID name=ProductID />";
  163.  
  164. echo "<input type=image src=$Image width=201 heigth=144> </form>";
  165.  
  166. echo "</td><td colspan=2>$Name</td></tr>";
  167.  
  168. echo "<tr><td>Model#:$ModelNum</td><td>Product ID: $ProductID</td></tr>";
  169.  
  170. echo "<tr><td colspan=2>If You are interested in this product click (I Want IT Button). REMEMBER only submite once, if you submit more then one time you will be discualified from this product.<form action=addmembid.php method=post><input type=hidden value=$ProductID name=ProductID /><input type=submit name=submit value='I Want It'></form></td></tr>";
  171.  
  172. echo "</table>";
  173.  
  174. echo "<hr width=969>";
  175.  
  176. }
  177.  
  178.  
  179.  
  180. ?>
  181.  
  182. </div>
  183.  
  184. </center>
Last edited by juanki09; May 23rd, 2009 at 8:13 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
juanki09 is offline Offline
1 posts
since May 2009
May 23rd, 2009
0

Re: if statement /mysql help please

You're missing an ending parenthesis on almost all of those conditions. You don't need to disambiguate so much.
php Syntax (Toggle Plain Text)
  1. if ( (strlen($Type) > 0) && (strlen($Brand) > 0 ) && (strlen($Size) > 0 ) && (strlen($Price) > 0 ) )
can just be
php Syntax (Toggle Plain Text)
  1. if ( strlen($Type) > 0 && strlen($Brand) > 0 && strlen($Size) > 0 && strlen($Price) > 0 )
Last edited by ShawnCplus; May 23rd, 2009 at 8:56 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
May 23rd, 2009
0

Re: if statement /mysql help please

and you need to escape your parenthesis
Reputation Points: 11
Solved Threads: 4
Junior Poster
sacarias40 is offline Offline
113 posts
since Nov 2008
May 24th, 2009
0

Re: if statement /mysql help please

Click to Expand / Collapse  Quote originally posted by juanki09 ...
...
PHP Syntax (Toggle Plain Text)
  1. $sql= "select * from PRODUCTS where Type="$Type" and Brand="$Brand" and Size="$Size" order by ProductID");
  2. ...
This statement (as well as others like it) is not quite correct. Foremost, delete the close paren ')' at the end of the line. Next, you are concatenating strings, which usually requires the concatenation (.) operator as in:
PHP Syntax (Toggle Plain Text)
  1. $sql= "select * from PRODUCTS where Type=" . $Type . " and Brand=" . $Brand . " and Size=" . $Size . " order by ProductID";
But it may then generate an SQL error.

This statement would be better written as:
PHP Syntax (Toggle Plain Text)
  1. $sql= "select * from PRODUCTS where Type='$Type' and Brand='$Brand' and Size='$Size' order by ProductID";
or
PHP Syntax (Toggle Plain Text)
  1. $sql= "select * from PRODUCTS where Type='" . $Type . "' and Brand='" . $Brand . "' and Size='" . $Size . "' order by ProductID";
The former requires less typing and is probably clearer.

MySQL prefers values in queries to be in single quotes.

Next, on line 65,
PHP Syntax (Toggle Plain Text)
  1. $result=mysql_query("$sql");
the quotes around $sql are not needed. But they, like your explicit groupings in your if statements, won't hurt anything.
Last edited by Fest3er; May 24th, 2009 at 12:40 am.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to split text into pages?
Next Thread in PHP Forum Timeline: DateTime CLASS problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC