Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Navigation

Help Needed - Combine 2 Scripts Into One

Last updated on 5 years ago
G
GrimlochSenior Member
Posted 5 years ago
I am really disappointed that no one will even take a stab at this problem to help me out.
R
Anonymous UserVeteran Member
Posted 5 years ago
Well, I'm focusing only on issues related to core.


Anyway, did you tried LEFT JOIN and nested loops?
G
GrimlochSenior Member
Posted 5 years ago

RobiNN wrote:

@RobiNN - Well, I'm focusing only on issues related to core.


Anyway, did you tried LEFT JOIN and nested loops?

All I have so far is this which should give me the root topics:

$result = dbquery("SELECT tp.topic_id, tp.topic_title, tp.topic_parentid FROM ".DB_GRIMS_BLOG_TOPICS." tp
 LEFT JOIN ".DB_GRIMS_BLOG_POSTS." ps ON tp.topic_id=ps.post_id WHERE tp.topic_parentid='0'


And this which should give me the sub-topics:
$result = dbquery("SELECT tp.topic_id, tp.topic_title, tp.topic_parentid FROM ".DB_GRIMS_BLOG_TOPICS." tp
 LEFT JOIN ".DB_GRIMS_BLOG_POSTS." ps ON tp.topic_id=ps.post_id WHERE tp.topic_parentid=tp.topic_id");


I have never completely learned JOINS so I don't know where to go from here. This is what I need to do. Display all topics and sub-topics in the proper order and show all posts within them as seen here: https://blog.whisperwillow.net/grims_.../index.php . They only show up correctly here because I modified the table itself to be in order. But if I add another topic or sub-topic it doesn't show up at the proper location in the heirarchy but shows up at the bottom of the list. I can't use a ul list because I can't show the related post count for them. I hope this makes sense,
Edited by Grimloch on 09-05-2021 20:35, 5 years ago
G
GrimlochSenior Member
Posted 5 years ago
Come on guys please help me out with this. I just cannot get a handle on doing a JOIN or INNER JOIN on the same table to get the desired results.
Here is the script that produces the output as currently seen on my blog at https://blog.whisperwillow.net/grims_.../index.php
<?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."");
   while($data = dbarray($result)) {
      $id = $data['topic_id'];
      $title = $data['topic_title'];
      $sub = $data['topic_parent'];
         $result1 = dbquery("SELECT * FROM ".DB_GRIMS_BLOG_POST." WHERE topic_id='$id'");
         $num_rows = dbrows($result1);
if($sub>0) {
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";
   }
}
echo "</table><p></div>\n";
?>

Table data image and browser output image:
I need to concat the sub-topics to be underneath their associated main topic in the correct order as seen in one of the images at the top of this post. I just cannot figure out how to do it. I think I can figure out the table joins but then how to configure the output from that is beyond my ability. Please Help!
Edited by Grimloch on 16-05-2021 19:57, 5 years ago
Grimloch attached the following image:
Image not found Image not found
D
douwe_yntemaSenior Member
Posted 5 years ago
I.m.o the seconde query inside the loop does nothing more than returning 1 row. Should it not be something like: topic_parent=id?
G
GrimlochSenior Member
Posted 5 years ago
Look again; the first query returns topics and sub-topics. The second query returns POSTS by the topic id. That's how I get the post count for each topic/sub-topic.
Edited by Grimloch on 17-05-2021 01:28, 5 years ago
D
douwe_yntemaSenior Member
Posted 5 years ago
Did some googling and tests with the following code:



$result = dbquery("SELECT child.id, child.title as subtitle, parent.title as parenttitle FROM test AS parent JOIN test AS child ON (child.parent = parent.id)");

while ($data = dbarray($result)) {
 echo $data['parenttitle'];
 echo "---".$data['subtitle']."<br>";
}
Edited by douwe_yntema on 17-05-2021 10:34, 5 years ago
douwe_yntema attached the following image:
Image not found Image not found
G
GrimlochSenior Member
Posted 5 years ago

douwe_yntema wrote:

@douwe_yntema - Did some googling and tests with the following code:



$result = dbquery("SELECT child.id, child.title as subtitle, parent.title as parenttitle FROM test AS parent JOIN test AS child ON (child.parent = parent.id)");

while ($data = dbarray($result)) {
 echo $data['parenttitle'];
 echo "---".$data['subtitle']."<br>";
}

I tried it with appropriate changes. Does not work for my system. I get an ARRAY(NULL) error.
D
douwe_yntemaSenior Member
Posted 5 years ago
Can you send me a SQL-dump of your database?
G
GrimlochSenior Member
Posted 5 years ago
If you look at Post#6 there is an image of the db table.
G
GrimlochSenior Member
Posted 5 years ago
Tell me if this will work for the join. Even if it will I have no idea how to gather and display the data below the query. I'm grabbing at straws here folks.
$result = dbquery("SELECT tp.topic_id, tp.topic_title, tp.topic_parent, sp.topic_title, sp.topic_parent FROM ".DB_GRIMS_BLOG_TOPICS." tp
JOIN ".DB_GRIMS_BLOG_TOPICS." sp.topic_id, sp.topic_title, sp.topic_parent WHERE tp.topic_id = sp.topic_parent");
D
douwe_yntemaSenior Member
Posted 5 years ago

Grimloch wrote:

@Grimloch - If you look at Post#6 there is an image of the db table.


I Know, that's why I made a test table with similar content. For me it works, but probably there is something in your database what causes the script is not working. That is why I asked for a dump.
D
douwe_yntemaSenior Member
Posted 5 years ago

Grimloch wrote:

@Grimloch - Tell me if this will work for the join. Even if it will I have no idea how to gather and display the data below the query. I'm grabbing at straws here folks.
$result = dbquery("SELECT tp.topic_id, tp.topic_title, tp.topic_parent, sp.topic_title, sp.topic_parent FROM ".DB_GRIMS_BLOG_TOPICS." tp
JOIN ".DB_GRIMS_BLOG_TOPICS." sp.topic_id, sp.topic_title, sp.topic_parent WHERE tp.topic_id = sp.topic_parent");


This is my result:


Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.topic_id, sp.topic_title, sp.topic_parent WHERE tp.topic_id = sp.topic_parent' at line 2
G
GrimlochSenior Member
Posted 5 years ago
OK. I tried it your way:
echo "<table align='center' width='80%' border='0'><tr><td>\n";
$result = dbquery("SELECT child.topic_id, child.topic_title as subtitle, parent.topic_title as parenttitle FROM ".DB_GRIMS_BLOG_TOPICS." AS parent
JOIN ".DB_GRIMS_BLOG_TOPICS." AS child ON (child.topic_parent = parent.topic_id");

   while ($data = dbarray($result)) {
       echo $data['parenttitle'];
       echo "---".$data['subtitle']."<br>";
   }

echo "</td></tr></table><p></div>\n";

And here is the result:
[17-May-2021 07:42:05 America/Chicago] PHP Fatal error: Uncaught Error: Call to a member function setFetchMode() on null in /home/whisperw/blog.whisperwillow.net/includes/db_handlers/pdo_functions_include.php:120
Stack trace:
#0 /home/whisperw/blog.whisperwillow.net/grims_blog/include/latest_topics.php(7): dbarray(NULL)
#1 /home/whisperw/blog.whisperwillow.net/grims_blog/include/modules.php(6): include('/home/whisperw/...')
#2 /home/whisperw/blog.whisperwillow.net/grims_blog/index.php(43): include('/home/whisperw/...')
#3 {main}
 thrown in /home/whisperw/blog.whisperwillow.net/includes/db_handlers/pdo_functions_include.php on line 120
[17-May-2021 07:42:07 America/Chicago] PHP Fatal error: Uncaught Error: Call to a member function setFetchMode() on null in /home/whisperw/blog.whisperwillow.net/includes/db_handlers/pdo_functions_include.php:120
Stack trace:
#0 /home/whisperw/blog.whisperwillow.net/grims_blog/include/latest_topics.php(7): dbarray(NULL)
#1 /home/whisperw/blog.whisperwillow.net/grims_blog/include/modules.php(6): include('/home/whisperw/...')
#2 /home/whisperw/blog.whisperwillow.net/grims_blog/index.php(43): include('/home/whisperw/...')
#3 {main}
 thrown in /home/whisperw/blog.whisperwillow.net/includes/db_handlers/pdo_functions_include.php on line 120
[18-May-2021 07:25:37 America/Chicago] PHP Fatal error: Uncaught Error: Call to a member function setFetchMode() on null in /home/whisperw/blog.whisperwillow.net/includes/db_handlers/pdo_functions_include.php:120
Stack trace:
#0 /home/whisperw/blog.whisperwillow.net/grims_blog/include/latest_topics.php(8): dbarray(NULL)
#1 /home/whisperw/blog.whisperwillow.net/grims_blog/include/modules.php(6): include('/home/whisperw/...')
#2 /home/whisperw/blog.whisperwillow.net/grims_blog/index.php(43): include('/home/whisperw/...')
#3 {main}
 thrown in /home/whisperw/blog.whisperwillow.net/includes/db_handlers/pdo_functions_include.php on line 120
D
douwe_yntemaSenior Member
Posted 5 years ago
You have a typo in your query, the closing parenthesis at the end is not good, it should be: )");

See my example
G
GrimlochSenior Member
Posted 5 years ago
This almost works but is not complete:
$result = dbquery("SELECT child.topic_id, child.topic_title as subtitle, parent.topic_title as parenttitle FROM ".DB_GRIMS_BLOG_TOPICS." AS parent
JOIN ".DB_GRIMS_BLOG_TOPICS." AS child ON (child.topic_parent = parent.topic_id)");
   while ($data = dbarray($result)) {
      $id = $data['parent(topic_id)'];
      $maintitle = $data['parenttitle'];
      $subtitle = $data['subtitle'];
if ($maintitle) {
       echo "<tr><td colspan='2'><a class='lnk-side' href='".BASEDIR."grims_blog/topics_page.php?topic_id=".$id."'>$maintitle</a></td></tr>\n";
    }
if ($subtitle) {
       echo "<tr><td width='15'></td><td>&raquo;&nbsp;<a class='lnk-side' href='".BASEDIR."grims_blog/topics_page.php?topic_id=".$id."'>$subtitle</td></tr>\n";
   }
}

I need a distinct 'topic_id' for the links. Also I can't seem to get the POST COUNT to work right because of not having a distinct 'topic_id'. Also I need for the main topics which contain posts(with a topic_parent of zero) to be displayed only once with the sub-topics underneath. Here is an image of the current results:
Grimloch attached the following image:
Image not found
D
douwe_yntemaSenior Member
Posted 5 years ago
Because there is always a maintitle and a subtitle, both links are displayed. That's why you have the maintitles twice.
G
GrimlochSenior Member
Posted 5 years ago

douwe_yntema wrote:

@douwe_yntema - Because there is always a maintitle and a subtitle, both links are displayed. That's why you have the maintitles twice.

You are not understanding me; I DO NOT want it that way. I want main title with sub-titles underneath each main title in the proper sequence. Main titles AND sub-titles can have their own distinct posts. If I forget all this and use the added 'topic_order' field that I manually added to the table I can make everything display properly with post counts AND the correct topic_id's but it's a cheat and I can't do it that way. Because the 'topic_order' will change everytime a sub-topic is added or deleted which would require manually changing that field every time.
Anyway I think your suggestion will work I just have to figure out the display part but I need a 'distinct topic_id'; so far everything I've tried won't work with your code.
D
douwe_yntemaSenior Member
Posted 5 years ago

Grimloch wrote:

@Grimloch -

douwe_yntema wrote:

@douwe_yntema - Because there is always a maintitle and a subtitle, both links are displayed. That's why you have the maintitles twice.

You are not understanding me; I DO NOT want it that way. I want main title with sub-titles underneath each main title in the proper sequence. Main titles AND sub-titles can have their own distinct posts. If I forget all this and use the added 'topic_order' field that I manually added to the table I can make everything display properly with post counts AND the correct topic_id's but it's a cheat and I can't do it that way. Because the 'topic_order' will change everytime a sub-topic is added or deleted which would require manually changing that field every time.
Anyway I think your suggestion will work I just have to figure out the display part but I need a 'distinct topic_id'; so far everything I've tried won't work with your code.


I understand what you want, but the way it is displayed now is because there is always a maintitle and a subtitle. That fact is causing it is not displayed you want it.
G
GrimlochSenior Member
Posted 5 years ago
Thanks for trying to help douwe. Your code actually works just not in the way I need. I guess I'll just stick with the added dbfield and will just have to modify it manually on sub-topic changes.
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You can download attachments in this forum.
You cannot up or down-vote on the post in this discussion thread.
You cannot set up a bounty in this discussion thread.
Moderator: Support Team
Users who participated in discussion: Grimloch, douwe_yntema, RobiNN