2016-08-01 3 views
1

Dieser PHP-Code nimmt Datensätze aus meiner Datenbank und zeigt sie an. Grundsätzlich möchte ich, dass jedes Vorkommen der Zeichenfolge "cross" (nur Kleinbuchstaben) in ein Bild geändert wird, das sich auf meinem Webserver befindet.
Ersetzen Sie das Auftreten von String mit Bild in PHP + SQL-Schleife?

Einträge sehen derzeit so aus: crossJohn Doe. Wenn es also auf der Seite erscheint, sollte es Kreuz mit dem IMG ersetzen und den Rest behalten.

Der 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>";} 

Antwort

1

mit der REPLACE mysql String-Funktion könnte man zu bekommen, genug

$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

+0

Ich bekomme derzeit nur die 'Warnung : mysqli_num_rows() erwartet Parameter 1 als mysqli_result, boolean gegeben in C: \ xampp \ htdocs \ webtest \ screen4.php in Zeile 33' –

+0

Stop Verstecken Fehler. Sie erhalten einen Fehler und Sie haben report_mode eingestellt, um über Fehler Stillschweigen zu bewahren. direkt nach dem Verbinden: $ conn-> report_mode = MYSQLI_REPORT_ALL; – hanshenrik

+0

Okay, ich habe es zur Arbeit gebracht! Vielen Dank! Eine letzte Frage. Wie kann ich das tun, habe aber zwei Möglichkeiten? dh wenn das Wort "cross" ist, zeige Bild A und wenn das Wort "option2" ist, dann mache Bild B? –

0

Sie mysqli_fetch_assoc statt mysqli_fetch_array verwenden und Hinzufügen eines weiteren Argument verwenden kann nur das assoziative

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 
} 
+0

Wird nur die Zeichenfolge "cross" im Feld ersetzt und nicht die anderen Inhalte gelöscht? –

+0

ja versuchen Sie es einfach ersetzen Sie den 'Schlüsselname' mit Ihrem Spaltennamen –

+0

es ändert nichts. Ich habe sogar den "Pfad/zu/Bild" in einen anderen String geändert, um sicherzustellen, dass es nicht mein Bild ist. –