...

View Full Version : Interesting question



moos3
08-24-2007, 06:09 PM
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?

Inigoesdr
08-24-2007, 06:18 PM
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.

moos3
08-24-2007, 07:25 PM
where is what I so far


/*
* 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?

Inigoesdr
08-24-2007, 08:50 PM
That line should be $tags = explode(",",$tags); unless that line before it is wrong. Are you using $t_temp somewhere else?

That foreach line should be: foreach($tags as $tag)

moos3
08-25-2007, 03:29 AM
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.

moos3
08-25-2007, 04:38 AM
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


/*
* 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?

moos3
08-25-2007, 03:37 PM
any ideas on how to update the tags?

Inigoesdr
08-25-2007, 08:04 PM
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:

$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.

moos3
08-25-2007, 08:18 PM
whats the best way to compare arrays? and check for changes?

Inigoesdr
08-26-2007, 05:02 AM
I changed my earlier post, you should delete all of the tags and insert the new ones.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum