0v3k Shi3ld3r 0v3k Shi3ld3r - 4 months ago 8
MySQL Question

Displaying Multiple rows (WHILE) ICONS

I have two

while loops
one is to loop through a chat log to retrieve date , username , message and the other
while loop
is to retrieve icons from a separate table this has two columns
chars
and
image
(image-name.*) I can display everything from the
table chat
but cannot seem to get my second
while loop
that contains the
str_replace
to loop through each row in the
emo_cons
table
that may contain values , It only displays the last row in the table if I add a new
chars
value and
image-name.*
it shows me back that last one

// select all from table emo_cons that contain character values and image location
$emocom = mysqli_query($con,"SELECT * FROM `emo_cons`");

// fetch rows from query
while($emo_row= mysqli_fetch_assoc($emocom))
{
// assign chars row characters that represent the coresponding image
$chars = $emo_row['chars'];

// assign imagetag to row images that represent the coresponding characters
$imagetag = "<img width='50' class=image height='50' src='chaticons/".$emo_row['image']."' />";

echo " ";

echo "<br>";
} // end while emo_car check




$chat_log = mysqli_query($con,"SELECT * FROM chat ORDER BY id DESC");

// fetch all rows that contain characters and image locations
while($chat_row = $chat_log ->fetch_array()){ // i WANT THIS WHILE LOOP TO OUTPUT ALL ROWS THAT CONTAIN CHARS & IMAGES-LOCATIONS

// name of user in chat log
$username = $chat_row['username']; // this line is for usrer
echo "<br>";

// timestamp
echo $chat_row['date'];

echo "<br>"; // line break

//users profiler avatar image from chat_row while
echo "<img width='50' class=image height='50' src='avatars/".$chat_row['imagelocation']."' alt='Profile Pic'>";
echo " "; // space


// THIS LINE ONLY OUTPUTS THE LAST ROW OF AN ICON IN THE TABLE
// I would like my while to output all rows that contain str_replace chars TO imagetag from its table AND HAVE IT THEN PARSE TO THE MESSAGE [msg]
echo $username. " Says ".$new_str = str_replace($chars,$imagetag,$chat_row['msg']);

echo "<br>";


} // end while chat_log



echo '</div>'; // end div


?>


I need help understanding what I doing wrong I have tried moving my loops around to see if I had incorrectly structured them but had no luck so far
I just want to be able to display the chat message and if the message contains a character to replace that with an icon and do this always and NOT just for last row in my table .

I understand I may not get any help on this but its worth a mention maybe someone can see my errors . Thanks in advance .

enter image description here
enter image description here

UPDATE WORKING
enter image description here

Answer

Make following changes in your emo_cons while loop

<?php
// select all from table emo_cons that contain character values and image location
$emocom = mysqli_query($con,"SELECT * FROM `emo_cons`");

// fetch rows from query
$chars = array();
$imagetag = array();

while($emo_row= mysqli_fetch_assoc($emocom))
{
    // assign chars  row characters that represent the coresponding image
    array_push($chars, $emo_row['chars']);

    // assign imagetag to row images that represent the coresponding characters
    array_push($chars, "<img width='50' class=image height='50' src='chaticons/".$emo_row['image']."' />");

    echo "  ";

    echo "<br>";
} // end while emo_car check

?>

Let me know if it works for you.


Why your code was not working

Both the variables $chars and $imagetag were inside while loop and with every iteration values were overwriting. Hence the last value was stored in both the variables.

So to fix this, I created 2 arrays with same name and all the required values pushed into the arrays, which we later used in the str_replace function.

Comments