1

This PHP code takes records from my database and displays them. Basically, I want every occurrence of the string "cross" (lowercase only) to be changed to an image that is on my web server.

Records currently look like: crossJohn Doe. So when it appears on the page, it should replace cross with the img and keep the rest.

The code:

$sql = "SELECT DisplayName, LastName, FirstName FROM donor WHERE DonationAmount = 1000 ORDER BY LastName ASC LIMIT 154";
 $result = mysqli_query($conn, $sql); // query
 if (mysqli_num_rows($result) > 0) { // as long as the query returns something, do the calcs.
  $array = array(); // create a variable to hold the information
  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ // whule there are results, put them in the array
     $array[] = $row; // add the row in to the results (data) array
  }
  $counter = (count($array)); // find the amount of items in the array
  $divisorCount = ceil($counter/2); // column count
  $forEachCount = 1;

  //loop while there are items in the array
  foreach ($array as $row){
     $forEachCount++; //increment counter

     // naming logic
     if (empty($row['DisplayName'])) { // if there is 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 into a "block"
         $forEachCount = 0;
         end($block);
         $key = key($block);
         $block[$key] .= "</div><div class='column'>"; // insert all "blocks" into a css div
     }
  }
  unset($row,$key,$forEachCount,$divisorCount); //cleanup

  //insert the div and populate it with the blocks
  $output = "<div class='tableContainer'>
    <div class='column'>".implode($block)."</div>
    </div>";
    print_r($output); // display all of it!
    unset($array,$block);
 }else{echo "<p>There are no donors in this category.</p>";}

2 Answers 2

0

using the REPLACE mysql string function might be enough

$sql = "SELECT REPLACE(DisplayName,'cross','<img src=\"path/to/image\" />') AS `DisplayName`, REPLACE(LastName,'cross','<img src=\"path/to/image\" />') AS `LastName`, REPLACE(FirstName,'cross','<img src=\"path/to/image\" />') AS `FirstName` FROM donor WHERE DonationAmount = 1000 ORDER BY LastName ASC LIMIT 154";

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

Sign up to request clarification or add additional context in comments.

4 Comments

I currently just get the Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\webtest\screen4.php on line 33
stop hiding errors. you get an error and you have report_mode set to be silent about errors. right after connecting, do: $conn->report_mode=MYSQLI_REPORT_ALL;
Okay, I got it to work! Thanks! One last question. How can I do this but have two options? ie if the word is cross show image A and if the word is option2 then do image B?
this should replace "criss" with image A and "cross" with image B: REPLACE(REPLACE(DisplayName,'criss','<img src=\"path/to/image\A\" />'),'cross','<img src=\"path/to/image\B\" />')
0

you can use mysqli_fetch_assoc instead of using mysqli_fetch_array and adding another argument to get only the associative one

while ($row = mysqli_fetch_assoc($result)) { 

if (stripos('cross',$row['keyname']) !== false)
$row['keyname'] = str_replace('cross','<img src="path/to/image"/>',$row['keyname']);

$array[] = $row; // add the row in to the results (data) array
}

6 Comments

Will this only replace the string that says "cross" in the field and not erase the other contents?
yes try it just replace the 'keyname' with your column name
it does not change anything. i even changed the 'path/to/image' to another string to insure it wasn't my image.
i might specify that records currently look like: crossJohn Doe. So when it appears on the page, it should replace cross with the img and keep the rest.
use stripos, not a strpos & strtolower combo :P (it works but it's just weird.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.