Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/136438-update-mysql-field/
Share on other sites

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;
}

 

Link to comment
https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-712137
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-716131
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/136438-update-mysql-field/#findComment-716182
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.