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.

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.