MyBB Community Forums

Full Version: While Inside While Duplicating?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Secondary issue in regards to this post that was solved for that issue. I've got a new one.

I attempted to initiate a while inside a while and its not doing what I had hoped. Its actually duplicating one of the entries.

Code:
        $icgroups_sql = "
            SELECT p.pid, p.fid, p.uid, p.tid, p.subject
            FROM ".TABLE_PREFIX."posts p
            WHERE p.fid = 16
            AND p.uid = $uidc
        ";
        $icgroups_result = $db->query($icgroups_sql);
        while ($icgrecords = $db->fetch_array($icgroups_result)) {
            $icgroupid = $icgrecords['tid'];

            $icgroupsid_sql = "
                SELECT t.tid, t.subject
                FROM ".TABLE_PREFIX."threads t
                WHERE t.tid = $icgroupid
            ";
            $icgroupsid_result = $db->query($icgroupsid_sql);
            while ($icgentry = $db->fetch_array($icgroupsid_result)) {
                eval('$icgentries .= "' . $templates->get('member_profile_icgrouplist_entry') . '";');
            }
            eval('$icgrouplist .= "' . $templates->get('member_profile_icgrouplist') . '";');
        }

Template member_profile_icgrouplist:
<div>{$icgentries}</div>

Template member_profile_icgrouplist_entry:
<span><a href="{$mybb->settings['bburl']}/showthread.php?tid={$icgrecords['tid']}">{$icgentry['subject']}</a> ({$icgrecords['subject']})</span>

Output:
Herds of Essentia (Rogue)
Herds of Essentia (Rogue) The Cursed Vikings of Valhalla (Clan Member)

Screenshot below. Upon inspecting elements to search for the div in member_profile_icgrouplist I can see that this template is being duplicated but only one of the duplicates is showing both threads/post information being pulled.

[Image: attachment.php?aid=46271]

[Image: attachment.php?aid=46272]

I wrote this way too soon. I fixed! Removed the period from eval('$icgentries .=
I hope this isn't unwanted/redundant advice, but I'll take the risk and offer it anyway:

Yep, that fix works, because there'll only ever be one result returned by $icgroupsid_sql: only one thread will ever match the single tid of its WHERE clause. That means that the second while loop is redundant. It also means that you can accomplish your task with a single query using a join.

Here's my suggested (but untested other than a lint pass) rewrite of your code, also including an initialisation of the $icgrouplist variable, because uninitialised variables generate warnings in PHP 8, so we need to avoid them, also differentiating the different subject fields using AS, and, finally, removing some unused columns from the SELECT:

        $icgrouplist = '';
        $icgroups_sql = "
            SELECT p.tid, p.subject AS post_subject, t.subject AS thread_subject
            FROM ".TABLE_PREFIX."posts p
            LEFT OUTER JOIN ".TABLE_PREFIX."threads t
            ON t.tid = p.tid
            WHERE p.fid = 16
            AND p.uid = $uidc
        ";
        $icgroups_result = $db->query($icgroups_sql);
        while ($icgrecords = $db->fetch_array($icgroups_result)) {
            eval('$icgrouplist .= "' . $templates->get('member_profile_icgrouplist_entry') . '";');
        }

You then need only one template, say, member_profile_icgrouplist_entry, which could look something like this (note that it uses both thread_subject and post_subject now):

<div><span><a href="{$mybb->settings['bburl']}/showthread.php?tid={$icgrecords['tid']}">{$icgrecords['thread_subject']}</a> ({$icgrecords['post_subject']})</span></div>
Oh my gosh thank you!!!

I actually really appreciate this. I did the secondary while loop because every time I used ['subject'] when I initially had this as very similar to what you did (except I did LEFT JOIN not LEFT OUTER JOIN) it was either one or the other. I couldn't figure out how to call the thread vs the post subject.

(2023-08-23, 12:33 AM)Laird Wrote: [ -> ]an initialisation of the $icgrouplist variable, because uninitialised variables generate warnings in PHP 8, so we need to avoid them

        $icgrouplist = '';

^ I will keep this in mind in the future. I've read this before, I've seen it, and yet I always forget to do it lol.

Again, I really appreciate this!
No worries - I'm glad you appreciate it!