darklight Posted April 30, 2007 Share Posted April 30, 2007 I'm trying to make it so it counts up by 1 when you enter the page. I'm just learning PHP so it's kinda hard lol. <? function do_count($tra) { $tra = 0; $tra++; } do_count($tra); return ($tra); ?> help? Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/ Share on other sites More sharing options...
Barand Posted April 30, 2007 Share Posted April 30, 2007 Firstly, the return needs to be at the end of the function <? function do_count($tra) { // $tra = 0; //remove, otherwise the function always returns 1 $tra++; return ($tra); } $tra = 1; echo do_count($tra); // need to do something with returned result, such as echo it. ?> Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241976 Share on other sites More sharing options...
MadTechie Posted April 30, 2007 Share Posted April 30, 2007 in addition to Barand corrections i think its important to point out that for a counter (like a site counter) your need to store the data somewhere ie file/database as your starting i would suggest a file heres a very basic read and write (i'll leave you to workout how to join them up) fwrite fread Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241987 Share on other sites More sharing options...
darklight Posted April 30, 2007 Author Share Posted April 30, 2007 Firstly, the return needs to be at the end of the function <? function do_count($tra) { // $tra = 0; //remove, otherwise the function always returns 1 $tra++; return ($tra); } $tra = 1; echo do_count($tra); // need to do something with returned result, such as echo it. ?> Seems to stop at 2 ??? Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241989 Share on other sites More sharing options...
MadTechie Posted April 30, 2007 Share Posted April 30, 2007 it works but you need to store the old value to prove it works try <?php function do_count($tra) { // $tra = 0; //remove, otherwise the function always returns 1 $tra++; return ($tra); } $tra = 1; echo do_count($tra); // need to do something with returned result, such as echo it. echo "<br>"; echo do_count($tra); echo "<br>"; echo do_count($tra); echo "<br>"; echo do_count($tra); echo "<br>"; ?> Barand's posting i'll leave him to it Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241990 Share on other sites More sharing options...
Barand Posted May 1, 2007 Share Posted May 1, 2007 See MadTechies addendum. You need to store $tra somewhere, as he said. Retrieve the value, increment it, re-store it for next time. Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241991 Share on other sites More sharing options...
MadTechie Posted May 1, 2007 Share Posted May 1, 2007 OK heres a quick one (for testing) in other words this will last as long as a session, a kinda bad example but you've just started to learn PHP see my first post for adding a file. <?php session_start(); function do_count($tra) { // $tra = 0; //remove, otherwise the function always returns 1 $tra++; return ($tra); } if(isset($_SESSION['counter'])) { $tra = $_SESSION['counter']; }else{ $tra = 1; } echo do_count($tra); // need to do something with returned result, such as echo it. ?> Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241995 Share on other sites More sharing options...
Barand Posted May 1, 2007 Share Posted May 1, 2007 or try a file-based version. Look up the functions in the manual to see what they do <?php if (file_exists('tra.txt')) { $tra = file_get_contents('tra.txt'); } else $tra = 0; $tra++; $fh = fopen('tra.txt', 'w'); // save new value fwrite($fh, $tra); fclose ($fh); echo $tra; ?> Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241999 Share on other sites More sharing options...
Barand Posted May 1, 2007 Share Posted May 1, 2007 @MadTechie Saving the new value ??? Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-242002 Share on other sites More sharing options...
darklight Posted May 1, 2007 Author Share Posted May 1, 2007 It always echos 2 lol Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-242003 Share on other sites More sharing options...
Barand Posted May 1, 2007 Share Posted May 1, 2007 I think you'll find it works fine, if you read it (reply #7). Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-242006 Share on other sites More sharing options...
john010117 Posted May 1, 2007 Share Posted May 1, 2007 If you've got a MySQL database available, store it on there. Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-242008 Share on other sites More sharing options...
MadTechie Posted May 1, 2007 Share Posted May 1, 2007 @MadTechie Saving the new value ??? Its been a long week.. corrected (kinda) <?php session_start(); function do_count($tra) { // $tra = 0; //remove, otherwise the function always returns 1 $tra++; return ($tra); } if(isset($_SESSION['counter'])) { $tra = $_SESSION['counter']; }else{ $tra = 1; } $_SESSION['counter'] = do_count($tra); // need to do something with returned result, such as echo it. echo $_SESSION['counter']; ?> But Barand's is a "real" solution Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-242009 Share on other sites More sharing options...
darklight Posted May 1, 2007 Author Share Posted May 1, 2007 I think you'll find it works fine, if you read it (reply #7). It's not working. It always echo's 1 or 2. If you've got a MySQL database available, store it on there. I don't know mySQL. @MadTechie Saving the new value ??? Its been a long week.. corrected (kinda) <?php session_start(); function do_count($tra) { // $tra = 0; //remove, otherwise the function always returns 1 $tra++; return ($tra); } if(isset($_SESSION['counter'])) { $tra = $_SESSION['counter']; }else{ $tra = 1; } $_SESSION['counter'] = do_count($tra); // need to do something with returned result, such as echo it. echo $_SESSION['counter']; ?> But Barand's is a "real" solution Why does it add 2? Quote Link to comment https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-242010 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.