Solved

Filtering php variables with mysql query

Posted on 2004-04-28
11
348 Views
Last Modified: 2013-12-12
basically what i have is a drop down box with a bunch of years and a Any option....The any option is working by checking with an if statement if it is used and then just uses ($query=select * from table;)  but what isn't working yet is the dates...basically in the table the column year has values like:

87-04
87-93
94-95
96-97
96-98
79-95

And the drop down box will be like (Any,04,03,02,01)  So if the user chosses 97 it should display the row even if the column contains 87-04 or 96-97 or 96-98.....That isn't working yet and is what i need help with..here is my code so far....Any help would be appreciated thanks




<form method="post">
<br>
<hr width="40%" align="left">
Manufacturer Filter:


<select name="Man">
<option value="*" selected>Any Manufacturer</option>
<option value="MAC">MAC</option>
<option value="Bread">Hot Bread</option>
</select>


<input type=submit name="Man_filter" value="Filter">
<BR><BR>
Year Filter:


<select name="Year">
<option value="*" selected>Any Year</option>
<option value="04">04</option>
<option value="03">03</option>
<option value="02">02</option>
<option value="01">01</option>
<option value="00">00</option>
<option value="99">99</option>
<option value="98">98</option>
<option value="97">97</option>

</select>


<input type=submit name="Year_filter" value="Filter">


</form>



<?

if(isset($_POST['Year_filter'])){
if(isset($_POST['Year'])){


$Year=$_POST['Year'];
}Else{
$Year="";
}
mysql_connect("mysql","cis245","tucker") or die("Could not connect to MySQL");

mysql_select_db("productsdb")  or die("Could not connect to database");


if($Year=='*'){

$query = "select * from parts;";
}Else{
$query = "select * from parts where year = '$Year';";
}


/*---------------------------------*/
$result = mysql_query($query);


print "<table border='1'>";



//Rows for table
$x=0;
while($x<mysql_numrows($result))
{
      //Get the data  for the current row
      $id = mysql_result($result,$x,'product_id');
      $manufacturer=mysql_result($result,$x,'manufacturer');
      $name=mysql_result($result,$x,'name');
      $year=mysql_result($result,$x,'year');
      $model=mysql_result($result,$x,'model');
      $description=mysql_result($result,$x,'description');
      $type=mysql_result($result,$x,'type');
      $item=mysql_result($result,$x,'item');
      $price=mysql_result($result,$x,'Price');
      $picture=mysql_result($result,$x,'picture');

      //Make a row for that table
      print "<tr>
            <td column=2>$picture</td>
            <td>$manufacturer</td>
            <td>$name</td>
            <td>$year</td>
            <td>$model</td>
            <td>$description</td>
            <td>$type</td>
            <td>$item</td>
            <td>$price</td>
            
<Tr><td><BR>
<BR></td>
</tr>
      </tr>";

      $x++;
      }
print "</table>";

//------------------
}
0
Comment
Question by:nitroususer
  • 4
  • 4
  • 3
11 Comments
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10937527
SELECT * FROM 'parts' WHERE left('year',2) > '$year';
0
 
LVL 9

Accepted Solution

by:
AlanJDM earned 100 total points
ID: 10938081
"SELECT * FROM 'parts' WHERE left('year',2) > '$year';"

Almost, but should be...

SELECT * FROM 'parts' WHERE '$year' between left('year',2) and right('year',2);



Alan
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10939004
i was goin to do that but the problem is with years like 04 (for 2004)

so i thought all that matters is that the year is greater than the lowest year which is the first part of the string
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 10939207
jkna_gunn,

Very good point... I did overlook that.


nitroususer,

As jkna_gunn has pointed out, you are going to have a problem doing exactly what you want due to the fact you are using 2 digit years.


Alan




0
 

Author Comment

by:nitroususer
ID: 10943628
so if i change the database to being like 1987-2004 and so on then

SELECT * FROM 'parts' WHERE '$year' between left('year',2) and right('year',2);

should work?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:AlanJDM
ID: 10943647
Yes.
0
 

Author Comment

by:nitroususer
ID: 10945666
i tried that and it doesn't work??

I even went:

$query = "SELECT * FROM 'parts' WHERE '/*$year*/97' between left(/*'year',2*/96) and right(/*'year',2*/98);";

so you would think that everything should show up and that didn't work either?? anybody know why?
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10946887
well if you changed it to be 4 digits years instead of two then you must change the left and right function
to read 4 characters not two.

SELECT * FROM 'parts' WHERE '$year' between left('year',4) and right('year',4);
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 10948168
jkna_gunn is correct in that you need to look at all 4 chars of the year field in your comparison. Also, I am not sure what you thought the "/*" was doing but it is incorrect. If you where trying to use wildcards you should be aware that wildcards are represented by the percent sign(%) and can only be used in conjunction with certain operators.


Alan

0
 

Author Comment

by:nitroususer
ID: 10955171
its all working guys....i had to take out the '' around parts and year like:

SELECT * FROM parts WHERE '$year' between left(year,2) and right(year,2);
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10956936
im confused, i thought you wre going for the 4 digit version? the above wont work for strings like 87-04
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 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

15 Experts available now in Live!

Get 1:1 Help Now