doubiankang2845 2016-09-14 03:26
浏览 46

PHP将变量从一个循环传递到另一个循环并在循环外部进行排序

First I have a loop that displays a given users friend list.

$stmt=$db->prepare('SELECT friend1, friend2 
                    FROM list_friends 
                    WHERE (friend1 = :user AND friend2 <> :user) 
                    OR (friend2 = :user AND friend1 <> :user)');

$stmt->bindParam(':user', $username);
$stmt->execute();
$row = $stmt->fetchAll()
foreach  ($row AS $row) {
    if ($row[friend1] !== $username) {
        $friend = $row[friend1]; 
    } else {
        $friend = $row[friend2]; 
    }
    echo $friend;
}

Second I have a series of arrays being compared and sorted by time and date, then echo'd

$stmt=$db->prepare('SELECT * FROM banners WHERE username = :user');
$stmt->bindParam(':user', $friend);
$stmt->execute();
$row = $stmt->fetchAll();

$stmt=$db->prepare('SELECT * FROM favorites WHERE username = :user');
$stmt->bindParam(':user', $friend);
$stmt->execute();
$row1 = $stmt->fetchAll();

$stmt=$db->prepare('SELECT * FROM sites WHERE username = :user');
$stmt->bindParam(':user', $friend);
$stmt->execute();
$row2 = $stmt->fetchAll();

$stmt=$db->prepare('SELECT * FROM social_posts WHERE username = :user');
$stmt->bindParam(':user', $friend);
$stmt->execute();
$row3 = $stmt->fetchAll();

if ($db2->query("SHOW TABLES LIKE 'elfinder_file_".strtolower($friend)."'"
           )->rowCount() > 0
){
$stmt=$db2->prepare("SELECT * FROM elfinder_file_".strtolower($friend)." WHERE mime <> 'directory' GROUP BY time");
$stmt->execute();
$row4 = $stmt->fetchAll();
}


foreach( $row AS $banner_table ) {
    $data[] = array('type' => 'banner', 'time' => $banner_table["time"]);
}
foreach( $row1 AS $favorites_table ) {
    $data[] = array('type' => 'favorite', 'time' => $favorites_table["time"]);
}
foreach( $row2 AS $sites_table ) {
    $data[] = array('type' => 'sites', 'time' => $sites_table["time"], 'site' => $sites_table["url"], 'title' => $sites_table["title"]);
}
foreach( $row3 AS $social_table ) {
    $data[] = array('type' => 'social', 'time' => $social_table["time"], 'thetype' => $social_table["type"]);
}

if ($db2->query("SHOW TABLES LIKE 'elfinder_file_".strtolower($_GET[user])."'")->rowCount() > 0 )
{
    foreach( $row4 AS $photos_table ) {
        $data[] = array('type' => 'photo', 'time' => $photos_table["time"]);
    }
}

function cmp($a, $b)  {
    $ad = new DateTime($a['time']);
    $bd = new DateTime($b['time']);   
    if ($ad == $bd) {
        return 0;
    }
    return $ad < $bd ? -1 : 1;
}
if ($data !== NULL) {
    usort($data, "cmp");
    for($i=(count($data)-1);$i>=0;$i--){
        $tttime = $data[$i]['time'];
        $ttime = new DateTime($tttime);
        $stmt=$db->prepare('SELECT timezone FROM member_credits WHERE username = :user');
        $stmt->bindParam(':user', $username);
        $stmt->execute();
        $row = $stmt->fetch();
        if ($row[timezone] === NULL) { $row[timezone] = 'America/Denver'; }
        $usersTimezone = (new DateTimeZone($row[timezone]));
        $ttime->setTimeZone($usersTimezone);
        $ttimee = $ttime->format('D M j, Y g:i A');
        if($data[$i]['type']=='favorite'){
            echo '<li style="padding: 8px 5px 8px 95px;"><span class="date"><b>'.$ttimee.'</b></span>'.$friend.' added a website to their favorites while surfing the exchange.</li>';
        } elseif($data[$i]['type']=='banner'){
            echo '<li style="padding: 8px 5px 8px 95px;"><span class="date"><b>'.$ttimee.'</b></span>'.$friend.' added a banner to the banner exchange.</li>';  
        } elseif($data[$i]['type']=='sites'){
            echo '<li style="padding: 8px 5px 8px 95px;"><span class="date"><b>'.$ttimee.'</b></span>'.$friend.' added the website &nbsp;<a href="'.$data[$i]['site'].'" target="_blank" style="font-weight:bolder;">'.$data[$i]['title'].'</a>&nbsp; to the traffic exchange.</li>';  
        } elseif($data[$i]['type']=='social'){
            echo '<li style="padding: 8px 5px 8px 95px;"><span class="date"><b>'.$ttimee.'</b></span>'.$friend.' has requested a social exchange on '.$data[$i]['thetype'].'.</li>';  
        } elseif($data[$i]['type']=='photo'){
            echo '<li style="padding: 8px 5px 8px 95px;"><span class="date"><b>'.$ttimee.'</b></span>'.$friend.' uploaded a photo to their profile.</li>';  
        }
    }
}

The problem I am having is that if I do the first foreach, then the second, it only echo's results from the last friend in the loop (logically), and if I contain the second foreach inside the first, it generates all results for each user one by one, so all of user1's results will be returned and sorted by time, then all of user2's, and so on.

The result I am looking for, is a list of results all joined together, and then each returned based on time.

SAMPLE RESULTS:

user1 uploaded a photo at 12:00pm
user2 added a banner at 11:00 am
user2 uploaded a photo at 10:00 am
user1 added a favorite 9:00 am
etc.
etc.
etc.
  • 写回答

2条回答 默认 最新

  • dongyi1939 2016-09-14 06:04
    关注

    Instead of echoing the results, use an array with the time as an index. Gather all your results and sort the array by the index. Something along the lines of

    $li[$ttimee] .= '<li style="padding: 8px 5px 8px 95px;"><span class="date"><b>'.$ttimee.'</b></span>'.$friend.' uploaded a photo to their profile.</li>';  
    

    I use .= so things happening at the same time are not overitten.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)