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

 

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

 

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 by tsz
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.