Jump to content

Recommended Posts


I want to setup a system for letting my members tag their messages and so fourth. But I am really not sure where to start this endeavor. I have gotten most of the basics of php and mysql down and am good at understanding concepts, but I need a point in the right direction on this. I am a little confused as to how I could handle this.

So I want my users to be able to tag messages and friends of theirs, and I want other people to be able to see the tags that are given to the friends and also to be able to use the tags to search for content with the same tags and to be able to search content that would be referenced by tags.

So any help would be greatly appreciated.
Do you want users to store their own tags of particular things or have their tags added to the post or another user? I'll assume you want each user to be able to view a post and tag it and then they have their own personal tags of everything but then you want other users to be able to search for content according to what tags other people have put on things?

Basically, what you need is a table called, say `tags` which contains fields for content ID (like the post id or user id stored in another table), the user id that tagged it and a field for tags. Give users something to separate their tags by (like a space or a comma). Then whenever a user tags something a new row is added to the table with their tags. That way, you can search the table for a certain user id and spit out all the rows when the user wants to look at their tags.

Searching is a fairly easy, you should only need to do a query on your tags table with something like [!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]`tags`[/color] [color=green]WHERE[/color] `tags` [color=orange]LIKE[/color] [color=red]'%$search_string%'[/color] [!--sql2--][/div][!--sql3--]. It's not 100% perfect, but it should do nicely.

If you have any other questions, just ask.
Thanks for the quick reply. I relize thay my question above was rather generalized, so I went and read as much as I could find on the net. And came to a conclusion much like yours.

Here is the plan, so far--stay tuned as I will still need help ;)

I have one table for say their messages, we'll call it "messages" that has the normal columns... id, to_user, from_user, title, subject...whatever.

Then I have another table called "tag map" which would have the following columns... id, message_id, tag_id and user_id.

Then have a final table for the tag itself called "tags", which would have id and tag for columns.

So I have most of that figured out, so maybe an easier question now.

So say they enter three tags in the text field.

Soccer, World Cup, BRAZIL

How can I seperate these, make them all lowercase and ditch all the spaces and commas so that I can have each one make it's own entry in the tags table?

Wow, hope some others learn from this too. haha
That sounds like a good idea. To do all that you wanted you'd need the following:[code]$string = $_REQUEST['tags'] /* from a form where users enter their tags */
$ex = explode(',', $string); /* splits the string up whenever it finds a , character and places each section in an array */
foreach($ex as $tag) // Loop through the resulting array
{
$tag = trim($tag); // get rid of extra spaces
$tag = strtolower($tag); // make everything lowercase

$sql = mysql_query("
SELECT COUNT(*)
FROM `tags`
WHERE `tag_name`='$tag
'); // query to count occurences of $tag
if(mysql_result($sql, 0) < 1) /* check whether the tag's already in the database */
{
mysql_query("
INSERT INTO `tags`
(`tag_name`)
VALUES ('$tag')
"); // Insert the new tag into the database
}
}[/code]
Sounds good, I hope you get the system on the road. I'd also like to mention that your questions were very well done. If I could get a moderator's attention I think this could be used as a model for how to ask questions.

That said, if you need help with anything else and need a response fairly quick, feel free to PM or even email me, I should be able to give you a response in good time (unless I happen to be on holiday) and will post a summary thread if I feel it could benefit other users.
Thank Fyorl!

Ok, so I have adding tags and viewing them down. This is how I am adding them.

[code]
if(isset($_POST['add_tags'])){
  $string = $_POST['tags'];
  $ex = explode(',', $string);
  foreach($ex as $tag){
    $tag = trim($tag);
    $tag = strtolower($tag);

    $sql = mysql_query("SELECT COUNT(*) FROM tags WHERE tag_name='$tag'");
    if(mysql_result($sql, 0) < 1){
      mysql_query("INSERT INTO tags SET tag='$tag'");
    }
    $q_tag_id = "SELECT * FROM tags WHERE tag='$tag'";
    $r_tag_id = mysql_query($q_tag_id);
    $a_tag_id = mysql_fetch_array($r_tag_id);
    $tag_id = $a_tag_id['id'];
    mysql_query("INSERT INTO tag_map SET tag_id='$tag_id', map_to='$message_id', user_id='$user_id', type='message'");
  }
}
[/code]

And then I am having the tags that pertain to the message displayed in the messages page. And I am having them link to a tag search of sorts, that is basically functional, just needs some cleanup. Here is that code.

[code]<?
$q_get_tags = "SELECT * FROM tag_map WHERE tag_id='$tag_id'";
$r_get_tags = mysql_query($q_get_tags);
$nr_get_tags = mysql_num_rows($r_get_tags);
if($nr_get_tags >= 1){
  echo "<div class=\"body_text\" style=\"border-bottom: 1px dashed silver; padding: 5px; margin-left: 15px\">";
  while($a_get_tags = mysql_fetch_array($r_get_tags)){
    $map_to = $a_get_tags['map_to'];
    $item_type = $a_get_tags['type'];
    if($item_type == "message"){
      $q_info = "SELECT * FROM messages WHERE id='$map_to'";
      $r_info = mysql_query($q_info);
      $a_info = mysql_fetch_array($r_info);
      $title = $a_info['subject'];
      $message = $a_info['message'];
      echo "<a href=\"/userpage/messages/view_message.php?id=".$map_to."\">".$title."</a>
        <div style=\"font-size: 0.8em; margin-left: 15px\">".$message."</span>";
    }
  }
  echo "</div><br />";
}
?>[/code]

Now this is only for if a tag link gets clicked from another page. A different full search engine will need to be made later.

So I was wondering if you of you masterminds, could take a look at the above code and clean it up and simplify it any. So it isn't...bulky.

I can't think of anything myself, but I was never any good at simplifying code unless it's glaringly obvious. A lot of other people on the forum are good at that stuff though so hopefully you'll get a reply from them. The only thing I can suggest is for looking ahead. I assume you're going to have multiple item types such as message, post, image. Instead of putting a bunch of if{}elseif{}elseif{}else statements to accomodate them, you can use a switch:
[code]switch($item_type)
{
case 'message':
// get the relevant info
break;

case 'image':
// do whatever you need for images
break;

default:
// Invalid item_type!
// You don't need a break; here
}

// You can also use the item_type to link to the relevant page
echo "<a href=\"/userpage/view/view.php?type=$item_type&amp;id=$map_to\">";[/code] Note: when you're using double quotes you don't need "text ".$var." more text" you can just do "text $var more text" this doesn't work for single quotes.
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.