Forum Moderators: coopster
The problem is as folows...
I working on a news editor where the news articles can be in several categories (diffrent parts of the site).
I have 4 tables:
news (news_id, language_iso, headline, intro, text)
news_categories (category_id)
news_categories_descriptions (category_id, language_iso, name)
index_news_categories (news_id, category_id)
when i add an article i basicly check the boxes where i want the article to be seen. (internal page, public page, store_page). This saves a row in index_news_categories for every checkbox saved.
This works. Deleting aswell.
The problem is when i need to update the categories.
So here are some code from the editpage:
// Gets all categories
$get_categories = $db->get_results("SELECT DISTINCT news_categories.category_id, news_category_description.name FROM news_categories
LEFT JOIN news_category_description ON news_categories.category_id = news_category_description.category_id
LEFT JOIN index_news_categories ON news_categories.category_id = index_news_categories.category_id
WHERE language_iso = '".$person_language_iso."'");
// Gets saved categories
$id_array = $db->get_results("SELECT category_id FROM index_news_categories WHERE news_id = '".$id."'",ARRAY_N);
// The loop that doesnt work
foreach ($get_categories as $cat) {
$checked = in_array($cat->category_id, $id_array)? ' checked=\"checked\"' : '';
echo "<input type=\"checkbox\" name=\"form_news_category[]\" id=\"form_news_category[]\" value=\"".$cat->category_id."\" ".$checked."/>".$cat->name."<br />";
}
This i think should loop through all categories, display a checkbox for each, and then check the ones that are saved in the db.
But i just can't get it to work. Been on this for about 9hours strait. I can't do it :/
Any help would be much apreciated!
Where do i go from here?
BTW I use ezSQL DB Abstraction if your wondering.
Peace
/Emil
The loop i mention at the end where all the categories are listed with checkboxes works to the point that all the boxes are shown as they should, so does the names. They all have the right values. But i don't get any of them "pre-checked" even though they should be.
example (for an article with two categories(rows) in index_news_categories):
as is now:
uncheckedbox1 categoryname1
uncheckedbox2 categoryname2
uncheckedbox3 categoryname3
when it should be
uncheckedbox1 categoryname1
checkedbox2 categoryname2
checkedbox3 categoryname3
I was thinking that if i get them prechecked from the db i could just delete every row in the index_news_categories for the newsarticle when i submit the form, and then insert new rows instead of updating them. (since a non-checked checkbox doesn't return a value, i guess)
Ive tried alot of diffrent combinations of the code.
Is the wrong approach?
I thought of adding the category_id directly into the news table. But that would limit me from using the same article on several categories?
Or is there a workaround?
I looked into the explode and implode functions alittle. But as i have this aaaalmost working (just the updating that doesnt work) i rather not start over again.
Sorry for my bad english.
If the problem still needs clarification, please let me know! :)
Peace
/Emil
Yeah i know that might look strange. It's how ezSQL works.
// An example from the ezSQL manual
$users = $db->get_results("SELECT id, name FROM users");
foreach( $users as $user ){
echo $user->id;
echo $user->name;
}
But i've been up for almost 24 hours so i will double check that tomorrow with fresh eyes.
thanx again
Peace
/Emil