Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

I am Stuck

Links for php scripts

Moderators: macek, egami, gesf

I am Stuck

Postby barira » Sat May 05, 2012 1:20 pm

Hello everyone!
I am new to this forum. I created a function that was counting user visits at every post.
My function is like this...


function getPostViews($postID)
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count=='')

{
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');

}
return $count;
}
function setPostViews($postID)
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count=='')

{
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');

}
else
{
$count++;
update_post_meta($postID, $count_key, $count);
}

}

i called setPostviews on home.php where i was calling posts from loop and the render that.

it was working fine. Now i tried to make it object oriented and its not working the right way...
my object oriented function is

<?php
class myClass {
function myClass()
{

}
function getPostViews($postID){
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count=='')

{
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');

}
return $count;
}
}
function setPostViews($postID)
{
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count=='')

{
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');


}
else
{
$count++;
update_post_meta($postID, $count_key, $count);

}

}
}
}
$mine=new myClass();
$mins= new myClass();
$mine->getPostViews($postID)
?>

Can anyone help me in this?
Thanks and looking forward for some cool and helpful replies
barira
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat May 05, 2012 12:58 pm

Re: I am Stuck

Postby freshnet » Mon May 07, 2012 9:44 am

Generally within a class you need to tell PHP which instance of a variable or function you're talking about, which means converting all the names to $this->name.

When you say it doesn't work, does that mean you get an error?
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 252
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron