Welcome Guest, Not a member yet? Register   Sign In
page gets loaded twice
#1

[eluser]zenzi[/eluser]
i came across a very strange thing while i was implementing a counter for the number of times an article has been displayed.

that's my code:

controller
Code:
function show($id = 0)
{
$this->data['article'] = $this->Article_model->get_article($id);
$this->Log_model->inc_article_view($this->data['article']->id);
$this->load->view('page', $this->data);
}

and the modell
Code:
function inc_article_view($id)
{
$this->db->query("UPDATE article SET views=views+1 WHERE id=?", array($id));
}


everything works fine, but looking in the db i noticed, that the views get incremented twice.
to check that i created a new db-table and changed the function inc_article_view() so that it inserts the article id and a timestamp as a new row in the new table. and the result were two new records for every time the article page was loaded, the two entries having slightly different time stamps (allways 2 or 3 seconds of difference).
i also made sure that the function inc_article_view() only gets called once in my code.

i then wondered if i had some routing issues or redirects that could cause this behaviour, but couldn't find anything.
so i inserted a post_system hook consisting only of a die() command to make sure, that the page only gets loaded once, but the doubled increment remains.
only if i make the die()-hook a post_controller-hook does the increment remain at 1 (but obviously then the page doesn't get displayed).
so it seems that somewhere between the end of the controller and the final output something gets messy?

does anyone have an idea what i can do to have my article-views incremented correctly?
#2

[eluser]boltsabre[/eluser]
I doubt this is your problem, but try changing your model names to start with a lower case letter rather than an uppercase.

Can you post your code for:
- Article_model->get_article($id);

What's in $data at this stage?
Code:
...
var_dump($this->data);
$this->load->view('page', $this->data);
#3

[eluser]Aken[/eluser]
Turn logging on and set it to debug mode, that should help narrow it down.
#4

[eluser]zenzi[/eluser]
thanks for your suggestions

here's the code of get_article()
Code:
function get_article($id)
{
$query = $this->db->query("SELECT * FROM article WHERE id=?", array($id));
if ($query->num_rows() > 0):
  return $query->row();
else:
  return FALSE;
endif;
}

... but try changing your model names to start with a lower case letter rather than an uppercase.
you're sure about this? if i do like so, i get an error, and the user guide states "Class names must have the first letter capitalized".

i turned the log on and this is the result of one page display:

DEBUG - 2012-04-16 12:25:43 --> Config Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Hooks Class Initialized
DEBUG - 2012-04-16 12:25:43 --> URI Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Router Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Output Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Input Class Initialized
DEBUG - 2012-04-16 12:25:43 --> XSS Filtering completed
DEBUG - 2012-04-16 12:25:43 --> XSS Filtering completed
DEBUG - 2012-04-16 12:25:43 --> Global POST and COOKIE data sanitized
DEBUG - 2012-04-16 12:25:43 --> Language Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Loader Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Helper loaded: url_helper
DEBUG - 2012-04-16 12:25:43 --> Helper loaded: html_helper
DEBUG - 2012-04-16 12:25:43 --> Helper loaded: form_helper
DEBUG - 2012-04-16 12:25:43 --> Database Driver Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Session Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Helper loaded: string_helper
DEBUG - 2012-04-16 12:25:43 --> Session routines successfully run
DEBUG - 2012-04-16 12:25:43 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:43 --> Controller Class Initialized
DEBUG - 2012-04-16 12:25:46 --> File loaded: app3/views/fixies.php
DEBUG - 2012-04-16 12:25:46 --> File loaded: app3/views/artikel_full.php
DEBUG - 2012-04-16 12:25:46 --> File loaded: app3/views/page.php
DEBUG - 2012-04-16 12:25:46 --> Final output sent to browser
DEBUG - 2012-04-16 12:25:46 --> Total execution time: 2.5201
DEBUG - 2012-04-16 12:25:46 --> Config Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Hooks Class Initialized
DEBUG - 2012-04-16 12:25:46 --> URI Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Router Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Output Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Input Class Initialized
DEBUG - 2012-04-16 12:25:46 --> XSS Filtering completed
DEBUG - 2012-04-16 12:25:46 --> XSS Filtering completed
DEBUG - 2012-04-16 12:25:46 --> Global POST and COOKIE data sanitized
DEBUG - 2012-04-16 12:25:46 --> Language Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Loader Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Helper loaded: url_helper
DEBUG - 2012-04-16 12:25:46 --> Helper loaded: html_helper
DEBUG - 2012-04-16 12:25:46 --> Helper loaded: form_helper
DEBUG - 2012-04-16 12:25:46 --> Database Driver Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Session Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Helper loaded: string_helper
DEBUG - 2012-04-16 12:25:46 --> Session routines successfully run
DEBUG - 2012-04-16 12:25:46 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Controller Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Final output sent to browser
DEBUG - 2012-04-16 12:25:46 --> Total execution time: 0.1078
DEBUG - 2012-04-16 12:25:46 --> Config Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Hooks Class Initialized
DEBUG - 2012-04-16 12:25:46 --> URI Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Router Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Output Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Input Class Initialized
DEBUG - 2012-04-16 12:25:46 --> XSS Filtering completed
DEBUG - 2012-04-16 12:25:46 --> XSS Filtering completed
DEBUG - 2012-04-16 12:25:46 --> Global POST and COOKIE data sanitized
DEBUG - 2012-04-16 12:25:46 --> Language Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Loader Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Helper loaded: url_helper
DEBUG - 2012-04-16 12:25:46 --> Helper loaded: html_helper
DEBUG - 2012-04-16 12:25:46 --> Helper loaded: form_helper
DEBUG - 2012-04-16 12:25:46 --> Database Driver Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Session Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Helper loaded: string_helper
DEBUG - 2012-04-16 12:25:46 --> Session routines successfully run
DEBUG - 2012-04-16 12:25:46 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Model Class Initialized
DEBUG - 2012-04-16 12:25:46 --> Controller Class Initialized
DEBUG - 2012-04-16 12:25:49 --> File loaded: app3/views/fixies.php
DEBUG - 2012-04-16 12:25:49 --> File loaded: app3/views/artikel_full.php
DEBUG - 2012-04-16 12:25:49 --> File loaded: app3/views/page.php
DEBUG - 2012-04-16 12:25:49 --> Final output sent to browser
DEBUG - 2012-04-16 12:25:49 --> Total execution time: 2.4910

that seems to be even a triple load!!

i'm absolutely clueless and if i can't sort this out my next step would be a workaround like to check befor incrementing if this page has been already incremented in the last three seconds from the actual ip-address.
#5

[eluser]Samus[/eluser]
Turn profiling on.

It'll show you if multiple queries are being run, that'll help narrow it down.

Put this in your construct:

Code:
$this->output->enable_profiler(TRUE);
#6

[eluser]Reneesh T K[/eluser]
Can you try this:

function inc_article_view($id)
{
$this->db->query("UPDATE article SET views=views+1 WHERE id=$id");
}
#7

[eluser]zenzi[/eluser]
the profiler output looks like this:


URI Zeichenkette
/Artikel/2032/Elbjazz-Festival/0

KLASSE/METHODE
artikel/show

Speicher Nutzung
5,506,900 bytes

BENCHMARKS
Loading Time Base Classes 0.0103
Controller Execution Time ( Artikel / Show ) 2.5152
Total Execution Time 2.5256

GET-DATEN
Keine GET-Daten vorhanden

POST-DATEN
Keine POST-Daten vorhanden

DATENBANK: db10963609-fww SQL-ABFRAGEN: 8
0.0002 SELECT * FROM lum_produkte WHERE frei='ja' AND status = 'Top' AND frei_ab <= '2012-04-18' AND frei_bis >= '2012-04-18' ORDER BY status,sort,titel
0.0005 SELECT * FROM artikel WHERE domain LIKE '%radtouren-im-norden%' AND frei = 'ja' AND frei_ab <= '2012-04-18' AND frei_bis >= '2012-04-18' AND id='2032'
0.0001 SELECT * FROM artikel WHERE domain LIKE '%radtouren-im-norden%' AND frei = 'ja' AND frei_ab <= '2012-04-18' AND status != 'normal' AND menu = 'Aktuelles' ORDER BY frei_ab DESC
0.0004 SELECT submenu,url_submenu FROM artikel WHERE url_menu='Aktuelles' AND LENGTH(url_menu) > 4 GROUP BY submenu ORDER BY submenu
0.0001 SELECT * FROM lum_ads WHERE von <= '2012-04-18' AND bis >= '2012-04-18' ORDER BY position,prio
0.0002 INSERT INTO lum_log (bereich,artikel) VALUES ('Aktuelles','Elbjazz Festival')
0.0003 UPDATE artikel SET views=views+1 WHERE id=2032
0.0001 INSERT INTO logcheck (artid) VALUES ('2032')

i modified inc_article_view() as suggested

... and we're still incrementing by two

that's really driving me crazy!
#8

[eluser]zenzi[/eluser]
anybody with an idea? any?
#9

[eluser]Aken[/eluser]
Why don't you try adding a log_message() in your inc_article_view() method, and have it log the URI that is currently loading. Then you can see if it's actually the same page, or some other asset or something.

If it's the same page, then you need to go over your code, because something is wonky if your code is getting called twice. Try backing up your files, then start fresh and slowly add things until the problem arises.
#10

[eluser]coddim[/eluser]
Hello,

I got into the same issue as well, what is really strange is that if I use on the constructor the _profiler running I just get one increment, if I take it out I get it incrementing twice again.

I don't think it is something to do with the way the queries are run, like active records or manual, since both ways it reacts the same.

So pretty strange why would it work with the profiler activated and not without.

I might leave it like this for now and just split it by 2, to get the exact count.

Some years ago I got kinda in the same issue with the way other then IE browsers loaded content, but now doesn't seem like it is browser side.





Theme © iAndrew 2016 - Forum software by © MyBB