/    Sign up×
Community /Pin to ProfileBookmark

Incrementing script

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″>&nbsp;&nbsp;&nbsp;&nbsp; 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 &nbsp;&nbsp;&nbsp;&nbsp;”;

?></td>[/code]

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[/code]

So what I came up with was

[code=php]
$recent = X;
$totals = 1130 + $recent ;
echo “$totals”;[/code]

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
?

to post a comment
PHP

28 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Jul 15.2009 — 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.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 15.2009 — 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]

With regards to DB's, I have yet to break ground in MySQL. Atm, it's purely PHP that I am trying to get a handle on.

Is there not a way to do the script purely using PHP???

I was driving around and thinking about using the increment somehow (++)

[code=php]
$recent = x ;
$pics = 1130 ;
$total = ($pics + $recent)++ $recent ;

echo "total photos: $total";
[/code]


Or something...

?
Copy linkTweet thisAlerts:
@criterion9Jul 15.2009 — If you don't want to mess with a DB you might still find it easier to put those images into their own folder. Then the PHP can check the folder for the number of images. I may be misunderstanding the need for the different numbers though. Maybe you could explain a little more about how you get those numbers to enter into the script?
Copy linkTweet thisAlerts:
@MindzaiJul 15.2009 — ++ just increments by 1. There is no need for any kind of database, or manually creating sums. You can use pure PHP to get the total number of images using the PHP filesystem functions to count the files directly.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 15.2009 — Right... Here is the full on explanation :-)

I have a home.html (which is now a home.php) file. I manually update the RECENT IMAGES section, with regards to the days image/photo submissions.

Then, I manually open up the calculator application, take the previous days image count and add on the latest images amount to give me the total. I then physically type it in, on the home.php (then home.html) page. Once all my updates are done, I upload the updated files onto the server using FTP.

Unfortunately, as the website grows with more and more images, I am unable (for convenience sakes) to have all the images in one folder. I seperate my images accoring to years, months and weeks. (IE: 09 folder, contains Jan, Feb etc, which contains 1, 2 (for the week ending Sunday))

So... Currently, with my skill set and the way I currently run my website, I would like to carry on updating, manually... However, I would just like to add the days newest amount of image update into a variable, eg: $recent, and it must spit out the result when the home.php is uploaded.

What I am battling with, so it would seem... Is to get $total to take on the new value, once $recent is entered.

Does this make sense???
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 15.2009 — Something like this maybe? Am I getting closer???

[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]
Copy linkTweet thisAlerts:
@criterion9Jul 15.2009 — 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!
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 15.2009 — 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]

Kinda defies the objective, doesn't it? If I have a text file??? I will still have to do the "maths" of adding up the amount of images.

I would like the script to the that. I just manually add a # to a $variable and then it spits out the $total. The number in the $variable will change on a daily basis. But then again, so will $total.

hmmmm...?
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 15.2009 — OR.... What about if I create a script that uses a date function. That way, it will check the date, if its a newer date, it will count the latest images (which I could assign an ID, to distinguish that they newer files) and then it will add the newer files to the current total...
Copy linkTweet thisAlerts:
@MindzaiJul 15.2009 — 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!
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 15.2009 — 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]


Thanks for your opinion. What I am trying to achieve is to start implementing php into my web design ie: my current site.

Messy and unnecessary? Maybe... But it's all part of my learning process.

No one has stated that I am "hot or cold" People have just given their opinions, as have you. Much appreciated... I will take the advice on board, but would still like to know if there is a way that it can be done, without implementing databases.

?
Copy linkTweet thisAlerts:
@criterion9Jul 15.2009 — 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?
Copy linkTweet thisAlerts:
@MindzaiJul 15.2009 — Yes I realise nobody has literally said "cold", I was speaking figuratively. Over a few posts people have recommended you use PHP's filesystem functions to do this. If you would like to know if it can be done without implementing databases - yes it can, re-read posts #4 - #11 of this thread!

