我已经设置了消息类型系统,并且我正在使用mysql while循环从表中回显内容,并且还试图回显用户个人资料图像,以便它出现在每个被回显的结果旁边。由于这是一种双向消息系统,因此它们是“ to_user_id” /收件人和“ from_user_id” /发送者的照片。这被编码为$ photo和$ photo2

一切正常,图像会按while循环回显每个结果的样子显示出来,但是由于某种原因,第一个结果不是回显图像,只是div容器

有人知道为什么是这样吗。这是我的代码,谢谢。

<div class="message_field">
<div class="inner_frame">

<?
$query_for_result=mysql_query("SELECT *
                        FROM ptb_messages
                        WHERE msg_id like '%".$query."%'");
                        $inbox_set = get_inbox();
while($data_fetch=mysql_fetch_array($query_for_result))
if (isset($_SESSION['user_id'])) {
if ($data_fetch['from_user_id'] == $_SESSION['user_id']){

{


$photo = "data/photos/{$data_fetch['from_user_id']}/_default.jpg";
if (!file_exists($photo)) {
    $photo = "data/photos/0/_default.jpg";


}


$photo2 = "data/photos/{$data_fetch['to_user_id']}/_default.jpg";
if (!file_exists($photo2)) {
    $photo2 = "data/photos/0/_default.jpg";


}



?>


<div class="message_bubble2">
<div class="msg_bubble_img"><a href=\"profile.php?id={$inbox['id']}\"><img src="<?php echo $photo ?>" width="40" heigh="40" border="0"></a></div><?php echo "".$data_fetch['content']."";?><div class="message_bubble_tail2"></div><div class="message_bubble_tail2_shadow"></div><div class="message_bubble_tail2_shadow_bottom"></div><div class="message_bubble_tail2_outline"></div>
<div class="message_bubble_footer2">
<?php
$inbox_set = get_inbox();
while ($inbox = mysql_fetch_array($inbox_set)) { ?>
<?php echo "".$review_from."";  ?>&nbsp;<?
$datetime1 = new DateTime();
$datetime2 = new DateTime ($reviews['date_added']);
$interval = $datetime1->diff($datetime2);
$mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
$labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
$i = 0;
foreach($mdhms as $t){
  if($t > 0) break;
  $i+=1;
}
echo "sent by you ".$t.$labels[$i]." ago"; } ?>
</div></div>


<?
}
}else{


if (isset($_SESSION['user_id'])) {
if ($data_fetch['from_user_id'] !== $_SESSION['user_id']){

{

?>


<div class="message_bubble"><div class="msg_bubble_img2"><a href=\"profile.php?id={$inbox['id']}\"><img src="<?php echo $photo2 ?>" width="40" heigh="40" border="0"></a></div><?php echo "".$data_fetch['content']."";?><div class="message_bubble_tail"></div><div class="message_bubble_tail_shadow"></div><div class="message_bubble_tail_shadow_bottom"></div><div class="message_bubble_tail_outline"></div>
<div class="message_bubble_footer">
<?php
$inbox_set = get_inbox();
while ($inbox = mysql_fetch_array($inbox_set)) { ?>
<?php echo "".$review_from."";  ?>&nbsp;<?
$datetime1 = new DateTime();
$datetime2 = new DateTime ($reviews['date_added']);
$interval = $datetime1->diff($datetime2);
$mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
$labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
$i = 0;
foreach($mdhms as $t){
  if($t > 0) break;
  $i+=1;
}
echo "sent from ".$inbox['from_user_id']." ".$t.$labels[$i]." ago"; } ?>
</div></div>


<? } } } } } ?>

最佳答案

尝试强制执行SELECT语句的顺序:-


   

<?

if (isset($_SESSION['user_id']))
{
    $query_for_result=mysql_query("SELECT *, IF(from_user_id= '".mysql_real_escape_string($_SESSION['user_id'])."', 0, 1) AS ForceOrder
                            FROM ptb_messages
                            WHERE msg_id like '%".$query."%'
                            ORDER BY ForceOrder, ");
                            $inbox_set = get_inbox();
    while($data_fetch=mysql_fetch_array($query_for_result))
    {
        if ($data_fetch['from_user_id'] == $_SESSION['user_id'])
        {
            {
                $photo = "data/photos/{$data_fetch['from_user_id']}/_default.jpg";
                if (!file_exists($photo))
                {
                    $photo = "data/photos/0/_default.jpg";
                }
                $photo2 = "data/photos/{$data_fetch['to_user_id']}/_default.jpg";
                if (!file_exists($photo2))
                {
                    $photo2 = "data/photos/0/_default.jpg";
                }
                ?>
                <div class="message_bubble2">
                <div class="msg_bubble_img"><a href=\"profile.php?id={$inbox['id']}\"><img src="<?php echo $photo ?>" width="40" heigh="40" border="0"></a></div><?php echo "".$data_fetch['content']."";?><div class="message_bubble_tail2"></div><div class="message_bubble_tail2_shadow"></div><div class="message_bubble_tail2_shadow_bottom"></div><div class="message_bubble_tail2_outline"></div>
                <div class="message_bubble_footer2">
                <?php
                $inbox_set = get_inbox();
                while ($inbox = mysql_fetch_array($inbox_set))
                { ?>
                    <?php echo "".$review_from."";  ?>&nbsp;<?
                    $datetime1 = new DateTime();
                    $datetime2 = new DateTime ($reviews['date_added']);
                    $interval = $datetime1->diff($datetime2);
                    $mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
                    $labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
                    $i = 0;
                    foreach($mdhms as $t)
                    {
                      if($t > 0) break;
                      $i+=1;
                    }
                    echo "sent by you ".$t.$labels[$i]." ago";
                } ?>
                </div></div>


                <?
            }
        }
        else
        {
            if (isset($_SESSION['user_id']))
            {
                if ($data_fetch['from_user_id'] !== $_SESSION['user_id'])
                {
                    {
                        ?>
                        <div class="message_bubble"><div class="msg_bubble_img2"><a href=\"profile.php?id={$inbox['id']}\"><img src="<?php echo $photo2 ?>" width="40" heigh="40" border="0"></a></div><?php echo "".$data_fetch['content']."";?><div class="message_bubble_tail"></div><div class="message_bubble_tail_shadow"></div><div class="message_bubble_tail_shadow_bottom"></div><div class="message_bubble_tail_outline"></div>
                        <div class="message_bubble_footer">
                        <?php
                        $inbox_set = get_inbox();
                        while ($inbox = mysql_fetch_array($inbox_set))
                        { ?>
                            <?php echo "".$review_from."";  ?>&nbsp;<?
                            $datetime1 = new DateTime();
                            $datetime2 = new DateTime ($reviews['date_added']);
                            $interval = $datetime1->diff($datetime2);
                            $mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
                            $labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
                            $i = 0;
                            foreach($mdhms as $t)
                            {
                                if($t > 0) break;
                                $i+=1;
                            }
                            echo "sent from ".$inbox['from_user_id']." ".$t.$labels[$i]." ago";
                        } ?>
                        </div></div>


                        <?
                    }
                }
            }
        }
    }
}
?>

关于php - echo 出MySQL中的图像,而循环不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20495277/

10-14 13:00
查看更多