tsz Posted May 1, 2015 Share Posted May 1, 2015 soo I am trying to create a navigation bar to my website. I am using a small template engine I have build(with preg replace functions) but i have this problem. I am trying to output a number of mysql rows from a return function inside an template assign class but the thing is I am getting only the first row outputed when I use return. meanwhile if I echo the rows from the function I get all the rows but I don't get them inside the template but I get them at the top of the Html file above everything. here is the function with her assignment into template file: function navigation(){ global $result, $db; $page_query = $db->query("SELECT * FROM `pages` "); if ($db->num_rows($page_query) > 0) { while($result = $db->fetch_array($page_query)){ return '<li> <a href="index.php?page='. $result['id'] .'">'.$result['name'].'</a> </li>'; } } } $tp->assign('NAV_BAR', navigation()); and here is the template displaying and assignment class: public $filename; public $assigned_vars = array(); public $value = array(); public function assign($key, $value) { $this->assigned_vars[$key] = $value; } public function display($filename) { if(file_exists($filename)) { $output = file_get_contents($filename); foreach($this->assigned_vars as $key => $value) { $output = preg_replace('/{%'.$key.'%}/', $value, $output); $output = str_replace('{%' . $key . '%}', $value, $output); } echo $output; } else{ echo "Missing template error"; } } any ideas what to do??? Quote Link to comment https://forums.phpfreaks.com/topic/295994-helptrying-to-output-a-number-of-mysql-rows-in-html-template-engine/ Share on other sites More sharing options...
Ch0cu3r Posted May 1, 2015 Share Posted May 1, 2015 (edited) You dont want to use return in the while loop. What you should do loop over the results from the query, build a string containing the html for the links and then return the generated html after the loop. $results = ''; while($result = $db->fetch_array($page_query)) { // build the links $results .= '<li> <a href="index.php?page='. $result['id'] .'">'.$result['name'].'</a> </li>'; } // return page links return $results; Edited May 1, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/295994-helptrying-to-output-a-number-of-mysql-rows-in-html-template-engine/#findComment-1510496 Share on other sites More sharing options...
tsz Posted May 1, 2015 Author Share Posted May 1, 2015 (edited) You dont want to use return in the while loop. What you should do loop over the results from the query, build a string containing the html for the links and then return the generated html after the loop. $results = ''; while($result = $db->fetch_array($page_query)) { // build the links $results .= '<li> <a href="index.php?page='. $result['id'] .'">'.$result['name'].'</a> </li>'; } // return page links return $results; now after i call the function I get only the last mysql row. function navigation(){ global $result, $db; $page_query = $db->query("SELECT * FROM `pages` "); if ($db->num_rows($page_query) > 0) { $output = ''; while($result = $db->fetch_array($page_query)){ $output = '<li> <a href="index.php?page='. $result['id'] .'">'.$result['name'].'</a> </li>'; } return $output; } } Edited May 1, 2015 by tsz Quote Link to comment https://forums.phpfreaks.com/topic/295994-helptrying-to-output-a-number-of-mysql-rows-in-html-template-engine/#findComment-1510497 Share on other sites More sharing options...
Barand Posted May 1, 2015 Share Posted May 1, 2015 Have you forgotten the ".=" and just used "$results =" Quote Link to comment https://forums.phpfreaks.com/topic/295994-helptrying-to-output-a-number-of-mysql-rows-in-html-template-engine/#findComment-1510498 Share on other sites More sharing options...
tsz Posted May 1, 2015 Author Share Posted May 1, 2015 (edited) Have you forgotten the ".=" and just used "$results =" whoops forgot. didn't notice. ty now it works!! Edited May 1, 2015 by tsz Quote Link to comment https://forums.phpfreaks.com/topic/295994-helptrying-to-output-a-number-of-mysql-rows-in-html-template-engine/#findComment-1510499 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.