Php Like函数-检索和传递值
我正在尝试创建一个like函数,但在将like id分配给注释id时遇到了一个问题 代码: INDEX.PHPPhp Like函数-检索和传递值,php,mysql,Php,Mysql,我正在尝试创建一个like函数,但在将like id分配给注释id时遇到了一个问题 代码: INDEX.PHP include "includes/db.php"; include "comment.class.php"; ..... ..... $comment = array(); $result = mysql_query("SELECT * FROM box"); while ($rows = mysql_fetch_assoc($resu
include "includes/db.php";
include "comment.class.php";
.....
.....
$comment = array();
$result = mysql_query("SELECT * FROM box");
while ($rows = mysql_fetch_assoc($result)) {
$comment[] = new Comment($rows);
}
foreach ($comment as $c) {
echo $c->createComment();
}
<div id="addCommentArea">
<p>Add a Comment</p>
<form id="addCommentForm" method="post" action="">
<div>
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<label for="body">Comment Body</label>
<textarea name="body" id="body" cols="30" rows="5"></textarea>
<input type="submit" id="submit" value="Submit" />
</div>
</form>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
include“includes/db.php”;
包括“comment.class.php”;
.....
.....
$comment=array();
$result=mysql_查询(“从框中选择*);
while($rows=mysql\u fetch\u assoc($result)){
$comment[]=新注释($rows);
}
foreach(注释为$c){
echo$c->createComment();
}
添加评论
名称
评论体
COMMENT.PHP
.....
.....
class Comment
{
private $data = array();
public function __construct($row)
{
// The constructor
$this->data = $row;
}
public function createComment()
{
$d = &$this->data;
$link_open = '';
$link_close = '';
return "<div class='comment'>
<div class='username'>".$link_open.$d['name'].$link_close."</div>
<p>".$d["body"]."</p>
<a href='' id='$comment_id' class='like_button'><img src='thumbs_up.png'/></a> <span class='like_show$comment_id'
style='font-weight:bold;font-family:Georgia, Times New Roman, Times, serif;fontsize:12px;position:relative;top:-2px;'>$like_count</span>
</div>";
}
}
。。。。。
.....
课堂评论
{
private$data=array();
公共函数构造($row)
{
//构造器
$this->data=$row;
}
公共函数createComment()
{
$d=&$this->data;
$link_open='';
$link_close='';
返回“
“$link_打开。$d['name']。$link_关闭。”
“$d[“正文”]”
$like_计数
";
}
}
因此,我试图在这里传递注释id和类似计数:
<a href='' id='$comment_id' class='like_button'><img src='thumbs_up.png'/></a> <span class='like_show$comment_id' style='font-weight:bold;font-family:Georgia, Times New Roman, Times, serif;fontsize:12px;position:relative;top:-2px;'>$like_count</span>
$like\u计数
我试图添加一个while循环,遍历表并获取注释id。但是我不能将返回值放在while循环中,因为这样我只能得到一个结果(因此每个注释的like按钮将获得相同的id,如果我喜欢一条注释,所有其他注释都会被喜欢)。如果我只是创建一个while循环并将结果放入一个数组中,那么我也无法将它们传递到comment_id中,以便为每个注释打印like按钮
我还尝试为like按钮创建一个单独的函数,从第一个函数(createComment)获取comment_id,并将其传递给第二个函数(比如“likeComment”)。虽然效果不太好,但我不确定我是否做对了。如何将值从一个函数传递到另一个函数?有没有其他方法可以获得我想要的值
有什么想法吗?这个解决方案可能很简单,但我真的很努力。一切都很好,我很快就能让评论框工作了
任何帮助都将不胜感激为了在对象的方法之间共享数据,您应该将每个函数中的变量分配/访问为$this->variable name。我不确定这是否正是你想要的
class myClass {
public $test_var;
function one () {
$this->test_var = 'a string'; // sets the value
}
function two() {
echo $this->test_var; // prints 'a string'
}
}
我不明白这个问题。您可以使用行中的数据创建注释。那么,哪些数据是您无法访问的?注释id应该已经存在。发布您的所有代码可能有助于了解您遇到的问题。注释id是我无法访问的数据。它说“注释id似乎未初始化”。为了发布所有代码,我将编辑第一篇文章。@Supericy我刚刚添加了所有代码。(我有一个javascript和一个php文件,但这些文件工作正常,我想做的是找到一种方法来检索每个注释id,以便为其分配一个相似的id。注释id是表中的一列吗?如果是,那么您应该使用
$d['comment\u id']
进行访问。或者如果愿意,您可以设置$comment\u id=$d['comment\u id']
在您的返回声明上方,然后您当前的代码应该有效。您刚刚度过了我的一天,非常感谢您……它有效:)谢谢您。我不确定是否可以对数组执行此操作,但我会尝试一下。