while loop

i am a student who does not understand the while loop, my programming knowledge is very basic. I am trying to pull data from a .txt file and make the $numCopies add everything in the data sheet they should add up to 52 and the $numOrders should add up to 20. what am i doing wrong with this code, i am completely lost.

html code is

Software1

SOFTWARE ORDERS: REPORT

php code is

<?php $totalCopies = $_POST['totalCopies']; $totalOrders = $_POST['totalOrders']; $nextOrder = $_POST['nextOrder']; $totalCopies = 0; $totalOrders = 0; $orderFile = fopen("ordersBoles.txt", "r"); $nextOrder = fgets ($orderFile); while ($totalCopies == 20 and $totalOrders == 20) { list($totalCopies, $totalOrders) = explode (":", $empRecord); $totalCopies = $totalCopies + $nextOrder; $totalOrder = $totalOrder + nextOrder; } fclose($orderFile); print ("

SOFTWARE ORDERS: REPORT

"); print ("

TOTAL COPIES ORDERED: $totalCopies

"); print ("

TOTAL ORDERS: $totalOrders

"); ?>

my .txt file is

1 Linux:1
2 Macintosh:1
3 Windows:1
4 Macintosh:1
5 Macintosh:2
6 Linux:5
7 Macintosh:10
8 Windows:10
9 Macintosh:1
10 Windows:1
11 Windows:1
12 Linux:1
13 Macintosh:5
14 Linux:4
15 Windows:1
16 Macintosh:1
17 Windows:1
18 Linux:2
19 Macintosh:2
20 Windows:1
21

i know my code is a mess but any help would be wonderful

your arguments for your while loop are wrong. The arguments should be written so that if it is true it will run through loop, in this case it should be something like while not end of the file run

[php]while ($totalCopies == 20 and $totalOrders == 20)
^--------------replace all this -------------------^
[/php]

with the eof (end of file function), here is a link about it http://php.net/manual/en/function.feof.php

Yes, albertle5 is correct. You have to understand how a “while” clause works. In basic terms it works like this:

While (some-condition)
Do some line of code…

or

While (some-condition)
{
Do lots of lines of code…
}

Fairly simple. But, the condition is the important part. You can compare most anything. Usually it is in the form of “while some condition exist” such as a file not at EOF (end of file) or a variable less than your set limit.
While are used a lot for displaying database information.

Well, I just wanted to make sure you understood the basics of the WHILE clause… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service