PDA

View Full Version : PHP Creating a list shouldn't be this hard should it?



philsbury
February 10th, 2009, 04:00 PM
Hi everyone,

I'm trying to create a list of comments from a class. I'm just learning OOP, but I know really I should have a class to deal with db connections etc, but for now I have this class:

class comments{

function get_comments($post){
$query = "SELECT * FROM blog_comments WHERE identifier = '$post' AND status = 1";

$result = mysql_query($query) or die("Error: ".mysql_error());

$rows = mysql_fetch_array($result);

return $rows;

}

}

from this I'd like to list all the comments on the page, I've tried a variety of for's, foreachs but with no success, for example:


$comments = new comments;
$post = $_GET['article'];
$rows = $comments->get_comments($post);

for($i = 0; $i <= sizeof($rows); $i++){
$mycomment = $rows[$i]['id'];
print $mycomment;
}

If i print the size of $rows, it tells me there are 14 entries. There should only be 2 (with 7 elements in each). I think I need to get and array with further arrays inside it to achieve what I'm after, but I'm completely stumped!

Any ideas?

Thanks as always,
Phil

Oh, I get the errors:
Notice: Undefined offset: 7 in /Users/phil/Sites/philsbury/includes/comments.inc.php on line 11

Notice: Undefined offset: 8 in /Users/phil/Sites/philsbury/includes/comments.inc.php on line 11

Notice: Undefined offset: 9 in /Users/phil/Sites/philsbury/includes/comments.inc.php on line 11

Notice: Undefined offset: 10 in /Users/phil/Sites/philsbury/includes/comments.inc.php on line 11

Notice: Undefined offset: 11 in /Users/phil/Sites/philsbury/includes/comments.inc.php on line 11

Notice: Undefined offset: 12 in /Users/phil/Sites/philsbury/includes/comments.inc.php on line 11

Notice: Undefined offset: 13 in /Users/phil/Sites/philsbury/includes/comments.inc.php on line 11

Notice: Undefined offset: 14 in /Users/phil/Sites/philsbury/includes/comments.inc.php on line 11

binime
February 10th, 2009, 04:56 PM
function get_comments($post){
$query = "SELECT * FROM blog_comments WHERE identifier = '$post' AND status = 1";

$result = mysql_query($query) or die("Error: ".mysql_error());

while( $rows = mysql_fetch_array($result) ) {
$return[] = $rows;
}

return $return;

}
</SPAN>

try echoing the size of the return value now to see if you have two. then the size of the $return[0] should be 7

philsbury
February 10th, 2009, 05:05 PM
Spot on, thanks, I also had <= in the sizeof which should have only been <

Thanks binime!