this is the first time that I need to work with smarty
and it seems quite straight forward.
however, there is twist in my PHP code and that is causing an issue.
here is what I am trying to do:
Before you start beating me for not using mysqli functions etc, please note that this code is just a simple test for me to understand the smarty first. so i won't be using mysql in my project and I do not recommend anyone to do so...
any way, here is what I am trying to do:
I am using the following code in my index.php
page:
<?php
header("Content-type: text/html; charset=utf-8");
function isSubdomain()
{
$host = $_SERVER['HTTP_HOST'];
$host = explode('.',$host);
$host = (is_array($host)?$host[0]:$host);
return $host;
}
?>
<?php
// These are the smarty files
require 'libs/Smarty.class.php';
// This is a file which abstracts the DB connecting functionality (Check out PEAR)
include "config/connect_to_mysql.php";
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->use_sub_dirs = false;
$smarty->caching = true;
// This SQL statement will get the 5 most recently added new items from the database
$storeShop = isSubdomain();
echo $storeShop;
$sql = 'SELECT * ';
$sql .= 'FROM $storeShop ';
$sql .= 'ORDER BY `id` ';
$result = mysql_query($sql) or die("Query failed : " . mysql_error());
// For each result that we got from the Database
while ($line = mysql_fetch_assoc($result))
{
$value[] = $line;
}
// Assign this array to smarty...
$smarty->assign('storeShop', $value);
// Assign this array to smarty...
$smarty->assign('$storeShop', $value);
// Display the news page through the news template
$smarty->display('index.tpl');
// Thanks to David C James for a code improvement :)
?>
and this is the index.tpl
file:
<!-- This is the DOC type declaration and links in the CSS stylesheet etc -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<meta name="author" content="Steve Rendell" />
<meta name="generator" content="EditPlus" />
<link rel="stylesheet" type="text/css" href="style.css" title="Default CSS Sheet" />
<title>News Page</title>
</head>
<body id="top">
<!-- OK display the page header to keep it nice-->
<div id="header">
<span>Steve's News Page</span>
</div>
<!-- This is where the news article will be going -->
<div id="bodyText">
<!-- Have a title -->
<h1 id="welcome">Read All About It</h1>
<!-- OK this is a section which will loop round for every item in $news (passed in through from PHP) -->
{section name=storeShop loop=$storeShop}
<!-- For every item, display the Title -->
<h2 id="{$storeShop[$storeShop].id}">{$storeShop[storeShop].product_name}</h2>
<!-- Write out the Author information and the date -->
<h3>{$storeShop[storeShop].price}, {$storeShop[storeShop].details}</h3>
<!-- Now show the news article -->
{$storeShop[storeShop].details}
{/section}
</div>
<!-- Show copyright information etc -->
<div id="footer">All Contents Copy Written :)</div>
<!-- Close the html tags -->
</body>
</html>
when I run the index.php
in my browser, I get the following error:
Query failed : Table 'mrshoppc_mainly.$storeShop' doesn't exist
But when I use the following code, I get the right output which is the name of the subdomain
and the name of the table in mysql database
as well:
$storeShop = isSubdomain();
echo $storeShop;
and I know the table exist. P.S. the table name $storeShop
is dynamic, so it could be any name that user chooses and it will be created in the mysql database.
I hope I explained it good enough for someone to be able to help me.
Could someone please tell me why I get the mentioned error and how to solve it?
I suspect this is caused by smarty as I never used to get this error before I started using smarty.
Thanks in advance.
You are not parsing your string that contains the PHP variable.
$sql .= 'FROM $storeShop ';
To PHP single-quoted stings ' '
are literally what you have between the quotes.
" "
Double quoted string will be Interpreted by PHP.
Try this:
$sql .= "FROM $storeShop "; // OR
$sql .= 'From '. $storeShop .' ';