Hello, I'm trying to create a like function but I have a problem with assigning like id to the comment id. Code: INDEX.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(); } .... .... PHP: COMMENT.PHP ..... ..... class Comment { private $data = array(); .... .... public function createComment() { ........ ........ ........ 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>"; } } .... .... PHP: So i'm trying to pass the comment_id and like_count here: <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> 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. 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? 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. Any help would be much appreciated
Hm... so is it better not to use the second php file and put the code in index.php instead? with no classes and functions?
You've basically overworked the plumbing -- and that's why the drain is clogging every five inches. It's ok to separate out things like that -- but really this MESS of setting values is just gonna take too long to run, in particular storing a result instead of just echo'ing it out.... don't loop through the same data three times if you don't have to! Don't store it more than you need to... ... and you probably shouldn't be using the mysql_ functions either, given the giant red warning box on every mysql_ function on php.net telling you not to -- that everyone STILL seems to be ignoring... you want to use objects, do it where it matters by leveraging PDO or mySQLi, instead of the deprecated and soon to be obsolete and completely removed from PHP mysql_ functions. ... and I'd also suggest easing up on the double quotes as they just make life harder and all that string addition is just gonna be slow. Would also help if you practiced semantic markup since all that STYLE has no business in your markup... and are those actually headings? Assuming $db is a PDO object: $statement=$db->query('SELECT * FROM box'); while ($row = $statement->fetch()) { createComment($row); } Code (markup): With this as the output function: function createComment($data) { echo ' <div class="comment"> <h2>',$link_open,$data['name'],$link_close,</h2> <p>',$data['body'],'</p> <a href="" id="',$comment_id,'" class="like_button"> <img src="thumbs_up.png" alt="like" /> </a> <span class="likeCount">',$like_count,'</span> <!-- .comment --></div>'; } Code (markup): As an array/object $data would be passed by reference, though I have no idea where the rest of your values are even coming from. Are those globals? (boo, hiss), did you just not finish making them actual properties? I can understand wanting the output function separate -- it's handy if you're going to make a system that has multiple skins that might have different output... but this really would be a case of 'objects for nothing' when you could just pass the data directly. IF, IF you are making a full blown getter/setter where you're building all the data before sending it to the theme, I'd consider making the data storage object a singleton so you could "getInstance" it from the theme... keeping the theme function based instead of object based. In that case I'd make a database object private to it passed during or immediately after construction, and make the query part of the data object instead of the other way around. That's actually how the CMS I'm in the tenth generation rewrite of handles data... I have the theme call a singleton that has the database secured and can only do specific actions for the theme like pull the next record.