Jump to content

Calculate Percent relative to the highest number


graves_it

Recommended Posts

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.

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;.

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>

 

 

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. >_<

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.