Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Stemming from the data provided by the OP, I am assuming, that <code>$departures</code> has a 1:n relationship with <code>$userinfo</code>, <code>$userinfo</code> being the 1 containing the pilotid.</p> <p>So, in oder to find out how many departures that pilot had in total, there's two possible ways, one by using a subquery, which would mean something like this:</p> <pre><code>SELECT (SELECT COUNT(*) FROM `departures` WHERE `pilot_id` = ID) as total, * FROM pilots; </code></pre> <p>In this case, your total would be in the <code>total</code> column of your <code>$userinfo</code> query.</p> <p>The second attempt makes use of actual PHP. In this scenario, you do the counting yourself.</p> <p>First step: Getting the pilot information:</p> <pre><code>$userinfo = array(); while($row = fetch()) { $row-&gt;total = 0; $row-&gt;departures = array(); $userinfo[$row-&gt;pilotid] = $row; } </code></pre> <p>These lines will give you the pilot data keyed to their IDs in an array. Step two. Glueing the departures to the pilots.</p> <pre><code>while($row = fetch()) { if(isset($userinfo[$row-&gt;pilotid])) { $userinfo[$row-&gt;pilotid]-&gt;departures[] = $row; ++$userinfo[$row-&gt;pilotid]-&gt;total; } } </code></pre> <p>If this isn't what you're looking for, I will be needing more information from you, however like this you will be able to get the departures of the pilots either by making use of the <code>total</code> variable in the <code>$userinfo</code> object, or by simply calling <code>count</code> on the <code>departures</code> array.</p> <p>Another variant, which keeps the actual departures and the pilots apart would look like this:</p> <p>First step: Getting the pilot information:</p> <pre><code>$userinfo = array(); while($row = fetch()) { $row-&gt;total = 0; $userinfo[$row-&gt;pilotid] = $row; } </code></pre> <p>These lines will give you the pilot data keyed to their IDs in an array. Step two. Glueing the departures to the pilots.</p> <pre><code>$departures = array(); while($row = fetch()) { if(isset($userinfo[$row-&gt;pilotid])) { $departures[] = $row; ++$userinfo[$row-&gt;pilotid]-&gt;total; } } </code></pre> <p>I hope you will find these suggestions useful.</p> <p>Edit: After a few additional information from the OP, I suggest changing the query used to access the information in question.</p> <p>This is the original code by the OP</p> <pre><code>$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-&gt;depicao); $pilotid = Auth::$userinfo-&gt;pilotid; ?&gt; &lt;tr class="awards_table1"&gt; &lt;td width="10%"&gt;&lt;?php echo $departure-&gt;depicao; ?&gt;&lt;/td&gt; &lt;td width="10%"&gt;&lt;img src="&lt;?php echo Countries::getCountryImage($dep_airport-&gt;country); ?&gt;" /&gt;&lt;/td&gt; &lt;td width="60%"&gt;&lt;?php echo $dep_airport-&gt;name; ?&gt;&lt;/td&gt; &lt;td width="20%"&gt;&lt;?php echo $pilotid-&gt;{$departures-&gt;total}; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; </code></pre> <p>First thing we'll change is the query used to get the departures. Why fetch all the information, if we actually only want the one of the pilot in question?</p> <pre><code>$pilotid = $userinfo-&gt;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"; </code></pre> <p>This query will return the Top 5 of the departures from the different airports, which have been run by the pilot in question. As for the rest:</p> <pre><code>$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-&gt;depicao); ?&gt; &lt;tr class="awards_table1"&gt; &lt;td width="10%"&gt;&lt;?php echo $departure-&gt;depicao; ?&gt;&lt;/td&gt; &lt;td width="10%"&gt;&lt;img src="&lt;?php echo Countries::getCountryImage($dep_airport-&gt;country); ?&gt;" /&gt;&lt;/td&gt; &lt;td width="60%"&gt;&lt;?php echo $dep_airport-&gt;name; ?&gt;&lt;/td&gt; &lt;td width="20%"&gt;&lt;?php echo $departure-&gt;total; ?&gt;&lt;/td&gt; //Here is the actually changed Layout code &lt;/tr&gt; &lt;?php } } else echo "This pilot didn't have any departures yet."; </code></pre> <p>?></p> <p>With these alterations, your code should output the desired result. It is completely untested though. However it should give you the right idea.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload