Jump to content

[SOLVED] Searching dynamic driven list


karenn1

Recommended Posts

Hey everyone!

 

I have a members database will all their personal details in. I have a page running a SQL query to display a complete list of all these people. This is what my query looks like:

 

$sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' ORDER BY name ASC;
$result_prop = mysql_query($sql_prop) or die(error_report("Mysql Error", mysql_error()."\n\n$sql_prop", $_SERVER['REMOTE_ADDR']));

 

"Error Report" is just one of my functions. I have two forms fields on this page, "Name" and "Address". I want to be able to search the listing that this SQL query has returned for those fields, ie. the user enters "John" in the form field and it returns all entries within the query matching "John". The reason I'm using this:

 

area = '".$_REQUEST['area']. "'

 

in the WHERE clause, is just to ensure that the person viewing this list can only see members that are in his area. On the previous page there is a piece of code that gets the session "area" and brings it across to this page. I'm keen on keeping that code in. It works for me at the moment.

 

Any ideas? How can I search this already filtered list?

 

Thanks,

Karen

Link to comment
https://forums.phpfreaks.com/topic/58392-solved-searching-dynamic-driven-list/
Share on other sites

$sql_prop="SELECT * FROM $db.members WHERE `area`='".$_REQUEST['area']."' AND `name` LIKE '%".$_REQUEST['name']."%' OR `name` LIKE '%".$_REQUEST['name']."%' ORDER BY `name` ASC";

 

Something like that. The % symbol is a bit like a pattern match:

 

%string = everything with 'string' at the end

%string% = everything containing 'string'

string% = everything starting with 'string'

Depends on the data you're using and how you set up the select options. What sort of data are you thinking of using with what field in the database?

 

btw, I just noticed I forgot to add "address" into that query and am using two "name"s instead!

I think so.

 

Status <select name="status"><option value="active">Active</option><option value="inactive">Inactive</option></select>

That would be your HTML to make the drop-down selection box.

 

Use this to read the contents of that box:

$status=$_POST['status'];

 

Then modify your query to include this:

WHERE `status`='".$status."'

I've placed this bit "$status=$_POST['status'];" above the SQL query. Is that right? I'm getting the following error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'status = '' ORDER BY name ASC' at line 1

SELECT COUNT(*) FROM neigha_db1.members WHERE area = 'welgemoed' AND name LIKE '%%' status = '' ORDER BY name ASC

 

This is what my SQL now looks like, I'm not going to use address anymore:

 

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

 

Help!!

I've found the problem. The name of the field in the database is actually "active" and not "status". Now it works fine BUT when you first come to the page, it displays no rows. It only shows the info once you select an option from the dropdown and then click on search. Is there any way to have it display all the rows once you get to the page?

I think we have crossing lines, check my post above. I'll post it again:

 

I've found the problem. The name of the field in the database is actually "active" and not "status". Now it works fine BUT when you first come to the page, it displays no rows. It only shows the info once you select an option from the dropdown and then click on search. Is there any way to have it display all the rows once you get to the page?

 

 

What do you think?

 

I realise now that the "Active" dropdown works but when I search on name at the same time, it doesn't pick anything up.

 

This is the submit procedure on the search form on the same page:

 

<form action="list2.php?area=<?= $result2['area']; ?>" method="post" name="form" id="form">

 

The $result2 variable is there to pick up the session "area" of the logged in person like I explained in my first post on this thread. The coding you gave me for the SQL clause only worked once I put that variable there.

Edit this:

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";
}

 

That will make it so if the submit button is pressed the contents of the form are used. If the page is loaded for the first time the submit button hasn't been pressed so it defaults to showing all in the current area.

 

This is a crude way of doing it as it can be done a better way but it involves adding a lot more code.

The SQL_ROWS bit is another feature I have in there, it just counts all the rows and displays it no the page. The code below:

 

<?php
$validate = new validate("error");


$i = 0;
$status = $_POST['status'];

//$sql_rows = "SELECT COUNT(*) FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND active='".$status."' ORDER BY name ASC";
//$result_rows = mysql_query($sql_rows) or die(error_report("Mysql Error", mysql_error()."\n\n$sql_rows", $_SERVER['REMOTE_ADDR']));
//$rs_rows = mysql_fetch_array($result_rows);
//$numrows = $rs_rows[0];


//$sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND active='".$status."' ORDER BY name ASC";
//$result_prop = mysql_query($sql_prop) or die(error_report("Mysql Error", mysql_error()."\n\n$sql_rows", $_SERVER['REMOTE_ADDR']));


if ($_POST['submit']) {
  		$sql_prop = "SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND active = '".$status."' ORDER BY name ASC";
	$result_prop = mysql_query($sql_prop) or die(error_report("Mysql Error", mysql_error()."\n\n$sql_rows", $_SERVER['REMOTE_ADDR']));	

	$sql_rows = "SELECT COUNT(*) FROM $db.members WHERE area = '".$_REQUEST['area']. "' AND name LIKE '%".$_REQUEST['name']."%' AND active='".$status."' ORDER BY name ASC";
	$result_rows = mysql_query($sql_rows) or die(error_report("Mysql Error", mysql_error()."\n\n$sql_rows", $_SERVER['REMOTE_ADDR']));
	$rs_rows = mysql_fetch_array($result_rows);
	$numrows = $rs_rows[0];	

} else {
  		$sql_prop="SELECT * FROM $db.members WHERE area = '".$_REQUEST['area']. "' ORDER BY name ASC";
	$result_prop = mysql_query($sql_prop) or die(error_report("Mysql Error", mysql_error()."\n\n$sql_rows", $_SERVER['REMOTE_ADDR']));

	$sql_rows = "SELECT COUNT(*) FROM $db.members WHERE area = '".$_REQUEST['area']. "' ORDER BY name ASC";
	$result_rows = mysql_query($sql_rows) or die(error_report("Mysql Error", mysql_error()."\n\n$sql_rows", $_SERVER['REMOTE_ADDR']));
	$rs_rows = mysql_fetch_array($result_rows);
	$numrows = $rs_rows[0];	

	}


?>

heh I can't do anythign with that.

 

You'll need to find how the name variable is being pulled from the form and make sure the query is being populated with the data. Echo the query to the browser as well as the name variable to see exactly what is being used.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.