Codeignighter linking mishap - php

Im fairly new to codeignighter and have been trying to make a little Header, content, footer Template.
im currently having a little issue pointing to other views. Heres what i got for code
My head.php
<html>
<head>
<title>TEST HEADER</title>
</head>
My content (main.php)
<body>
Hello World
LINK2
<br><br>
My footer.php
the footer
</body>
</html>
Now the whole deal im having is that i want to link to a inner page (page1.php), i have it setup as a php file in the views folder
page1.php
<?php $this->load->view('head'); ?>
back
<br><br>
<?php $this->load->view('footer'); ?>
I would like to be able to make subpages like this and just inject the header and footer into them. But everytime i try linking to this page1.php from the main, i get a message "The requested URL /CI_Test/views/page1.php was not found on this server." but the file is there?
im a bit confused, any help is appreciated!

Try using <?=site_url('page1.php')?> in main.php:
Hello World
But it's likely that your problem is due to routing and .htaccess mod_rewrite. For CodeIgniter to work, the .htaccess file tells your server to reroute all requests (with a few exceptions depending on setup) to index.php, and CodeIgniter then routes the request to a corresponding controller which renders the view.
Try reading the CI documentation, e.g. about static pages:
http://ellislab.com/codeigniter/user-guide/tutorial/static_pages.html

Related

Where to start when building a website from scratch using index.php

