How to include php variable in script src - php

I want to create a variable that holds url to access css and js files. Now I successfully created in config and including it in header for <link rel="stylesheet" href="<?php echo $path; ?>/css/style.css" /> and it just works fine.
But how do I include the same way in the script tag placed in the footer?
In my config.php
$path = "http://localhost/mysite/";
<script>
var path = "<?php echo $path;?>";
</script>
In header :
<link rel="stylesheet" href="<?php echo $path; ?>/css/style.css">
<script src="<?php echo $path; ?>/js/vendor/modernizr.js"></script>//this works, doesn't leave any error.
But in footer:
<script src="<?php echo $path; ?>/js/jquery.myplugin.js"></script>//same style I used in header. But doesn't work!
If pressed f12 in chrome, under sources js files that placed in the footer not loaded but the one placed in header yes!

Just need to remove this part form config
<script>
var path = "<?php echo $path;?>";
</script>
I define it twice

Define a global variable like define('SITE_PATH','http://localhost/mysite/'); in your config file. Use <?php echo SITE_PATH; ?> where ever required.

Related

Use function to echo SQL results from external php file

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);

Declare variable outside of javascript file and use it in file

so i am declaring a variable inside of a .php file
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js">
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
But i want to use the variable templatePath inside of jscript_pages.js but when i do it like this my console says: Uncaught ReferenceError...
I hope someone can help :)
Thanks
The content of a script element is alternative content to use if the browser doesn't support src, it isn't a script to run before running the external script.
Use two script elements.
<script>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js"></script>
Set the variable before you reference it in script.
<script>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js">
</script>
As #Quentin said, it's either/or in a single tag.
In concept, you can do what you're looking for by having two script tags
<script type='text/javascript'>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js"></script>
But, in practice what's usually better (for testing, portability, etc) is for your script file to just define functions and then have your on-page scripts (if you have them) call those functions and apply the variables.
In this setup, your function bloginfo('template_directory'); should return a $GLOBALS variable.

Adding a links to js and css files within a conditional script in page header

I want to add something like this to my page header, in order to load a js and css file only if NOT an iPhone/iPad:
if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
//Link to js file
//Link to css file
}
The first line works fine, I've tested it by pasting in a script, but I'm not sure how to get the files linked... especially as I'd like the js and css links to start with the WP template tag <?php echo get_stylesheet_directory_uri(); ?>
Thank you in advance for your help!
<?php
if (!preg_match("{iPhone|iPod}i",$_SERVER['HTTP_USER_AGENT']))
{
echo '<script src="yourscript.js"></script>';
echo '<link href="yourstyle.css" rel="stylesheet" type="text/css">';
}
?>
It's better to check the user agent using PHP, since it's easier to then add the right HTML tags to load the right css and js files.
<?php if ( !preg_match( '/iPhone/', $_SERVER['HTTP_USER_AGENT'] ) && !preg_match( '/iPod/', $_SERVER['HTTP_USER_AGENT'] ) ): ?>
<link href="<?php echo get_stylesheet_directory_uri() ?>/style.css">
<script src="<?php echo get_stylesheet_directory_uri() ?>/script.js" type="text/javascript"></script>
<?php endif; ?>
Doing it using just javascript would add unneeded complexity, and need you to use a js loader as RequireJS http://requirejs.org/
Something like that:
<?php if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) : ?>
<link href="<?php echo get_stylesheet_directory_uri() ?>/main.css" type="text/css" rel="stylesheet" >
<script src="<?php echo get_stylesheet_directory_uri() ?>/main.js" type="text/javascript"></script>
<?php endif ?>

jquery and ajax conflict - Joomla

ok Here is my issue: I am using Joomla 1.6,
I have a Ajax JS to display the slide how on my site located: www.dhwnj.com The Jquery script i am using when enabled stops my slide show from working so I tried using the No conflict script but I do not know if I am using it correctly :
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo $this‐>baseurl ?>/templates/<?php echo $this->template ?>/js/jquery.metadata.min.js"></script>
<script type="text/javascript" src="<?php echo $this‐>baseurl ?>/templates/<?php echo $this->template ?>/js/jquery.maphilight.js"></script>
<script type="text/javascript" src="<?php echo $this‐>baseurl ?>/templates/<?php echo $this->template?>/js/fancybox/jquery.fancybox.js"></script>
<script>
jQuery.noConflict();
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
jQuery("jquery.maphilight.js").hide();
});
// Use Prototype with $(...), etc.
$('_class.noobSlide.packed.js').hide();
</script>
<script type="text/javascript" src="<?php echo this->baseurl; ?>/templates/
<?php echo $this->template ?>/js/_class.noobSlide.packed.js" >
</script>
Any Ideas?
Prototype can't work together with Mootools and Mootools is loaded on your page (probably directly by Joomla)
Maybe here's the reason behind your problem...
You should use jQuery after mootools. You can ensure mootools is looaded first by this code (use it in the module or in the view of a component)
JHTML::_('behavior.mootools');
$doc = &JFactory::getDocument();
....
$doc->addScript( "https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" );
$doc->addScriptDeclaration('jQuery.noConflict();');
$doc->addScriptDeclaration("
jQuery(document).ready(function(){
alert('jo');
});"
);
This is code for joomla 1.5, don't know if the functions are the same in 1.6.

How to make my a cms that can change themes like tumblr

Hey guys. I was wondering how would I make theme options for a cms of mine like tumblr? I understand how to use the code like tumblr, {description}, {text} and variables like that but how would I make a theme switcher like this in php?
Simply put a php variable in your head section. Be sure to create the $userCSSchoice at the beginning of your page or you will break it all! Doom!
<LINK REL=StyleSheet HREF="<?php echo $userCSSchoice; ?>.css" TYPE="text/css" MEDIA=screen>
Nah, seriously, I do this and it works just fine for me. Then you have to create all those css stylesheets, but that's not too hard.
An alternative is to create your CSS page in PHP so you can pass a variable to a single sheet to determine the style.
<link rel=StyleSheet href="styles.php?theme=<?php echo $userCSSchoice; ?>" type="text/css" media=screen>
Then your PHP/CSS page can determine colors/images..
<?php
header("Content-Type: text/css");
if (isset($_REQUEST['theme']))
$theme = $_REQUEST['theme'];
else
$theme="default";
$bgImage="images/bg_".$theme.".png";
if ($theme=="default")
$mainColor="0f8200";
else if ($theme=="green")
$mainColor="009900";
?>
body {
background:url('<?php echo $bgImage; ?>') repeat-x #<?php echo $bg; ?>;
color: #<?php echo $mainColor; ?>;
}

Categories