CCS_newbie
Posts: 6
|
| Posted: 11/19/2008, 5:54 PM |
|
I have spent hours trying to setup a SIMPLE listbox with a nice treeview format for Categories and subcategories, and the only help I found in CCforums are codes just for 2 level category (setting just in the SQL Query). What I really need (please) is a unlimited level subcategory formatted inside a listbosx for a Record Form.
LISTBOX:
CATEGORY_A
--SUBCAT1
--SUBCAT2
----SUBCAT2.1
--SUBCAT3
CATEGORY_B
--SUBCAT1
----SUBCAT1.1
----SUBCAT1.2
I 'm really surprised that a simple way to see a better format for subcategories inside the listbox (as the directory option in CCS tools) is almost impossible to find it.
I already Checked these links:
http://forums.codecharge.com/posts.php?post_id=71501
http://forums.codecharge.com/posts.php?post_id=95828
And I tried the first one, there is a code there (I'm not a coder but at least I try with CCS and the forums), and I don't understand why is not working for me. The only thing is when I put the CALL-draw line:
$your_form_component->my_tree_menu ->SetValue(list_categories() );
in the before show event that will make my page unavailable.
BESIDES, Where Exactly Do I have to put the function list_categories() ?
I will appreciate any help a lot.
Thanks
Danny.
PHP, MYSQL, CODECHARGE 4.0
[SOLVED]
|
 |
 |
mentecky
Posts: 321
|
| Posted: 11/19/2008, 8:05 PM |
|
Danny,
I've seen this question a few times so I posted it as a tip at: http://www.ccselite.com/forums_topics_view.php?forum_id=8&forum_topic_id=32
It involves a recursive function that you will have to edit table and field names in, but it should work for you.
Let me know.
Rick
_________________
http://www.ccselite.com |
 |
 |
feha
Posts: 712
|
| Posted: 11/20/2008, 1:14 AM |
|
Hi
My previous posts are too old and worked ok.
But better solution is http://www.ccselite.com/forums_topics_view.php?forum_id=8&forum_topic_id=32
and this should work for you.
This does alter your listbox and is more proper to CCS standard ...
regards
feha
_________________
Regards
feha
www.vision.to
feedpixel.com |
 |
 |
mentecky
Posts: 321
|
| Posted: 11/20/2008, 4:08 AM |
|
Quote feha:
Thank feha!
It's always nice when a long time member here says something nice about my code.
You're right. Your code was great and very valid. This just conforms to the newer model.
Rick
_________________
http://www.ccselite.com |
 |
 |
CCS_newbie
Posts: 6
|
| Posted: 11/20/2008, 12:19 PM |
|
Thank you very much for your fast replies guys.
I'm still having issues.
So, first, With the code of feha , everything is ok, but is not usefull in my case, because I'm using it inside a Editable GRID, so, I already tryied in a lot of ways (I repeat, I'm not a coder) putting inside of <select> tag in the function the
WINES_CATEGORYmy_tree_menu_{CATEGORY:rowNumber} code, but i Can't make it work.
So, in my case, I need that this listbox work per ROW in the GRID, showing the value according the ID of the ROW (and of course, since is a listbox, to assign a new category for an item or change it per request from the category that it already belongs).
Also, I tried the other code of mentecky (thanks for the fast reply), and I can't make it work. It appears the listbox in the HTML, I assign ListOfValues, Put the Before Show Event, and only appears a Listbox with "Select Value" as the unique option, the same for each row. I checked a lot of times the code and change the values of DB and Fields, but I can't.
Please, can you help me with ANYof the examples that you posted , give me please a hand here. I will be very thankful.
Here are the customized codes for the 2 samples.
=============================
The first one (FEHA)
//BEGIN function listbox
function list_categories() {
$names=array();
$cats=array();
global $dyn_menu;
$dyn_menu = "";
$db = new clsDBMenu();
$idrow = "WINES_CATparent_id_{WINES_CAT:rowNumber}";
$namerow = "{parent_id_Name}";
$dyn_menu .="<select id=\"$idrow\" name=\"$namerow\">";
// adjust the query below with the proper field names and table name
// DON'T change the 'id', 'name' and 'parent' aliases in the query!
$SQL = "SELECT id AS id, name AS name, catorder as catorder, parent_id AS parent FROM WINES_CAT ORDER BY catorder";
$result = $db->query($SQL);
//while ($row = mysql_fetch_assoc($result))
while ($db->next_record())
{
//$names[$row['id']] = $row['name'];
//$cats[$row['id']] = $row['parent'];
$names[$db->f("id")] = $db->f("name");
$cats[$db->f("id")] = $db->f("parent");
}
display_options(hierarchize($cats, 0), $names);
$db->close();
$dyn_menu .="</select>";
return $dyn_menu;
}
function hierarchize(&$cats, $parent) {
$subs = array_keys($cats, $parent);
$tree = array();
foreach ($subs as $sub) {
$tree[$sub] = hierarchize($cats, $sub);
}
return count($tree) ? $tree : $parent;
}
function display_options(&$tree, &$names, $nest = 0) {
global $dyn_menu;
foreach ($tree as $key => $branch) {
$indent = $nest ? str_repeat('--', $nest) . '| ' : '';
$dyn_menu .="<option value=\"$key\">$indent{$names[$key]}</option>\n";
if (is_array($branch)) {
display_options($branch, $names, $nest + 1);
}
}
}
//End function listbox
<><><><><><><><><><><<><<<<<<<<<<<<<<<<<<<<<<<
That one worked nice, but doens't assign the ID per ROW in a Editable GRID.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Second Case, Code of metencky
(this one doesn't populate the values, it only shows "SELECT VALUE" as the only option, the same for all ROWS)
function getCategoriesList($link_category_id=0, $prefix="")
{
// Create an empty result var
$values = "";
// Create a DB class object
$db = new clsDBMenu();
// Select all records where the parent ID = the current
// category ID
$SQL = "SELECT * FROM WINES_CAT WHERE parent_id=$link_category_id ORDER BY catorder";
$db->query($SQL);
$result = $db->next_record();
// This is where the fun happens. Scroll through all
// these categories and get their subcategories
while ($result)
{
$subvals = "";
// Add the current category
$link_cat_id = $db->f("id");
$link_category = $db->f("name");
$values[] = array($link_cat_id, $prefix.$link_category);
// Here's the magic. Get a category list for the
// current category by recalling this function.
$subvals = getCategoriesList($link_cat_id, $prefix.$link_category." - ");
// Scroll through the returned values and add any
// not empty values to our return array
for ($i=0; $i < count($subvals); $i++)
{
if (!empty($subvals[$i]))
{
$values[] = $subvals[$i];
}
}
$result = $db->next_record();
}
$db->close();
return $values;
}
<><><><><><><><><><><><><><><><><><><<><
I set up all the rest of the instructions, but I still can't make it work.
__________________________________
Sorry the longgggggg explanation 
Danny
|
 |
 |
GeorgeS
Posts: 206
|
| Posted: 11/20/2008, 1:40 PM |
|
@Second Case, Code of metencky
I'd check if getCategoriesList() function is called from the beforeshow event of the LISTBOX and not the grid.
_________________
GeorgeS |
 |
 |
mentecky
Posts: 321
|
| Posted: 11/20/2008, 3:38 PM |
|
Danny,
My code assumes that if parent_id=0 it's a top level category. If you are using NULL it may not return any values.
Rick
_________________
http://www.ccselite.com |
 |
 |
mentecky
Posts: 321
|
| Posted: 11/20/2008, 4:37 PM |
|
Danny,
If you have NULL in your table for categories without a parent you can probably change the function definition to:
function getCategoriesList($link_category_id="NULL", $prefix="")
Rick
_________________
http://www.ccselite.com |
 |
 |
CCS_newbie
Posts: 6
|
| Posted: 11/21/2008, 10:30 AM |
|
Quote mentecky:
Danny,
function getCategoriesList($link_category_id="NULL", $prefix="")
Rick
Rick, thanks for your replies. I worked perfect! it was my fault, I didn't check your DB config so, that was the NULL option not configured, NOW, it populate all the categories and Sub in perfect order and format......... BUT... (I hate these "BUT's"):
Like I told you, here in this case I'm working in a Editable grid, and always I leave 3 blank empty rows at bottom for add more categories if needed.
However, the name field in my form is REQUIRED, so, when you submit a change in another category, I assumes that you are also submitted the last 3 rows too (with the Listbox in 0 or Null Value ). And I got the message error that thos 3 name field are requiered.
I tried to fix that putting these combinations in the HTML Select code and none worked.
<option value="" selected> </option>
===================
<option value=""></option>
<option value="0" selected>----MAIN----</option>
======================
<option value="0"> </option>
========================
<option value="" > </option>
<option value="0">----MAIN----</option>
===================
i don'w want to create a ---MAIN-- category in the database directly, because it would involve to re-build all the other pages and code reading that as the MAIN catagory, and so on......
What would be the solution here?
Sorry to be persisten the same issue, although was partially solved (in my particular case)
Thnks any help
Danny
|
 |
 |
mentecky
Posts: 321
|
| Posted: 11/21/2008, 4:31 PM |
|
I don't use editable grid often, but this may work.
Click on your NAME editbox in the grid. In the On Validate event add something like this:
if ($Component->GetValue() == "")
{
$Container->InsertAllowed = false;
}
else
{
$Container->InsertAllowed = true;
}
That should prevent the insert and any errors if the name field is empty.
Rick
_________________
http://www.ccselite.com |
 |
 |
CCS_newbie
Posts: 6
|
| Posted: 11/21/2008, 9:40 PM |
|
THANKS A LOT!!!
that was perfect, that condition really worked.
BTW, this sentence:
function getCategoriesList($link_category_id="NULL", $prefix="")
$variable="Null" $variable=Null $variable='Null' either one never worked.
What I meant in the past posts is that when I realized my error I had to change all the NULL's values to zero 0 in the DB parent_id field.
Now, that field is NOT NULL and default "0", to make your code work. (in my case in the editable Grid)
Also, the HTML code of the listbox finally was left like this:
<select id="WINES_CATmy_tree_menu_{WINES_CAT:rowNumber}" name="{my_tree_menu_Name}">
<option value=""> </option>
<option value="0">------MAIN-------</option>
{my_tree_menu_Options}
</select>
Why?
The 1st value to make your condition work in the case of the NAME fields.
The second one was to be able to add a new category from root (not sub) and JUMP your condition.
Always when you want to edit a page in the grid, with a list of categories, some with sub, some with the NULL value (because they are MAIN and the first option in SELECt tag is NULL), the insert or Update functions will work perfect, updating the change and remaining the rest of values for MAIN categories always default to cero (according to MYSQL DB settings)
SO, Everything perfect.
I am designing a personal website too, I will put a link of your site in gratitude.
Thanks, now this issue can be CLOSED. I don't know how to close it but I'm very thankfull.
Good bye and Happy Hollidays!!
|
 |
 |
mentecky
Posts: 321
|
| Posted: 11/22/2008, 2:01 AM |
|
Danny,
Glad we could help!
To close an entry just scroll up to the top and it will let you edit your original post. Just add [SOLVED] to the end of the subject.
Rick
_________________
http://www.ccselite.com |
 |
 |
|