如何在signle one中添加多个while循环

时间:2017-07-22 08:24:58

标签: php while-loop

我的php文件中有多个while循环。首先加载循环数据,然后加载第二个循环数据,然后加载第三个。

但我希望以时间方式显示数据,例如最近的数据帖子是第一位的。

这是我的PHP代码示例:

//状态数据加载

<?php  
   $status_res=mysqli_query($cn,"SELECT datetime,status FROM status");    
   while($status=mysqli_fetch_array($status_res))
   {
     echo "load status";
      echo "<br>".$status['datetime'];
      echo "</br>". $status['status'];
   }
?>

//现在加载更改个人资料图片的循环

<?php
 $profile=mysqli_query($cn,"SELECT datetime,src FROM profile_photo");
 while($profile_r=mysqli_fetch_array($profile))
 {
     echo "load profile pic";
     echo "<br>".profile_r['datetime'];
     echo "<br>".$profile_r['src'];         
 }
?>

//现在加载其他数据

<?php
  $other_data=mysqli_query($cn,"SELECT fname,datetime FROM other_tbl")
  while($other=mysqli_fetch_array($other_data))
  {
      echo "<br> other data";
      echo "<br>".$other['datetime];
      echo "<br>".$other['fname'];
  }
?>

然后现在使用此代码我得到以下输出bt有显示数据的错误

load status
2017-07-22 16:22:12
this is my first status    
2017-07-22 16:20:12
this is my second status
load profile
2017-07-22 16:25:12
this is my first profile    
2017-07-22 16:24:12
this is my second profile    
load status
2017-07-22 16:30:12
vishal
2017-07-22 16:28:12
vishal

但我想这样出来。

load status
2017-07-22 16:30:12
vishal
2017-07-22 16:28:12
vishal
load profile
2017-07-22 16:25:12
this is my first profile    
2017-07-22 16:24:12
this is my second profile  
load status
2017-07-22 16:22:12
this is my first status     
2017-07-22 16:20:12
this is my second status
那么如何解决这个问题呢? 谢谢

2 个答案:

答案 0 :(得分:0)

  

您必须将所有数据放在一个表中,并使用日期时间

对它们进行排序
<?php  
    $array = array();

    $status_res=mysqli_query($cn,"SELECT datetime,status FROM status";    
    while($status=mysqli_fetch_array($status_res))
    {
        $array[] = array(
            'title' => 'status',
            'value' => $status['status'],
            'datetime' => $status['datetime'],
            'output' => "<br>{$status['datetime']}<br>{$status['status']}"
        );
    }

    $profile=mysqli_query($cn,"SELECT datetime,src FROM profile_photo";
    while($profile_r=mysqli_fetch_array($profile))
    {

        $array[] = array(
            'title' => 'profile',
            'value' => $profile_r['src'],
            'datetime' => $profile_r['datetime'],
            'output' => "<br>{$profile_r['datetime']}<br>{$profile_r['src']}"
        );        
    }


    $other_data=mysqli_query($cn,"SELECT fname,datetime FROM other_tbl")
    while($other=mysqli_fetch_array($other_data))
    {

        $array[] = array(
            'title' => 'other',
            'value' => $other['fname'],
            'datetime' => $other['datetime'],
            'output' => "<br>{$other['datetime']}<br>{$other['fname']}"
        );  
    }

    $array = array_sort($array, 'datetime', SORT_DESC);

    $titles = array();
    foreach($array as $key => $data){
        if(! in_array($data['title'],$titles)){
            $titles[] = $data['title'];
            echo "load: {$data['title'}<br>";
        }

        echo $data['output'];
    }
?>

答案 1 :(得分:0)

如果你想在一个循环中完成它,那么使用SQL将数据组合成你想要的顺序......

<?php  
   $status_res=mysqli_query($cn,
           "SELECT datetime,status FROM status
            UNION
            SELECT datetime,src FROM profile_photo
            UNION 
            SELECT datetime, fname FROM other_tbl
            ORDER BY 1
");    
   while($status=mysqli_fetch_array($status_res))
   {
     echo "load status";
      echo "<br>".$status['datetime'];
      echo "</br>". $status['status'];
   }
?>

问题在于你不知道每个记录的记录类型。您可以添加新列,例如

SELECT 'a' as type, datetime,status FROM status
            UNION
            SELECT 'b', datetime,src FROM profile_photo
            UNION
            SELECT 'c', datetime, fname FROM other_tbl
            ORDER BY 2

现在会给你一个名为type的列,因此$status['type']会告诉你数据的类型是什么。