chris11 Posted January 21, 2011 Share Posted January 21, 2011 Morning I have a table with many rows. Some of these rows have the same id number. I'm wondering how I print all the rows with the like id like so programing,design,help. My fucntion is below. function getPostTags($postid){ $sql = mysql_query("SELECT tag FROM tags WHERE id=$postid") or die(mysql_error()); if (mysql_num_rows($e)){ extract(mysql_fetch_array($sql)); $tags[$postid]=array(); $tags[$postid]['tag']; } return $tags; } So if I do this <?php print $tags; ?> It only prints out one result from my table. What do I have to do it get it to print all row field values from the table with a comma. Right now the only way I can do this is instead of printing "$tags" (which only gives me one of the many values) I query the database again and do a while statement with $row = mysql_fetch_array. This isn't the right way to do it. Can someone help or give me some pointers? Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/225184-function-help-display-many-rows-with-same-id/ Share on other sites More sharing options...
Valakai Posted January 21, 2011 Share Posted January 21, 2011 I'm not quite sure what you want, I think this is it though. echo(implode(',', $tags)); Quote Link to comment https://forums.phpfreaks.com/topic/225184-function-help-display-many-rows-with-same-id/#findComment-1162997 Share on other sites More sharing options...
trq Posted January 21, 2011 Share Posted January 21, 2011 Your function makes no sense. can we see how you call this function to get any result? Quote Link to comment https://forums.phpfreaks.com/topic/225184-function-help-display-many-rows-with-same-id/#findComment-1162999 Share on other sites More sharing options...
chris11 Posted January 21, 2011 Author Share Posted January 21, 2011 I'd do something like this $thisposttag = $show->getPostTags($postid); if ($thisposttag){ $tags = $thisposttag[$postid]['tag']; } I have a table in my database with two columns. id and tag. The id column has the same value as the posts id column ($postid). I do this using mysql_insert_id. This prints the rows with a comma for each post id. <?php $query = "SELECT tag FROM tags WHERE id = $postid GROUP BY tag"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['tag']; echo ","; } ?> What I am trying to do is build a function that does this and use foreach, I guess. My function above prints out one of the row with the matching postid. Not all rows. That is if I just print $tags. Ps. The e$ in my above post is meant to be $sql Quote Link to comment https://forums.phpfreaks.com/topic/225184-function-help-display-many-rows-with-same-id/#findComment-1163022 Share on other sites More sharing options...
trq Posted January 21, 2011 Share Posted January 21, 2011 My function above prints out one of the row with the matching postid. Not all rows. That is if I just print $tags. The function you have posted does nothing. if you want help with code, it's always best to show us your actual code. Quote Link to comment https://forums.phpfreaks.com/topic/225184-function-help-display-many-rows-with-same-id/#findComment-1163038 Share on other sites More sharing options...
chris11 Posted January 21, 2011 Author Share Posted January 21, 2011 function getPostTags($postid){ $tags = array(); $sql = mysql_query("SELECT tag FROM tags WHERE id=$postid") or die(mysql_error()); if (mysql_num_rows($sql)){ extract(mysql_fetch_array($sql)); $tags[$postid]=array(); $tags[$postid]['tag']=$tag; } return $tags; } This works. I just tried it. But it only returns one row when I print $tags. Quote Link to comment https://forums.phpfreaks.com/topic/225184-function-help-display-many-rows-with-same-id/#findComment-1163077 Share on other sites More sharing options...
BlueSkyIS Posted January 21, 2011 Share Posted January 21, 2011 the code only grabs one record using mysql_fetch_array(), loads the data into $tags and returns $tags. you'll need to loop over the records if you want to use more than one. Quote Link to comment https://forums.phpfreaks.com/topic/225184-function-help-display-many-rows-with-same-id/#findComment-1163081 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.