Arianne Arianne - 2 months ago 17
MySQL Question

Mysql database and php

MySQL Tables:

UserInfo
--------
UserID
Status (if user is active in a specific group)
GroupCode

UploadInfo
--------
Title
Description
Filename
UserID
GroupCode


I want to have a search box that will search some data from table
UploadInfo that will depend on the User's GroupCode and its Status is
active

My function in PHP (im using meekrodb)

function search($searchVal, $whoIsLogin){
$searchResult =DB::query("SELECT * FROM UserInfo as tbl1 INNER JOIN UploadInfo as tbl2 on(tbl1.UserID=tbl2.UserID) where (tbl1.UserID=%s and tbl1.Status='active'", $whoIsLogin);
}


This function only shows all files from the users group where it is active. However this function has no search function.

Any kind of method or function?

Answer

I have created only the basic.Update as per your requirement.Assuming only 'and' clause. Add condition for 'or'.

Here is the example of calling:

$arrSearch = array("Filename" => 'abc.txt');
search($arrSearch,'<whoisloginval>','like');


function search($searchVal, $whoIsLogin,$conditionType=''){
    $like = 'no';
    switch($conditionType)
    {
       case "equal": $clause = "=";
       break;
       case "notequal": $clause = "!=";
       break;
       case "like": $clause = "%"; $like = 'yes';
       break;
     }
    if(is_array($searchVal))
    {
       foreach($searchVal as $k=>$v)
       {

          $cond .= " and". $k .(($like == 'yes') ? ' like \' ' : '') . $clause. (($like == 'yes') ? $clause .'\'' : '') . "$v";
       }
     }
   $searchResult =DB::query("SELECT * FROM UserInfo as tbl1 INNER JOIN UploadInfo as tbl2 on(tbl1.UserID=tbl2.UserID) where (tbl1.UserID=%s and tbl1.Status='active' $cond ", $whoIsLogin);
  return $searchResult;
}