Jump to content

Function help -display many rows with same id-


chris11

Recommended Posts

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.

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

  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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.