How to set directory in header in php - javascript

I have a small problem with importing links. Once I started using pretty URL directories stops working. What can I do ?
Screenshots:
Before
After

Make your css and js links dynamic.
For example:
<?php define( 'SCRIPT_ROOT', 'http://'.$_SERVER['HTTP_HOST'].'/' );?>
<link href="<?php echo SCRIPT_ROOT ?>site/static/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" media="all">
After this doesnt matter your directory structure.

Related

Create an external file for side menu

How do I create an external file for my side menu?
<a class="active" href="C.html" style="font-size: 1.1em;font-weight: 900;">C Programming</a>
C++
C#
Go
Java
JavaScript
PHP
Python
Ruby
Swift
Currently I have these kind of links in every file and the code is just getting too much. Is there a way to create an external file for the menu items?
I tried this but the problem is class="active". I want to highlight a menu item when my menu item is in index.php
I Know how to achieve this when my menu is in each individual HTML file but how do I do that when it's in external PHP file ?
Index.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="CSS/Tags.css">
<link rel="stylesheet" type="text/css" href="CSS/Nav.css">
<link rel="stylesheet" type="text/css" href="CSS/Card.css">
<link rel="stylesheet" type="text/css" href="CSS/Buttons.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="sidenav">
<h1>&nbspOthers</h1>
MS Excel
MS PowerPoint
MS Word
WordPress
</div>
</body>
</html>
Main file
<?php include('index.html'); ?>
You should NOT use javascript to include your menu .
you can use PHP to simply include your menu ( but i sugget header ) file to your page :
<?php include('/template/header.html'); ?>
or you can use pre processors like Gulp :
##include('./template/header.html')
var fileinclude = require('gulp-file-include'),
gulp = require('gulp');
gulp.task('fileinclude', function() {
gulp.src(['index.html'])
.pipe(fileinclude({
prefix: '##',
basepath: '#file'
}))
.pipe(gulp.dest('./'));
});
or Pug or Grunt and ......
About your problem in comments . lets say you are in your main folder and you have this 3 files :
index.php
header.html
homepage.html
footer.html
index.php should be like this :
<?php
include('header.html');
include('hompage.html');
include('footer.html');
?>
and then in your browsers you should open index.php .
** don't use index.html and index.php in the same time .
if it didn't work . you should get error in your index.php file .
when your code works . i suggest you to use readfile() instead of include . if your another files are just html and not have any php codes in it .
As pointed out by Abolfazl Ghaemi :
You should NOT use javascript to include your menu . you can use PHP to simply include your menu ( but i sugget header ) file to your page :
<?php include('/template/header.html'); ?>
The Gulp/Grunt solution is fine but I'll stick with the <?php include(); ?> solution for your problem.
Does the hosting machine speak PHP ?
If browsers can read HTML just fine to give you a webpage, they can't read PHP. Instead, PHP need to be executed by something on the hosting machine and you will need a PHP interpreter for that like Apache.
If you didn't already, I strongly recommend you to install it or even a full AMP stack.
PHP in HTML files won't work
You need to make sure your file using PHP instructions are .php files. Otherwise, the code in your <?php ?> tag (the PHP code) won't be interpreted at all.
Make sure your main file is a .php file.
Are you calling the correct file ?
Also, you need to be sure the file name you are including is correct. The content as shown in your question is
Main file
<?php include('index.html'); ?>
but your file is actually index.php not index.html.
HTML is prettier with CSS
If you want to "highlight" the item with active class, you will need CSS. You can quickly add CSS in your file with a <style> tag. See the example below.
<ul>
<li>Not highlighted</li>
<li>Still not highlighted</li>
<li class="active">Highlighted ! Spotlight's on me baby !</li>
<li>Not highlighted</li>
</ul>
<style>
.active {
color: red;
font-weight: bold;
}
</style>

Loading CSS/JS files when redirecting to next view

I want my users to be able to click a button on the front-end of my website, and be redirected to my page-login.php.
I'm pretty much doing it, but I'm missing some things, because it doesn't load my CSS or JS files. Even though if I set that page into my main page, it will load everything alright.
This is the error message:
GET http://localhost/projeto/index.php/favicon.ico 404 (Not Found).
This happens to CSS, JS files, images, etc.
First of all:
I think that I need to insert my controller and view on the routes page, am I right? If so, how?
The way I call my Controller is messing up the way the page appears. What's the right way?
This is my code:
Main page HTML:
href="index.php/login_controller"
login_controller:
public function index (){
$this->load->view('Main/page_login');
}
So, what am I missing? Is anything that I said true?
Find the config.php file in the /application/config folder. Edit this file and find:
$config['base_url'] =
You should add here correct path. In your current situation you have to add: http://localhost/projeto/
so this line must be:
$config['base_url'] = 'http://localhost/projeto/';
You should include the URL helper into your autoload configuration. /application/config/autoload.php add 'url' into $autoload['helper'] array:
$autoload['helper'] = array( 'url');
after that you can refer your CSS, img, JS and other files with base_url() function. For example:
<link rel="shortcut icon" type="image/png" href="<?php echo base_url();?>/favicon.png"/>
or
<link rel="stylesheet" href="<?php echo base_url('favicon.png');?>" type="text/css" />
redirect(base_url() . 'login_controller/index', 'refresh');

Set constant for reusing HTML

