jmfillman Posted November 23, 2006 Share Posted November 23, 2006 I need to adapt the query within this function, below, to perform a different query if $ptAxisISearchType==1. Function is as follows, but is only accurate if $ptAxisISearthType==0: public function ptAxisISearch($ptAxisISearchText, $ptAxisISearchType) { if (!$result=$this->mysqli->query("SELECT Code, Code_Description from icd WHERE Code_Description LIKE '%$ptAxisISearchText%'")){ $msg=$this->err_prefix."SELECT query error: ".$this->mysqli->error; $this->mysqli->close(); throw new Exception($msg); } while ($row = $result->fetch_assoc()) { $user_array[] = $row; } return($user_array); } Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/ Share on other sites More sharing options...
pthurmond Posted November 23, 2006 Share Posted November 23, 2006 Your code is kinda difficult to read but I think I see what you are getting at.First for the readability issue you can break it down into smaller parts. That will help that issue and will also make it span alot more lines, but many times readability is more important.Also pull the query build out of the condition header of the if statement, that can limit your use of the results.Try this:[code]$query = "SELECT Code, Code_Description from icd WHERE Code_Description LIKE '%$ptAxisISearchText%'";$result = mysql_query($query) or die('Query failed: ' . mysql_error());if($result) //if the result has something in it do whatever.{ while($row = mysql_fetch_array($result, MYSQL_NUM)) { } //Do whatever}else //No results to be done{ $msg=$this->err_prefix."SELECT query error: ".$this->mysqli->error; $this->mysqli->close(); throw new Exception($msg);}[/code]Anyways try an "else if($ptAxisISearchType==1)" followed by what you want to do in that case. You can chain these else ifs indefinitely and it will only do the instructions when that condition is met. Or you could just use a big switch statement, but that might be a bit more overwhelming.Another thing, do you need it to be only similar to the variable, or do you need an exact match? If you need an exact match then replace LIKE with an equal sign. Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-129022 Share on other sites More sharing options...
jmfillman Posted November 23, 2006 Author Share Posted November 23, 2006 I'm pretty new to PHP, but I don't think I can take the $query out of the if statement because I need two different SELECT statements. Here is a very high level of what I need this to do.if($ptAxisISearchText==0){$query = "SELECT Code, Code_Description from icd WHERE [color=red]Code_Description [/color] LIKE '%$ptAxisISearchText%'";}if($ptAxisISearchText==1){$query = "SELECT Code, Code_Description from icd WHERE [color=red]Code[/color] LIKE '%$ptAxisISearchText%'";} Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-129024 Share on other sites More sharing options...
pthurmond Posted November 23, 2006 Share Posted November 23, 2006 Then just put the query build inside the body of the if statements, not in the condition area. Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-129036 Share on other sites More sharing options...
JasonLewis Posted November 23, 2006 Share Posted November 23, 2006 you could just go like this for that:[code=php:0]if($ptAxisISearchText == 0){ $select = "Code_Description"; }elseif($ptAcisISearchText == 1){ $select = "Code"; }$query = "SELECT Code, Code_Description from icd WHERE `".$select."` LIKE '%$ptAxisISearchText%'";[/code] Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-129037 Share on other sites More sharing options...
jmfillman Posted November 23, 2006 Author Share Posted November 23, 2006 The problem I'm having is with the syntax. This is the best I can come up with, but just gives an error. public function ptAxisISearch($ptAxisISearchText, $ptAxisISearchType) { if [color=red]($ptAxisISearchType==0){ ([/color]!$result=$this->mysqli->query("SELECT Code, Code_Description from icd WHERE Code_Description LIKE '%$ptAxisISearchText%'")) $msg=$this->err_prefix."SELECT query error: ".$this->mysqli->error; $this->mysqli->close(); throw new Exception($msg); } while ($row = $result->fetch_assoc()) { $user_array[] = $row; } return($user_array); } Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-129038 Share on other sites More sharing options...
JasonLewis Posted November 23, 2006 Share Posted November 23, 2006 [code=php:0]if ($ptAxisISearchType==0){ (!$result=$this->mysqli->query("SELECT Code, Code_Description from icd WHERE Code_Description LIKE '%$ptAxisISearchText%'")) $msg=$this->err_prefix."SELECT query error: ".$this->mysqli->error; $this->mysqli->close(); throw new Exception($msg); }[/code]are you supposed to be opening a bracket at the !$result Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-129044 Share on other sites More sharing options...
jmfillman Posted November 24, 2006 Author Share Posted November 24, 2006 I'm not where to put the brackets. Putting the bracket where I did in my last post errors. The problem is I don't know the syntax to acheive what I'm attempting. Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-129692 Share on other sites More sharing options...
JasonLewis Posted November 24, 2006 Share Posted November 24, 2006 see the opening bracket at the !$result. it looks like this atm: (!$resultwell remove that bracket so its just: !$result Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-129800 Share on other sites More sharing options...
jmfillman Posted November 27, 2006 Author Share Posted November 27, 2006 Modifying it to display like this returns a mysql error, "Channel Disconnected". public function ptAxisISearch($ptAxisISearchText, $ptAxisISearchType) { if ($ptAxisISearchType==0){ !$result=$this->mysqli->query("SELECT Code, Code_Description from icd WHERE Code_Description LIKE '%$ptAxisISearchText%'") $msg=$this->err_prefix."SELECT query error: ".$this->mysqli->error; $this->mysqli->close(); throw new Exception($msg); } while ($row = $result->fetch_assoc()) { $user_array[] = $row; } return($user_array); } Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-130816 Share on other sites More sharing options...
JasonLewis Posted November 27, 2006 Share Posted November 27, 2006 remove the ! in front of $result. maybe thats causing the error, i dont no but i dont think a ! should be there. Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-130843 Share on other sites More sharing options...
jmfillman Posted November 27, 2006 Author Share Posted November 27, 2006 Removing the ! still returns the error. Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-131074 Share on other sites More sharing options...
jmfillman Posted November 28, 2006 Author Share Posted November 28, 2006 I get the following error:'my_class' class database SELECT query error: Link to comment https://forums.phpfreaks.com/topic/28205-if-else-help/#findComment-131280 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.