br3nn4n Posted July 2, 2009 Share Posted July 2, 2009 Hey guys, I know that you are basically never supposed to directly output anything from a function, and such you should use return to do it. But what about when I have a bunch of text and stuff I want to also output? Take this example: <?PHP function getApprovedComments() { $query2 = mysql_query("SELECT * FROM comments WHERE `is_approved`='1'"); echo "<div id=\"allcomments\" style=\"border:4px solid black;padding:0px 2px 2px 5px;\">All comments"; while ($result = mysql_fetch_array($query2)) { for ($i = 0; $i < mysql_num_rows($query2) { echo "<div style=\"border:0px solid black;padding:0px 2px 2px 5px;\">"; echo "<a href=\"http://bllllarrrrgggg/index.php?page={$result['page']}&article={$result['art_id']}\">{$result['page']} - Article {$result['art_id']}</a>"; echo "<form name=\"appordel\" method=\"POST\" action=\"\">"; echo "<b><font size=\"+2\" face=\"arial\">$result[name]</font></b> said on $result[date]...</a><br>"; echo "<span style=\"font-size:50pt;color:blue;z-index:-1;float:left;\">“</span><span style=\"z-index:2;\">$result[comments]</span><span style=\"font-size:25pt;color:blue;\">”</span><br /><hr width='25%' align='left'>"; echo "<input type=\"hidden\" name=\"page\" value=\"$result \">"; echo "<input type=\"hidden\" name=\"art_id\" value=\"$result[art_id]\">"; echo "<input type=\"hidden\" name=\"com_id\" value=\"$result[com_id]\">"; echo "IP address: $result[ip]<br>"; echo "<input type=\"radio\" name=\"comment_action\" value=\"approve\" id=\"approve_action_$result[com_id]\"> <label for=\"approve_action_$result[com_id]\">Approve</label> - "; echo "<input type=\"radio\" name=\"comment_action\" value=\"delete\" id=\"delete_action_$result[com_id]\"> <label for=\"delete_action_$result[com_id]\">Delete</label>"; echo " <input type=\"submit\" value=\"<-- Do it!\">"; echo "</form>"; echo "</div>"; break; echo "</div>"; }} } ?> I'm just learning classes, so this is a function inside a class. As it is, and as expected, it works and outputs everything correctly right now. But I KNOW I'm not doing it the best way and thus comes my question. How would YOU go about outputting everything that function does? I'm thinking I can just assign each line to a variable and then call those variables? Not sure if that makes any sense, but I want to try to do this correctly and as efficiently as possible, so any and all help is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/164468-correct-way-to-output-data-from-a-function/ Share on other sites More sharing options...
Philip Posted July 2, 2009 Share Posted July 2, 2009 Honestly, there isn't anything wrong with outputting straight from a function (at least in my eyes.) Typically, you'd want to return the variable so you can separate code from design, thus having like: echo some_function('title'); For your specific function, you could do something like this: <?php function getApprovedComments( ) { // Call your query for comments $query = mysql_query("SELECT * FROM `comments` WHERE `is_approved`=1"); // Setup a blank array $comments = array(); while($row = mysql_fetch_assoc($query)) { // Attach the current to the array of comments. $comments[] = $row; } // Return all the comments return $comments; } // grab all comments.... $comments = getApprovedComments(); // And see, now you can break out of PHP - this will allow for more seperated code using the function return ?> <div id="allcomments" style="border:4px solid black;padding:0px 2px 2px 5px;">All comments <?php // Loop through each comment in the comments array. foreach($comments as $comment) { echo "<div style=\"border:0px solid black;padding:0px 2px 2px 5px;\">"; echo "<a href=\"http://bllllarrrrgggg/index.php?page={$comment['page']}&article={$comment['art_id']}\">{$comment['page']} - Article {$comment['art_id']}</a>"; echo "<form name=\"appordel\" method=\"POST\" action=\"\">"; echo "<b><font size=\"+2\" face=\"arial\">{$comment['name']}</font></b> said on {$comment['date']}...</a><br>"; echo "<span style=\"font-size:50pt;color:blue;z-index:-1;float:left;\">“</span><span style=\"z-index:2;\">{$comment['comments']}</span><span style=\"font-size:25pt;color:blue;\">”</span><br /><hr width='25%' align='left'>"; echo "<input type=\"hidden\" name=\"page\" value=\"{$comment['page']}\">"; echo "<input type=\"hidden\" name=\"art_id\" value=\"{$comment['art_id']}\">"; echo "<input type=\"hidden\" name=\"com_id\" value=\"{$comment['com_id']}\">"; echo "IP address: {$comment['ip']}<br>"; echo "<input type=\"radio\" name=\"comment_action\" value=\"approve\" id=\"approve_action_{$comment['com_id']}\"> <label for=\"approve_action_{$comment['com_id']}\">Approve</label> - "; echo "<input type=\"radio\" name=\"comment_action\" value=\"delete\" id=\"delete_action_{$comment['com_id']}\"> <label for=\"delete_action_{$comment['com_id']}\">Delete</label>"; echo " <input type=\"submit\" value=\"<-- Do it!\">"; echo "</form>"; echo "</div>"; } ?> </div> <?php //.. continue script... ?> I'd suggest if you did it the above way to do something like the following, where you function will define the name of the indexes. That way in the future, if you were to change the column names in the database, you wouldn't have to change every script that you calls the column names - just the function. <?php function getApprovedComments( ) { $query = mysql_query("SELECT * FROM `comments` WHERE `is_approved`=1"); $comments = array(); while($row = mysql_fetch_assoc($query)) { // When creating the array, you would define easy to use keys... // like cID for comment ID, etc. $comments[] = array( 'cID' => $row['commentID'], 'uID' => $row['userID'], 'name' => $row['firstname'], 'date' => $row['postdate'], ); } return $comments; } $comments = getApprovedComments(); foreach($comments as $comment) { echo $comment['cID'],'--',$comment['name'],'<br>'; } ?> Hopefully that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/164468-correct-way-to-output-data-from-a-function/#findComment-867574 Share on other sites More sharing options...
br3nn4n Posted July 2, 2009 Author Share Posted July 2, 2009 Alright I read it over a couple times and that does make sense. Is it really okay to output directly from a function? I thought that was a BIG no-no. Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/164468-correct-way-to-output-data-from-a-function/#findComment-867579 Share on other sites More sharing options...
Philip Posted July 2, 2009 Share Posted July 2, 2009 It really depends on what your function is for and how you plan to use it now & in the future. Quote Link to comment https://forums.phpfreaks.com/topic/164468-correct-way-to-output-data-from-a-function/#findComment-867581 Share on other sites More sharing options...
br3nn4n Posted July 2, 2009 Author Share Posted July 2, 2009 Well I'm really just trying to clean up my code and I had multiple copies of that code posted above in one file...it was a mess. So I'm looking to make it OOP (which I only JUST learned ) and I'm thinking it'll turn out something like this: $comments = $com->getApprovedComments(); foreach($comments as $comment) { echo $comment['cID'],'--',$comment['name'],'<br>'; } And make it SUPER clean, like that. Quote Link to comment https://forums.phpfreaks.com/topic/164468-correct-way-to-output-data-from-a-function/#findComment-867583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.