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? '; } Link to comment https://forums.phpfreaks.com/topic/42597-running-code-inside-of-quotes/ 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 Link to comment https://forums.phpfreaks.com/topic/42597-running-code-inside-of-quotes/#findComment-206702 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. Link to comment https://forums.phpfreaks.com/topic/42597-running-code-inside-of-quotes/#findComment-206714 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; } Link to comment https://forums.phpfreaks.com/topic/42597-running-code-inside-of-quotes/#findComment-206822 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.