Category Checkboxes - One query and determine if is checked?
Posted: Sun Nov 16, 2008 3:34 am
I have an interesting challenge...I'm working on the edit blog page for my blog. I am trying to determine the best way to implement a checkbox list of categories. The catch is I'm not sure if I can in a single MySQL query fetch all the rows and also determine whether the checkbox should already be checked or not. It may end up being two separate MySQL queries (one executing each time the PHP loop is executed) so I'm looking for a little guidance in how others have approached it.
I've exported the category and relational category tables for my blog below in case any one is curious. I'm initially interested with various views of how to approach the challenge and once something clicks in my head I'll post whatever correlating code I can. Additionally I've been looking through the WordPress code without much luck...my goal pretty much with my blog is to have everything managable including my own code...WordPress is very difficult to work with though it does have the category checkbox feature I'm trying to figure out how I could build myself. Thoughts please?
I've exported the category and relational category tables for my blog below in case any one is curious. I'm initially interested with various views of how to approach the challenge and once something clicks in my head I'll post whatever correlating code I can. Additionally I've been looking through the WordPress code without much luck...my goal pretty much with my blog is to have everything managable including my own code...WordPress is very difficult to work with though it does have the category checkbox feature I'm trying to figure out how I could build myself. Thoughts please?
Code: Select all
-- Database: jabcreat_members-- Table structure for table blog_categories CREATE TABLE IF NOT EXISTS blog_categories ( cat_id INT(6) NOT NULL AUTO_INCREMENT, cat_name VARCHAR(128) NOT NULL, cat_name_base VARCHAR(128) NOT NULL, PRIMARY KEY (cat_id)) ENGINE=MyISAM DEFAULT CHARSET=hp8 AUTO_INCREMENT=8 ; INSERT INTO blog_categories (cat_id, cat_name, cat_name_base) VALUES(1, 'Web Design & Development', 'web_design_and_development'),(2, 'PC Gaming', 'pc_gaming'),(3, 'Philosophy', 'philosophy'),(4, 'Politics', 'politics'),(5, 'Science', 'science'),(6, 'Software', 'software'),(7, 'Hardware', 'hardware'); -- Database: jabcreat_members-- Table structure for table blog_xhref_categories CREATE TABLE IF NOT EXISTS blog_xhref_categories ( xhref_id INT(10) NOT NULL AUTO_INCREMENT, xhref_category_id INT(6) NOT NULL, xhref_thread_id INT(6) NOT NULL, PRIMARY KEY (xhref_id)) ENGINE=MyISAM DEFAULT CHARSET=hp8 AUTO_INCREMENT=9 ; INSERT INTO blog_xhref_categories (xhref_id, xhref_category_id, xhref_thread_id) VALUES(1, 1, 1),(2, 1, 2),(3, 1, 3),(4, 1, 4),(5, 1, 5),(6, 1, 6),(7, 1, 7),(8, 6, 7);