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? Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/ 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 />"; } Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629266 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. Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629371 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;. Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629372 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. Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629382 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. Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629384 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. Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629392 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...? Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629394 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 - ∞ Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629396 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> Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629398 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. >_< Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629403 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. Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629410 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. Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629414 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 Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629422 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. Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629426 Share on other sites More sharing options...
graves_it Posted August 30, 2008 Author Share Posted August 30, 2008 BINGO!!! Thank you so much!! Link to comment https://forums.phpfreaks.com/topic/121932-calculate-percent-relative-to-the-highest-number/#findComment-629430 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.