Jump to content

selecting MAX, kinda.


mforan

Recommended Posts

<?php
$query = "SELECT allianceno FROM alliances WHERE draft=1";
$result = mysql_query($query) or die("Error: ".mysql_error());
$draf = array();
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {$draf[] = $row[0];}

for ($i = 0; $i < count($draf); $i++) {
$query = "SELECT COUNT(username) FROM users WHERE alliance='".$draf[$i][0]."'";
$result = mysql_query($query) or die("Error: ".mysql_error());
$usersina = array();
$row = mysql_fetch_array($result, MYSQL_NUM);
$usersina = $row[0];
echo "alliance number = $draf[$i] users: $usersina<br>";
}

?>

 

this simply echos out the count of usernames within the selected alliance number. the count comes from the users table.

 

my problem is that i want to grab only the alliance number with the highest amount of users in it so i can use this alliance number elsewhere. any ideas peoplez?

Link to comment
https://forums.phpfreaks.com/topic/204045-selecting-max-kinda/
Share on other sites

Yep, you are right. I had my kid in my hands so yea. Here is a better version, given my sql guesses of your system are correct.

 

<?php
$query = "SELECT count(u.username) as membercnt, u.alliance FROM alliances a, users u WHERE a.draft=1 AND u.alliance = a.allianceno GROUP BY a.allianceno ORDER BY count(u.username) DESC LIMI 10 ";
$result = mysql_query($query) or die("Error: ".mysql_error());

$topTen = array();
$display = "";
while ($row = mysql_fetch_assoc($result)) {
    $topTen[] = $row["membercnt"];
    $display .= "Alliance: " . $row['alliance'] . " has " . $row['membercnt'] . "<br />";
}

echo $display;

?>

Link to comment
https://forums.phpfreaks.com/topic/204045-selecting-max-kinda/#findComment-1068759
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.