Welcome Guest, Not a member yet? Register   Sign In
bug: method was executed twice
#1

[eluser]Unknown[/eluser]
The following is a partial code of my program. It count the page view.
But, it seem like the method was executed twice.
So, the count view were 0, 2, 4, 6, 8, 10 ... and so on

Code:
class Blog extends Controller{

function Blog(){
parent::Controller();
}

function view($id=-1){

$this->load->database();
$result = mysql_query("UPDATE mytable SET num_view = num_view + 1 WHERE id = '$id'");
            
}
}

When i remove the sql statement. the counter do nothing. So, it means that the method was executed twice. Is it a bug of the CI.

Sorry, I found the problem. It came from the url that can mislead to execute the function twice.
#2

[eluser]mrahman[/eluser]
I'm not sure. but try to change the name from "view" to anything else.
May be it's overriding a base method. so the runtime calls Controller.View() before calling yours. just guessing.
#3

[eluser]esra[/eluser]
View is a reserved word for CI when running on PHP4. However, Coolfactor's proposed View library for CI is called View and appears to be working fine.

view is a method in Loader.php.
#4

[eluser]noland[/eluser]
hi there; i've seen that you're using this code:

Code:
$this->load->database();
$result = mysql_query("UPDATE mytable SET num_view = num_view + 1 WHERE id = '$id'");

why don't you try to write out an "echo" to see if actually is calling the function twice.

and then, if so, why don't you try to get manually the maximum value of view in an "isolated way" ???

for example:

Code:
[...]
//$num_view will contain the maximum num_view value throught something like 'select max(num_view) from mytable
$num_view++;

$result = mysql_query("UPDATE mytable SET num_view = " . $num_view . " WHERE id = '$id'");




Theme © iAndrew 2016 - Forum software by © MyBB