mysql - Filtering using php if statement
Quick question I have a fully functional database website apart from the filter aspect. I had a javascript filter but it would not work with the database properly as it filtered via li class and making it li class=' . name . ' did not work.
Anyway I have attempted to use this method to get around the filter problem.
However I do not know where to run the query that wont create an error hence why it is commented out. Can anyone help or even suggest a better method of going about this?
$result = mysql_query("SELECT * FROM ddcompanies");
/*
while($row = mysql_fetch_array($result)) {
$cname = $row["company"]; //runs through the company column and populates the array varcompany with those names
$csect = $row["sector"]; //runs through the website column and populates the array varwebsite with those names
$cslog = $row["slogan"]; //runs through the story column and populates the array varstory with the text
*/
echo "<ul id='portfolio'>";
if($_GET['link']=='design'){
echo "design";// TEST
$result .= "AND sector = 'design'";
echo "<li>
<a href='single.php?link=" . $cname . "'><img src='images/companies/" . $cname . ".png' alt='' height='115' width='200' />$cname</a>
<div id='sector'>$csect</div>
<div id='details'>$cslog</div>
</li>";
}else{
echo "no way";// TEST
};
Hope you can help or even tell me a better way to do it please.
Thanks in advance!
*EDIT**
On my index page i have a grid or gallery of links (which have an image and some info all pulled from a database).
Each database entry has a field called sector.
I want to be able to only show (filter) the grid items that fall under e.g. Digital (sector name) or Non-Digital (sector name).
So the filter will be using the sector field value in each database entry.
At the moment my links on the index page for filtering are like the following e.g. Design or Development
I hope this is clear enough.
*EDIT**
//QUERY IN EACH
$query = "SELECT * FROM ddcompanies ";
if($_GET['link']=='design') {
$query .= "WHERE sector = 'design'";
$result = mysql_query($query);
}elseif($_GET['link']=='development') {
$query .= "WHERE sector = 'development'";
$result = mysql_query($query);
};
//OR
$query = "SELECT * FROM ddcompanies ";
if($_GET['link']=='design') {
$query .= "WHERE sector = 'design'";
}elseif($_GET['link']=='development') {
$query .= "WHERE sector = 'development'";
};
$result = mysql_query($query);
Answer
Solution:
it looks like you're trying to append to your query string based on the URL parameter (link) in the case you posted above. The way you're doing it won't work though as $result is a resource, not a string so you can't just append another string to the resource.
You'd have to create a string variable for your query, then plug that into your mysql_query() function.
You'd also have to move the logic up above the string declaration that checks how to revise the query.
Finally, you need a WHERE clause put in there as just using AND sector = 'design' would break the query as it is now.
EDIT (added some pseudo-code):