I am trying to set a WWW_ROOT in order to reuse HTML code in different pages. This is an example of users.php using a header, where the CSS files are not found - 404 (Not Found) and I got Notice: Undefined variable: project_end in /Applications/XAMPP/xamppfiles/htdocs/lpweb/assets/php/initialize.php on line 11
Folder structure
>localhost
>lpweb
>index.php
>pages
>users.php
>assets
>php
>initialize.php
>header.php
>css
>bootstrap.min.css
initialize.php
<?php
define("PHP_PATH", dirname(__FILE__));
define("ASSETS_PATH", dirname(PHP_PATH));
$public_end = strpos($_SERVER['SCRIPT_NAME'], '/lpweb') + 7;
$doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $project_end); //line 11
define("WWW_ROOT", $doc_root);
?>
header.php
<link rel="stylesheet" media="all" href="<?php echo WWW_ROOT . 'assets/css/bootstrap.min.css'; ?>"> />
users.php
<?php require_once('../assets/php/initialize.php'); ?>
<?php include(PHP_PATH . '/header.php'); ?>
You are mixing $public_end and $project_end
Also after reviewing your code from GitHub I believe this will work for you.
define("WWW_ROOT",$_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].rtrim($_SERVER['REQUEST_URI'],'/'));
Alternatively, you can use the relative path to refer to CSS files which will make your life easy
<link rel="stylesheet" media="all" href="assets/css/bootstrap.min.css" />

PHP: How do I can access .js and .csss in Web dir from my Module in yii2?

I've an application build in Yii2 framework, and I've develop the application in backend side, but know I want to copy them become a module.
This my application dir
backend
--modules
--pstk
--controllers
--ValidationController.php
--DefaultController.php
--InterviewController.php
--StudentController.php
--SiteController.php
--UsersController.php
--models
--Validation.php
--Interview.php
--Student.php
--Users.php
--views
--validation
--_form.php
--view.php
--index.php
--update.php
--create.php
--interview
--student
--users
--Module.php
--web
--css
--manual_general.css
--js
--upload_header.js
--style
For example I've successfully run view.php from the module in browser, but the module can't access the .css and .js from backend/web/.
So the view displayed, but it's view mashed-up and all button didn't do any action when clicked.
Anyone know why the module didn't access the manual_general.css and upload_header.js from backend/web/? and how I can solve this problem?
Any help, will be appreciated. Thanks :)
EDITED:
This is the code in mi view.php to connect to .js and .css
<link rel="stylesheet" type="text/css" href="../../../../web/css/manual_general.css">
<script type="text/javascript" src="../../../../web/js/upload_header.js"></script>
Try using yii\helpers\Url::to, like:
<link rel="stylesheet" type="text/css" href="<?= Url::to('#web/css/manual_general.css') ?>">
<script type="text/javascript" src="<?= Url::to('#web/js/upload_header.js') ?>"></script>
Alternatively, in view.php you may do (assuming $this is your View instance):
<?php $this->registerCssFile('#web/css/manual_general.css'); ?>
<?php $this->registerJsFile('#web/js/upload_header.js'); ?>

How to replace stylesheets and javascripts only for one page template in wordpress

I'm about an four hours browsing an internet in order to find an answer.
I have an javascript/php/html application with rich functionality.
It includes 3 css files and 5 javascript files.
I want to build it in wordpress page.
I have already made custom page template: checkout.php
Now i need to replace all the stylesheets and javascripts with my own.
I know how to check the template:
<?php if(is_page_template('checkout.php')) :?>
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/stylesheets/main.css"/>
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/stylesheets/jquery.Jcrop.css" />
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/stylesheets/styles.css" />
<script src="<?php echo get_template_directory_uri(); ?>/javascripts/js_checkout/jquery.min.js"></script>
<script src="<?php echo get_template_directory_uri(); ?>/javascripts/js_checkout/jquery-ui.js"></script>
<script src="<?php echo get_template_directory_uri(); ?>/javascripts/js_checkout/jquery.Jcrop.js"></script>
<script src="<?php echo get_template_directory_uri(); ?>/javascripts/js_checkout/script.js"></script>
I also guess that this functions will add all stylesheets and javascripts to my new template:
function theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
But do wp_enqueue_style and wp_enqueue_script replaces the default styles and scripts or just add another????
Can some one just tell me how to do this and what file to edit??
header.php or general-template.php??
Thanks in advance alot
Its really going to depend on the theme you are using.
Are you using a child theme?
Where are the existing stylesheets located?
How are those stylesheets and javascript files inserted / injected into the page?
In looking at your existing code, you are correct in assuming that all you are doing at this point, is adding to what already exists on the page.
Do you want to remove all other stylesheets?
What about the existing javascript?
From what you're adding -- it appears to me - you are looking to only add to... as jquery-ui requires jquery
ps don't forget your
<?php endif; ?>
For one time use --- as a programmer I opt for the "simpler the better" method.
The code above would best be added to functions.php within your child theme.
Unless you could benefit from adding additional .js and .css files to other pages... in that case, it would be better to construct a partial. But thats discussion for another time.
Let me know if you need further assistance with this
If you are editing the templates directly, then use your
if(is_page_template///////
to insert the stylesheets, and close it with
}else{ // place default stylesheets here}
If you want to remove ALL the .js files you could use wp_dequeue_script
http://codex.wordpress.org/Function_Reference/wp_dequeue_script
or you could wrap the .js and css inside the same if statement.
Although there are certainly better practices.....
ONE would be the stylesheet --
Why not utilize your if is_template statement to add a new body class and change only the necessary items.
EX: body.cart p{background: red;} would make paragraphs have a red background BUT ONLY on the cart page.

Categories