Web Hosting Talk







View Full Version : php / mysql help with counting columns


MMD
12-13-2009, 05:41 PM
Hi there, it's been a while since I last used these forums, but I was hoping the helpful folk at WHT could lend me a hand with the following problem;

I have two columns that contain names, I want to count the names in column scorer1 and scorer2 and see how many times each name appears (each name can appear in either scorer1 or scorer2 so I need to add them together). Is this possible?

Eventually I want this information to go into a graph, so preferably the output would be in an array with; name and count so that I can sort through the names who appear the most.

Is this possible? If anyone would lend a hand or point me in the right direction that would be great. Even a sudo explanation would be helpful :)

I have attached a pic of my table so you can see the names reoccurring in scorer1 and scorer2 that need to be aggregated.

foobic
12-13-2009, 06:34 PM
What do scorer1 and scorer2 represent? Are you sure you won't need a scorer3 at some stage?

Subject to answers to the above, I suspect you'd probably be better off pulling the scorers out to a separate table containing, for example:
some_sort_of_game_id,
player_name
scorer_suffix (1 or 2)
etc.

From which the counts could be obtained directly with "SELECT player_name, count(*) FROM new_table WHERE conditions GROUP BY player_name".

(And then for more efficient data storage you might also consider another table of player_id, player_name and use player_id in the other table).

MMD
12-13-2009, 06:41 PM
Hi Foobic - thanks for the reply. Based on strict rules (to simplify the game design) players will only ever choose two scorers to predict (this game works at the moment, I'm just trying to count the number of times people have predicted a certain player will score - as these are stored in separate columns but the name of a player can exist in scorer1, scorer2 or both columns it made it tricky to count.. however...

I have managed to do it, and it was simpler than I first thought;

$result = mysql_query("SELECT scorer1, scorer2 FROM predictions");

while($row = mysql_fetch_array($result)){
$playername[] = $row['scorer1'];
$playername[] = $row['scorer2'];
}

print_r(array_count_values($playername));

Thanks for the reply, but this should suffice for now :) thanks for reading / replying (foobic)

MMD
12-13-2009, 08:11 PM
And the spanky new graph can be seen here; http://finestfootball.co.uk/predictionleague/wycombewanderers/index.php (lower graph) :)

addcomex
12-16-2009, 07:52 AM
I have put mysql content in cells fine before just this time I need it so there are 3 cells in each row and info in each cell. There is also some pagination info in here so that it splits it up 12 chunks of info per page.

PHP Code:
<?
require ('dbconnect.php');

$Limit = 12;


$page=$_GET["page"]; //Get the page number to show
If($page == "") $page=1; //If no page number is set, the default page is 1

//Get the number of results
$tquery = "SELECT * FROM portfolio imageid DESC";
$tmyresult = mysql_query($tquery) or die(mysql_error());
$NumberOfResults = mysql_num_rows($tmyresult);

//Get the number of pages
$NumberOfPages=ceil($NumberOfResults/$Limit);

$query = "SELECT * FROM portfolio imageid DESC LIMIT " . ($page-1)*$Limit . ",$Limit";
$myresult = mysql_query($query) or die(mysql_error());
$Nav="";
If($page > 1) {
$Nav .= "<A HREF=\"portfolio.php?page=" . ($page-1) . "\"><<Prev</A>";
}
For($i = 1 ; $i <= $NumberOfPages ; $i++) {
If($i == $page) {
$Nav .= " <B>$i</B> ";
}Else{
$Nav .= " <A HREF=\"portfolio.php?page=" . $i . "\">$i</A> ";
}
}
If($page < $NumberOfPages) {
$Nav .= "<A HREF=\"portfolio.php?page=" . ($page+1) . "\">Next>></A>";
}

echo "<table>\n";
$columns = 3;
$col_rows = intval($NumberOfResults / $columns);
$count = 1;
$max = $page * $Limit;
$firstone = $page - 1;
$firsttwo = $firstone * $Limit;
$first = $firsttwo +1;
if($page == $NumberOfPages){Echo '<tr><td colspan=3 bgcolor=#404040><p class=size>Showing '.$first.' thru '. $NumberOfResults.' out of '.$NumberOfResults.', Images.</p>&nbsp;&nbsp;&nbsp;'.$nav.'</td></tr>';}else{ Echo '<tr><td colspan=3 bgcolor=#404040><p class=size>Showing '.$first.' thru '.$max.' out of '.$NumberOfResults.', Images.</p>&nbsp;&nbsp;&nbsp;'.$nav.'</td></tr>';}
while($row = mysql_fetch_array($myresult)) {
$row['client'] = stripslashes($row['client']);
$row['type'] = stripslashes($row['type']);
$row['location'] = stripslashes($row['location']);
$count = $count++;
if($count == $col_rows){
echo '<tr>';
echo '<td class=border onMouseOver="this.bgColor = \'#383838\'" onMouseOut ="this.bgColor = \'#000000\'" bgcolor="#000000" width=180><img src="./images/'.$row['location'].'thumb.jpg"><p class=padding><span class=h2>Client:&nbsp;</span>'.$row['client'].'</br><span class=h2>Type:&nbsp;</span>'.$row['type'].'</p></td>';
} else {
echo '<td class=border onMouseOver="this.bgColor = \'#383838\'" onMouseOut ="this.bgColor = \'#000000\'" bgcolor="#000000" width=180><img src="./images/'.$row['location'].'thumb.jpg"><p class=padding><span class=h2>Client:&nbsp;</span>'.$row['client'].'</br><span class=h2>Type:&nbsp;</span>'.$row['type'].'</p></td>';
}
Echo '<tr><td colspan=3 bgcolor=#404040>'.$nav.'</td></tr>';
echo "</table>";
?>