将两个PHP对象链接在一起

时间:2013-02-24 15:12:20

标签: php

我有这两个对象:

$userinfo->pilotid;
$departures->total; 

我正试图$departures->total = pilotid中的特定$userinfo获取$userinfo->pilotid

但是,我不知道如何将它们链接起来,因此它与A相呼应。我有类似的东西,但它没有显示任何内容。

<?php echo $pilotid->$departures->total; ?>

此外,第一个对象被调用如下:

$pilotid = Auth::$userinfo->pilotid;

这是使用查询从中收集对象的表的结构。

http://i.stack.imgur.com/ZaYVe.jpg

2 个答案:

答案 0 :(得分:3)

根据OP提供的数据,假设$departures$userinfo的关系为1:n,$userinfo为包含pilotid的1。

所以,为了找出飞行员总共离开的次数,有两种可能的方式,一种是使用子查询,这意味着这样:

SELECT (SELECT COUNT(*) FROM `departures` WHERE `pilot_id` = ID) as total, * FROM pilots;

在这种情况下,您的总数将位于total查询的$userinfo列中。

第二次尝试使用实际的PHP。在这种情况下,您自己进行计数。

第一步:获取试点信息:

$userinfo = array();
while($row = fetch()) {
    $row->total = 0;
    $row->departures = array();
    $userinfo[$row->pilotid] = $row;
}

这些行将为您提供在数组中键入其ID的导频数据。 第二步。向飞行员着手离开。

while($row = fetch()) {
    if(isset($userinfo[$row->pilotid])) {
        $userinfo[$row->pilotid]->departures[] = $row;
        ++$userinfo[$row->pilotid]->total;
    }
}

如果这不是你想要的,我将需要你的更多信息,但是这样你就可以通过使用total变量来获得飞行员的离去在$userinfo对象中,或只是在count数组上调用departures

保持实际离场和飞行员分开的另一种变体看起来像这样:

第一步:获取试点信息:

$userinfo = array();
while($row = fetch()) {
    $row->total = 0;
    $userinfo[$row->pilotid] = $row;
}

这些行将为您提供在数组中键入其ID的导频数据。 第二步。向飞行员着手离开。

$departures = array();
while($row = fetch()) {
    if(isset($userinfo[$row->pilotid])) {
        $departures[] = $row;
        ++$userinfo[$row->pilotid]->total;
    }
}

我希望你会发现这些建议很有用。

编辑: 在OP的一些额外信息之后,我建议更改用于访问相关信息的查询。

这是OP的原始代码

$dep_query = "SELECT COUNT(pilotid) as total, depicao, pilotid FROM phpvms_pireps GROUP
    BY depicao, pilotid ORDER BY total DESC LIMIT 5";

    $fav_deps = DB::get_results($dep_query);

    foreach($fav_deps as $departure)
    {
            $dep_airport = OperationsData::getAirportinfo($departure->depicao);
            $pilotid = Auth::$userinfo->pilotid;
    ?>
            <tr class="awards_table1">
                    <td width="10%"><?php echo $departure->depicao; ?></td>
                    <td width="10%"><img src="<?php echo Countries::getCountryImage($dep_airport->country); ?>" /></td>
                    <td width="60%"><?php echo $dep_airport->name; ?></td>
                    <td width="20%"><?php echo $pilotid->{$departures->total}; ?></td>
            </tr>
    <?php
    }
    ?>

我们要改变的第一件事是用于获得离开的查询。如果我们实际上只想要一个有问题的飞行员,为什么要获取所有信息?

$pilotid = $userinfo->pilotid; //As per Chat discussion
$dep_query = "SELECT COUNT(depicao) as total, depicao FROM phpvms_pireps WHERE pilotid = $pilotid GROUP BY depicao ORDER BY total DESC LIMIT 5";

此查询将返回来自不同机场的离开前五名,这些机场由相关飞行员运行。至于其余的:

$fav_deps = DB::get_results($dep_query);

if(is_array($fav_deps)) {    //For the general use
    foreach($fav_deps as $departure) {
        $dep_airport = OperationsData::getAirportinfo($departure->depicao); ?>
        <tr class="awards_table1">
            <td width="10%"><?php echo $departure->depicao; ?></td>
            <td width="10%"><img src="<?php echo Countries::getCountryImage($dep_airport->country); ?>" /></td>
            <td width="60%"><?php echo $dep_airport->name; ?></td>
            <td width="20%"><?php echo $departure->total; ?></td> //Here is the actually changed Layout code
        </tr>
<?php 
    } 
} else echo "This pilot didn't have any departures yet.";

&GT;

通过这些更改,您的代码应输出所需的结果。但它完全未经测试。但它应该给你正确的想法。

答案 1 :(得分:0)

我认为你需要的是这个(注意大括号):

<?php echo $pilotid->{$departures->total}; ?>

除非我误解了这个问题......