Solved

Filtering php variables with mysql query

Posted on 2004-04-28
11
345 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MYSQL simple update statement 3 32
Php Array Diff 3 26
Wordpress update causing pages to crash 1 18
Filter wordpress query 3 8
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

16 Experts available now in Live!

Get 1:1 Help Now