Jump to content

forum list


doddsey_65

Recommended Posts

here is my code which is supposed to return all forums from the database and then display them in their respective categories. however it only shows one of the forums and not all of them. Anyone know why?

 

function display_forum_list($crumbs)
{
    global $link, $template, $settings, $lang, $config, $user;
    
    $template->replace(
    array(
          '{CRUMBS}' => display_crumbs($crumbs)
    ), 'index_page');
    
    $query = $link->query("SELECT * FROM ".TBL_PREFIX."forums
                        ORDER BY f_lid ASC");
    $result = $query->fetchAll();            
    foreach($result as $key => $val)
    {
        if($result[$key]['f_pid'] == 0)
        {
            $cat_name = $result[$key]['f_name'];
        }
        else
        {
            $forum_name = $result[$key]['f_name'];
        }
        $forum_desc = $result[$key]['f_description'];
    }
    
    
    $template->replace(
        array(
          '{CATEGORY}' => $cat_name,
          '{F_ICON}' => '',
          '{F_NAME}' => $forum_name,
          '{F_DESC}' => $forum_desc,
          '{SITE_ROOT}' => $config['asf_root']
        ), 'forum_list');
}

Link to comment
Share on other sites

perhaps you need to append the data each time then using '.='

foreach($result as $key => $val)
    {
        if($result[$key]['f_pid'] == 0)
        {
            $cat_name .= $result[$key]['f_name'];
        }
        else
        {
            $forum_name .= $result[$key]['f_name'];
        }
        $forum_desc = $result[$key]['f_description'];
    }

 

Link to comment
Share on other sites

for better clarification here is how i display a page:

 

$template->replace(
        array(
          '{CATEGORY}' => '',
          '{F_ICON}' => '',
          '{F_NAME}' => $forum_name,
          '{F_DESC}' => $forum_desc,
          '{SITE_ROOT}' => $config['asf_root']
        ), 'forum_list');

 

and the function

public function replace($array, $file) 
    {
        $file = './html/'.$file.'.html';
        $file = file_get_contents($this->template.$file);
        foreach ($array as $key => $val) 
        $file = str_replace($key, $val, $file);
        echo $file;
        return;
    }

 

and the html file forum_list.html

<div class="category_header">
    {CATEGORY}
</div>
<ol class="forum_list">
    <li class="forum">
        <div class="forum_info">
            <span class="forum_icon">
                <img src="{SITE_ROOT}templates/default/icons/forum.png" />
            </span>
            <div class="forum_text">
                <h3 class="forum_name">
                    {F_NAME}
                </h3>
                <h4 class="forum_description">
                    {F_DESC}
                </h4>
            </div>
        </div>
    </li>
</ol>

Link to comment
Share on other sites

but appending all of the forum names to 1 variable would print:

 

forum1forum2forum3forum4

 

I need it to output something like

 

forum1(f_pid=0)

forum2

forum3

 

forum4(f_pid=0)

forum5

forum6it may help to state that i am using the modified preorder tree traversal system to store heirachal data using th eleft and right ids like phpbb.

Link to comment
Share on other sites

do you mean:

 

$cat_name = array();
    $forum_name = array();
    
    foreach($result as $key => $val)
    {
        if($result[$key]['f_pid'] == 0)
        {
            $cat_name[] .= $result[$key]['f_name'];
        }
        else
        {
            $forum_name[] .= $result[$key]['f_name'];
        }
        $forum_desc = $result[$key]['f_description'];
    }

 

 

Link to comment
Share on other sites

$cat_name = array();
    $forum_name = array();
    
    foreach($result as $key => $val)
    {
        if($result[$key]['f_pid'] == 0)
        {
            $cat_name[i] .= $result[$key]['f_name'];
            i++;
        }
        else
        {
            $forum_name[k] .= $result[$key]['f_name'];
            k++;
        }
        $forum_desc = $result[$key]['f_description'];
    }

 

Then to display you grab the lengths of each array, and use a for loop to cycle through all of them.

 

Denno

Link to comment
Share on other sites

i tried this but it didnt work:

 

    $cat_name = array();
    $forum_name = array();
    
    foreach($result as $key => $val)
    {
        if($result[$key]['f_pid'] == 0)
        {
            $cat_name[$c] .= $result[$key]['f_name'];
            $c++;
            for($c=0; $c<count($cat_name); $c++)
            {
            echo $cat_name[$c];
            }
        }
        else
        {
            $forum_name[$f] .= $result[$key]['f_name'];
            $forum_desc = $result[$key]['f_description'];
            $f++;
            for($f=0; $f<count($forum_name); $f++)
            {
            echo $forum_name[$f];
            }
        }   
    }

Link to comment
Share on other sites

Don't display them right away. Wait until the for each loop has completed, and then run the display for loops..

$cat_name = array();
    $forum_name = array();
    foreach($result as $key => $val)
    {
        if($result[$key]['f_pid'] == 0)
        {
            $cat_name[$c] .= $result[$key]['f_name'];
            $c++;
        }
        else
        {
            $forum_name[$f] .= $result[$key]['f_name'];
            $forum_desc = $result[$key]['f_description'];
            $f++;
        }   
    }
for($c=0; $c<count($cat_name); $c++)
{
echo $cat_name[$c];
}
for($f=0; $f<count($forum_name); $f++)
{
echo $forum_name[$f];
}

 

This way you wait until the array is full with all names, and then you display them all.

 

Denno

Link to comment
Share on other sites

i appear to be getting closer but not all database results are displayed using:

 

    $cat_name = array();
    $forum_name = array();
    foreach($result as $key => $val)
    {
        if($result[$key]['f_pid'] == 0)
        {
            $cat_name[$c] .= $result[$key]['f_name'];
            $c++;
        }
        else
        {
            $forum_name[$f] .= $result[$key]['f_name'];
            $forum_desc = $result[$key]['f_description'];
            $f++;
        }   
    }
    for($c=0; $c<count($cat_name); $c++)
    {
        echo '<b>'.$cat_name[$c].'</b><br />';
        var_dump($cat_name);
    }
    for($f=0; $f<count($forum_name); $f++)
    {
        echo '<em>'.$forum_name[$f].'</em><br />';
    }

 

the var_dump($cat_name) shows:

 

array(2) {  [""]=>  string(7) "General"  [1]=>  string(7) "Testing"}

 

"General" is missing from the display, for some reason the key for it is "" instead of 0.

Link to comment
Share on other sites

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.