Jump to content

Recommended Posts

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;'>&nbsp;[$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;'>&nbsp;[$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.

topics.jpg

topics-table.jpg

Link to comment
https://forums.phpfreaks.com/topic/312628-topic-and-sub-topic-problems/
Share on other sites

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

image.png.4c25043603a94b311b5671dababf8ad7.png

 

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 by Barand

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.

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 by cyberRobot
added column names and formatting

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.

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.