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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  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.

Link to comment
Share on other sites

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.