Basic PHP collection
I have an entry that could have several comments associated with that entry.
If I were doing this in C# I would build a collection of these comments. However, I'm unfamiliar with a way to do this in PHP.
My current thought process is to have a comment object that has a method that would build and data_array based off of IDs and then just call that object and utilize the data that way.
Is that the easiest most basic way of doing that? Or is there a better (right way) of building a collection for PHP?
My pseudo code:
class entry
var $id
var $text
class comments
var $id
var $entryId
var $text
var $data_array;
function getCollectionById(){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$da[$i] = array('entryId'=开发者_C百科>$row["entryId"],'text'=>$text);
}
$this->data_array = $da;
}
The arrays can simulate collections very effectively:
$my_collection = array();
// some loop to populate your collection
$my_collection[] = new Comment(...); // adds a new object to the end of the collection
//end loop
you can loop through it with foreach
foreach($my_collection as $comment):
print_r($comment);
endforeach;
精彩评论