Jump to content


Photo

Need Help with Multiple Categories


  • Please log in to reply
1 reply to this topic

#1 Adastra

Adastra
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 01 June 2006 - 09:30 PM

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.

...

# 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.";
}
/*}*/


}


#2 Adastra

Adastra
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 02 June 2006 - 12:29 PM

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\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users