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>";}
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' –
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
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? –