Wordpress Themes - Debt Consolidation - Wordpress Theme - Debt Consolidation - Loans

PDA

View Full Version : Fairly complex question about grouping and counting rows in different tables


jpigford
Apr 8th 2006, 11:26 am
Okay, so the end goal here is to list items and change their size based on the number of "votes" for/against the item.

ie. item1 item2 item3 item4

In the case above, item4 has the most votes for it and item1 has the least.

From a previous post about something like this, I was able to get a block of code that pulls from one table and then counts the number of times a tag is in that table...so if the tag is in there 100 times it'd be larger than a tag only in there 4 times. My situation now is a bit different as I'm pulling from 2 tables and the site of the item is based on the votes.

Here's the previous block of code:

// Font size
$maxfontsize = 1.8;
$minfontsize = 0.6;


$query = mysql_query('SELECT itemName, COUNT(itemName) AS count FROM item GROUP BY itemName');
$max = $low = 0;
while($row = mysql_fetch_array($query))
{
$max = ($row['count'] > $max) ? $row['count'] : $max;
$low = ($row['count'] < $low) ? $row['count'] : $low;

$counts[$row['itemName']] = $row['count'];

$totalCount += $row['count'];
}

// Calculate our range.
$staticrange = $maxfontsize - $minfontsize;
$dynamicrange = $max - $low;

// Figure out what 1 dynamicrange increment will affect the $staticrange
$inc = $staticrange / $dynamicrange; // static = 10, dynamic = 4, inc = 2.5. Each dynamic increment requires 2.5 increments on the scale.

// Output
foreach($counts AS $tagname=>$count)
{
$percent = ($count / $totalCount) * 100;
$percent *= 10;
if ($percent < 5)
{
$backgroundColor = 'level1';
}
elseif ($percent < 15)
{
$backgroundColor = 'level2';
}
elseif ($percent < 100)
{
$backgroundColor = 'level3';
}

$size = $minfontsize + $count * $inc;
echo '<span style="font-size: '.$size.'em;" class="'.$backgroundColor.'"><a title="'.$tagname.'" href="/tag/'.$tagname.'/">'.$tagname.'</a></span> ';
}
That code above successfully pulls all the items from the "items" table, but doesn't resize them because there aren't duplicate items.

Now, I need to pull from a separate database which is setup like this:

bansId | bansItemId | bansUserId | bansForAgainst
---------------------------------------------------------
1 1 3 1
2 2 3 1
12 8 4 0
13 8 6 1
14 8 9 1

This table is used to record people who are for/against an item. The more people for an item (a "1"), the larger the text needs to be.

In the case above, rows 12-14 would have a total of "1" to go towards making the item larger as a "0" here means "against" the item. Sort of like saying 1+1-1...which would equal 1.

Soooo...not sure what other info is needed here, but some help would be much appreciated.

Slapyo
Apr 10th 2006, 8:28 am
You can do it in 1 query by doing a LEFT JOIN. Can you provide the table structure for the 2 tables. I'm assuming that bansItemId is the same ID that is in the item table?