Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Design Forum > Web Design & Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Web Design & Programming Discussion of web design, and server-side & client-side scripting

Reply
 
LinkBack Thread Tools
Old 12-17-2008, 03:47 PM   #1 (permalink)
Registered User
 
Join Date: Dec 2008
Posts: 16
OS: Windows Vista Service Pack 1


[SOLVED] PHP foreach - HELP!!!

Hello,

I am working on a PHP rating script. Everything is going good but I am not sure how to add the ratings based on this code:

PHP Code:
//get old ratings line by line
$old_results file('results.txt');
foreach (
$old_results as $rate_value
{
         
$get_old_rates explode(':',$rate_value);
echo 
$get_old_rates[1];


This code reads a text file that has the rating and IP address of a rater separated by a semicolon (70.238.71.44:2). The code above just echos the ratings but I want it to add them up and then show the sum. Any suggestions?
Moatist is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 12-17-2008, 06:54 PM   #2 (permalink)
Design Team Member
 
jamiemac2005's Avatar
 
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,878
OS: Vista, various linux distros


Re: PHP foreach - HELP!!!

Would something like this work?:

PHP Code:
//get old ratings line by line
$old_results file('results.txt');
$Total 0;
$AmountOfRatings 0;
foreach (
$old_results as $rate_value
{
         
$get_old_rates explode(':',$rate_value);
//you may need to ensure this is an integer before adding it.
         
$Total += $get_old_rates;
         
$AmountOfRatings += 1;
}  
$Average $Total/$AmountOfRatings;
echo 
$Average
Cheers,
Jamey
__________________

Myspace
jamiemac2005 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-17-2008, 08:39 PM   #3 (permalink)
Moderator: Design
 
dm01's Avatar
 
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,427
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 BETA

My System

Re: PHP foreach - HELP!!!

Quote:
$get_old_rates = explode(':',$rate_value);
//you may need to ensure this is an integer before adding it.
$Total += $get_old_rates;
$AmountOfRatings += 1;
You could use radio buttons to ensure valid input:

HTML Code:
<form>
<p>Rate our Site!</p>
<p>
<input type="radio" name="rate1">1</input>
<input type="radio" name="rate2">2</input>
<input type="radio" name="rate3">3</input>
<input type="radio" name="rate4">4</input>
<input type="radio" name="rate5">5</input>
</p>
</form>
I'm not sure on the PHP or the <form> attribs, so I haven't included them. It has been a while since I have used forms.
__________________


Validate your Markup Validate your CSS Notepad++
Please use [html], [php], and [code] when posting code or markup.
I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message.
dm01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-18-2008, 08:39 AM   #4 (permalink)
Registered User
 
Join Date: Dec 2008
Posts: 16
OS: Windows Vista Service Pack 1


Re: [SOLVED] PHP foreach - HELP!!!

Thanks guys, everything is good now.
Moatist is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

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




All times are GMT -7. The time now is 11:25 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85