Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Filtering php variables with mysql query

Posted on 2004-04-28
11
Medium Priority
?
372 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
[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
  • 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 300 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

670 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