And I realise that creating messy and complicated code is inevitable while you are learning, but if you want to learn and not just 'make it work', learning to do things the right way is important which is why the filesystem suggestion has been repeated a few times.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 15.2009 — 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]


I will be manually editing the PHP script as it resides with a <td> tag on my home page. SO, as I am manually updating the html, it's no big deal to pop on over the the $recent_updates = X ; and slot in a new number.

Yes... I am aware that this is probably the "wrong" way to do it. And if it continues down this path, probably scrap it and try something else.

With regards to "[U]overwrite its own source file with new "yesterday's total" values...the answer is yes and no[/U]", I think that is what I am trying to achieve. Why is the answer yes and no???
Copy linkTweet thisAlerts:
@criterion9Jul 15.2009 — Because it isn't good practice and could become a huge security risk to have a script edit itself.

If you really want to manually add a number to the values when you upload use a text file that only has yesterday's value stored in it and don't want to just put in the number to the script (using a calculator as suggested by Mindzai). Then when you update your number (through a form or alternate php script so you don't have to edit the php file) have the script update that file. Then in your display (<td in this case) have the php script pull that number from the text file.

The "best practice" for this type of problem is to have the php script automatically add up the images from the folder system. This is actually much easier than it sounds and wouldn't require using a database at all.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 16.2009 — Fair nuff...

I think I will attempt the file.txt idea.

As I've mentioned, it's purely to start getting to know PHP and start writing my own stuff. I am hoping some day in the future, due to the nature of my website, to make it practically automated... Content is emailed, placed, numbered etc etc all on its own.

Busy investigating the file handling functions. Will let you know what I come up with. Think I am getting immune to Red Bull. Having a full time job that ain't web design, I rely on RB to get me extra hours behind the keyboard, but seems to be wearing off. hehe.
Copy linkTweet thisAlerts:
@MindzaiJul 16.2009 — Programming is its own red bull when you get into it! I can lose days at a time!
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 16.2009 — Question:

Why does the fwrite() function return a number value?

Update:

$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]


Doesn't return anything now...
Copy linkTweet thisAlerts:
@MindzaiJul 17.2009 — 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.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 17.2009 — 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]

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:

$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]
Copy linkTweet thisAlerts:
@MindzaiJul 17.2009 — 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]


Lots of ways, but for your purpose file_get_contents is probably easiest:

[code=php]echo file_get_contents('/path/to/file.txt');[/code]
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 17.2009 — 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]


I tried that function when I found it, about an hour ago. But it didn't work... Hmmm... Maybe I made a typo.

You guys are champion. Will play with this and show you what I end up with. But right now... My wife and my bed call. LoL...

Peace!
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 20.2009 — Why does gettype($file); return as a string, when its the number 25? Remember, $file ="pic_count.txt";


//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]


Can't seem to change the type from a string to an integer...
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 21.2009 — [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
Copy linkTweet thisAlerts:
@Hooded_VillianauthorJul 23.2009 — [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]


Whoa... What happened there? The above script looks like I got way off base. But... It looks like I have cracked it and learnt a few new functions in the process!!! Take a look at the result of what I was trying to achieve, for those that are interested: Pls excuse all the comments. LoL

[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]


?
Copy linkTweet thisAlerts:
@MindzaiJul 23.2009 — I still don't get how any of this is easier than just opening the textfile and typing in the new number yourself!
Copy linkTweet thisAlerts:
@Hooded_VillianauthorNov 29.2009 — I still don't get how any of this is easier than just opening the textfile and typing in the new number yourself![/QUOTE]

I have finally been working on MySQL and now know that the way I was doing it, was.... Wrong, to say the least. With a Database in Place, the result I wanted to achieve is sooooooo much easier.

?
Copy linkTweet thisAlerts:
@MindzaiNov 30.2009 — Glad you saw the light ?
×

Success!

Help @Hooded_Villian spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...