0

我有这两个对象:

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

我正在尝试获取$departures->total特定pilotid的 in $userinfo= $userinfo->pilotid

但是,我不确定如何链接它们,以便它与 A 对 B 相呼应。我有类似的东西,但它没有显示任何内容。

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

此外,第一个对象的调用方式如下:

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

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

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

4

2 回答 2

3

源于 OP 提供的数据,我假设它$departures与 1:n 关系$userinfo$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";

此查询将返回由相关飞行员运行的来自不同机场的前 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.";

?>

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

于 2013-02-24T15:29:49.743 回答
0

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

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

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

于 2013-02-24T15:18:04.140 回答