Jump to content

Recommended Posts

I'm currently trying to wrap my mind around multiple categories. So far the whole selecting works perfectly fine, but I can't figure out the how to post everything to the catjoin table. I have three tables, the blog table, the blog_cat table with the id's & names of the categories and the blog_catjoin table with the id's of the entry and of the catID.

I don't understand how to use the cat_array and foreach elements. Can someone help me out? My PHP knowledge is still very basic and PHP.net just confuses me. Thank you :)

Relevant parts of the script (with some weird testing-stuff and some stuff commented out):
Currently, this version posts both an entry to the blog table, but it only posts one category into the catjoin table (always the one with the highest ID), however, it should of course make seperate entries for each checked category checkbox. It would be terrific if someone could just show me what I have to do by editing this. I won't understand a thing if you try to explain it with theoretical stuff, I need to see it in use.

[code]
...

# select the categories from the cat table to create a list of checkboxes
include('connect.php');
$result = mysql_query("SELECT blog_cat.catID, catname FROM blog_cat LEFT JOIN blog_catjoin ON blog_cat.catID=blog_catjoin.catID GROUP BY blog_cat.catID ORDER BY catname ASC") or print ("Can't select categories.
" . $result . "
" . mysql_error());

while($row = mysql_fetch_array($result)) {
$catID = $row["catID"];
$catname = $row["catname"];

echo "<.input type=\"checkbox\" name=\"$cat_array\" value=\"$catID\"";
echo " /> $catname   ";
}
....

<./form>

....
# insert all the stuff into the database
$sql = "INSERT INTO $table (...) VALUES (....)";
$result = mysql_query($sql) or print ("Unable to post data.
" . $sql . "
" . mysql_error());

if ($result != false) {
print "Entry has been posted! ($title)";
}


$idresult = mysql_query("SELECT id from $table ORDER by id DESC LIMIT 1") or print ("Can't select categories.
" . $result . "
" . mysql_error());
while($idrow = mysql_fetch_array($idresult)) {
$id = $idrow["id"];
}
/*foreach ($cat_array as $cat) {*/
$query_cat = "INSERT INTO blog_catjoin VALUES ('','$id','$catID')";
$result_cat = mysql_query($query_cat);

if ($result_cat != false) {
echo "All categories successfully added.";
}
/*}*/


}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/10988-need-help-with-multiple-categories/
Share on other sites

Anyone? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] I know posting checkbox data to a db isn't THAT difficult. I just have no clue how to 'make it work' in my script here. [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]
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.