Jump to content

Recommended Posts


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!

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.

 

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 :D) 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. :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.