Jump to content

Getting views from each topic


Davie33

Recommended Posts

Hi am trying to get views to show from each topic in my category page.Me and a friend have been working on this and can't seem to fix the problem.

You can see here on what i mean http://www.games-flash.co.uk/forumcats/1/1.html

 

My file

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
include_once($setting['sitepath'].'/includes/pagination.class.php');
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$cache = RayCache::getInstance($cat_data['name'].$page, null, array('prefix' => $setting['theme'].'-'.$setting['skin'].'_', 'path' => 'cache/', 'expire' => $setting['cachelife']));
$data = $cache->read('home');
if ($data) {
    echo $data;
} else {
    $cache->start_caching();?>
<div id="center">
<?php
    $runOnce = false;
    function echoOnce() {
        global $runOnce;
        if(!$runOnce) {
            $runOnce = true; return "<b>Sub categories:</b><br><div class='catpages'>";
        }
    }
?>
<div class="container_box1"><div class="forumheader"><?php echo $cat_data ['name'];?></div>
<?php
    $query = yasDB_select("SELECT * FROM forumcats WHERE active='yes' AND parent='no' ORDER BY `order` desc");
    while($row = $query->fetch_array(MYSQLI_ASSOC)) {
        if ($id = $row['pid']) {
           echo echoOnce();
           if ($setting['seo'] == 'yes') {
               $catlink = $setting['siteurl'].'forumcats/'.$row['id'].'/1.html';
           } else {
               $catlink = $setting['siteurl'] . 'index.php?act=forumcats&id='.$row['id'] ;
           }
            echo '<a href="' . $catlink . '">' . $subject. '</a>';
        }
    }
    $count = yasDB_select("SELECT count(id) AS count FROM `forumtopics` where cat = " . yasDB_clean($_GET['id']));
    $total = $count->fetch_array(MYSQLI_ASSOC);
    if($total['count'] < 0) {
        echo '<br/><center><span style="font-size:200%;">There are no topics in this category yet.</span></center>';
    } else {
?>
<div class="table">
<table class="listing" cellpadding="0" cellspacing="0">
<tr>
<th class="first">Thread</th>
<th>Replies</th>
<th>Views</th>
<th class="last">Last Post</th>
</tr>
<?php
    $pageurl = new pagination($total['count'], $setting['seo'], '', $setting['gperpage'], 3);
    $query = yasDB_select("SELECT * FROM `forumtopics` where cat = " . yasDB_clean($_GET['id']) . " order by `id` desc LIMIT " . $pageurl->start . ", " . $pageurl->limit);
    while($row = $query->fetch_array(MYSQLI_ASSOC)) {
    $id = $row['id'];
    $subject = $row['subject'];
    $name = $row['name'];
    if ($setting['seo']=='yes') {
        $postlink = $setting['siteurl'] . 'forumtopics/' . $id . '/' . $subject . '.html';
    } else {
        $postlink = $setting['siteurl'] . 'index.php?act=forumtopics&id=' . $id;
    }
?>
<tr>
<td class="first style1"><h3><a href="<?php echo $postlink;?>"><?php echo $subject;?></a></h3>by <?php echo $name;?></td>
<td class="style3">
<?php //This will show how many replies from each topic
$result = yasDB_select("SELECT COUNT(forumposts.topic) AS count FROM forumposts WHERE topic = '$id' ");
$row = $result->fetch_array(MYSQLI_ASSOC);
echo ''.$row['count'].'';
?>
</td>
<td class="style3">
<?php //This will show how many times the topic has been viewed from each topic
$id='id';
$result = yasDB_select("SELECT `views` FROM `forumtopics` WHERE `id` = $id");
$rows = $result->fetch_array(MYSQLI_ASSOC);
$views = $rows['views'];
echo $views;
?>
</td>
<td class="last style2">
<?php //This will show only the last date,time username from each topic
$result = yasDB_select("SELECT id, name, date FROM forumposts WHERE topic = '$id' ORDER BY date DESC LIMIT 1");
    $row = $result->fetch_array(MYSQLI_ASSOC);
    $id = $row['id'];
    $date = $row['date'];
    $name = $row['name'];
    if ($setting['seo'] == 'yes') {
    $postlink = $setting['siteurl'].'forumposts/'.$id.'/1.html';
    } else {
    $postlink = $setting['siteurl'] . 'index.php?act=forumposts&id='.$id ;
    }
echo 'by <a href="">'.$name.'</a> on '.$date.'';
?>
</td>
<?php
}
?>
</tr>
</table>
</div>
<br style="clear: both"/>
<div id="page_box"><?php echo $pageurl->showPagination();?></div>
<?php
}
?>
<div align="left"><button type="button">New Topic</button></div><br/>
<div class="clear"></div>
</div>
    <?php
    $cache->write($cat_data['name'].$page);
    $cache->stop_caching();
    echo $cache->read($cat_data['name'].$page);
}
?>

 

Here is my sql for it.

CREATE TABLE IF NOT EXISTS `forumtopics` (
  `id` int( NOT NULL AUTO_INCREMENT,
  `subject` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `cat` int( NOT NULL,
  `date` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `text` text COLLATE utf8_unicode_ci NOT NULL,
  `views` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `cat` (`cat`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=8 ;

Link to comment
https://forums.phpfreaks.com/topic/266720-getting-views-from-each-topic/
Share on other sites

Its this part of the code am having prob;ems with if anyone can help please.

<?php //This will show how many times the topic has been viewed from each topic
$id='id';
$result = yasDB_select("SELECT `views` FROM `forumtopics` WHERE `id` = $id");
$rows = $result->fetch_array(MYSQLI_ASSOC);
$views = $rows['views'];
echo $views;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.