Jump to content

HELP|trying to output a number of mysql rows in HTML template engine


tsz

Recommended Posts

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???

 

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;

 

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;


}


}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.