如何在PHP条件下连接字符串?

I wrote query in php file ,my query is:

$query = "SELECT cc.NAME complex_check_name,f.name server_name,
                sgk.NAME single_check_name,cc.operator 
          FROM complex_check cc, 
               lnksinglechecktocomplexcheck lk,
               single_checks sgk,
               functionalci f ,
               lnkconfigurationitemtosinglecheck lkcg 
           WHERE cc.id = lk.complex_check_id AND 
                 sgk.id = lk.single_check_id and 
                 sgk.id = lkcg.single_check_id AND 
                 lkcg.config_item_id = f.id ";

if ($result=mysqli_query($link,$query))
{
    while ($obj=mysqli_fetch_object($result))
    {
       $list= $obj->complex_check_name . 
              "@".
              $obj->server_name .
              ";". 
              $obj->single_check_name . 
              $obj-> operator;

        echo  $list .'<br>'; 
    }
 }

The result is :

test_com_check_sep01@INFRASEP01;cpu check sep01&
test_com_check_sep01@INFRASEP01;DB check sep01&
test_com_check_sep01@INFRASEP01;disk space check sep01&
test_com_check_sep02@INFRASEP02;cpu check sep02||
test_com_check_sep02@INFRASEP02;db check sep02||
test_com_check_sep02@INFRASEP02;disk space check sep02||

How can I concatenate the string as:

"test_com_check_sep01=INFRASEP01;cpu check sep01&INFRASEP01;DBcheck sep01&INFRASEP01;disk space check sep01"

"test_com_check_sep02=INFRASEP02;cpu check sep02||INFRASEP02;db check sep02||INFRASEP02;disk space check sep02"

You could store the values into an array and implode afterwards.

$check  =   array();
if($result = mysqli_query($link,$query)) {
        while($obj = mysqli_fetch_object($result)) {
            // If value is not stored in check array proceed
            if(!in_array($obj->complex_check_name,$check))
                $list[$obj->complex_check_name][]   =   $obj->complex_check_name."=";

            $list[$obj->complex_check_name][]   =   $obj->server_name.";".$obj->single_check_name.$obj->operator;

            // Store in check array
            $check[]    =   $obj->complex_check_name;
        }

    // Loop through stored rows and implode with break
    foreach($list as $array) {
            echo implode("<br />",$array);
        }
 }

try this

$concat1 = '';
$concat2 = '';
if ($result=mysqli_query($link,$query))
  {
      while ($obj=mysqli_fetch_object($result))
        {
           if($obj->server_name == 'INFRASEP01'){
                concat1 .= $obj->complex_check_name . "@".$obj->server_name .";". $obj->single_check_name . $obj-> operator;
           }else{
                concat2 .= $obj->complex_check_name . "@".$obj->server_name .";". $obj->single_check_name . $obj-> operator;
           }
        }
   }
echo  $concat1 .'<br>'.$concat2; 

Assuming you want to keep the lines separate for use, you could do this

$list = array("INFRASEP01", "INFRASEP01", "INFRASEP02");
$concatINFRASEP01 = "";
$concatINFRASEP02 = "";
for($lineCounter = 0; $lineCounter < sizeof($list); $lineCounter++)
    if(strpos($list[$lineCounter], "INFRASEP01") !== false){
        //Found it
        $concatINFRASEP01 .= " " . $list[$lineCounter];
    }
    else if(strpos($list[$lineCounter], "INFRASEP02") !== false){
        $concatINFRASEP02 .= " " . $list[$lineCounter];
    }

NOTE: I did not test this code so there may be errors.

If you do not need the $list array, then you can do as @Malakiof suggested.

EDIT (01/22/2015): I have improved my answer but left the old one there in case I misunderstood.

//In your case the rows come from the $obj (which most would call $row)
$listOfRows = array("test_com_check_sep01@INFRASEP01;cpu check sep01&","test_com_check_sep01@INFRASEP01;DB check sep01&", "test_com_check_sep02@INFRASEP02;cpu check sep02||");

//Create this array as you go through each row by adding the server_name
//to this array. You can skip making this array and simply make the
//unique array by checking if the array contains the server_name.
$listServerNames = array("INFRASEP01","INFRASEP02","INFRASEP01", "INFRASEP02", "INFRASEP01");

//Only have unique server names to avoid duplicates
$listUniqueServerNames = array_unique($listServerNames);

//this will be your list of concatenated strings
$concatByServerName = array();

//Go through the unique server names one by one
foreach($listUniqueServerNames as $serverName)
//While looking at each unique server name, look through the rows
foreach($listOfRows as $line)
if(strpos($line, $serverName) !== false){
    //Found it
if(array_key_exists($serverName, $concatByServerName))
    $concatByServerName[$serverName] .= " " . $line;
else
    $concatByServerName[$serverName] = $line;
}

If the $listOfRows is very large, consider making a copy of $listOfRows and removing the lines from $listOfRows when you add them to $concatByServerName.

Although this is not the best way to do it, I am trying to keep it simple in order for you to be able to get it to work, while fully understanding what is happening. You can easily improve on this code by looking at your project and streamlining these tasks.