Webmaster Forum


Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 07-16-2012, 07:47 AM
nathan.joshua's Avatar
Contributing Member
 
Join Date: 03-31-12
Location: Dorset
Posts: 123
iTrader: 0 / 0%
YouTube like/dislike bar formula

Hello guys,

I've been working on a project recently and now my client wants a YouTube like/dislike bar

Anyway, I have been tampering with this like/dislike formula for a few hours now and it is starting to make me frustrated. This is what I have so far:

Code:
$likes and $dislikes sql query~ $a = $likes; $b = $dislikes; $c = $a/$b*100; <div style="width:<?php echo $c; ?>%;"></div>
I am trying to echo out a percentage to display the dislike/like ratio using a divs width.

Unfortunately this code is not scalable and bugs under certain conditions i.e. if $b=>$a or $a==$b.

Does anyone have a solid formula I could learn from?
__________________
Freelance Web Designer
Follow on Twitter for my weekly blog post.
 
Reply With Quote
  #2 (permalink)  
Old 07-18-2012, 02:21 AM
JohnnyS's Avatar
Contributing Member
 
Join Date: 07-05-11
Location: philippines
Posts: 312
iTrader: 0 / 0%
i think the formula of getting the percentage should be:


Code:
$a = $likes; $b = $dislikes; $ap = ($a / ($a + $b)) * 100; // percentage if likes $bp = ($b / ($a + $b)) * 100; // percentage if dislikes
 
Reply With Quote
  #3 (permalink)  
Old 07-18-2012, 07:01 AM
nathan.joshua's Avatar
Contributing Member
 
Join Date: 03-31-12
Location: Dorset
Posts: 123
iTrader: 0 / 0%
You are some kind of mathematical sorcerer! Worked perfectly!


Full code below:

The PHP:
Code:
if($a > $b) { $c = ($a / ($a + $b)) * 100; } if($b > $a) { $c = ($b / ($a + $b)) * 100; } if($a == $b) { $c = 50; }
The HTML:
Code:
<div style="background-color:#000;width:200px;"> <div style="background-color:#fff;height:30px;width:<?php echo $c; ?>%;"></div> </div>
Thanks V7!
__________________
Freelance Web Designer
Follow on Twitter for my weekly blog post.
 
Reply With Quote
  #4 (permalink)  
Old 07-22-2012, 03:02 AM
Junior Member
 
Join Date: 07-22-12
Posts: 3
iTrader: 0 / 0%
I think you would like to use "else", because the latter statements can't be true if one of the preceding was.
 
Reply With Quote
  #5 (permalink)  
Old 07-23-2012, 04:57 AM
nathan.joshua's Avatar
Contributing Member
 
Join Date: 03-31-12
Location: Dorset
Posts: 123
iTrader: 0 / 0%
Quote:
Originally Posted by countryqt30 View Post
I think you would like to use "else", because the latter statements can't be true if one of the preceding was.
As all the statements have unique queries and outputs there is little need for else. However, to create better syntax and readability, here are some elseif's added to the previous PHP:

Code:
if($a > $b) { $c = ($a / ($a + $b)) * 100; } elseif($b > $a) { $c = ($b / ($a + $b)) * 100; } elseif($a == $b) { $c = 50; }
__________________
Freelance Web Designer
Follow on Twitter for my weekly blog post.

Last edited by nathan.joshua; 07-23-2012 at 05:01 AM.
 
Reply With Quote
  #6 (permalink)  
Old 07-24-2012, 04:36 AM
cbsmth's Avatar
v7n Mentor
 
Join Date: 08-17-10
Location: Mexico
Posts: 176
iTrader: 0 / 0%
Quote:
Originally Posted by nathan.joshua View Post
As all the statements have unique queries and outputs there is little need for else.
This is just nitpicking from my part, but there really is a difference: the three consecutive ifs will always be evaluated, whereas in the if/elseif variant, the interpreter will only proceed evaluating for as long as the preceding conditionals are false.
__________________
tinyFeedback WP Plugin - Free visitor feedback plugin for WordPress
Hiragana Learner - Practice the hiragana syllabary through this free, quiz-like game
 
Reply With Quote
  #7 (permalink)  
Old 09-20-2012, 09:03 AM
Contributing Member
Latest Blog:
None

 
Join Date: 08-07-12
Posts: 115
iTrader: 0 / 0%
Not sure if my comment would help a lot, but there are pre-made like dislike bar scripts online. They have been designed nicely and their license allows for commercial use too. For the same reason I don't calculate the 531 * 54, I just use a calculator.
__________________
At Adsonmycar.net, we help you to find advertisers for your car. You simply can get paid to advertise on your car. We do support a large number of countries.
 
Reply With Quote
  #8 (permalink)  
Old 10-28-2012, 04:59 PM
freeadstime's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 07-09-12
Posts: 335
iTrader: 0 / 0%
This page may help: 9lessons.info/2010/07/youtube-like-rating-jquery-and-ajax.html
__________________
We make sure that everyone has access to a Free Ads classifieds. Post an Ad under pets, vehicles, services, jobs, apartment rentals and other similar categories.
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply

Tags
like dislike bar youtube


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What do you most like and most dislike about the internet? StrongInTheArm Tech Talk 44 06-15-2009 06:23 AM
Do Search Engines Dislike Redirects? wbstein SEO Forum 4 02-06-2009 04:20 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 10:50 PM.
Powered by vBulletin
Copyright © 2000-2013 Jelsoft Enterprises Limited.
Copyright © 2003 - 2013 Escalate Media LP




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.