I have decided to build my site website from scratch, rather than using Wordpress, Magento or a bootstrap template.
I'm looking for a good guide to do so, Code Academy and w3schools are good for learning specific elements of HTML and CSS but I'm looking for a good guide for how to structure my site.
I am playing around with creating an index.php using e.g. to include all element of the page to make creating the individual pages of my site clean and more efficient that including the include for header and footer on each page.
One issue I am having is that I am struggling to understand how to include the different pages within the index.php. I have searched for this but I obviously am not finding the correct words to search for this as I'm struggling to find a decent answer. I think I need something along the lines of a wildcard so that I can say to call all html files within my pages folder so that all pages are using my index.php template.
Below is my index.php to help explain. Thanks in advance and apologies if this question is answered elsewhere on the site, I searched but did not find anyone else answering the same question!
<!DOCTYPE html>
<html>
<php include "head.html" ?>
<body>
<div class="container">
<php include "navigation.html" ?>
<div class="wrapper">
<php include pages/*.html ?>
</div>
</div>
</body>
<php include "footer.html" ?>
</html>
It looks to me like you are almost there. I am not sure of the syntax you are using works, but the code below is used in some of my sites to include a header:
<?php include('includes/header.php'); ?>
This means include the code in the file 'header.php' that is in the folder 'includes', which is on the same level as the file that is calling the code.
The result would be that the 2 files are merged in to one when the page is loaded, with the code from header.php being inserted in place of the line
<?php include('includes/header.php'); ?>
You need to pass variables in URL e.g index.php?page=home and then in your index.php file you shoud get that variable $page = $_GET['page']. Now in $page you have name of the file to include
<?php include($page.'.html') ?>
For now, I skipped security issues of that solution.

How to realize the wordpress permalink function on an homemade php website

I'm realizing a php website and I'm trying to split all the files in a way that should give me the possibility to update them easily.
In the current structure I've:
header.php
menu.php
footer.php
These files are included in the following pages:
page1.php
page2.php
page3.php
On each one of the pages above I write specific content. At the end my urls are:
http://www.example.com/page1.php
http://www.example.com/page2.php
http://www.example.com/page3.php
But what I would like to do is to reproduce the wordpress structure that uses index.php only as output.
So, just to go more in detail, I would like to realize an index.php inside of which I'll include just one time:
header.php
menu.php
footer.php
and including the page1.php content, the page2.php content and the page3.php content. At the end I need to have as URL a permalink that says:
http://www.example.com/page1
http://www.example.com/page2
http://www.example.com/page3
I've checked on the web but seems there's nothing online useful for my needs and when I check "permalinks" users talk only of wordpress.
Could you help me please on this activity?
Tell me if my request is not so clear or I'm wrong on something.
Thanks!
Thanks to the link suggested by #nevermind I solved the issue. Below the solution...
I created a .htaccess file with inside the following code:
RewriteEngine on
RewriteRule ^page/([^/.]+)/?$ index.php?page=$1 [L]
I created an index.php file with the following code:
<html>
<head>
<title>Test Page</title>
</head>
<body>
<?php include $_GET['page'].'.php'; ?>
</body>
</html>
I uploaded the two files in the same folder (/test/) together with the file "introduction.php".
Considering I wanted to open the page "introduction.php" I pasted the following URL in the browser:
http://www.example.com/page/test/introduction
That's all! Thanks guys for your support. I hope this can help other users looking for the same solution.
page#.php will have to be like this:
<?php
include_once('header.php');
include_once('menu.php');
//body content of page#
include_once('footer.php');
?>
you will still have 3 files with the same url but the header ,footer and menu will be the same across the pages

Codeigniter URL index.php twice

Hi evrybody ‘im new to codeigniter and to MVC model as well,
I need help to get my project working couse i’ve got this issues:
1) i’m in the main page and i clik on the link of the “about” page, the css file does not seems to load.
and even if i include the css in the head section the images are still missing in the page.
2) in the configure file i’ve set the
$config[‘base_url’] = ‘’;
$config[‘index_page’] = ‘index.php’;
now if i’m in the about page and i clik again on the about link the link it’s missing becouse i set the link in the menu:
About
and so the index.php it’s loaded twice: localhost/mywebsite/index.php/index.php/about.
i could set the condition to cut the link if i’m in the page i need but i would like to know if there is a more polite solution
and hope that the solution it’s not to put my hands in to the mod_rewrite .
I’ll like codeigniter because seems to be easy to configure and so really portable.
and even if it’s simple i can’t figure out how to solve this issues
Thank you everybody for your time!
Even i a newbie to CodeIgniter. I was facing the css problem. I added the css files in separate folder located at the root directory. I solved the problem by adding $this->load->helper('url'); in the controller function of the page.
You need to set $config['base_url'] in config.php to http://www.yourdomain.com and provide full path when linking css to the page like this :
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>resources/css/file.css">
just add
> <?php echo base_url();?>
before or or URL link
> (example: <script src="<?php echo
> base_url();?>assets/js/jquery-1.7.1.min.js"></script>)
to load the link to all page and separate the header and footer in separate file which could help you a lot minimize the page content.

Understanding CI routing and $page -> can't find .css or .js

The hardest part I'm having with learning Codeigniter is the URI and URL 'theory' if you will.
I followed the original tutorial concerning static pages, and now the code seems to mess EVERY single link up except the main nav bar, requiring me to constantly add routing parameters. I figure I must be doing something wrong.
In my controller, I currently have this code, based off the tutorial:
public function view($page = 'home') {}
My folder structure is:
+ applications
+ views
- welcome.php
+ main
+ css
+ js
- home.php
- about.php
- etc.php
I should point out that the welcome.php page is for the login page. On that page, a link will direct you to home.php (main/home/)
My routing code looks like this:
$route['default_controller'] = 'welcome';
$route['main/(:any)'] = 'main/view/$1';
$route['main/home/home'] = 'main/view/$1';
$route['404_override'] = '';
As you can see, I already had to put a bandaid on it with the <code>$route['main/home/home'] = 'main/view/$1';</code> portion, due to the fact that clicking on "home" while already on the home page would result in linking to main/home/home/ displaying my nav bar, and creating a brand new set of missing links labeled, main/home/about/
In short, I am now trying to reference a .js file and a .css file, but even though the links correctly point to /main/css/style.css it does not recognize it.
Here is my view code for the header (where i load my .css and .js)
<html>
<head>
<title><?php echo $title ?> - TownBuilder - Prototype</title>
<link href="css/structure.css" rel="stylesheet" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<header>
<ul>
<li> <?php echo $username ?></li>
<li>Account</li>
</ul>
</header>
Any advice on how to set up routing so that it works correctly?
IMHO they need to remove the route's piece from the codeigniter tutorial because it's not necessary, and it screws everyone up.
Here's the gist of how things work. First comment out all of your routes.
Controller:
Class FirstController extends CI_Controller {
public function home() {
// do stuff
$this->load->view('home');
}
}
Make sure you have a view called home.php in your view folder.
Then the url should be <base_url>/controller/method, so in this case it would be <base_url>/FirstController/home
I think you should add the css/js file on the root directory(outsite of the application folder crete a folder called css) as CI has .htaccess files that deny all access to the application folder. You can only include those files from within your scripts.

Do search engines have trouble with urls like somesite.com/index.php?page=photos

I'm new to php coding, web development, and search optimization - so newbie overall. In the process of learning php and web development I've been trying out different website architectures and layouts. One I'm working on uses an approach like the following:
I have one index.php page which always loads a header.php, sidebar.php, and footer.php. The index.php also contains a switch so that depending on the page variable the index.php is passed it loads different core content. So for example a examplesite.com/index.php?page=photos and examplesite.com/index.php?page=stories would both have the same header, footer, and side bar but one would have photos and one would have stories as the main content.
<?php $page = $_GET['page'];?>
<?php include("header.php"); ?>
<?php include("nav.php"); ?>
<?php
switch ($page)
{
case 'play':
include("photos.php");
break;
case 'cards':
include("stories.php");
break;
default:
include("frontpage.php");
}
?>
<?php include("footer.php"); ?>
My navigation is made up of href="index.php?page=..." links so choosing a menu button on the index page essentially calls itself passing it the new core to load.
I have no idea if this is a totally unorthodox approach but it all started because I was initially going to create a wordpress theme but then halfway through decided not to do it in wordpress.
What i'm concerned about are what drawbacks could be associated with this approach when it comes to search engines, indexing, seo etc.
What are other drawbacks or issues I should be thinking about that maybe I'm not?
Thanks in Advance!
I have no idea if this is a totally unorthodox approach
There is nothing essentially "unorthodox" in using a query string to load various pages. Billions of sites using this approach. Search engines can index such pages all right.
Nevertheless,
I have one index.php page which always loads a header.php, sidebar.php, and footer.php.
This is wrong concept.
Having an index.php file only to load header and footer makes no sense and makes your site plainly unusable.
Here are main faults in your design:
You're assuming that header.php would be called with the every page call. That's wrong.
You're assuming that header.php will always be static. That's wrong.
You forgot to create a template for the page itself.
The main rule everyone have to learn by heart:
Not a single character has to be sent into browser, until all data gets ready.
Why?
it's 2012 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page?
there is a thing called HTTP header. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.
Separating display logic from the business logic will let you use the same php code on many sites. You will have to change only templates and don't touch engine files. That's really great benefit.
Imagine you're going to make a custom <title> tag for your pages, based on the page content. Isn't it extremely common thing? But you can't make it without using templates.
So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.
And these templates have to be called only when all business logic is done - i.e. you have got all your data ready.
An example layout is going to be like this:
.1. page itself.
it outputs nothing but only gathers required data and then calls a template:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.tpl.php";
include "template.php";
?>
.2. template.php which is your main site template,
consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
.3. and finally links.tpl.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<? endforeach ?>
<ul>
this way you'd need no index with includes at all

Categories