Jump to content

if else HELP!!!


jmfillman

Recommended Posts

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

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

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

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

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

[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

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

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.