PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi all,
Can someone please point me in the right direction.
If I have a table called banks2 with a field called name and it could have any number of entries that are the same.
Eg
Netbank
Netbank
Commonwealth
Netbank
Westpac
How would I loop through to get the number of times each name appears is it SELECT DISTINCT or will that only give me each unique name and therefore 1 entry. I have the code below to get one name.
$query = 'SELECT * FROM `banks2` WHERE '
. ' `name`= \'Commonwealth\' LIMIT 0, 30 ';
$result = @mysql_query ($query); // Run the query.
$num1 = mysql_num_rows($result);
if ($num > 0) { // If it ran OK, display the records.
} else { // If it did not run OK.
echo '<p class="error">There are currently no entries.</p>';
}
echo '
<tr><td >Commonwealth Bank</td><td ><img src="includes/column.gif" width="'.( $num * 10).'" height="10" /> '.$num.'</td></tr>
';
$query = 'SELECT `name`, COUNT(*) as num FROM `banks2` GROUP BY `name`';
$result = @mysql_query ($query); // Run the query.
$num = mysql_num_rows($result);
if ($num > 0) { // If it ran OK, display the records.
} else { // If it did not run OK.
echo '<p class="error">There are currently no entries.</p>';
}
while ($num = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '
<tr><td >' . $num['name'] . '</td><td ><img src="includes/column.gif" width="'.( $num * 10).'" height="20" />'.$num.'</td></tr>';
}
echo '</table>';
mysql_close(); // Close the database connection.
?>