B34ST Posted December 10, 2008 Share Posted December 10, 2008 I am trying to update a mysql integar field increasing the value by 1 every time the page is viewed however the result is it increases the value by 2 here is the code i am using: //connect to db $conn = db_connect(); //add 1 to topic viewa $update_views = $conn->query("UPDATE topics SET topic_views = topic_views+1 WHERE topic_id = '$topic_id'"); //query posts for this topic $result = $conn->query("SELECT * FROM posts WHERE topic_id = '$topic_id'"); //add results into array $i = 0; $post_data = array(); while($post = $result->fetch_object()) { $post_id = $post->post_id; $post_data[$i]['poster_id'] = $post->poster_id; $post_data[$i]['post_id'] = $post->post_id; $post_text = $conn->query("SELECT * FROM post_text WHERE post_id = '$post_id'"); while($post_row = $post_text->fetch_object()) { $post_data[$i]['subject'] = $post_row->subject; $post_data[$i]['message'] = $post_row->message; } $i++; } //add variables to template foreach($post_data as $r => $value) { $template->assign_block_vars('postrow', array( 'SUBJECT' => $post_data[$r]['subject'], 'MESSAGE' => $post_data[$r]['message']) ); } I found that this only happens when I add the template class: $template->assign_block_vars('postrow', array( 'SUBJECT' => $post_data[$r]['subject'], 'MESSAGE' => $post_data[$r]['message']) ); if I remove the template class the script updates the value by 1 like it should. thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/ Share on other sites More sharing options...
.josh Posted December 11, 2008 Share Posted December 11, 2008 So what does that method look like? Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-712098 Share on other sites More sharing options...
B34ST Posted December 11, 2008 Author Share Posted December 11, 2008 Thanks for the quick reply, and sorry if this sounds dumb but I dont understand your question. Which method are you refering to? Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-712100 Share on other sites More sharing options...
.josh Posted December 11, 2008 Share Posted December 11, 2008 The one you said is causing 2 updates to happen... $template->assign_block_vars Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-712125 Share on other sites More sharing options...
B34ST Posted December 11, 2008 Author Share Posted December 11, 2008 At the moment I am trying to implement the template class from phpbb2 as its the one that I am used to using, I intend to write my own template class soon as this one is overkill for what I need. however untill then here is the snippet from the template class: function assign_block_vars($blockname, $vararray) { if (strstr($blockname, '.')) { // Nested block. $blocks = explode('.', $blockname); $blockcount = sizeof($blocks) - 1; $str = '$this->_tpldata'; for ($i = 0; $i < $blockcount; $i++) { $str .= '[\'' . $blocks[$i] . '.\']'; eval('$lastiteration = sizeof(' . $str . ') - 1;'); $str .= '[' . $lastiteration . ']'; } // Now we add the block that we're actually assigning to. // We're adding a new iteration to this block with the given // variable assignments. $str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;'; // Now we evaluate this assignment we've built up. eval($str); } else { // Top-level block. // Add a new iteration to this block with the variable assignments // we were given. $this->_tpldata[$blockname . '.'][] = $vararray; } return true; } Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-712137 Share on other sites More sharing options...
B34ST Posted December 15, 2008 Author Share Posted December 15, 2008 Im still having problems with this. If no soloution can be found can anybody suggest a simple template class which can handle loops? or a tutorial to get me started on writting my own class? Thanks again for any help Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-716045 Share on other sites More sharing options...
premiso Posted December 15, 2008 Share Posted December 15, 2008 Smarty template system. Probably the best out there for PHP and is free, and easy to work with. Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-716126 Share on other sites More sharing options...
B34ST Posted December 15, 2008 Author Share Posted December 15, 2008 Hi, thanks for the reply. I have heard of smarty and thought about using it but isnt it more advanced than the phpbb way? At the moment the phpbb way is overkill so I think the smarty template system will also be overkill for what I require? As I said in a previous post I only need to pass the variables and loops into the template. Or would it be best to have the more adbanced script for future modifications? Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-716131 Share on other sites More sharing options...
premiso Posted December 15, 2008 Share Posted December 15, 2008 Ummm, overkill is nothing. I would code it in smarty since it is full supported and does not seem to be going obsolete, also it is meant to be used in other systems. The PHPBB does not care, it is only meant to be used with PHPBB. Another system, which is far outdated, is the ETS (Easy Template System). That may be more along your lines, but honestly Smarty is the way to go. Easy to implement with a ton of help from the smarty community and full documentation on usage. Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-716182 Share on other sites More sharing options...
B34ST Posted December 15, 2008 Author Share Posted December 15, 2008 While I was waiting for a reply I decided to have a closer look at smarty and I think I will try to implement this system. As you say overkill is nothing I guess Im just being picky. Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-716198 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.