Jump to content

[SOLVED] Searching dynamic driven list


karenn1

Recommended Posts

To display the contents of the query add the ECHO immediately after you populate the $sql_prop variable.

 

To display the contents of the $name variable you can echo this anywhere like this:

echo $_REQUEST['name'];

 

Just wondering, any specific reason why you're using $_REQUEST and not $_POST?

Link to comment
Share on other sites

That is the contents of the MySQL query. In the SQL query, when I take away the "AND active =....." bit and only leave the AND for name, then it works fine. As soon as I add that back in again, it falls flat.

Link to comment
Share on other sites

Add it after the IF statement - I've added it for you:

if ($_POST['submit']) {
  $sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND status = '".$status."' ORDER BY name ASC";
} else {
  $sql_prop="SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' ORDER BY name ASC";
}
echo $sql_prop;

Link to comment
Share on other sites

Try this:

$sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND status = '".($status=='active' ? 'active' : 'inactive')."' ORDER BY name ASC";

 

Basically we've added condition ? true : false. If $active contains 'active' then use it. If $active contains anything else then use 'inactive'

Link to comment
Share on other sites

$sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND status = '".($status ? 'active' : 'inactive')."' ORDER BY name ASC";

 

Sorry, I thought they contained either 'active' or 'inactive', not true or false.

 

See how that works.

Link to comment
Share on other sites

I've changed it to this:

 

active = '".($status ? 'true' : 'false')."'

 

and it seems to work but when I select "Inactive" (value = false) from the dropdown, it still returns true. SQL echo:

 

SELECT * FROM neigha_db1.members WHERE area = 'welgemoed' AND name LIKE '%%' AND active = 'true' ORDER BY name ASC 

 

As soon as I get this code, and I then search on name, it reverts to false. Any ideas? Other than that, when active is true, then I can search for name as well. That's at least one good thing.

Link to comment
Share on other sites

In the meantime I'll break it down so you can maybe understand what is happening.

 

All the line is doing is populating the variable $sql_prop with a MySQL query.

 

The extra bit I added was the condition ? true : false bit. This is exactly the same as this:

<?php
$sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND status = '";
if ($_REQUEST['status']==true) {
  $sql_prop .= 'active';
} else {
  $sql_prop .= 'inactive';
}
$sql_prop .= "' ORDER BY name ASC";
?>

 

Here's the amended line:

$sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND status = '".($_REQUEST['status'] ? 'active' : 'inactive')."' ORDER BY name ASC";

 

I suspect that the variable $status isn't being set to the contents of the drop-down box. Difficult to tell without seeing the entire script.

 

Can you show the HTML that makes the drop-down box? We need to make sure we're checking the right contents.

Link to comment
Share on other sites

Here's the code:

 

<select name="status">
                    <option value="True" selected>Active</option>
                    <option value="False">Inactive</option>
                  </select>

Link to comment
Share on other sites

Ah!!!

 

I see you're using the WORDS "true" and "false" instead of numerical values!

 

$sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND status = '".($_REQUEST['status']=='True' ? 'active' : 'inactive')."' ORDER BY name ASC";

Link to comment
Share on other sites

Hallelujah! It works!!!! Regarding your question - not really. Didn't think there was really a difference. One last question, is there any way that I can add an option for "All" to my dropdown then it will show all members in my database? Is that too much to aks?

Link to comment
Share on other sites

I have to head off now. Thank you SOOO much for all your help! You are a star. Can you perhaps just answer that last question about displaying all members from the dropdown box? I would appreciate just one last bit of help!

 

Thanks,

Karen

Link to comment
Share on other sites

It can be added but you'd need to change your code to change the query accordingly.

 

At the moment you're checking for either active or inactive. If you want to check for both then simply remove that part of the query.

 

Try this:

<select name="status"><option="All">All</option><option value="True">Active</option><option value="False">Inactive</option></select>

 

That adds "All" into the drop-down box with the value "All".

 

Next we need to change the way the query is built:

if ($_POST['submit']) {
  switch ($_REQUEST['status']) {
    case "True":$sqlstatus="AND status = 'active' ";break;
    case "False":$sqlstatus="AND status = 'inactive' ";break;
    default:$sqlstatus="";
  }
  $sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' ".$sqlstatus."ORDER BY name ASC";
} else {
  $sql_prop="SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' ORDER BY name ASC";
}

Link to comment
Share on other sites

$_GET reads values from the URL. When a form is set to method GET it adds the form data onto the URL.

 

$_POST reads data from a form when method is set to "POST"

 

$_REQUEST uses both the above methods and will also read from cookies.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.