Creating a new function in Codeigniter Controller - php

I just installed CodeIgniter 2.1.4. I want to call several functions in a controller. I have the following code:
Controller galeria.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Galeria extends CI_Controller {
public function index()
{
$this->load->view('vgaleria');
}
public function tg()
{
$this->load->view('tgaleria');
}
}
?>
Views:
<!DOCTYPE html PUBLIC>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Galería de imágenes</title>
<style type="text/css">
</style>
<script src="<?php echo base_url(); ?>javascript/funciones.js"
language="javascript" input="type/text">
</script>
</head>
<body>
<div id="imgs">
<img src="imagenes/imagen1.jpg" id="eimg">
</div>
<input type="button" id="bant" onclick="imgav('atras')" value="Anterior"></button>
<input type="button" id="bsig" onclick="imgav('adelante')" value="Siguiente"></button>
</body>
</html>
View tgaleria.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Galería de imágenes</title>
<style type="text/css">
</style>
<script src="<?php echo base_url();?>javascript/funciones.js"
language="javascript" input="type/text">
</script>
</head>
<body>
<div id="imgs">
<img src="imagenes/imagen1.jpg" id="eimg">
</div>
<input type="button" id="bant" onclick="imginter('arrancar')" value="Arrancar">
</button>
<input type="button" id="bsig" onclick="imgparar()" value="Parar"></button>
The default controller is galeria.php.
$route['default_controller'] = "galeria";
When I type
http://example.com/codeigniter I have everything ok.
But when I type
http://example.com/codeigniter/tg.php
I obtain the error 404, url not found.
How can I fix it?
Thanks.

Have you used .htaccess to remove the index.php? if not then you must append it like:
http://example.com/codeigniter/index.php/galeria/tg
else
http://example.com/codeigniter/galeria/tg
Here, galeria being your Controller and the first segment and tg your function and the second segment of the URI.

Related

view in another view page not getting displayed

I want to display Welcome to my 3rd Blog! of blogvieww.php in
blogview.php using codegniter.
But in the code below, what I've tried is that even Welcome to my 2nd Blog! of blogvieww.php is getting displayed in blogview.php.
Actually I just want to display only Welcome to my 3rd Blog!, how to do this can any one tell please tell me where my mistake was?
Blogcontroller.php(controller file)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blogcontroller extends CI_Controller {
public function index()
{
$data['blogvieww'] = $this->load->view('blogvieww', '', TRUE);
$this->load->view('blogview', $data);
}
public function blogvieww()
{
$this->load->view('blogvieww');
}
}
?>
blogview.php (view file)
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<div><?php echo $blogvieww; ?></div>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>
blogvieww.php (view file)
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div><h1>Welcome to my 2nd Blog!</h1></div>
<div><h1>Welcome to my 3rd Blog!</h1></div>
</body>
</html>
Change in your blogview.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<div><?php echo $data->blogvieww; ?></div>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>

How to use Header in Codeigniter

In the example above, we are using “dynamically added data”, which you will see below.
controller
public function index()
{
$data['main_menus'] = '';
if(count($this->menu_model->findActive()) > 0){
$data['main_menus'] = $this->link_class->bootstrap_menu($this->menu_model->findActive());
}
$data['header'] = $this->load->view('common/header',$data);
$data['posts'] = $this->home_model->find_active(3);
$data['content'] = $this->load->view('common/content',$data);
$this->load->view('layout',$data);
}
view
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="DBlog : Material Style Personal Blog Theme from Wowbootstrap">
<meta name="author" content="wowbootstrap.com">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,700|Merriweather:400,400italic,700italic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,500,400italic,700,700italic,900,900italic,300,300italic,500italic" rel="stylesheet" type="text/css">
<title><?php echo $page_title?> - CI Blog</title>
</head>
<body>
<div id="wrapper">
<?php echo $header;?>
<section class="main">
<div class="container">
<div class="row">
<div class="col-md-8">
<?php //echo $content;?>
</div>
<div class="col-md-4">
<?php //echo $right_sidebar;?>
</div>
</div>
</div>
</section>
<?php //echo $footer;?>
</div>
<script>
$.material.init();
</script>
<?php if(!empty($home_page)):?>
<script type="text/javascript">
$(window).scroll(function(){
if ($(this).scrollTop() > 1){
$('.top-navbar').removeClass('navbar-transparent');
}else{
$('.top-navbar').addClass('navbar-transparent');
}
});
</script>
<?php endif;?>
<script type="text/javascript">
$(document).ready(function () {
$('[data-toggle="tooltip"]').tooltip();
});
</script>
</body>
</html>
I am using codeigniter to develop php web site. Now I have the question.
In controller,$this->load->view('index', $data);
I want $data can also share in header.php that included in index.php.
How to do???
If i do templating on codeigniter I do like this
In Layout.php
<!--Header -->
<?php $this->load->view('path/to/header'); ?>
<!--Content -->
<?php $this->load->view($content); ?>
<!--Footer-->
<?php $this->load->view('path/to/footer'); ?>
In controller
$data['content'] = 'path/to/content/page'; # or any dynamic data with $this->model...
$this->load->view('template',$data);
In Your Controller : to load view file like this,
public function index()
{
$data['main_menus'] = '';
if(count($this->menu_model->findActive()) > 0){
$data['main_menus'] = $this->link_class->bootstrap_menu($this->menu_model->findActive());
}
$data['posts'] = $this->home_model->find_active(3);
// To load view files
$this->load->view('common/header',$data); // header view file
$this->load->view('common/content',$data); // body content view file
$this->load->view('layout',$data); // footer view file
}

