LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2013, 09:31 AM   #1
ashevillan
LQ Newbie
 
Registered: Mar 2013
Posts: 5

Rep: Reputation: Disabled
trouble with a FOR loop not adding values from an array like it should


so my $summerTotal value is coming up to equal 0 every time and i have no idea why. no error messages or anything and everything else sums up correctly

<html>
<head>
<title>Annual Rainfall Report</title>
<link rel ="stylesheet" type="text/css" href="sample.css" />
</head>

<body>
<?php
print ("<h1>Annual Rainfall Report</h1>");

$rainData = array(0.3, 0.6, 1.2, 1.1, 0.35, 0.0, 0.25, 0.6, 0.0, 0.12, 0.23, 0.2, 0.234,
1.2, 0.1, 2.1, 2.45, 1.3, 0.7, 0.0, 0.0, 0.13, 0.12, 0.07, 0.1, 0.0,
0.8, 1.0, 0.55, 0.12, 0.5, 0.21, 1.01, 0.0, 0.0, 0.08, 0.09, 1.01, 0.76,
0.08, 0.3, 0.0, 0.9, 0.7, 1.3, 0.7, 0.09, 2.32, 1.28, 0.953, 0.75, 1.01);

$totalRain = 0;
$traceCount = 0;
$heavyRainCount = 0;
$summerTotal = 0;

for ($i = 0; $i < sizeof($rainData); $i=$i+1)
{
$totalRain = $totalRain + $rainData[$i];

if ($rainData[$i] <= .02)
$traceCount = $traceCount + 1;

if ($rainData[$i] >= 2)
$heavyRainCount = $heavyRainCount + 1;
}
$averageRain = $totalRain / sizeof($rainData);

for ($i = 0; $i < sizeof($rainData) ; $i=$i+1)
{
if ($i >= 27 or $i <= 39)
$summerTotal = $summerTotal + $raindata[$i];
}
print("<p>TOTAL RAIN: $totalRain.</p>");
print("<p>AVERAGE WEEKLY RAIN: $averageRain.</p>");
print("<p>WEEKS WITH TRACE OR NO RAIN: $traceCount.</p>");
print("<p>WEKS WITH HEAVY RAIN(2in. OR MORE): $heavyRainCount.</p>");
print("<p>TOTAL SUMMER RAIN: $summerTotal.</p>");
?>

</body>
</html>
 
Old 05-11-2013, 10:46 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well I see 2 issues:

1. Assuming variables are case sensitive, you have $raindata instead of $rainData

2. Check your boolean logic for the if, currently it is ALWAYS true
 
1 members found this post helpful.
Old 05-12-2013, 05:05 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Not a php user here, but some general comments on the code anyway.

Can't you move the content of the second loop into the first, and calculate $SummerTotal at the same time as the full count?

Since "sizeof($rainData)" appears several times, you might consider running it once and saving the value into a variable to use instead.

I also imagine that php can accept "i++" style incrementation, instead of the clunkier "$i=$i+1"? It might even have something like "$totalRain += $rainData[$i];", for that matter.

Edit: Yes, it has both operators:
http://php.net/manual/en/language.op....increment.php

One comment says that "$i += 1" works the fastest in his tests.


Oh, yeah. Before I forget again, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Last edited by David the H.; 05-12-2013 at 05:10 PM. Reason: as stated
 
  


Reply

Tags
php



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
print values from array (Is there a better way?) awreneau Programming 2 04-29-2013 08:27 PM
[SOLVED] reference bash array values, using a variable with a value of the array name gusthecat Programming 5 03-07-2012 03:41 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
Comparing Multiple Values in while loop rahulruns Programming 5 10-27-2009 05:59 AM
extracting values from an array bahadur Programming 2 08-14-2006 01:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration