graves_it Posted August 29, 2008 Share Posted August 29, 2008 I am looking to show percentages relative to the highest value in the list—so there is always at least one 100% and everything else is a percentage of that maximum value. How would I go about doing this? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 <?php $numbers = array(3, 4, 19, 49, 2, 45, 32); $outof = max($numbers); foreach ($numbers as $number) { echo "$number: " . (($number / $outof) * 100) . "\n<br />"; } Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 What if I wanted to do this for numbers in a mysql table, would I just replace the numbers in the array with the table name? Like... <?php $numbers = array($tag->count); $outof = max($numbers); foreach ($numbers as $number) { echo "$number: " . (($number / $outof) * 100) . "\n<br />"; } ?> Didn't seem to work to work for me. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Get the array of numbers exactly how you'd normally get an array of numbers from mysql. You seem to be using some class, so I'm not sure how you do it, but I can tell you right now that you can ditch the array( ) construct around $tag->count;. Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 I am a noob to php and just trying to customize something so I am not really sure how to get the array of numbers. If you could throw some knowledge my way it would be much appreciate. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Yeah, umm, I'm not sure what $tag is doing though. @_@ Or how it's storing info. Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 $tag have 4 elements, tag_id, name, slug, count. Count is the one I am after and it stores the number of tags 0 - the highest number. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Okay, but that's a single number, not an array of numbers, right...? Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 Each entry into the database is a single number, meaning 0 - ∞ Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 Maybe this will help, here is the section of the file I am working on. I want to enter the percentage just before the % sign. <h3>Popular Tags</h3> <ul class="chartlist"> <?php foreach($popular_tags as $tag): ?> <li><a href="<?php echo $this->config->item('base_url')?>items/tag/<?php echo $tag->slug?>"> <span class="label"><?php echo $tag->name?></span></a><span class="index" style="width: %"></span></li> <?php endforeach; ?> </ul> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Ohhh, that makes a lot more sense then. Try something like: <h3>Popular Tags</h3> <ul class="chartlist"> <?php foreach ($popular_tags as $tag) { $totals[] = $tag->count; } $outof = max($totals); <?php foreach($popular_tags as $tag): ?> <li><a href="<?php echo $this->config->item('base_url')?>items/tag/<?php echo $tag->slug?>"> <span class="label"><?php echo $tag->name?></span></a><span class="index" style="width: <?php echo ($tag->count / outof) * 100; ?>%"></span></li> <?php endforeach; ?> </ul> Try that while I think of a more efficient way of doing it. >_< Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 This does not seem to work, throws a syntax error. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Yeah, woops, forgot a ?>. <h3>Popular Tags</h3> <ul class="chartlist"> <?php foreach ($popular_tags as $tag) { $totals[] = $tag->count; } $outof = max($totals); ?> <?php foreach($popular_tags as $tag): ?> <li><a href="<?php echo $this->config->item('base_url')?>items/tag/<?php echo $tag->slug?>"> <span class="label"><?php echo $tag->name?></span></a><span class="index" style="width: <?php echo ($tag->count / outof) * 100; ?>%"></span></li> <?php endforeach; ?> </ul> In the future, please tell me what the syntax error is instead of just saying that there is one. >_> I knew what it was thing time, luckily. Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 The first one worked and then I get the error A PHP Error was encountered Severity: Warning Message: Division by zero Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Grr, another tiny error: <h3>Popular Tags</h3> <ul class="chartlist"> <?php foreach ($popular_tags as $tag) { $totals[] = $tag->count; } $outof = max($totals); ?> <?php foreach($popular_tags as $tag): ?> <li><a href="<?php echo $this->config->item('base_url')?>items/tag/<?php echo $tag->slug?>"> <span class="label"><?php echo $tag->name?></span></a><span class="index" style="width: <?php echo ($tag->count / $outof) * 100; ?>%"></span></li> <?php endforeach; ?> </ul> Lol, try that one. Quote Link to comment Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 BINGO!!! Thank you so much!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.