[eluser]anyamanggar[/eluser]
dear LuckyFella73,
i have been modify my script, and this is my database
Code:
-- phpMyAdmin SQL Dump
-- version 2.10.3
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 15, 2012 at 07:54 AM
-- Server version: 5.0.51
-- PHP Version: 5.2.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";# MySQL returned an empty result set (i.e. zero rows).
--
-- Database: `berita`
--
-- --------------------------------------------------------
--
-- Table structure for table `news`
--
CREATE TABLE `news` (
`news_id` int(11) NOT NULL auto_increment,
`title` varchar(250) NOT NULL,
`slug` varchar(250) NOT NULL,
`content` text NOT NULL,
`ip_address` int(20) NOT NULL,
PRIMARY KEY (`news_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;# MySQL returned an empty result set (i.e. zero rows).
--
-- Dumping data for table `news`
[...]
controller
Code:
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$this->load->view('frontend/news_view', $data);
}
public function view($slug)
{
$this->load->helper('url');
$this->load->model('news_model');
$data['news'] = $this->news_model->get_news();
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
exit('The error is here.');
}
$title = $slug;
$url_title = url_title($title);
$this->load->view('frontend/detail_view', $data);
}
}
news_model
Code:
<?php
class News_model extends CI_model{
function __construct()
{
parent::__construct();
}
function create() {
$data = array(
'title' => $this->input->post('title'),
'intro' => $this->input->post('intro'),
'text' => $this->input->post('text'),
'img_path' => $this->input->post('img_path'),
'caption' => $this->input->post('caption')
);
$data['slug'] = url_title($data['title']) ;
$data['date'] = date('Y-m-d H:i:s');
$this->db->insert('news',$data);
}
// ini untuk mengambil //
function getall($limit,$offset){
$this->load->database();
$this->db->order_by('news_id','asc');
$query = $this->db->get('news',$limit,$offset);
return $query->result();
}
function get(){
$this->load->database();
$query = $this->db->get('news');
return $query->result();
}
function findById($id) {
$this->db->where('news_id', $id);
$query = $this->db->get('news');
return $query->row_array();
}
function update(){
$this->load->database();
$data = array(
'title'=>$this->input->post('title'),
'img_path'=>$this->input->post('img_path'),
'intro'=>$this->input->post('intro'),
'text'=>$this->input->post('text'),
'caption' => $this->input->post('caption'),
);
$this->db->where('news_id',$this->input->post('news_id'));
$data['date'] = date('Y-m-d H:i:s');
$this->db->update('news',$data);
}
function delete($id){
$this->load->database();
$this->db->delete('news', array('news_id' => $id));
}
function cariberita($keyword) {
$this->db->like('title',$keyword);
return $this->db->get('news')->result();
}
public function num_rows_berita()
{
return $this->db->get('news')->num_rows();
}
function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$this->db->order_by('news_id','desc');
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
mysql_query("UPDATE news SET ip_address = ip_address + 1 where slug = '$slug'");
$count = mysql_fetch_row(mysql_query("SELECT ip_address FROM news"));
return $query->row_array();
}
}
news_view
Code:
<?php foreach ($news as $news_item): ?>
<ul class="listberitadepan">
<li>
<h2 class="judulberitadepan"><?php echo anchor('news/read/' . $news_item['slug'], $news_item['title']); ?></h2>
<?php echo $news_item['content'] ?>
</li>
</ul>
<?php endforeach ?>
detail_view
Code:
<h1 class="judulberitadepan"><?=$news_item['title']?></h1>
<h1 class="dateberita">Dilihat : <?=$news_item['ip_address']?> Kali</h1>
<?=$news_item['content']?>
route
Code:
$route['news/read/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
the result
Code:
Perawatan Mobil Dengan Kit
[b]has been view : 9 time[/b]
Perawatan Mobil Dengan Kit
If i use thats script, the script is running well, everytime i refresh my browser, the post i click the view count has increase.
, the question is, how if visitor has been refresh the view count doesnt increase, view count will increase if different
ip address.
i am sorry if my english doesnt good enough
thank you