tags:

views:

110

answers:

3

hi all.. i want after i've been submit form it can show hit counter.. but i want after it reach "20" it can back to zero..bcoz the limit of submit is 20 times so it can't over the limit.

how do i make it works?I've been try to this code...

<?
$limit="20";
$Query_counter=mysql_query("SELECT model FROM inspec");
$Show_counter=mysql_fetch_array($Query_counter);
$show_counter = $show_counter["model"]+1;

if($show_counter > $limit[0]) {
      $show_counter = 0;
}elseif ($show_counter > $limit[1]) {
      $show_counter = 0;
}


$Query_update=mysql_query("UPDATE inspec SET model=$Show_counter");
$Show_counter=number_format($Show_counter);
$Show_counter=str_replace(",",".",$Show_counter);
echo "Hit:</br><strong>$show_counter</strong>";
?>
A: 

The concept here is to test to see if the variable you're incrementing exceeds some acceptable range as a result of the next increment. Simple increment the variable, then test its value.

In your case, just add a test after you increment the counter:

$show_counter = $show_counter["model"]+1;
if($show_counter > $limit){
    $show_counter = 0;
}

Be sure to define $limit to whatever number you want to cycle on.

If you want to do this for multiple thresholds you can add additional tests. Note that you could hard-code $limit to any number or any variable you want, it's just the thing you're testing against.

Mark E
how must i do if i have two categories..limit[1]=20limit[2]=8
klox
@klox, updated my answer, hopefully it provides the clarification you need.
Mark E
@mark:how about my answer?
klox
A: 
<?
$Query_counter=mysql_query("SELECT model FROM inspec");
$Show_counter=mysql_fetch_array($Query_counter);
$show_counter = $show_counter["model"]+1;

$x = 0;
$limit = 20;
for ($i = 0; $i < 30; ++$i) {
    $x = ++$x % $limit;
    echo $x;
}

$Query_update=mysql_query("UPDATE inspec SET model= (model + 1) % 20");
$Show_counter=number_format($Show_counter);
$Show_counter=str_replace(",",".",$Show_counter);
echo "Hit:</br><strong>$show_counter</strong>";
?>
klox
+1  A: 

To make a counter go up to a certain value then loop back to zero, you can use the modulus operator, which in a lot of languages (including PHP and MySQL) is %

$x = 0;
$limit = 4;
for ($i = 0; $i < 10; ++$i) {
    $x = ++$x % $limit;
    echo $x;
}
// 1, 2, 3, 0, 1, 2, 3, 0, 1, 2

I hope that makes enough sense. I can't really figure out from the question what exactly you want... Perhaps something like this?

UPDATE `mytable` SET `mycounter` = (`mycounter` + 1) % {{the limit}}
nickf
look at my answer..is that correct?
klox
@klox - not really at all. the "`{{ }}`" isn't supposed to be added - it was just to highlight what you need to replace. You also don't need to do modulus twice (in your code you've put it in the PHP as well as the SQL). You also have to keep your capitalisation consistent (`$Show_counter` is different to `$show_counter`). You've just copied my example which is meant to show how it works. Unless you want your script to just print out a long line of numbers, then you might have to alter it yourself. I'll edit with something closer to what you might want.
nickf
...actually, I won't because I still have no clue what you actually want.
nickf
@nickf: what i want is after i've been submit form it can show hit counter..but i want after it reach "20" it can back to zero..bcoz the limit of submit is 20 times so it can't over the limit..
klox