hukadeeze Posted March 14, 2007 Share Posted March 14, 2007 I'm calling a function that returns a table in html. I want to populate this table with results from a query. I'm having a problem because the return statement is in quotes. function return_display($queryresults){ return ' How can I run a loop in here? '; } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 14, 2007 Share Posted March 14, 2007 You need to store the results of the loop in a variable and return the value of the variable. If you post more of your script, we should be able to give a better answer. Ken Quote Link to comment Share on other sites More sharing options...
hukadeeze Posted March 14, 2007 Author Share Posted March 14, 2007 public function content_productshome($directories, $booksquery, $cardsquery, $categoriesquery) { //Create array from categories query $num_categoriesresults = $categoriesquery->num_rows; $categories = array(); for ($i=0; $i <$num_categoriesresults; $i++) { $currentcatrow = $categoriesquery->fetch_assoc(); $categories[$currentcatrow['CategoryID']] = $currentcatrow['CategoryName']; } return ' <p> books: </p> <table width="80%" border="1" cellspacing="3" cellpadding="3"> <tr> <td>Title</td> <td>Category</td> <td>ISBN</td> <td>Price</td> </tr> $num_bookresults = $booksquery->num_rows; for ($i=0; $i<$num_bookresults; $i++) { $currentbookrow = $booksquery->fetch_assoc(); <tr> <td>echo $currentbookrow['Title'];</td> <td>echo $categories[$currentbookrow['CategoryID']];</td> <td>echo $currentbookrow['ISBN'];</td> <td>echo '$'.$currentbookrow['Price'];</td> </tr> } </table> <p> cards: </p> '; } The same process would take place beneath the cards paragraph as well. Quote Link to comment Share on other sites More sharing options...
btherl Posted March 14, 2007 Share Posted March 14, 2007 Try this. There may be a few typos as I am unable to test it, but you should be able to see the general idea. I am placing all the output into a variable $html, then returning that variable at the end. public function content_productshome($directories, $booksquery, $cardsquery, $categoriesquery) { //Create array from categories query $num_categoriesresults = $categoriesquery->num_rows; $categories = array(); for ($i=0; $i <$num_categoriesresults; $i++) { $currentcatrow = $categoriesquery->fetch_assoc(); $categories[] = $currentcatrow; } $html = ' <p> books: </p> <table width="80%" border="1" cellspacing="3" cellpadding="3"> <tr> <td>Title</td> <td>Category</td> <td>ISBN</td> <td>Price</td> </tr>'; $num_bookresults = $booksquery->num_rows; for ($i=0; $i<$num_bookresults; $i++) { $currentbookrow = $booksquery->fetch_assoc(); $html .= "<tr> <td>{$currentbookrow['Title']}</td> <td>{$categories[$currentbookrow['CategoryID']]}</td> <td>$currentbookrow['ISBN']}</td> <td>{$currentbookrow['Price']}</td> </tr>"; } $html .= '</table> <p> cards: </p> '; return $html; } Quote Link to comment 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.