gpt4 book ai didi

php - 显示多行(WHILE)图标

转载 作者:行者123 更新时间:2023-11-29 20:43:03 26 4
gpt4 key购买 nike

我有两个 while 循环,一个是循环遍历聊天日志以检索日期、用户名、消息,另一个 while 循环 是从单独的表中检索图标这有两列 charsimage (image-name.*) 我可以显示 table chat 中的所有内容,但似乎无法获得第二个包含 str_replacewhile 循环 循环遍历 emo_cons table 中可能包含值的每一行,它仅如果我添加新的 chars 值和 image-name.*,则会显示表中的最后一行,它会显示最后一个

// 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


?>

我需要帮助理解我做错了什么我尝试移动我的循环来看看我是否错误地构建了它们,但到目前为止还没有运气我只是希望能够显示聊天消息,如果消息包含一个字符,则用图标替换该字符,并且始终执行此操作,而不仅仅是表中的最后一行。

我知道我可能不会在这方面得到任何帮助,但值得一提的是也许有人可以看到我的错误。提前致谢。

enter image description here enter image description here

更新工作 enter image description here

最佳答案

emo_cons while 循环中进行以下更改

<?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

?>

请告诉我它是否适合您。

<小时/>

为什么您的代码不起作用

变量 $chars$imagetag 都在 while 循环内,并且每次迭代值都会被覆盖。因此最后一个值存储在两个变量中。

为了解决这个问题,我创建了 2 个具有相同名称的数组,并将所有必需的值插入数组中,稍后我们在 str_replace 函数中使用它。

关于php - 显示多行(WHILE)图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38525440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com