Jump to content

Setting an array of columns to search in


prw

Recommended Posts

I'm somewhat new to OOP PHP, and I'm still learning, but basically I want searchcolumns() to set which MySQL columns that it'll search inside but I don't know where to go from what I've already coded.

 

Here's the basic input to be sent to my classes file:

 

$getusers = new database;
$getusers->search($_GET['search']);
$getusers->searchcolumns('username','account','email');

 

And to put it simple, I need my $where variable to output this:

 

$where .= "(username LIKE '%$search%' OR account LIKE '%$search%' OR email LIKE '%$search%')";

 

Except replacing the already set columns with the ones that I assigned in the searchcolumns() array, and then adding an OR string if there's more than 1 column set.

 

Here's the code as I have it at the moment, for obvious reasons, it doesn't work how I want it to:

 

// Search columns
function searchcolumns($columns) {
		$this->column = explode(", ", $columns);
}

// Search results
function search($search) {

		$search = ($_POST['search']);

if (isset($_GET['search']) && strlen($_POST['search']) > 1) {

		$searchresult = explode(" ", $search);

		$where = "WHERE";

foreach ($searchresult as $key => $search) { 
		$where .= "($this->column LIKE '%$search%')";
if ($key != (sizeof($searchresult) - 1))   
		$where .= " AND ";
} 
		$this->where = $where;
}

}

 

Sorry if this has been somewhat confusing to explain, but hopefully someone understands what I'm trying to achieve.

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.