MyBB Community Forums

Full Version: Query Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm trying to run the following command to select all rows which are in a table, but it's only selecting/displaying one row, and not all of them.

	// get results
	$query = $db->simple_select("bb", "*");
	while($results = $db->fetch_array($query))
	{
	
		$return_feedback = $results['feedback'];
		$return_username = $results['username'];

	}
	// template to display results
	eval("\$bb_logs = \"".$templates->get("bb_logs")."\";"); 
	output_page($bb_logs);
You always rewrite both your variables in that loop, normal that you will end up with only the last row information... Huh
(2010-09-02, 05:55 PM)exdiogene Wrote: [ -> ]You always rewrite both your variables in that loop, normal that you will end up with only the last row information... Huh

What? Explain.
$return_feedback and $return_username are just going to get overwritten during each loop itteration, you'll need to eval the template to display it in the loop.

while($results = $db->fetch_array($query))
{
    
    $return_feedback = $results['feedback'];
    $return_username = $results['username'];
    // template to display results
    eval("\$bb_logs .= \"".$templates->get("bb_logs")."\";");
}
In your while loop :

    while($results = $db->fetch_array($query))
    {
    
        $return_feedback = $results['feedback'];
        $return_username = $results['username'];

    }

You will get in each step new values in your $return_feedback and $return_username variables. But you will write over your previous values in each other steps afterward.

If you want to keep all the values you would need to use an array or concatenate your results in a comma delimited string or in some sort of formated HTML string.

If you do not understand what i wrote, then you should ask a coder to do the work for you... Wink


(2010-09-02, 06:07 PM)MattRogowski Wrote: [ -> ]$return_feedback and $return_username are just going to get overwritten during each loop itteration, you'll need to eval the template to display it in the loop.

while($results = $db->fetch_array($query))
{
    
    $return_feedback = $results['feedback'];
    $return_username = $results['username'];
    // template to display results
    eval("\$bb_logs .= \"".$templates->get("bb_logs")."\";");
}

When I do that, it will display each row separately in a "different page".

For example, it'll display one result, then I scroll down, and there's a new page, displaying the second result.

I can send you a test account via PM if you'd like.
It'd be better if you have another template with just a table row, concat that as you loop through, and then when that's all been generated, put that into the final page.

while($results = $db->fetch_array($query))
{
    // a template that's <tr><td>{$results['feedback']}</td><td>{$results['username']}</td></tr> or something
    eval("\$bb_logs_row .= \"".$templates->get("bb_logs_row")."\";");
}
// has <table>{$bb_logs_row}</table> or something similar
eval("\$bb_logs_page .= \"".$templates->get("bb_logs_page")."\";");

That should work fine.
(2010-09-02, 06:23 PM)MattRogowski Wrote: [ -> ]It'd be better if you have another template with just a table row, concat that as you loop through, and then when that's all been generated, put that into the final page.

while($results = $db->fetch_array($query))
{
    // a template that's <tr><td>{$results['feedback']}</td><td>{$results['username']}</td></tr> or something
    eval("\$bb_logs_row .= \"".$templates->get("bb_logs_row")."\";");
}
// has <table>{$bb_logs_row}</table> or something similar
eval("\$bb_logs_page .= \"".$templates->get("bb_logs_page")."\";");

That should work fine.

Awesome. That worked perfectly.

Thanks! Big Grin

+Rep