Hi all. Back again… Still trying to get the hang of this PHP thing. It’s all fun and games.
Now, if possible, I am not looking for the answer. I won’t get anywhere if I don’t do it myself… But rather a “You’re getting hot or Cold” or “Rather try using this… “
I have my own little website where pics are updated on a daily basis. I have a little total images thingy on the site. Usually I have to take the previous days total photo amount, open the calculator function, add the latest updates and Boom. Have a new total images amount.
With PHP, I am trying to create one that will be less hassle…
This is what I have come up with so far.
[code=php] <tr>
<td width=”150″> recent updates:</td>
<td width=”650″ align=”right”>
<!– testing out a PHP script where I don’t have to fill in the totals myself –>
<?php
// keep adding an ADD symbol and the update amount. The script will do the maths.
$recent = 10+5 ;
$pics = 1130 + $recent ;
echo “total photos: $pics ”;
?></td>
Now its late, I been up on a Red Bull high and my brain can’t function. This is not ideally what I want to have, coz after a few weeks $recent will look like this
[code=php]$recent = 10+5+3+34+65+3+76+3+43+etc
So what I came up with was
[code=php]
$recent = X;
$totals = 1130 + $recent ;
echo “$totals”;
Or something of the sort. What I wanna do is create it that $totals will increment with the added value, then take on that value. So that the following day, when I add a value to $recent, it will add to the new $totals. Does this make sense???
Please note: I little push in the direction needed will be sufficient, rather than the answer.
Any words of advise welcome. The simpler the better. Basically easy me into this PHP stuff…
Thanks ya’ll
?
If you are adding the images to a directory or database you can use php to find the total number of images that way. Then you just display your total. see php.net/dir or the documentation for your database of choice.[/QUOTE]
[code=php]
$recent = x ;
$pics = 1130 ;
$total = ($pics + $recent)++ $recent ;
echo "total photos: $total";
[/code]
[code=php]$pics = 1000 ;
$recent = X ;
$current_total = $pics + $recent ;
function $total()
{
if ($current_total == $current_total)
{
echo "$current_total" ;
}
else
{
$current_total + $recent
echo "$total" ;
}
}
[/code]
If you use a root level image folder such as "/images/uploads/..." PHP can scan up the directory tree to grab all applicable image files and spit out the number for you. If you want to update the number manually then you will need to make a counter file (usually a text file) that only has the total in it. Then you can change the number manually or eventually using a more automated script. When the page is loaded you would then have the php open the file and read the number. Voila![/QUOTE]
If you want to update manually, then do the calculation manually. I'm not sure how it is easier to write something that lets you type an ever increasing list of numbers and adds them up. This is just creating more work. If you still have to manually open the PHP file and type in the day's image total, this doesn't seem any more convenient than typing the numbers into a calculator. Even if images are split across directories you can still use PHP to count them automatically.
You seem to be trying to implement a complicated and messy solution to what is a very simple problem. If you want to learn to program this is the #1 habit to break.
You asked to be told when you are hot and cold. Well a few times in this thread now people have said "cold" - take the advice on board![/QUOTE]
Are you asking if it can be done for you to manually edit the script each day...the answer is yes. If you are asking if the PHP script will be able to overwrite its own source file with new "yesterday's total" values...the answer is yes and no. If you are asking if what you are trying to pose as the solution is really not the most efficient and probably the "wrong" way to do it...then yes.
If you are asking if PHP can count your images automatically without you having to edit the php file at all...then yes.
Do you need a direction to go to figure out how PHP can "automatically" count the images for you?[/QUOTE]
$file = "pic_count.txt" ;
$latest = 25 ;
$file_open = fopen("pic_count.txt", "w") or die("Cannot open requested file") ;
fwrite($file_open, $latest) ;
fclose($file_open) ;[/QUOTE]
It returns the number of bytes written, or false on failure. And it always returns something, but you aren't checking the return value in the code you posted.[/QUOTE]
$file = "pic_count.txt" ; // declare variable
$latest = "123" ; //just some random content
$file_open = fopen("pic_count.txt", "w") or die("Cannot open requested file") ; //opens the text file
fwrite($file_open, $latest) ; //clears the txt file & rewrites it with $latest variable content
echo fgets($file_open) ; //supposed to echo the text file
fclose($file_open) ; //closes the text file[/QUOTE]
Is there a way to echo, the content of the txt file, not in bytes?
:eek:
OOookkkk... Getting the hang of the fwrite() function.
Update:[/QUOTE]
[code=php]echo file_get_contents('/path/to/file.txt');[/code]
Lots of ways, but for your purpose file_get_contents is probably easiest:
[code=php]echo file_get_contents('/path/to/file.txt');[/code]
[/QUOTE]
//the text file containing the current pic count
$file = "pic_count.txt" ;
//opens the text file
$file_open = fopen("pic_count.txt", "a+")
or die("Cannot open requested file") ;
//does this change the txt from a string to an interger???
settype($file = "pic_count.txt", "integer") ;
//need to try change this from a string to an intergar
gettype($file);
echo gettype($file) ;
echo "<br />" ;
//clears the txt file & rewrites it with $latest variable content
//fwrite($file_open, $latest) ;
//writes the new text file
echo file_get_contents($file) ;
//closes the text file
fclose($file_open) ;
[/QUOTE]
[code=php]//file variable
$file = "pic_count.txt" ;
//file open
$file_open = fopen("pic_count.txt", "w") or die("Cannot open requested file") ;
//updates
$update = 25 ;
if($file_open <= $file)
{
echo file_get_contents($file) ;
}
else
{
echo "$file + $update" ;
}
fclose($file_open) ;[/code]
[code=php]//file variable
$file = "pic_count.txt" ;
//file open
$file_open = fopen("pic_count.txt", "w") or die("Cannot open requested file") ;
//updates
$update = 25 ;
if($file_open <= $file)
{
echo file_get_contents($file) ;
}
else
{
echo "$file + $update" ;
}
fclose($file_open) ;[/code]
:p[/QUOTE]
[code=php]
//open the text file
$pic_count = fopen("pic_count.txt","r+") or die("We are sorry, but the File needed, does not exist!!!") ;
/* the update amount - this will change on
regular basis due to photo update amounts.
all you need to do is change this one number
according to how many photos you are going to
upload for the days updates, and when you put it
on the server, it will auto change the photo total
amount */
$update = 5 ;
/* the maths behind the whole thing. take yesterdays
total and add todays amount. this will give you the
latest total ie $result */
$result = file_get_contents("pic_count.txt") + "$update" ;
//then rewrite the txt file with that new amount
fwrite($pic_count, "$result") ;
/*echo for it to be displayed. (obviously some
formatting will occur here with the finished script */
echo $result ;
//close the file
fclose($pic_count) ;
/* next challenge. create it that the script counts only the
latest added images/files by date and auto updates the txt file
or $updates variable */
[/code]
I still don't get how any of this is easier than just opening the textfile and typing in the new number yourself![/QUOTE]
0.1.9 — BETA 5.25