I'm getting weird results when trying to kind of make a "templating engine". Basically, I want to be able to use PHP variables that contain data from an SQL database.
What happens is that everything works properly with the PHP side, what does not is the page that needs to display this information (index.php).
I'm working on a way to get the website's name from the sql database, so I have something like that on my index:
<?php
include ('php/data/sitename.php');
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $sitename; ?> - Home</title>
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div class="container">
<!-- HEADER: Navbar -->
<?php $navbar; ?>
<!-- MAIN: Index Page contents -->
<?php $page_index ?>
<!-- FOOTER: Footer -->
<?php $footer; ?>
<?php $sitename; ?>
</div>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js"></script>
</body>
</html>
This variable comes from a file (that has been included) called sitename.php, with the following code:
<?php
include ('../db.php');
$sql = "SELECT id, sitename FROM GeneralData";
$getname = mysqli_query($conn, $sql);
if ($getname->num_rows > 0) {
while($row = $getname->fetch_assoc()) {
$sitename = $row['sitename'];
echo $sitename;
}
}
?>
Yes, I used echo $sitename;, I know it wont echo the actual data, but I did it to test some things, and here are the results:
Including the file sitename.php to index.php will do nothing, it would be like if it did not exist. However, if I write "echo "123";" on it, it will echo 123 on index. What does not work is what I need.
If I go to sitename.php directly, it will simply output the correct SQL value I requested because I told it to echo (as I stated before). But, it wont work in index, it will simply not work.
Also, I'll leave my project structure here. It might help.
What can I do?
Thanks in advance!
try set GLOBAL for sitename
GLOBAL $sitename;
or
GLOBALS['sitename'];
$sitename = ...
EDIT
try use
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/yourfile.php";
include_once($path);
Related
Ok, let me explain:
I have a some files, something basic like this:
index.php
<html>
<head>
<title>Simple page</title>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php
<div class="thisWillBeBlue">Still not blue</div>
style.css
.thisWillBeBlue {background: blue}
Now the question: Using php I want to insert the style.css inside the head tag, calling it from the file home.php. Well, I came out with a solution, but it was not very effective:
index.php
<?php $css = array();
$css[] = 'linktothecss.css'
?>
<html>
<head>
<title>Simple page</title>
<?php
foreach($css as $item){
echo "<link rel='stylesheet' href='".$item."' />";
}
?>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
But the problem it is, If I call the css from home.php it will be added to the array later, therefore it will not be echoed inside the head tag. Any ideas?
You could do it using ob_start() and ob_end_flush() functions
e.g.
index.php
<?php
$csspage = "default.css";
function loadCSS($buffer) {
global $csspage;
return (str_replace('{{ css }}', $csspage, $buffer));
}
ob_start("loadCSS"); ?>
<html>
<head>
<!-- the string {{ css }} is just a placeholder that will be replaced
with the new value of $csspage defined later in the code, otherwise
it will replaced with its initial value (default.css)
-->
<link href="{{ css }}" />
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
<?php ob_end_flush(); ?>
home.php
<?php $csspage = "custom_style.css"; ?>
<div class="thisWillBeBlue">blue</div>
Further reference: http://it1.php.net/ob_start
I think you are looking for something like this ..(include a piece of code in their header files, so that it will allow you to add more stylesheets )
This will allow you to add more stylesheets to it on each page.
(add this to <head>)
<?php
if (!empty($styles) && is_array($styles)) {
foreach ($styles AS $style) {
echo '<link rel="stylesheet" href="/assets/css/'. $style .'">';
}
}
?>
You can put a variable at the top of an individual script if you need a specific stylesheet:
<?php
$styles = array('custom_style.css');
?>
CSS file references can be placed in the body of your code, if needed.
<body>
<link href="linktothecss.css" rel="stylesheet" type="text/css" />
<div class="thisWillBeBlue">
I'll be blue as soon as linktothecss.css finishes loading!
</div>
</body>
The only difference is that when in the HEAD, they are guaranteed to be loaded before the page is rendered. When they are in the BODY, there may be a split-second where they are still loading and the styles haven't been applied yet.
If you definitely want them in the HEAD, you could define the css requirements in a separate folder with the same file name, like so:
index.php:
<html>
<head>
<?php
include('css-requirements/home.php');
?>
</head>
<body>
<?php include('home.php'); ?>
</body>
</html>
and
css-requirements/home.php:
<link href="mycss.css" rel="stylesheet" type="text/css" />
<link href="myothercss.css" rel="stylesheet" type="text/css" />
I have a php include statement for the "head" of my website.
I am using the following code to call head.php...
<?php
include '../components/head.php'
?>
And in my head.php I have the following code...
<head>
<link rel="stylesheet" type="css" href="/style.css">
<title>Dummy Code</title>
</head>
How can I make it change the title by having a variable in my page on my page like Dummy Code | About being the title if I have $title = "About" on my webpage.
Is there anyway to do this?
They all belong to the global namespace, so you just can do this:
<?php
$title = 'about';
include '../components/head.php'
?>
head.php:
<head>
<link rel="stylesheet" type="css" href="/style.css">
<title>Dummy Code | <?=$title; ?></title>
</head>
But make sure that you understand: this is very simplified code and should not be used on the production projects.
I have created a function inside of my jquery mobile code. its working fine locally. But in the server where I have hosted its not working properly.
please help
Here is a sample code
<!DOCTYPE html>
<html>
<head>
<title>Site title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="images/scbd.ico">
<link rel="stylesheet" href="css/themes/default/jquery.mobile-1.1.0.css" />
<link rel="stylesheet" href="docs/_assets/css/jqm-docs.css"/>
<script src="js/jquery.mobile-1.1.0.js"></script>
<script src="js/jquery.js"></script>
<script src="docs/_assets/js/jqm-docs.js"></script>
</head>
<body>
<div data-role="page" class="type-index">
<div data-role="header" data-theme="f">
<h1 id="jqm-logo"><img src="images/logo.png"></h1>
Home
</div><!-- /header -->
<div id="header_rightpart">
<a name="header"><h2>sfdshsjdhfjdsh</h2></a>
</div>
<?php
here I have written the function
?>
But there is nothing wrong with the function its working fine in the local machine. But cant understand why it is not working in the server. I am getting error "error loading page". Is it just because of using multipel php tag?? I mean I have used <?php ?> twice in the page. Please help me to find out the solution
I have got the solution. I declared a variable like $names=[]; it did not work on my server. That was reason behind the error. I think this was because of php version. Sever php version was 5.2.17 and my local is 5.4.7 so it worked fine on local machine. But something was wrong with server php version.
function f_do ($rootname)
{
$query = "SELECT course_name FROM onlinecourses WHERE root_name = '$rootname'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
//$names=[]; this caused error
//$row=[]; this caused error
if($num > 0)
{
while($row =mysql_fetch_array($result))
{
$names[] = $row['course_name'];
}
}
return $names;
}
commented part shows the variable.
If anyone can think of a better title, please let me know.
Right now I'm using a technique from this tutorial to get the width and height of the user's viewing window. It works, but only on the index.php page. I'm using a php based css file.
In the php based css file, everything would normally work fine, except that the first line at the top *$width_div_center = $GET['width']0.8; thinks the width is in string form. (Or something like that.) As a result, the $width_div_center variable is set to zero which causes a lot of issues. What am I doing wrong, or how can I get the php based css file to do a multiplication on *$GET['width']0.8 properly? Thank you for your assistance.
<html>
<head>
<title>Taylor Love</title>
<!--
<link rel="stylesheet" media="only screen and (color)" href="main.css" />
<link rel="stylesheet" href="main.css"/>
-->
<link rel="stylesheet" type="text/css" href="css/style.php" />
<?php
$content = "null";
include_once('content.php');
?>
</head>
<body class="body">
<!-- top header -->
<div class="div-center decorated-white">
<div class="header-background">
<div style="margin:10px;">
<font color="#AAA" >
hello, universe!
<?php
echo $_GET['width'] *.8;
echo "<h1>Screen Resolution:</h1>";
echo "Width : ".$_GET['width']."<br>";
echo "Height : ".$_GET['height']."<br>";
?>
</font>
</div>
</div>
</div><!-- div-center-->
<div class="div-center" style="margin-top:10px;">
<?php
include('sidenav.php');
?>
<div id="div-content" class = "decorated-white">
<?php echo $content; ?>
</div><!-- div-content-->
</div><!-- div-center-->
<div class="clear"></div>
<!-- top header
<div class="div-center decorated-white" style="margin-top:10px">
<?php
for ($i = 0; $i < 5; $i++){
echo "</br>";
}
?>
</div>-->
</body>
</html>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
I appear to be having issues separating two different pages of code.
You seem to be calling style.php without any $_GET parameters.
The $_GET parameters you use in index.php are not automatically passed to the style.php. script.
Try temporary hard-coding the 'width' parameter to see if it makes any difference
<link rel="stylesheet" type="text/css" href="css/style.php?width=100" />
This is because the request for the stylesheet is a separate GET request which has no idea about the referrer's get parameters.
this is probably because $_GET variables are usually strings. That is probably the cause of your problem. You could use the function floatval() to convert it to a float.
The first line should be $width_div_center = floatval($_GET['width'])*0.8;
The Venkman debugger says this:
$("#aboutbox") is null
[Break On This Error] $("#aboutbox").hide();
But I don't know what to do to fix it!
It worked perfectly in HTML: http://leventhan.webfactional.com/static/
But when I moved it to PHP, it just stopped working.
Here's the index.php:
<?php
require_once dirname(__FILE__)."/src/phpfreechat.class.php";
$params = array();
$params["title"] = "Quick chat";
$params["nick"] = "guest".rand(1,1000); // setup the intitial nickname
$params['firstisadmin'] = true;
//$params["isadmin"] = true; // makes everybody admin: do not use it on production servers ;)
$params["serverid"] = md5(__FILE__); // calculate a unique id for this chat
$params["debug"] = false;
$chat = new phpFreeChat( $params );
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>phpFreeChat- Sources Index</title>
<link rel="stylesheet" title="classic" type="text/css" href="style/styles.css" />
<link rel="stylesheet" title="classic" type="text/css" href="style/content.css" />
<script type="text/javascript"src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<SCRIPT type="text/javascript" src="oracle.js"></SCRIPT>
<script type="text/javascript">
// some google maps javascript code that works fine
</script>
</head>
<body>
<header>
<div id="aboutbox">
<br>
<p><strong>GeoChat is the perfect chat app to hatch your world domination plans. A place on the web where you can point to maps and chat at the same time.
A great way for teachers to teach and students to learn with an interactive map. </strong></p>
</div>
<div id ="nav">
<h1>GeoChat</h1><h2>Toggle backgroundChat HelpWhat's all this about?</h2>
</div>
</header>
<div id="map_canvas"></div>
<div class="content">
<?php $chat->printChat(); ?>
<?php if (isset($params["isadmin"]) && $params["isadmin"]) { ?>
<?php } ?>
</div>
<footer>
<p>❤ Made by <strong>Author</strong> with love.Go up ↑</p>
</footer>
</body></html>
Here's the live broken site: http://leventhan.webfactional.com/phpfreechat-1.3/
EDIT: Yeah, jQuery worked again when I commented out the chat app (phpFreeChat), this part:
<div class="content">
<?php $chat->printChat(); ?>
<?php if (isset($params["isadmin"]) && $params["isadmin"]) { ?>
<?php } ?>
</div>
And it turns out the problem is really because jQuery is conflicting with Prototype.js (phpFreeChat uses Prototype apparently.) The solution is to use:
jQuery.noConflict();
Edit: Oh, that is actually in your static version, so the static versions seems to be broken. Given this, it is impossible to tell what does not work in your PHP version.
I get an error
oracle.js:4 Uncaught ReferenceError: hide is not defined
The line where the error is thrown is
$("#aboutbox").live(hide());
Maybe you meant $("#aboutbox").hide(); ?
It worked!
It was jQuery and Prototype not getting along with each other.
Here's what I did:
add:
jQuery.noConflict();
change all occurences of:
$(document).ready(function(){
to:
jQuery(document).ready(function($) {
in my external js do a find and replace.
find: $
replace: jQuery (case sensitive).
I am seeing an error here (Chrome)
function showInDiv(text)
{
var sidediv = document.getElementById('content_window');
sidediv.innerHTML = text; // Cannot set property 'innerHTML' of null
}
What's content_window? I could not find it on the page