Reputation: 133
have a database with a list of business leads. They are all given a status of New, Hot, Cold, etc. I have links on the page to display only the New or only Hot that work fine, but I can't seem to get one working to display All. The default view is New. Here is what I'm working with, thanks in advance for your help.
$query = "SELECT * FROM contacts WHERE contactstatus = 'New' ORDER BY date DESC";
if(isset($_GET['contactstatus'])
&& in_array($_GET['contactstatus'], array('New', 'Hot', 'Warm', 'Cold', 'Rejected', 'Closed')))
{
$status = $_GET['contactstatus'];
$query = "SELECT * FROM contacts WHERE contactstatus ORDER BY date DESC";
}
if(isset($_GET['contactstatus'])
&& in_array($_GET['contactstatus'], array('New', 'Hot', 'Warm', 'Cold', 'Rejected', 'Closed')))
{
$status = $_GET['contactstatus'];
$query = "SELECT * FROM contacts WHERE contactstatus = '".$status."' ORDER BY date DESC";
}
The url that I'm using to get all is:
www.mydomain.com/leads.php?contactstatus=New&contactstatus=Hot&contactstatus=Cold&contactstatus=Rejected&contactstatus=Closed
I've also tried:
www.mydomain.com/leads.php?contactstatus=New&Hot&Cold&Rejected&Closed
Upvotes: 1
Views: 1086
Reputation: 517
As you are trying to pass the same variable through the address have you tried using an array otherwise you are overwriting the get variable.
$status_types = Array('New', 'Hot', 'Warm', 'Cold', 'Rejected', 'Closed');
$statuses = Array();
$query = 'SELECT * FROM contacts ';
for($i=0, $j=count($_GET['contactstatus']); $i<$j; $i++){
if(in_array($_GET['contactstatus'][$i], $status_types)){
if($i==0)
$query.= " WHERE contactstatus IN (";
$statuses[] = $_GET['contactstatus'][$i];
$query .= "'".$_GET['contactstatus'][$i]."'";
if($i==($j-1))
$query .= ")";
}
}
$query .= ' ORDER BY contacts.date DESC';
I have set this up to automaticly select all if no contactstatus is passed.
Upvotes: 2
Reputation: 839
Couldn't you just set the default behavior of your php page to display all? That is, if no $_GET
parameter is set (in the URL), your page simply selects all leads from database.
$query = " SELECT * FROM `contacts` ORDER BY date DESC";
$result = $mysqli->query($query);
if($result && $result->num_rows > 0){
// do something with all leads
}
To deal with specific statuses, we have
if( isset($_GET['contactstatus']) ){
// select leads based on status
}
If you want to pass multiple values to a single GET parameter, see Passing multiple values for same GET variable in URL
Upvotes: 0
Reputation: 23001
The first if looks the same as the second. You're also missing the WHERE qualifier on the first query.
If you want to get all of the contacts, change this
$query = "SELECT * FROM contacts WHERE contactstatus ORDER BY contacts.date DESC";
to this
$query = "SELECT * FROM contacts ORDER BY contacts.date DESC";
Upvotes: 0