OldGrim Posted May 7, 2021 Share Posted May 7, 2021 I am having trouble adding sub-topics to my home made blog system running under PHP-Fusion CMS with PHPver7.4.16 with cgi/fcgi interface and MySQL5.7.34-log. Here are 2 images: Here is the module that produces the output. <?php echo "<div class='col-sm-12'>\n"; echo "<table width='100%' border='0'><tr><td><span class='hdspan2'><b>".$locale['gb_810']."</b></span></td></tr></table>\n"; echo "<table align='center' width='80%' border='0'>\n"; $result = dbquery("SELECT * FROM ".DB_GRIMS_BLOG_TOPICS." ORDER BY topic_order ASC"); if (dbrows($result)) { $cnt = 0; while($data = dbarray($result)) { $id = $data['topic_id']; $title = $data['topic_title']; $sub = $data['topic_sub']; $result1 = dbquery("SELECT * FROM ".DB_GRIMS_BLOG_POST." WHERE topic_id='$id'"); $num_rows = dbrows($result1); if ($sub == '1') { echo "<tr><td width='15'></td><td><a class='lnk-side' href='".BASEDIR."grims_blog/topics_page.php?topic_id=".$id."'>$title</a><span style='font-size:11px;color:white;'> [$num_rows posts]</span></td></tr>\n"; } else { echo "<tr><td colspan='2'><a class='lnk-side' href='".BASEDIR."grims_blog/topics_page.php?topic_id=".$id."'>$title</a><span style='font-size:11px;color:white;'> [$num_rows posts]</span></td></tr>\n"; } } $cnt++; } echo "</table><p></div>\n"; ?> The topic_order field is a new field I added to get the desired output but it's not standard procedure and is in fact not really workable in a live setting because I would have to use php_myadmin to modify it everytime I added or deleted a topic or sub-topic. So the bottom line is that I can't figure out anyway to code the script to always show the sub-topic right under the associated main topic and all in order. If I add a sub-topic to one of the upper main topics it shows up at the bottom; hence the addition of the topic_order field. So right now it's basically a mess and I can't figure out how to code everything to work correctly. I have searched the forums here as well as several other sites and cannot get any clues. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/ Share on other sites More sharing options...
Barand Posted May 7, 2021 Share Posted May 7, 2021 (edited) Here's sample code I wrote a few years back. DATA mysql> select * from category; +----+-----------+--------+ | id | service | parent | +----+-----------+--------+ | 1 | Cat 1 | 0 | | 2 | Cat 2 | 0 | | 3 | Cat 3 | 0 | | 4 | Cat 1 1 | 1 | | 5 | Cat 1 2 | 1 | | 6 | Cat 2 1 | 2 | | 7 | Cat 2 2 | 2 | | 8 | Cat 2 3 | 2 | | 9 | Cat 3 1 | 3 | | 10 | Cat 1 1 1 | 4 | | 11 | Cat 1 1 2 | 4 | | 12 | Cat 2 3 1 | 8 | +----+-----------+--------+ OUTPUT Â CODE <?php $sql = "SELECT id, service, parent FROM category"; $res = $db->query($sql); // // store arrays of items for each parent in an array // while (list($id, $name, $parent) = $res->fetch_row()) { $data[$parent][] = array('id'=>$id, 'name'=>$name); } /** * recursive function to print a category then its child categories * * @param array $arr category data * @param int $parent parent category * @param int $level hierarchy level */ function displayHierarchy(&$arr, $parent, $level=0) { if (isset($arr[$parent])) { echo "<ul class='ul$level'>\n"; foreach($arr[$parent] as $rec) { echo "<li class='li$level'>{$rec['name']}\n"; if (isset($arr[$rec['id']])) displayHierarchy($arr, $rec['id'], $level+1); echo "</li>\n"; } echo "</ul>\n"; } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Subcategories Example</title> </head> <body> <?php displayHierarchy($data, 0); ?> </body> </html> Â Edited May 7, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586382 Share on other sites More sharing options...
OldGrim Posted May 7, 2021 Author Share Posted May 7, 2021 Well I get the gist of what you are showing me but am not sure how I would relate it to the actual topic db table. For instance: Is this what you mean? | id | topic_title | topic_sub | topic_parent_id | | 3 | People | 0 | 0 | | 9 | Celebrities | 1 | 3 | I can figure out most things but I am a little confused here. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586383 Share on other sites More sharing options...
cyberRobot Posted May 7, 2021 Share Posted May 7, 2021 All you should need is the id, topic_title, and topic_parent_id. Where Barand uses "service", you would use "topic_title". Where Barand uses "parent", you would use "topic_parent_id". Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586384 Share on other sites More sharing options...
OldGrim Posted May 7, 2021 Author Share Posted May 7, 2021 Well that's fine and dandy but! What is this? | 10 | Cat 1 1 1 | 4 | I don't understand what the 1 1 1 is after Cat. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586385 Share on other sites More sharing options...
cyberRobot Posted May 7, 2021 Share Posted May 7, 2021 (edited) Those are just sample category names. Yours should be +----+--------------+-----------------+ | id | topic_title | topic_parent_id | +----+--------------+-----------------+ | 1 | Authors | 0 | | 2 | General | 0 | | 3 | People | 0 | | 4 | Celebrities | 3 | | 5 | Historical | 3 | +----+--------------+-----------------+ ...etc. Edited May 7, 2021 by cyberRobot added column names and formatting Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586386 Share on other sites More sharing options...
OldGrim Posted May 7, 2021 Author Share Posted May 7, 2021 OK thanks. I have to go to work now but will apply the changes tonight and report back on the results. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586387 Share on other sites More sharing options...
OldGrim Posted May 8, 2021 Author Share Posted May 8, 2021 OK I have tried every way I can think of to make this work but it gives errors. [08-May-2021 07:16:17 America/Chicago] PHP Parse error:Â syntax error, unexpected 'function' (T_FUNCTION) in /home/whisperw/blog.whisperwillow.net/grims_blog/include/latest_topics.php on line 18 I tried the function in an external file to no avail. Here is what I have now: <?php echo "<div class='col-sm-12'>\n"; echo "<table width='100%' border='0'><tr><td><span class='hdspan2'><b>".$locale['gb_810']."</b></span></td></tr></table>\n"; echo "<table align='center' width='80%' border='0'><tr><td>\n"; $result = dbquery("SELECT topic_id, topic_title, topic_parentid FROM ".DB_GRIMS_BLOG_TOPICS.""); while (list($topic_id, $topic_title, $topic_parentid) = dbrows($result) { $data[$topic_parentid][] = array('id'=>$topic_id, 'name'=>$topic_title) } /** * recursive function to print a category then its child categories * * @param array $arr category data * @param int $topic_parentid parent category * @param int $level hierarchy level */ function displayHierarchy(&$arr, $topic_parentid, $level=0) { if (isset($arr[$topic_parentid])) { echo "<ul class='ul$level'>\n"; foreach($arr[$topic_parentid] as $rec) { echo "<li class='li$level'>{$rec['name']}\n"; if (isset($arr[$rec['id']])) displayHierarchy($arr, $rec['id'], $level+1); echo "</li>\n"; } echo "</ul>\n"; } } displayHierarchy($data, 0); echo "</td></tr></table><p></div>\n"; ?> BTW in case you haven't really noticed our CMS has built-in core DB functions already defined such as dbquery, dbarray, dbrows etc etc etc. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586407 Share on other sites More sharing options...
maxxd Posted May 8, 2021 Share Posted May 8, 2021 You're missing a semicolon on this line: $data[$topic_parentid][] = array('id'=>$topic_id, 'name'=>$topic_title) $data[$topic_parentid][] = array('id'=>$topic_id, 'name'=>$topic_title) Â Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586408 Share on other sites More sharing options...
Barand Posted May 8, 2021 Share Posted May 8, 2021 1 hour ago, OldGrim said: BTW in case you haven't really noticed our CMS has built-in core DB functions already defined such as dbquery, dbarray, dbrows etc etc etc. Point taken. In future I shall have to remember not to respond to any of your problems because I, like the majority of others, do not have your CMS. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586410 Share on other sites More sharing options...
OldGrim Posted May 8, 2021 Author Share Posted May 8, 2021 Well, OK I guess. I converted my topics table to use InnoDB instead of MyISAM; so I guess I'll proceed from there and seek help elsewhere. Thanks for all your help in the past Barand. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586411 Share on other sites More sharing options...
OldGrim Posted May 9, 2021 Author Share Posted May 9, 2021 23 hours ago, Barand said: Point taken. In future I shall have to remember not to respond to any of your problems because I, like the majority of others, do not have your CMS. As a final remark I would like to say that after reading through thousands of posts on this forum over the years, that there are many users that use many different CMS systems and I've never seen anyone get denied help just because of their system in use. Most of the time when given suggestions or sample code I am able to modify it to fit within my particular system parameters. So I am sorry Barand that you feel that way. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586428 Share on other sites More sharing options...
Barand Posted May 9, 2021 Share Posted May 9, 2021 You were complaining that the code sample I gave you didn't use the functions in your CMS. As I don't have your CMS, or even the function documentation for it, there would be no way I could test the code before posting. You are not being denied help. I am saying that under those restrictions, I personally cannot help. There are plenty of others on this forum willing to help. Also, any solutions I post here are not just for you but for others who may come along later looking for a solution to a similar problem, and they too will probably not use your CMS. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586429 Share on other sites More sharing options...
OldGrim Posted May 9, 2021 Author Share Posted May 9, 2021 Point taken. Thanks for explanation. Quote Link to comment https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/#findComment-1586431 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.