Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post

    Interesting question

    I have a form that gets a the title, body, author, and tags, I can handle the pushing of title, body and author to the database. But I cant figure out how to push tags to the tags table that is setup as t_id, name, b_id_blog(this is the entry id) and each tag as its own entry in the table. I was thinking I need to do some kind of foreach and then send it up. but the other issue is getting sql to return b_id. Any ideas?

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    explode() them into an array and use a foreach() loop to insert them into the database. You will have to know the b_id ahead of time. I don't know how your script is setup but you can probably post/get it with the form.

  • #3
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    where is what I so far
    PHP Code:
    /*
        *    Function Add Blog Post 
        *    This handles the adding of a post
        */
        
    function addPost($title,$author,$body,$tags){
            
    $sql "INSERT INTO blog (`title`,`body`,`author`) VALUES ('$title','$body','$author')";
            
    $reults connect($sql);
            
    $getID "SELECT b_id FROM blog ORDER BY b_id DESC  LIMIT 0,1";
            
    $rs connect($getID);
            
    $id mysql_fetch_row($rs);
            
    // Need to step through the tags that are stored in a array
           
    $t_temp str_replace(","," ",$tags);
           
    $tags explode(" ",$tags);
           foreach(
    $tags as $tags=>tag){
            
    $mktags "INSERT INTO tags(`name,`b_id_blog`) VALUES ('$tag','$id')";
                    
    connect($mktags);
           }
            if(
    $results == false){
                echo 
    "<div class=\"error_msg\">Sorry Couldn't Update the Blog Post</div>";
            }
            else{
                echo 
    "<div class=\"ok_msg\">Blog Post Update Completed Successful</div>";
            }
        } 
    Does that look right?

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    That line should be $tags = explode(",",$tags); unless that line before it is wrong. Are you using $t_temp somewhere else?

    Edit: That foreach line should be: foreach($tags as $tag)
    Last edited by Inigoesdr; 08-24-2007 at 07:55 PM.

  • #5
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    the reason for the str_replace is for removing the , commas that I told for the user to sperate the tags. unless theres a better suggestion for handling the seperation of the tags.

  • #6
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    ok, I got it working. now I just need to figure out how to update tags when a user edits a page and suggestions? my post update function looks like
    PHP Code:
        /*
        *    Function Update POst 
        *    This handles the changes to the post, With out updating the POST date
        */
        
    function updatePost($postID,$title,$author,$bdate,$body,$tags){
            
    $sql ="UPDATE `blog` SET `title`='$title',`body`='$body',`author`='$author',`date`='$bdate' WHERE `id`='$postID' ";
            
    $reults connect($sql);
            
    $tags explode(",",$tags);
            foreach(
    $tags as $tag){
                   
    $mktags "INSERT INTO tags VALUES ('','$tag') WHERE tags.b_id_blog = '$postID";
                   
    $results connect($mktags);
               } 
            if(
    $results == false){
                echo 
    "<div class=\"error_msg\">Sorry Couldn't Add the Blog Post due to the Tags.</div>";
            }
            else{
                echo 
    "<div class=\"ok_msg\">Blog Post Add Completed Successfully.</div>";
            }
            if(
    $results == false){
                echo 
    "<div class=\"error_msg\">Sorry Couldn't Update the Blog Post</div>";
            }
            else{
                echo 
    "<div class=\"ok_msg\">Blog Post Update Completed Successful</div>";
            }
        } 
    I'm not sure how to store each t_id for the tag so I can remove or change the name of the tag upon update. Suggestions? Complete delete of all the tags with the b_id_blog or should I figure out how to update each tag. I think it would be simpler to recreate each tag. suggestion?

  • #7
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    any ideas on how to update the tags?

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Either select the current ones and delete/insert them as needed or delete them all and insert the ones that were posted. The delete query would be something like this:
    PHP Code:
    $query 'DELETE FROM `tags` WHERE `b_id_blog` = ' $postID
    And you should be able to use the same code to insert them as when making a new post.
    Last edited by Inigoesdr; 08-26-2007 at 03:48 AM.

  • #9
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    whats the best way to compare arrays? and check for changes?

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    I changed my earlier post, you should delete all of the tags and insert the new ones.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •