Note that there are some explanatory texts on larger screens.

plurals
  1. POLike Function - Retrieve And Pass Value
    text
    copied!<p>I'm trying to create a like function but I have a problem with assigning like id to the comment id.</p> <p>Code:</p> <p>INDEX.PHP</p> <pre><code>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-&gt;createComment(); } &lt;div id="addCommentArea"&gt; &lt;p&gt;Add a Comment&lt;/p&gt; &lt;form id="addCommentForm" method="post" action=""&gt; &lt;div&gt; &lt;label for="name"&gt;Name&lt;/label&gt; &lt;input type="text" name="name" id="name" /&gt; &lt;label for="body"&gt;Comment Body&lt;/label&gt; &lt;textarea name="body" id="body" cols="30" rows="5"&gt;&lt;/textarea&gt; &lt;input type="submit" id="submit" value="Submit" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt; </code></pre> <p>COMMENT.PHP</p> <pre><code>..... ..... class Comment { private $data = array(); public function __construct($row) { // The constructor $this-&gt;data = $row; } public function createComment() { $d = &amp;$this-&gt;data; $link_open = ''; $link_close = ''; return "&lt;div class='comment'&gt; &lt;div class='username'&gt;".$link_open.$d['name'].$link_close."&lt;/div&gt; &lt;p&gt;".$d["body"]."&lt;/p&gt; &lt;a href='' id='$comment_id' class='like_button'&gt;&lt;img src='thumbs_up.png'/&gt;&lt;/a&gt;&amp;nbsp;&lt;span class='like_show$comment_id' style='font-weight:bold;font-family:Georgia, Times New Roman, Times, serif;fontsize:12px;position:relative;top:-2px;'&gt;$like_count&lt;/span&gt; &lt;/div&gt;"; } } </code></pre> <p>So i'm trying to pass the comment_id and like_count here:</p> <pre><code>&lt;a href='' id='$comment_id' class='like_button'&gt;&lt;img src='thumbs_up.png'/&gt;&lt;/a&gt;&amp;nbsp;&lt;span class='like_show$comment_id' style='font-weight:bold;font-family:Georgia, Times New Roman, Times, serif;fontsize:12px;position:relative;top:-2px;'&gt;$like_count&lt;/span&gt; </code></pre> <p>I tried to add a while loop, go through the table and get the comment_id. But I can't put the return inside the while loop cause then I'll get only one result (so the like button for each comment will get the same id and if i like one comment, all the other comments will be liked). If I just create a while loop and put the results in an array, then I also cannot pass them in the comment_id to print the like button for each comment.</p> <p>I also tried to create a separate function for the like button, get the comment_id from the first function (createComment) and pass it to the second function (let's say ''likeComment''). It didn't work as well but I'm not sure if I did it right. How can i pass values from one function to another? Is there any other way to get the value I want?</p> <p>Any ideas? The solution is probably simple, but I'm really struggling with it. Everything works fine and I'm so close to make the commentbox works.</p> <p>Any help would be much appreciated</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload