Jump to content

Recommended Posts

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.

?>

Link to comment
https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241976
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241987
Share on other sites

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  ???

Link to comment
https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241989
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241990
Share on other sites

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.
?>

 

Link to comment
https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241995
Share on other sites

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;

?>

Link to comment
https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-241999
Share on other sites

@MadTechie

 

Saving the new value ???

 

Its been a long week.. ;D

 

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

Link to comment
https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-242009
Share on other sites

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.. ;D

 

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?

Link to comment
https://forums.phpfreaks.com/topic/49379-need-count-help/#findComment-242010
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.