Replace occurrence of string with image in PHP+SQL loop? -


this php code takes records database , displays them. basically, want every occurrence of string "cross" (lowercase only) changed image on web server.

records like: crossjohn doe. when appears on page, should replace cross img , keep rest.

the code:

$sql = "select displayname, lastname, firstname donor donationamount = 1000 order lastname asc limit 154";  $result = mysqli_query($conn, $sql); // query  if (mysqli_num_rows($result) > 0) { // long query returns something, calcs.   $array = array(); // create variable hold information   while ($row = mysqli_fetch_array($result, mysqli_assoc)){ // whule there results, put them in array      $array[] = $row; // add row in results (data) array   }   $counter = (count($array)); // find amount of items in array   $divisorcount = ceil($counter/2); // column count   $foreachcount = 1;    //loop while there items in array   foreach ($array $row){      $foreachcount++; //increment counter       // naming logic      if (empty($row['displayname'])) { // if there no displayname          if (empty($row['firstname'])) { // show lastname              $block[] = "<div class='block'>".$row['lastname']."</div>\n";          }           else { //show first + last if no display name              $block[] = "<div class='block'>".$row['firstname']." ".$row['lastname']."</div>\n";          }       } else { // show display name          $block[] = "<div class='block'>".$row['displayname']."</div>\n";      }        if($foreachcount > $divisorcount){ //insert each record "block"          $foreachcount = 0;          end($block);          $key = key($block);          $block[$key] .= "</div><div class='column'>"; // insert "blocks" css div      }   }   unset($row,$key,$foreachcount,$divisorcount); //cleanup    //insert div , populate blocks   $output = "<div class='tablecontainer'>     <div class='column'>".implode($block)."</div>     </div>";     print_r($output); // display of it!     unset($array,$block);  }else{echo "<p>there no donors in category.</p>";} 

using replace mysql string function might enough

$sql = "select replace(displayname,'cross','<img src=\"path/to/image\" />') `displayname`, replace(lastname,'cross','<img src=\"path/to/image\" />') `lastname`, replace(firstname,'cross','<img src=\"path/to/image\" />') `firstname` donor donationamount = 1000 order lastname asc limit 154"; 

-- http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace


Comments