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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.