Laravel angularjs configuration

I try to include Angularjs in Laravel but i don't know what mistake i made i got error
Use of undefined constant name - assumed 'name'
myHead.blad.php
<title>Title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="controllers/homeController.js"></script>
<script type="text/javascript" src="controllers/aboutController.js"></script>
myWelcome.blade.php
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<?php echo View::make('layouts/head'); ?>
</head>
<body>
<?php echo View::make('layouts/header'); ?>
<div ng-controller="HelloController">Hi {{name}} welcome to AngularJS Tutorial Series</div>
<button class="btn" name="test" value="test">Test</button>
</body>
<?php echo View::make('layouts/footer'); ?>
</html>
Hellocontroller.js
MyApp.controller('HelloController', hello);
function hello($scope)
{
$scope.name = "Test";
}
Please tell me how to configure Angularjs in Laravel
interpolated expression need to change if you want to work with laravel because laravel and angular js both use same {{}} interpolation symbol for expression exicuition
Add the code where you define your app
var MyApp = angular.module('MyApp', [])
MyApp.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
myWelcome.blade.php
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<?php echo View::make('layouts/head'); ?>
</head>
<body>
<?php echo View::make('layouts/header'); ?>
<div ng-controller="HelloController">Hi <%name%> welcome to AngularJS Tutorial Series</div>
<button class="btn" name="test" value="test">Test</button>
</body>
<?php echo View::make('layouts/footer'); ?>
</html>
Kindly Check into your code the issue persist with injection due to which your code was not working, it's working with your code, please have a look
(function(angular) {
'use strict';
var myApp = angular.module('MyApp', []);
myApp.controller('HelloController', function($scope) {
$scope.name = 'Test';
});
})(window.angular);
https://plnkr.co/edit/30nM20o7MHrxPifVu9Ig?p=preview

how to make background css URL in codeigniter?

I'm trying to display some images using the background url in CSS but is not working. I put this code in one of my CSS selector
background:url(../assets/images/teksture2.png);
and view code
<!doctype html>
<html lang="en">
<head>
<meta content-type: "text/css" charset="UTF-8">
<title>Aplikasi Inventori Barang</title>
<link rel="stylesheet" href="<?php echo base_url("assets/css/layout.css");?>">
</head>
<body>
<div id="header">
<div class="image"><img src="<?php echo base_url("assets/images/logo.png");?>" alt="logo"></div>
</div>
</body>
</html>
and in controller file is
<?php
/**
*
*/
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper('url');
$this->load->view('index');
}
}
?>
can you help me solve this problem?
Hope css and images folders exist inside the asset folder. Then in your css file, you have to use below code.
background:url(../images/teksture2.png);

Php templating - Getting the html right

I'm developing a web app and I'm having a problem with the templating fase. The problem concerns the generation o html code in a php object. The code is as follows:
<?php class abcHtml {
function prepare() { ?>
<!DOCTYPE html>
<html>
<head>
<?php echo $this->meta; ?>
</head>
<body>
<div id="bg">
<div id="container_wrapper">
<?php echo $this->cont; ?>
</div>
</div>
</body>
</html>
<?php }} ?>
The goal is to have a php file with the cleanest html code possible(Don't want to use any framework).
<?php
class abc extends abcHtml
{
protected $meta;
protected $cont;
public function render(){
return parent::prepare();
}
public function setMeta($meta){
$this->meta = $meta;
return $this;
}
?>
This class inherites abcHtml to make possible to add diferent html modules. The problem is that when calling render(), the returned html code is note in the right order, the html code in the $meta and $cont variables appear before the html that is inside the prepare function.
For example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="public/css/base_style.css"/>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="bg">
<div id="container_wrapper">
</div>
</div>
</body>
</html>
This problem obviously means that php is running the php code first and then attaching the html. Is there a solution to fix this?

Categories