Jump to content

Running code inside of quotes?


hukadeeze

Recommended Posts

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
Share on other sites

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
Share on other sites

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