从 mysql 组织 PHP 数组

发布于 2024-09-06 11:06:23 字数 1382 浏览 7 评论 0原文

你好,我有一个社交网站。

我想要它做的是提取我朋友的状态更新。

基本上它的作用是我有一个 mysql 查询,可以提取我所有的朋友,在 while 循环中,还有另一个 mysql 查询,可以从我的朋友那里提取状态。

我希望它按日期顺序排列,但由于它在另一个 while 循环中,它所做的是从朋友 1 然后 2 然后 3 中提取所有状态,而不是按日期顺序。我什至尝试过按日期订购,但只是在朋友内按日期订购。 我的想法是,我可以将所有内容放入一个数组中,朋友是一回事,值是统计数据。然后只需按值排序这是否有效以及我该如何做到这一点。

朋友和统计数据位于两个不同的表中,

非常感谢

代码:

$friendssql = mysql_query("SELECT * FROM Friends WHERE sender='$id'"); while($row = mysql_fetch_object($friendssql)) { $friendid = $row->;接受者;

    $frsql = mysql_query("SELECT * FROM myMembers WHERE id='$friendid'");
    while($rowa = mysql_fetch_object($frsql)) {
        $ufirstname = $rowa-> firstname;
        $ulastname = $rowa-> lastname;
    }


$blabsql = mysql_query("SELECT * FROM blabbing WHERE mem_id='$friendid' ORDER BY blab_date DESC");
while($rowb = mysql_fetch_object($blabsql)) {
    $blab = $rowb-> the_blab;
    $blabd =$rowb-> blab_date;

    $ucheck_pic = "members/$friendid/image01.jpg";
    $udefault_pic = "members/0/image01.jpg";
    if (file_exists($ucheck_pic)) {
    $blabber_pic = "<img src=\"$ucheck_pic\" width=\"50px\" border=\"0\" />"; // forces picture to be 100px wide and no more
    } else {
    $blabber_pic = "<img src=\"$udefault_pic\" width=\"40px\" border=\"0\" />"; // forces default picture to be 100px wide and no more
    }

Hi i have a social networking website.

what i want it to do is pull out my friends status updates.

basically what it does is i have a mysql query that pulls out all of my friends and in that while loop there is another mysql query that pulls out the status's from my friends.

i want it to be in order of date but since its one while loop in another what it does is pull out all status's from friend 1 then 2 then 3 and not in order by date. i even tried ORDER BY DATE but that just ordered it by date within the friend..
my thought is that i could putt it all in an array and friends is one thing and the values is the stats. then just sort by values would this work and how could i do it.

the friend and stats are in two differants tables

THANKS SO MUCH

CODE:

$friendssql = mysql_query("SELECT * FROM friends WHERE sender='$id'");
while($row = mysql_fetch_object($friendssql)) {
$friendid = $row-> accepter;

    $frsql = mysql_query("SELECT * FROM myMembers WHERE id='$friendid'");
    while($rowa = mysql_fetch_object($frsql)) {
        $ufirstname = $rowa-> firstname;
        $ulastname = $rowa-> lastname;
    }


$blabsql = mysql_query("SELECT * FROM blabbing WHERE mem_id='$friendid' ORDER BY blab_date DESC");
while($rowb = mysql_fetch_object($blabsql)) {
    $blab = $rowb-> the_blab;
    $blabd =$rowb-> blab_date;

    $ucheck_pic = "members/$friendid/image01.jpg";
    $udefault_pic = "members/0/image01.jpg";
    if (file_exists($ucheck_pic)) {
    $blabber_pic = "<img src=\"$ucheck_pic\" width=\"50px\" border=\"0\" />"; // forces picture to be 100px wide and no more
    } else {
    $blabber_pic = "<img src=\"$udefault_pic\" width=\"40px\" border=\"0\" />"; // forces default picture to be 100px wide and no more
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知所踪 2024-09-13 11:06:23

将数据放入数组后,您可以查看 PHP 中的一些各种数组排序函数:http://php.net/manual/en/array.sorting.php

Once you've put your data into the array, you could take a look at some of the various array sorting functions in PHP: http://php.net/manual/en/array.sorting.php

淡淡離愁欲言轉身 2024-09-13 11:06:23

为什么不在一个查询中完成这一切呢?这是伪 sql,因此您必须使用真实的表和关系进行修改。

select f.name,s.statustext
from friends f 
     inner join status s
         on s.friend_id = f.id
     inner join myfriends mf
         on mf.friend_id = f.id
where mf.myid = 'myid'
order by f.name, s.datestamp

或类似的东西。

why not do it all in one query? this is psuedo sql, so you'll have to modify with your real tables and relationships.

select f.name,s.statustext
from friends f 
     inner join status s
         on s.friend_id = f.id
     inner join myfriends mf
         on mf.friend_id = f.id
where mf.myid = 'myid'
order by f.name, s.datestamp

or something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文