说说功能实现带评论和点赞和数据库

站长手记 作者: 2024-09-03 06:40:01
说说功能实现带评论和点赞和数据库
<?php
/*
CREATE TABLE `wedding_shuoshuo` (
  `id` int(10) NOT NULL,
  `user_id` int(10) NOT NULL,
  `content` text NOT NULL,
  `images` text,
  `create_time` datetime NOT NULL,
  `comments` int(10) NOT NULL DEFAULT '0',
  `zans` int(10) NOT NULL DEFAULT '0',
  `location` varchar(200) NOT NULL DEFAULT '',
  `is_del` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `wedding_shuoshuo`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `wedding_shuoshuo`
  MODIFY `id` int(10) NOT NULL AUTO_INCREMENT;
  
CREATE TABLE `wedding_shuoshuo_comment` (
  `id` int(10) NOT NULL,
  `shuoshuo_id` int(10) NOT NULL,
  `user_id` int(10) NOT NULL,
  `content` text NOT NULL,
  `images` text,
  `create_time` datetime NOT NULL,
  `is_del` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `wedding_shuoshuo_comment`
  ADD PRIMARY KEY (`id`);
  
ALTER TABLE `wedding_shuoshuo_comment`
  MODIFY `id` int(10) NOT NULL AUTO_INCREMENT;
  
CREATE TABLE `wedding_shuoshuo_zan` (
  `id` int(10) NOT NULL,
  `shuoshuo_id` int(10) NOT NULL,
  `user_id` int(10) NOT NULL,
  `create_time` datetime NOT NULL,
  `is_del` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `wedding_shuoshuo_zan`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `wedding_shuoshuo_zan`
  MODIFY `id` int(10) NOT NULL AUTO_INCREMENT;
*/
class shuoshuo
{
	public static function add( $user_id, $content='', $images=array(), $location='')
	{
		$data = array(
			'user_id' => $user_id,
			'content' => $content,
			'images' => join(',',$images),
			'create_time' => date('Y-m-d H:i:s'),
			'comments' => 0,
			'zans' => 0,
			'location' => $location,
			'is_del' => 0
		);
		
		return model('shuoshuo')->setData($data)->add();
	}
	
	public static function del( $shuoshuo_id , $is_del = 1)
	{
		return model('shuoshuo')->setData(array('is_del'=>$is_del))->update('id = '.$shuoshuo_id);
	}
	
	public static function comment( $shuoshuo_id , $user_id , $content='' , $images=array())
	{
		$shuoshuo = model('shuoshuo')->getObj('id = '.$shuoshuo_id);
		if(!$shuoshuo)
		{
			return false;
		}
		
		$data = array(
			'shuoshuo_id' => $shuoshuo_id,
			'user_id' => $user_id,
			'content' => $content,
			'images' => join(',',$images),
			'create_time' => date('Y-m-d H:i:s'),
			'is_del' => 0
		);
		
		$id = model('shuoshuo_comment')->setData($data)->add();
		if(!$id)
		{
			return false;
		}
		
		model('shuoshuo')->setData(array('comments'=>$shuoshuo['comments']+1))->update('id = '.$shuoshuo_id);
		
		return $id;
	}
	
	public static function comment_del( $comment_id , $is_del = 1)
	{
		return model('shuoshuo_comment')->setData(array('is_del'=>$is_del))->update('id = '.$comment_id);
	}
	
	public static function zan( $shuoshuo_id , $user_id)
	{
		$shuoshuo = model('shuoshuo')->getObj('id = '.$shuoshuo_id);
		if(!$shuoshuo)
		{
			return false;
		}
		
		$zan = model('shuoshuo_zan')->getObj('shuoshuo_id = '.$shuoshuo_id.' and user_id = '.$user_id);
		if($zan)
		{
			$id = $zan['id'];
			if($zan['is_del'])
			{
				model('shuoshuo_zan')->setData(array('is_del' => 0))->update('id = '.$id);
				model('shuoshuo')->setData(array('zans'=>$shuoshuo['zans']+1))->update('id = '.$shuoshuo_id);
			}else{
				model('shuoshuo_zan')->setData(array('is_del' => 1))->update('id = '.$id);
				model('shuoshuo')->setData(array('zans'=>$shuoshuo['zans']-1))->update('id = '.$shuoshuo_id);
			}
		}else{
			$data = array(
				'shuoshuo_id' => $shuoshuo_id,
				'user_id' => $user_id,
				'create_time' => date('Y-m-d H:i:s'),
				'is_del' => 0
			);
			
			$id = model('shuoshuo_zan')->setData($data)->add();
			if(!$id)
			{
				return false;
			}
			
			model('shuoshuo')->setData(array('zans'=>$shuoshuo['zans']+1))->update('id = '.$shuoshuo_id);
		}
		
		return $id;
	}
	
	public static function had_zan( $shuoshuo_id , $user_id)
	{
		$zan = model('shuoshuo_zan')->getObj('shuoshuo_id = '.$shuoshuo_id.' and user_id = '.$user_id);
		if($zan && !$zan['is_del'])
		{
			return true;
		}
		return false;
	}
	
	public static function get( $shuoshuo_id )
	{
		$item = model('shuoshuo')->getObj('id = '.$shuoshuo_id);
		if(!$item)
		{
			return false;
		}
		
		$item['images_data'] = explode(',',$item['images']);
		$item['user'] = model('user as u,member as m')->getObj('u.id=m.user_id and u.id = '.$item['user_id'],'m.*,u.username,u.head_ico');
		
		return $item;
	}
	
	public static function find($where , $page = 1, $pagesize = 100)
	{
		return db('shuoshuo as s',array(
			'where' => 's.is_del = 0 '.($where?'and '.$where:''),
			'join' => 'left join user as u on u.id = s.user_id',
			'fields' => 's.*,u.username,u.head_ico',
			'order' => 's.create_time desc',
			'page' => $page,
			'pagesize' => $pagesize
		));
	}
	
	public static function comment_find( $shuoshuo_id, $page = 1, $pagesize = 100)
	{
		return db('shuoshuo_comment as c',array(
			'where' => 'c.is_del = 0 and c.shuoshuo_id = '.$shuoshuo_id,
			'join' => 'left join user as u on u.id = c.user_id',
			'fields' => 'c.*,u.username,u.head_ico',
			'order' => 'c.create_time desc',
			'page' => $page,
			'pagesize' => $pagesize
		));
	}
	
	public static function zan_find( $shuoshuo_id, $page = 1, $pagesize = 100)
	{
		return db('shuoshuo_zan as z',array(
			'where' => 'z.is_del = 0 and z.shuoshuo_id = '.$shuoshuo_id,
			'join' => 'left join user as u on u.id = z.user_id',
			'fields' => 'z.*,u.username,u.head_ico',
			'order' => 'z.create_time desc',
			'page' => $page,
			'pagesize' => $pagesize
		));
	}
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_73438.html