在循环不工作时回显mysql中的图像?

时间:2013-12-10 13:00:20

标签: php html mysql

我有一个消息类型系统设置,我正在使用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>


<? } } } } } ?>

1 个答案:

答案 0 :(得分:0)

尝试强制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>


                        <? 
                    } 
                } 
            } 
        } 
    }
}
?>