?=$topic->user->name;错误还有edit_topic在存的时候提示没有id属性?
<?php
namespace app\index\controller;
use app\index\model\User;
class Index extends \think\Controller
{
public function index()
{
return '这是-面向大海-的-论坛项目';
}
public function register()
{
//检测数据是否提交
if(request()->isPost())
{
// 接收数据
$postData = input('post.');
// 验证验证码
if(!captcha_check($postData['verifycode']))
{
return $this->error('验证码不对');
}
// 验证密码
if(!$this->checkPassword($postData))
{
return $this->error('密码不对');
}
// 实例化对应数据表类并赋值
$user = new User();
$user->name = $postData['username'];
//密码加密
$user->password = md5(md5($postData['password']));
$user->email = $postData['email'];
$user->avatar = 'images/avatar.jpg';
$user->created_at= intval(microtime(true));
$user->save();
return $this->success('恭喜,注册成功');
}
return $this->fetch();
}
// 定义验证密码函数
private function checkPassword($postData)
{
if(!$postData['password'])
{
return false;
}
if($postData['password_confirmation'] !== $postData['password'])
{
return false;
}
return true;
}
public function login()
{
if(request()->isPost())
{
$login = input('post.login');
$password = input('post.password');
$cond = [];
$cond['name|email'] = $login;
$cond['password'] = md5(md5($password));
$user = User::get($cond);
if($user){
session('user',$user);
return $this->success('恭喜,登陆成功。');
}
return $this->error('抱歉,登陆失败。');
}
echo $this->fetch('login',['user'=>session('user')]);
}
public function logout()
{
session('user',null);
echo $this->fetch('login',['user' => session('user')]);
}
public function memc()
{
$memcache = new Memcache();
$memcache->connect('127.0.0.1', 11211) or die('shit');
$memcache->set('key', 'hello memcache!');
$out = $memcache->get('key');
echo $out;
}
}
<?php
namespace app\index\model;
class Topic extends \think\Model
{
public static function getTopic($topicId)
{
return self::withCount(['praises'])->find(['id' => $topicId]);
}
public function user()
{
return $this->belongsTo('User','user_id');
}
public function praises()
{
return $this->hasMany('Praise','topic_id');
}
public static function getTopics()
{
return self::withCount(['praises'])->select();
}
public static function getPageInfo($page,$limitPage)
{
$page = intval($page)<1 ? 1 :intval($page);
$count = self::count();
$pageNum = ceil($count/$limitPage);
$page = $page>$pageNum ? $pageNum : $page;
$showPages = [];
for ($leftPage = $page-3;$leftPage<=$page;$leftPage++)
{
if($leftPage>0){
$showPages[] = $leftPage;
}
}
for ($i=1;$i<3;$i++)
{
if($i+$page<=$pageNum)
{
$showPages[] = $i+$page;
}
}
return ['page'=>$page,'showPages'=>$showPages,'pageNum'=>$pageNum];
}
}
common文件夹下面的topic_list.htmll
<div class="col-md-9 threads-index main-col">
<div class="panel panel-default">
<div class="panel-heading">
<div class="pull-left hidden-sm hidden-xs">
<i class="fa fa-list"></i> 首页
</div>
<ul class="pull-right list-inline remove-margin-bottom thread-filter">
<li>
<a href="">
<i class="fa fa-history"></i> 最新话题
</a>
</li>
<!--li>
<a href="thread.html">
<i class="fa fa-diamond"> </i> 优质帖子
</a>
</li-->
<li>
<a href="">
<i class="fa fa-thumbs-o-up"> </i> 最多点赞
</a>
</li>
<!--li>
<a href="thread.html">
<i class="fa fa-eye"></i> 等待回复
</a>
</li-->
</ul>
<div class="clearfix"></div>
</div>
<div class="panel-body remove-padding-horizontal">
<ul class="list-group row thread-list">
<?php foreach($topics as $topic) :?>
<li class="list-group-item media " style="margin-top: 0px;">
<span class="badge badge-reply-count"> </span>
<div class="avatar pull-left">
<a href="post.html">
<img class="media-object img-thumbnail avatar-48" alt="imooc" src="__IMG__/avatar_small.jpg"/>
</a>
</div>
<div class="infos">
<div class="media-heading">
<i class="fa fa-thumb-tack text-danger"></i>
<a href="<?=url('topic/detail');?>?id=<?=$topic->id;?>" title="<?=$topic->title;?>">
<?=$topic->title;?>
</a>
</div>
<div class="media-body meta">
<a>
<span class="fa fa-thumbs-o-up"> <?=$topic->praises_count;?> </span>
</a>
<span> • </span>
<?php $categorys = getCategoryNames($topic->category_id);?>
<span><?=$categorys[0];?></span>
<span> / </span>
<span><?=$categorys[1];?></span>
<span> • </span>
<a><?=$topic->user->name;?></a>
<span> • </span>
<span class="timeago" data-toggle="tooltip" data-placement="top" title="<?=date('Y-m-d H:i:s', $topic->created_at);?>">
<?=date('Y-m-d H:i:s', $topic->created_at);?>
</span>
</div>
</div>
</li>
<?php endforeach;?>
</ul>
</div>
<div class="panel-footer text-right remove-padding-horizontal pager-footer">
<?php $keyword = isset($keyword) ? $keyword : '';?>
<?php $tagId = isset($tagId) ? $tagId : '';?>
<!-- Pager -->
<ul class="pagination">
<?php if($page <= 1):?>
<li class="disabled"><span>«</span></li>
<?php else:?>
<li><a href="?page=1&keyword=<?=$keyword;?>&tag=<?=$tagId;?>">«</a></li>
<?php endif;?>
<?php foreach($showPages as $showPage):?>
<?php if($page == $showPage):?>
<li class="active"><span><?=$page;?></span></li>
<?php else:?>
<li><a href="?page=<?=$showPage;?>&keyword=<?=$keyword;?>&tag=<?=$tagId;?>"><?=$showPage;?></a></li>
<?php endif;?>
<?php endforeach;?>
<?php if($page < $pageNum):?>
<li><a href="?page=<?=$pageNum;?>&keyword=<?=$keyword;?>&tag=<?=$tagId;?>" rel="next">»</a></li>
<?php else:?>
<li class="disabled"><span>»</span></li>
<?php endif;?>
</ul>
</div>
</div>
</div>
detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>面向大海的论坛</title>
<link rel="stylesheet" href="__CSS__/all-df86af5803.css">
<script src="__JS__/all-17476e6cc3.js"></script>
<script src="__JS__/app.js"></script>
</head>
<body class="forum" data-page="forum">
<?php require(APP_PATH.'index/view/common/header.html'); ?>
<div id="main" class="main-container container">
<div class="row max-width">
<div class="col-xs-12">
<ul class="breadcrumb">
<li>
<a href="index.html">首页</a>
</li>
<li>
<span>帖子详情</span>
</li>
</ul>
</div>
</div>
<div class="col-md-9 threads-show main-col">
<!-- Thread Detial -->
<div class="thread panel panel-default">
<div class="infos panel-heading">
<div class="pull-right avatar">
<a href="my.html"> <img src="__STATIC__/<?=$topic->user->avatar;?>" class="media-object img-thumbnail avatar-64"/> </a>
</div>
<h1 class="panel-title thread-title"><?=$topic->title;?></h1>
<div class="likes">
<a href="#" title="赞" data-url="<?=url('topic/praise');?>?topicId=<?=$topic->id;?>" class="fa fa-thumbs-o-up" id='likeTopic'>
<?=isset($topic->praises_count) ? $topic->praises_count : 0;?>
</a>
</div>
<div class="meta inline-block">
<a href="#" class="remove-padding-left"><?=$categoryNames[0]; ?></a> • <a href="#"><?=$categoryNames[1]; ?></a>
•
<a href="#"><?=$topic->user->name;?></a> 于 <abbr class="timeago"><?=date('Y-m-d H:i:s',$topic->created_at);?></abbr>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body content-body">
<div class="markdown-body" id="emojify">
<p><?=$topic->content;?></p>
</div>
</div>
<div class="panel-footer operate">
<div class="pull-left" style="font-size:15px;">
<a class=""
href="http://service.weibo.com/share/share.php?url=http%3A%2F%2Fimooc.com%2Fthread%2F10&type=3&pic=&title=imooc 接下来需要完善的功能"
target="_blank" title="分享到微博"> <i class="fa fa-weibo"></i> </a> <a
href="https://twitter.com/intent/tweet?url=http%3A%2F%2Fimooc.com%2Fthread%2F10&text=imooc 接下来需要完善的功能&via=imooc.com"
class="" target="_blank" title="分享到 Twitter"> <i class="fa fa-twitter"></i> </a> <a
href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fimooc.com%2Fthread%2F10" class=""
target="_blank" title="分享到 Facebook"> <i class="fa fa-facebook"></i> </a> <a
href="https://plus.google.com/share?url=http%3A%2F%2Fimooc.com%2Fthread%2F10" class=""
target="_blank" title="分享到 Google Plus"> <i class="fa fa-google-plus"></i> </a>
</div>
<div class="pull-right">
<a href="<?=url('topic/edit_topic');?>?topicId=<?=$topic->id;?>">编辑</a>
<span class="tag-list hidden-xs">
Tags:
<?php foreach($tags as $tags): ?>
<a href="<?=url('topic/tag');?>?tag=<?=$tags->tag->id;?>">
<span class="tag"><?=$tags->tag->name;?></span>
</a>
<?php endforeach; ?>
</span>
</div>
<div class="clearfix"></div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="" aria-labelledby="exampleModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">备注内容</h4>
</div>
<form method="POST" action="thread.html/append" accept-charset="UTF-8"><input name="_token"
type="hidden"
value="BAnCy9yzQxp30BiIkeU58OMjR5uWeDM9dCSMNwKH">
<div class="modal-body">
<div class="alert alert-warning">
附加备注, 使用此功能的话, 会给所有参加过讨论的人发送通知.
</div>
<div class="form-group">
<textarea class="form-control" style="min-height:20px" placeholder="请使用Markdown撰写内容"
name="content" cols="50" rows="10"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="submit" class="btn btn-primary">提交</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Reply List -->
<div class="replies panel panel-default list-panel replies-index">
<div class="panel-heading">
<div class="total">回复总数: <b>25</b></div>
</div>
<div class="panel-body">
<ul class="list-group row">
<li class="list-group-item media " id="reply11">
<div class="avatar pull-left">
<a href="my.html"> <img class="media-object img-thumbnail avatar" alt="strdemo"
src="__IMG__/avatar_small.jpg" style="width:48px;height:48px;"/>
</a>
</div>
<div class="infos">
<div class="media-heading meta">
<a href="my.html" title="strdemo" class="remove-padding-left author"> strdemo </a>
<abbr class="timeago" title="2016-06-14 21:19:02">2016-06-14 21:19:02</abbr> <a
name="reply1" class="anchor" href="#reply1" aria-hidden="true">#1</a>
<span class="opts pull-right">
<span class="hideable">
<a class="fa fa-reply btn-reply2reply" data-floor=1 data-username="strdemo" href="#"
title="回复 strdemo"></a>
</span>
<a class="likeable fa fa-thumbs-o-up" data-action="like" data-url="###/like" data-type="Reply" data-id="11" data-count="0" href="javascript:void(0);" title="赞">
</a>
</span>
</div>
<div class="media-body markdown-reply content-body">
<p>支持,不错的系统啊,加油!
</p>
</div>
</div>
</li>
</ul>
<!-- Pager -->
<div class="pull-right" style="padding-right:20px">
</div>
</div>
</div>
<!-- Reply Box -->
<div class="panel panel-default">
<div class="panel-heading">
发表回复
</div>
<div class="panel-body">
<div class="reply-box form">
<div style="padding:20px;">
需要 <a class="btn btn-success" href="login.html">登录</a> 后方可回复, 如果你还没有账号请点击这里 <a
class="btn btn-primary" href="register.html">注册</a>。
</div>
</div>
</div>
</div>
</div>
<div class="col-md-3 side-bar">
<div class="panel panel-default corner-radius">
<div class="panel-heading">
<h3 class="panel-title">BBS</h3>
</div>
<div class="panel-body text-center">
<a href="register.html" class="btn btn-primary"> <i class="fa fa-user"> </i> 注册 </a>
</div>
<div class="panel-footer text-center">
已注册用户请 <a href="login.html">登录</a>
</div>
</div>
<div class="panel panel-default corner-radius">
<div class="panel-heading">
<h3 class="panel-title">小贴士</h3>
</div>
<div class="panel-body">
心若没有栖息的地方,到哪里都是在流浪!
</div>
</div>
<div class="panel panel-default corner-radius">
<div class="panel-heading">
<h3 class="panel-title">统计信息</h3>
</div>
<ul class="list-group">
<li class="list-group-item">社区会员: 747</li>
<li class="list-group-item">主题数: 95</li>
<li class="list-group-item">回帖数: 218</li>
</ul>
</div>
</div>
<div class="clearfix"></div>
</div>
<?php require(APP_PATH.'index/view/common/footer.html'); ?>
</body>
</html>
edit_topic.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>面向大海的论坛</title>
<link rel="stylesheet" href="__CSS__/all-df86af5803.css">
<script src="__JS__/all-17476e6cc3.js"></script>
<script src="__JS__/app.js"></script>
</head>
<body class="forum" data-page="forum">
<?php require(APP_PATH.'index/view/common/header.html'); ?>
<div id="main" class="main-container container">
<div class="row max-width">
<div class="col-xs-12">
<ul class="breadcrumb">
<li>
<a href="index.html">首页</a>
</li>
<li>
<strong>编辑帖子</strong>
</li>
</ul>
</div>
</div>
<div class="thread_create">
<div class="col-md-9 main-col">
<div class="panel panel-default corner-radius">
<div class="panel-heading">编辑帖子</div>
<div class="panel-body">
<div class="reply-box form box-block">
<form method="POST" action="<?=url('topic/edit_topic');?>" accept-charset="UTF-8" id="thread_create_form" class="create_form">
<input name="_token" type="hidden" value="BAnCy9yzQxp30BiIkeU58OMjR5uWeDM9dCSMNwKH">
<div class="form-group">
<input class="form-control" id="thread_title" value="<?=$topic->title;?>" placeholder="标题" name="title" type="text">
</div>
<div class="form-group">
<select class="form-control selectpicker" id='category_id' name="category_id" >
<option value="<?php echo $categoryNames[2];?>" selected> - <?php echo $categoryNames[1];?></option>
<?php $category = isset($category) ? $category : []; ?>
<?php foreach($category as $c => $subCategory ): ?>
<optgroup label="<?=$c ; ?>">
<?php foreach($subCategory as $subc => $name ): ?>
<option value=<?=$subc;?>> - <?=$name ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
</div>
<!-- editor start -->
<div class="editor-toolbar">
<div class="opts pull-right">
<span class="dropdown dropdown-small" id="editor-toolbar-insert-code">
<a href="#editor-toolbar-insert-code" data-toggle="dropdown" title="插入代码"><i class="fa fa-code"></i></a>
<ul class="dropdown-menu insert-codes" role="menu">
<li><a data-lang="php" href="#">PHP</a></li>
<li><a data-lang="html" href="#">HTML</a></li>
<li><a data-lang="scss" href="#">CSS / SCSS</a></li>
<li><a data-lang="js" href="#">JavaScript</a></li>
<li><a data-lang="yml" href="#">YAML <i>(.yml)</i></a></li>
<li><a data-lang="coffee" href="#">CoffeeScript</a></li>
<li><a data-lang="conf" href="#">Nginx / Redis <i>(.conf)</i></a></li>
<li><a data-lang="python" href="#">Python</a></li>
<li><a data-lang="java" href="#">Java</a></li>
<li><a data-lang="ruby" href="#">Ruby</a></li>
<li><a data-lang="erlang" href="#">Erlang</a></li>
<li><a data-lang="shell" href="#">Shell / Bash</a></li>
</ul>
</span>
<a class="btn-upload" href="javascript:void(0);" data-toggle="tooltip" data-placement="bottom" title="上传图片"><i class="fa fa-imageimage"></i> </a>
<input type="file" name="file" class="input-file" style="display: none;" />
</div>
<ul class="nav nav-pills" style="clear:none;">
<li class="edit active"><a href="#">编辑</a></li>
<li class="preview"><a href="#">预览</a></li>
</ul>
</div> <!-- end -->
<div class="form-group">
<textarea class="post-editor form-control" rows="15" style="overflow:hidden" id="body_field" placeholder="请使用Markdown撰写内容" name="body" cols="50"><?=$topic->content;?></textarea>
</div>
<div class="form-group">
<!--显示默认的标签和选项-->
<select class="form-control js-tag-tokenizer" id='tags' multiple="multiple" name="tags[]">
<!--原来所有的供选择的标签-->
<?php $tags = isset($tags) ? $tags : []; ?>
<?php foreach($tags as $tags):?>
<option value="<?=$tags->id;?>"><?=$tags->name;?></option>
<?php endforeach;?>
<!--原来所有的供选择的标签结束-->
<!--显示这个帖子的默认标签-->
<?php foreach($tagsValue as $tagsValue): ?>
<option value="<?=$tagsValue->id;?>" selected><?=$tagsValue->tag->name;?></option>
<?php endforeach; ?>
<!--显示这个帖子的默认标签结束-->
</select>
<?php echo $topicId;?>
<input type="hidden" name="topicId" value="<?=$topicId;?>">
<!--显示默认的标签和选项结束-->
</div>
<div class="form-group status-post-submit">
<input class="btn btn-primary col-xs-2" id="thread-create-submit" type="submit" value="编辑完成,提交">
<div class="pull-right">
<small>支持在编辑框拖拽、复制粘贴或<a class="btn-upload" href="javascript:void(0);">浏览本地文件</a>进行图片上传.</small>
<a href="/markdown" target="_blank"><i class="fa fa-lightbulb-o"></i> 排版说明</a>
</small>
</div>
</div>
<div class="box preview markdown-body" id="preview-box" style="display:none;"></div>
</form>
</div>
</div>
</div>
</div>
<?php require(APP_PATH.'index/view/common/new_topic_sidebar.html'); ?>
</div>
</div>
<?php require(APP_PATH.'index/view/common/footer.html'); ?>
</body>
</html>
正在回答 回答被采纳积分+1
老师是这样:edit_topic在点击保存的时候,报如下错误
这是topic控制器里面的edit_topic方法
public function edit_Topic()
{
$user =session('user');
if(!$user){
$this->error('您还没有登陆,无法编辑,请登录后再操作!','Index/login');
}
if(request()->isPost())
{
$postData = input('post.');
$topic = new TopicModel();
$topicId = $postData['topicId'];
$user = session('user');
$Topic = new TopicModel();
$data = [
'title' => $postData['title'],
'content' => $postData['body'],
'category_id' => $postData['category_id'],
'created_at' => intval(microtime(true)),
'user_id' => $user->id,
];
$Topic->where('id',$topicId)->update($data);
// 处理标签
$tags = $postData['tags'];
foreach ($tags as $tag)
{
if(is_numeric($tag))
{
$this->createTopicTag($tag,$topic->id);
continue;
}
$newTag = $this->createTag($tag);
$this->createTopicTag($newTag->id,$topic->id);
}
$this->success('恭喜,帖子编辑成功。');
}else{
// 显示原来的默认信息
$topicId = input('get.topicId');
$user = session('user');
$topic = TopicModel::getTopic($topicId);
$categoryNames = getcategoryNames($topic->category_id);
$tagsValue = TopicTagModel::getTagsFromTagsByTopicId($topicId);
$tags = TagModel::all();
$this->assign([
'topicId' => $topicId,
'user' => $user,
'topic' => $topic,
'category' => config('category'),
'categoryNames' => $categoryNames,
'tagsValue' => $tagsValue,
'tags' => $tags,
]);
echo $this->fetch('edit_Topic');
}
}
你好,报错是说的是你用的不是一个对象,也就是说要么$topic不是对象,要么$topic->user不是对象,小慕测试了一下发现是因为有的帖子用户id字段对应的用户在用户表里是不存在的,所以才会报这个错误,你也可以自己在控制器里打印一下试试,在控制器里循环遍历时能够打印出一部分帖子的用户名,但下面还是会报这个non-object错误,怀疑是有的$topic里对应的用户数据没有,所以用了判断,如果是对象才让它打印,这样过滤掉那些不存在的用户就没有报错了,然后把下图测试代码注释掉,去html页面也使用这样的判断
topic_list.html判断
foreach循环时加一个判断条件:
另外你的detail页面的这个tags这里了,如下,as前面变量都一样
您说的edit_topic没有id属性小慕没搞明白你的意思,你这里贴出的控制器不是topic控制器,同学可以参考$topic->user这个问题的排错方式,认真检查一下问题出在哪里,排查错误很需要耐心的,加油~
如果解决了你的问题,请采纳,祝学习愉快!
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星