doddsey_65 Posted January 31, 2011 Share Posted January 31, 2011 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'); } Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/ Share on other sites More sharing options...
denno020 Posted January 31, 2011 Share Posted January 31, 2011 Does it only display the last one? Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1167718 Share on other sites More sharing options...
doddsey_65 Posted January 31, 2011 Author Share Posted January 31, 2011 yes Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1167719 Share on other sites More sharing options...
denno020 Posted January 31, 2011 Share Posted January 31, 2011 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1167720 Share on other sites More sharing options...
doddsey_65 Posted January 31, 2011 Author Share Posted January 31, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1167731 Share on other sites More sharing options...
BlueSkyIS Posted January 31, 2011 Share Posted January 31, 2011 denno020 has a point. the code loops over all posts, overwriting the previous values. so after the loop you have only the last values. Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1167891 Share on other sites More sharing options...
doddsey_65 Posted February 1, 2011 Author Share Posted February 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1168118 Share on other sites More sharing options...
denno020 Posted February 1, 2011 Share Posted February 1, 2011 Put the names into an array with index i that you increment each time. That will solve the problem. Denno Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1168138 Share on other sites More sharing options...
doddsey_65 Posted February 1, 2011 Author Share Posted February 1, 2011 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1168142 Share on other sites More sharing options...
denno020 Posted February 1, 2011 Share Posted February 1, 2011 $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 Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1168145 Share on other sites More sharing options...
doddsey_65 Posted February 1, 2011 Author Share Posted February 1, 2011 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]; } } } Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1168146 Share on other sites More sharing options...
denno020 Posted February 1, 2011 Share Posted February 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1168151 Share on other sites More sharing options...
doddsey_65 Posted February 1, 2011 Author Share Posted February 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1168155 Share on other sites More sharing options...
denno020 Posted February 1, 2011 Share Posted February 1, 2011 try initialising $c and $f to 0 before the for each loop? I'm not sure if that would help, but it may.. Denno Edit: sorry didn't realize the forum was marked solved. I came in through an email link and didn't see until after I posted it.. Quote Link to comment https://forums.phpfreaks.com/topic/226201-forum-list/#findComment-1168187 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.