I'm totally new to Codeigniter and Grocery CRUD, but I very like the concept, so I'm trying to make my little app with these frameworks.
So, right now the login system is working and after login on the "home" page, I see my Grocery CRUD table, it's filled, so far it's okay...
My problem is, that I can't use any function: add, edit, view, search is not working, it gives me 404 error for example this is one edit link:
http://subdomain.mysite.com/index.php/home/edit/1
The site is on a subdomain, I don't know if could be relevant or not.
This is my base_url:
$config['base_url'] = '/';
If I change this to '', the base_url(); method doesn't gives me the right value, I assume it's because the subdomain.
This is my complete "Home" view:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My system</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="page-header text-center">My system</h1>
<div class="row">
<div class="container text-right">
<?php
$user = $this->session->userdata('user');
extract($user);
?>
<p class="d-inline"><b>Logged in as:</b> <i><?php echo $fname; ?></i></p>
</div>
<div class="container">
<?php
$crud = new grocery_CRUD();
$this->grocery_crud->set_table('mobil_table');
$this->grocery_crud->columns('mobile_number', 'package_name', 'package_date');
$output = $this->grocery_crud->render();
$this->load->view('mobil.php', $output); ?>
Logout
</div>
</div>
</div>
</body>
</html>
My routes:
$route['default_controller'] = 'user';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['home'] = 'user/home';
What should I correct to make these functions work on this page? Thx :)
You must access your edit page via your controller/method manner
if your controller is user and it has a function edit with a param name $id, your accessing url will be:
http://subdomain.mysite.com/index.php/user/edit/1
or define new route for it:
$route['home/edit'] = 'user/edit';
or more clearly as
$route['home/edit/(:num)'] = 'user/edit/$1';
But you can get clean urls by following these steps:
First create a .htaccess file in your websites root folder with this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
After that change:
$config['base_url'] = 'http://subdomain.mysite.com/';
and
$config['index_page'] = '';
After doing the above steps, you can access your urls without index.php in urls
for example:
http://subdomain.mysite.com/index.php/user/edit/1
will become
http://subdomain.mysite.com/user/edit/1
Related
I am using CodeIgniter bootstrap stylesheet link URL can't work properly.
Controller code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->helper('url');
$data['title'] = "Login Home";
$this->load->view('home',$data);
}
}
HTML Code
<div class="container">
<div class="row">
<div class="col-lg-offset-4 col-lg-4">
<div class="well">
<h3>welcome</h3>
</div>
</div>
</div>
</div>
CSS link code
<link href="<?php echo base_url("register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
Place assets out side of application. Reason the .htaccess in application folder blocks it.
application
assets
assets > bootstrap > css > bootstrap.css
assets > bootstrap > js > bootstrap.js
system
index.php
First I would suggest
config/autoload.php
$autoload['helper'] = array('url');
then
config/config.php Set your base url
$config['base_url'] = 'http://localhost/projectnmame/';
in config.php set this line
$config['base_url'] = 'http://localhost/register/';
root directory set this way
application
system
bootstrap-3.3.6
ensure that you are using .htaccess file in your codeigniter application.create one folder assets.place your css file in assets folder.after that link that file like this,
in application folder config file paste this,
$root = 'http://' . $_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);;
$config['base_url'] = $root;
<link href="<?php echo base_url("assets/register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
if you are not using htaccess file,
<link href="<?php echo base_url("index.php/assets/register/application/bootstrap-3.3.6/css/bootstrap.min.css"); ?>" rel="stylesheet">
try this....
I am a new member in stackoverflow and beginner for php. I started one sample blog project. It works fine, but I want to change url for seo friendly. Can any one help me please? How to write htaccess code for this blog.
index.php
<?php
include_once("db.php");
$sql = mysql_query("SELECT * FROM post");
?>
<html>
<head>
<title>
Post
</title>
</head>
<body>
<h1>Post List</h1>
<ul>
<?php
while($row = mysql_fetch_array($sql)){
?>
<li><?php echo $row['title']; ?></li>
<?php }?>
</ul>
</body>
</html>
post.php
<?php
include("db.php");
if(isset($_GET['pid'])){
$id = $_GET['pid'];
$qry = mysql_query("SELECT * FROM post WHERE id=".$id);
}
if($qry === FALSE) {
die(mysql_error()); // TODO: better error handling
}
?>
<html>
<head>
<title>
View Post
</title>
<style>
body{
background-color: #c9c9c9;
}
h1, .para{
border: 2px solid red;
padding:10px;
}
</style>
</head>
<body>
<?php
while($row1 = mysql_fetch_array($qry)){
?>
<h1><?php echo $row1['title']; ?> </h1>
<p class="para"><?php echo $row1['desc']; ?></p>
<?php }?>
</body>
</html>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^post/([0-9]+) post.php?pid=$1 [NC]
</IfModule>
When i'm running this code, url shows like this:
http://localhost/blog/post.php?pid=1
I want to display url like this
http://localhost/blog/post/1
Solution is executed successfully, but css styles not working. Css styles placed in css folder
.htaccess is used to parse the url, its not to generate the URL. You need to rewrite line
<li><?php echo $row['title']; ?></li>
to
<li><?php echo $row['title']; ?></li>
and your .htaccess will work
try this in htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
and you can use a bootsrap class
class bootstrap {
function __construct() {
$url=isset($_GET['url']) ? $_GET['url'] : null;
$url=rtrim($url,'/');
$url = explode('/',$url );
//print_r($url);
if(empty($url[0])) {
require 'controllers/index.php';
$controller = new index();
return false;
}
echo $url .'<br>';
$file = 'controllers/' .$url[0]. '.php';
if(file_exists($file)) {
require $file;
}
else {
require 'controllers/error.php';
$controller = new error();
return false;
}
$controller = new $url[0];
if(isset($url[2])) {
$controller ->{$url[1]}($url[2]);
}
else {
if(isset($url[1])) {
$controller ->{$url[1]}();
}
}
}
}
Try using -
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?pid=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ post.php?pid=$1
I'm trying to build a website that having different pages.
I'm a newbie to PHP, I have an index page with certain tables for holding header, navigation menu, main body, a sidebar and for a footer. The index page is attached to all the above-mentioned elements in it using include("filename.extension");. The problem is I tried to load the main body i.e content of the site, dynamically when the menu is changed.
Below is my code, any suggestion on this is very appreciatable. Thanks in advance.
<body>
<div> <?php Include('header.php'); </div>
<div id="menu" align="center">
<table width="790" height="35">
<tr>
<td>Home</td>
<td>Register</td> // this page need to display on the same window with other elements, after click.
</tr>
</tabel>
</div>
<div id="sidebar" align="right"> <?php include ("sidebar.php");?> </div>
<div id="Content ">
<?php include("FILE"); // here i need to display the hyper linked page.?>
</div>
<div id="footer"> <?php include("footer.php");?> </div>
</body> `
guess Diogo's answer will get you to what you want. create your index.php file like this.
//menu area
<div id="menuWrap">
link to page1
link to page2
</div>
//side bar
<div id="sidebarWrap">...</div>
//content area
<div id="contentWrap">
<?php
switch($_GET['page'])
{
case 'page1':
include '/pages/page1.php';
break;
case 'page2':
include '/pages/page2.php';
break;
default:
include '/pages/notfound.php';
}
?>
</div>
//footer
<div id="footerWrap">...</div>
This way you can display other pages in your site in the same window, only the content area will change
update:
I have got corrected your typos, here is the answer according to your code segment.
<body>
<div>
<?php Include('header.php'); ?>
</div>
<div id="menu" align="center">
<table width="790" height="35">
<tr>
<td>Home</td>
<td>Register</td>
</tr>
</table>
</div>
<div id="sidebar" align="right">
<?php include ("sidebar.php"); ?>
</div>
<div id="Content ">
<?php
switch($_GET['page'])
{
case 'page1':
include '/pages/home.php';//file path of your home page
break;
case 'page2':
include '/pages/students/reg.php';
break;
default:
include '/pages/notfound.php';
}
?>
</div>
<div id="footer">
<?php include("footer.php");?>
</div>
</body>
First, create a file with exactly the name below: (including the dot)
.htaccess
and put it in the same folder of your index.
Inside the .htaccess, write the following code:
# .htaccess mod_rewrite
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
The .htaccess on this case will rewrite the URLs to change the http://127.0.0.1/index.php?page=register to accept the "blogging pretty url" http://127.0.0.1/register
inside of your index.php folder, create a new folder "pages".
Inside of the folder pages, create 4 simple PHP files (home.php, register.php, anyother.php and notfound.php), with any content like
<?php
echo "I'm the Register Page.";
?>
This will be an example of index based on your code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>¨title</title></head>
<body>
<div>
<?php include 'header.php'; ?>
</div>
<div id="menu" align="center">
<table width="790" height="35">
<tr>
<td>Home</td>
<td>Register</td>
<td>AnyOther</td>
<td>Broken Sample</td>
</tr>
</table>
</div>
<div id="sidebar" align="right">
<?php include 'sidebar.php'; ?>
</div>
<div id="Content ">
<?php
if(!isset($_GET['page']) || $_GET['page'] == ''){
$page = 'home'; //If no page specified
} else {
$page = $_GET['page'];
}
switch($page)
{
case 'home':
include '/pages/home.php';//file path of your home page
break;
case 'register':
include '/pages/register.php';
break;
case 'anyother':
include '/pages/anyother.php';
break;
default:
include '/pages/notfound.php'; //If any page that doesn't exists, then get back to home.
}
?>
</div>
<div id="footer">
<?php include("footer.php");?>
</div>
</body>
</html>
I am trying to create a search form in my codeigniter site header, however everytime the form is submitted, I receive a 404 error saying the page cannot be found! I have attempted to create a link to a test page and this gave me the same error.
Please observe my code below.
view(site_header)
<?php echo doctype(); ?>
<html lang="en">
<link href="<?php echo base_url(); ?>styles/style.css" type="text/css" rel="stylesheet"/>
<head>
<title>/title>
<div id="container">
<div id="search">
<?php
echo form_open('search_keyword');
echo form_label("Stumble a search ", "searchfor");
echo form_input("search","search");
echo form_submit("getSearch","Search");
echo form_close(); ?>
</div>
</div>
</head>
</html>
model (model_search)
<?php
class Model_search extends CI_Model {
public function get_results($search_term){
$query = $this->db->query('SELECT embed, title FROM videos WHERE tags LIKE '%$search_term%' order by RAND() LIMIT 1');
return $query->result();
}
}
?>
Controller (site.php) default controller
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
$this->load->model("model_get");
$data["results"] = $this->model_get->getRand();
$this->load->view("site_header");
$this->load->view("site_content", $data);
$this->load->view("site_footer");
}
public function search_keyword()
{
$this->load->model('model_search');
$search_term = $this->input->post('search');
$data['results'] = $this->model_search->get_results($search_term);
$this->load->view('site_header');
$this->load->view('search_content',$data);
$this->load->view('site_footer');
}
}
?>
Results page (search_content)
<body>
<link href="<?php echo base_url(); ?>styles/style.css" type="text/css" rel="stylesheet"/>
<div id="container">
<div id="intro">
<?php echo heading("Search Results",1);?>
</div>
<div id ="content">
<p>Stumble videos related to <?php echo $search_term; ?> </p>
<?php
foreach ($results as $row) {
$title = $row->title;
$vid = $row->embed;
}
echo heading($title, 3);
echo $vid;
?>
</div>
</div>
</body>
perhaps I am missing something obvious, however I think it may be to do with my .htaccess file which is posted below
(.htaccess)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /code/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
CodeIgniter URLs need both the controller name and the method.
form_open('site/search_keyword')
Maybe a stupid question, but did you remove the "index.php" from the $config['index_page'] variable in your config.php ?
And if you don't use any route to link 'search_keyword' to 'site/search_keyword', be sure to use the form_open('site/search_keyword') as specified previously.
Btw, here's the .htaccess I always use on my Codeigniter projects to rewrite the urls. Just add your RewriteBase on this to make it work.
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|img|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
I have two controllers in my project. The second controller do not displays in right way. If I open it with in browser line - thats ok, but if i pass it by the link in view its not ok. The second controller resembles on 1st.
Controller(s)
<?php
if ( ! defined('BASEPATH')) exit ('No direct script access allowed');
class Article extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
ini_set('display_errors',1);
error_reporting(E_ALL);
$this->load->view('header_view');
$this->load->view('menu_view');
$this->load->view('categories_view');
$this->load->model('mat_model');
$data = array();
$data['news'] = $this->mat_model->get_latest();
$data['latest'] = $this->mat_model->get_latest();
$this->load->view('useful_sites_view',$data);
$this->load->view('article_view',$data);
$this->load->view('footer_view');
}
}
?>
The view where I have a link:
<?php foreach ($news as $one):?>
<div id="right">
<div id="breadcrumb">Home » Somewhere</div>
<h1><?=$one['title']?></h1>
<p>
<div id="small_img"><?=$one['small_img']?></div>
<?=$one['description']?>
</p>
<div id=""> Читать далее...</div>
<span class="postinfo"> Posted by <?=$one['author']?> on
<?=$one['date']?></span>
<HR ALIGN="center" WIDTH="70%" SIZE="1px" COLOR="black">
</div>
<?php endforeach; ?>
I dont have so much reputation to browse images on the site, but i needs it so much, apologize for using otherwise resource
With line in browser:
With the link:
http://s020.radikal.ru/i710/1301/fd/76f313259454.jpg
With the browser line:
http://s020.radikal.ru/i713/1301/4e/a863cd18184d.jpg
Thanks.
In reply to the comment above...
<link rel="stylesheet" href="<?= base_url() ?>css/style.css">
is an absolute path.
Lets say that your base_url() is 'http://www.example.com/' so the above line will link to your css like so:
<link rel="stylesheet" href="http://www.example.com/css/style.css">
so no matter which page you're on, the CSS will always be linked to that css file.
On the other hand if you were using relative paths like this:
<link rel="stylesheet" href="/css/style.css">
on your site root, the css path would be correct. BUT on some other page, for example
http://www.example.com/somepage/testurl
your CSS would be linked like this:
<link rel="stylesheet" href="http://www.example.com/somepage/testurl/css/style.css">
which is obviously, wrong.
Try to google relative and absolute paths in html, that should make things a lot clearer.