My problem is this:
I have a masterpage:
<head>
<meta charset="utf-8" />
<title><?php echo isset($title)? $title: NULL; ?></title>
<?php $this->load->view('layout/header'); ?>
</head>
<body>
<!-- BEGIN PAGE BASE CONTENT -->
<?php $this->load->view($content);?>
<!-- END PAGE BASE CONTENT -->
</body>
and my dashboard controller:
class Dashboard extends CI_Controller {
public function index()
{
if($this->session->userdata('login') == true){
$data['title'] = 'Dashboard';
$data['content'] = 'pages/dashboard';
$this->load->view('layout/master', $data);
}
else{
redirect('auth');
}
}
and my add controller:
if($this->session->userdata('login') == true){
$data['title'] = 'افزودن مشتری';
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
else{
redirect('auth');
}
My problem is that at first when i call dashboard, everything is OK. but when i call add, everything mess up like there is no CSS attached or something.
Should i do something before set value to $content?
I cant understand what the problem is.
When in a function you can't do anything with it. But can't do anything outside. Like your else statement.
public function add(){
$data['title'] = 'افزودن مشتری';
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
else{
redirect('auth');
}
This is completely wrong practices. Just use
public function add(){
$data['title'] = 'افزودن مشتری';
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
Load Codeigniter for the Arabic Letters
Codeigniter by default is set to use UTF-8 for much of its internal functionality, so just make sure the charset is set to UTF-8 in your application/config/config.php file.
$config['charset'] = "UTF-8";
And set the header too
header('Content-Type: text/html; charset=utf-8');
Related
I cannot upload a file in codeigniter. Code I'm using:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->library('upload');
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->model('User_m');
}
public function form()
{
if($_POST){
//print_r($_POST);exit;
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
//Load upload library and initialize configuration
//print_r($config);exit;
$this->load->library('upload',$config);
//$this->upload->initialize($config);
if($this->upload->do_upload('userfile')){
$uploadData = $this->upload->data();
$userfile = $uploadData['file_name'];
//echo $userfile;exit;
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
$this->User_m->form_insert($data);
}
$this->load->view('form');
}
}
This is my controller function code for processing multipart form data in codeigniter. But file is not uploading.
as you have given code i have modified code to trace it.Here below i have attached the view file as well as controller file :
1) Below is a view file code.In this i have submitted form by giving method name in action attribute.If you are submitting the form through ajax then you can pass the method name over there.
<div id="container">
<h1>Please Fill up the Registration Form.!</h1>
<div id="body">
<!-- <?php echo $error;?> -->
<form method="post" action="<?php echo site_url() . '/Main/form' ?>" enctype="multipart/form-data"> Full name:<br> <input type="text" name="name"><br> Email:<br> <input type="text" name="email"><br> Phone:<br> <input type="text" name="phone"><br><br> Image:<br> <input type="file" name="userfile" /> <br /><br /> <input type="submit"><br> </form>
</div>
</div>
2) Below is a controller code.
<?php
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url','html'));
}
public function index()
{
$this->load->view('form');
}
public function form()
{
if($_POST){
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
//Load upload library and initialize configuration
//print_r($config);exit;
$this->load->library('upload',$config);
//$this->upload->initialize($config);
if($this->upload->do_upload('userfile')){
$uploadData = $this->upload->data();
$userfile = $uploadData['file_name'];
//echo $userfile;exit;
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
// Below $data getting all the form data
print_r($data);
}
$this->load->view('form');
}
}
Hope this will help you!
You can try this
public function form()
{
if($_POST){
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
$this->upload->initialize($config);
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$userfile = $_FILES['userfile']['name'];
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
$this->User_m->form_insert($data);
}
How do i convert this to codeigniter?
my php code for calling link is this
<ul>
<li>Register Here
<li>Login
</ul>
When I call this this specific link will be called.
<?php
#$opt = $_GET['option'];
if($opt=="") {
include('register.php');
error_reporting(1);
} else {
switch($opt) {
case 'register':
include('register.php');
break;
case 'login':
include('login.php');
break;
}
}
But I don't know how to do it in code igniter.
please help me
Try using a controller like Welcome.php or something
<?php
class Welcome extends CI_Controller {
public function index() {
// Load other data stuff.
// You should autoload the url helper.
$this->load->helper('url');
$opt = $this->input->get('option');
if($opt == "") {
$data['title'] = 'Register';
$this->load->view('header', $data);
$this->load->view('register');
$this->load->view('footer');
error_reporting(1);
} else {
switch($opt) {
case 'register':
$data['title'] = 'Register';
$this->load->view('header', $data);
$this->load->view('register');
$this->load->view('footer');
break;
case 'login':
$data['title'] = 'Login';
$this->load->view('header', $data);
$this->load->view('login');
$this->load->view('footer');
break;
}
}
}
Config.php
$config['base_url'] = 'http://localhost/yourproject/';
$config['index_page'] = 'index.php';
URI
$config['uri_protocol'] = 'REQUEST_URI';
To this
$config['uri_protocol'] = 'QUERY_STRING';
And then you can enable
$config['enable_query_strings'] = TRUE;
And use the built in codeigniter query strings.
Then you would use a link like
<li><?php echo anchor('c=welcome&option=register', 'Register');?></li>
The letter c mean controller in codeigniter query string but you can change that in config.php
I am a newbie in codeigniter and I am not the first web developer of the project however I discovered in views folder it has template.php which loads in all pages. how can i override the metadata of the header inside views folder which loads the header of the template.php? I wan it to have different metadata.
Here's the code of Template.php
<title>I Sold My Business - Your one-stop shop for online business brokerage</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="description" content="Whether you’re an entrepreneur trying to buy a business online or a broker who wants to sell businesses, I Sold My Business is here to help you. Visit us today!">
<meta name="keywords" content="sold business online, buy business online, sell business online">
<?php echo #$template['framework']; ?>
<?php echo #$template['bootstrap']; ?>
<?php echo #$template['head']; ?>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('templates/site/reconvert_style.css'); ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url('templates/site/stylesheet.css'); ?>" />
<script type='text/javascript'>
(function (d, t) {
var bh = d.createElement(t), s = d.getElementsByTagName(t)[0];
bh.type = 'text/javascript';
bh.src = '//www.bugherd.com/sidebarv2.js?apikey=rkdoxsrzmxvrsrt6ailjsa';
s.parentNode.insertBefore(bh, s);
})(document, 'script');
</script>
Here's the code inside controllers folder home.php
class Home extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('business_category_model');
$this->load->model('franchise_category_model');
$this->load->model('account_model');
$this->load->model('country_model');
}
public function index()
{
$template = array();
$template['title'] = "Home";
$this->load->model(array('business_listing_model', 'featured_business_model', 'franchise_model'));
$this->load->helper('text');
$page = array();
$page['countries'] = $this->country_model->get_all();
$featured_businesses = $this->featured_business_model->random(3);
$featured_count = 0;
foreach($featured_businesses->result() as $index => $featured_business)
{
if($featured_business->type == 'business' || $featured_business->type == 'video')
{
$page['featured_businesses'][$featured_count] = (array) $this->business_listing_model->get_one($featured_business->business_id);
}
else
{
$page['featured_businesses'][$featured_count] = (array) $this->franchise_model->get_one($featured_business->business_id);
}
$page['featured_businesses'][$featured_count]['f_type'] = $featured_business->type;
$featured_count++;
}
if($featured_businesses->num_rows() < 3)
{
$limit = 3 - $featured_businesses->num_rows();
$random_business = $this->business_listing_model->get_random($limit);
foreach($random_business->result() as $index => $featured_business)
{
$page['featured_businesses'][$featured_count] = (array) (array) $this->business_listing_model->get_one($featured_business->business_listing_id);
$page['featured_businesses'][$featured_count]['f_type'] = 'business';
$featured_count++;
}
}
$page['business_categories'] = $this->business_category_model->get_all('', array('business_category_title' => 'asc'));
$page['franchise_categories'] = $this->franchise_category_model->get_all();
$template['content'] = $this->template->get_view('home', $page, 'site');
$this->template->render($template, 'site');
}
public function sell_your_biz()
{
$username = $this->session->userdata('username');
$account_type = $this->session->userdata('account_type');
$account = $this->account_model->get_by_username($username);
if(strlen($username)==0)
{
$this->template->notification('Please login or create an account to post a listing', 'error');
redirect('/login/');
}
$template = array();
$template['title'] = "Sell Your Biz";
$page = array();
$template['content'] = $this->template->get_view('sell_your_biz', $page, 'site');
$this->template->render($template, 'site');
}
public function view_reconvert()
{
$template = array();
$template['title'] = "Home";
$page = array();
$page['countries'] = $this->country_model->get_all();
$page['business_categories'] = $this->business_category_model->get_all();
$page['franchise_categories'] = $this->franchise_category_model->get_all();
$template['content'] = $this->template->get_view('home2', $page, 'site');
$this->template->render($template, 'site' , 'template_rconvert');
}
}
This is just one way of doing it, but you can eg set/define your meta data in your controller as such:
public function index()
{
$template = array();
$template['title'] = "Home";
$page['meta_desc'] = "Description here";
$page['meta_key'] = "Keywords here";
Then change the view file to correspond to the same variables:
<meta name="description" content="<?= $meta_desc?>" />
<meta name="keywords" content="<?=$meta_key" />
I am using Facebook PHP SDK with Codeigniter to do login and registration using Facebook. For this I am using This tutorial
my controller 'facebooktest.php' code is:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Facebooktest extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('input');
$this->load->library('session');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('security');
}
function Facebooktest() {
parent::Controller();
$cookies = $this->load->model('facebook_model');
}
function index() {
$this->load->view('facebooktest/test2');
}
function test1() {
$data = array();
$data['user'] = $this->facebook_model->get_facebook_cookie();
$this->load->view('facebooktest/test1', $data);
}
function test2() {
$data['friends'] = $this->facebook_model->get_facebook_cookie();
$this->load->view('facebooktest/test2', $data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>
view 'test2.php' has code as:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Kent is Learning CodeIgniter - Test 2</title>
</head>
<body>
<fb:login-button autologoutlink="true"
onlogin="window.location.reload(true);"></fb:login-button>
<div style="width:600px;">
<?
if(isset($friends)){
foreach($friends as $friend){
?>
<img src="http://graph.facebook.com/<?=$friend['id'];?>/picture" title="<?=$friend['name'];?>" />
<?
}
}
?>
</div>
<p><?=anchor('facebook_connect/page4','Go back to page 4 of the tutorial');?></p>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '457228377680583', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.sessionChange', function(response) {
if (response.session) {
// A user has logged in, and a new cookie has been saved
window.location.reload(true);
} else {
// The user has logged out, and the cookie has been cleared
}
});
</script>
</body>
</html>
And model 'facebook_model.php' has code:
<?php
class Facebook_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get_facebook_cookie() {
$app_id = '457228377680583';
$application_secret = '01f660b73bc085f0b78cd22322556495';
if (isset($_COOKIE['fbs_' . $app_id])) {
echo "dfgdsf";exit;
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig']) {
return null;
}
return $args;
} else {
return null;
}
}
function getUser() {
$cookie = $this->get_facebook_cookie();
$user = #json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']), true);
return $user;
}
function getFriendIds($include_self = TRUE) {
$cookie = $this->get_facebook_cookie();
$friends = #json_decode(file_get_contents(
'https://graph.facebook.com/me/friends?access_token=' .
$cookie['access_token']), true);
$friend_ids = array();
foreach ($friends['data'] as $friend) {
$friend_ids[] = $friend['id'];
}
if ($include_self == TRUE) {
$friend_ids[] = $cookie['uid'];
}
return $friend_ids;
}
function getFriends($include_self = TRUE) {
$cookie = $this->get_facebook_cookie();
print_r($cookie);
$friends = #json_decode(file_get_contents(
'https://graph.facebook.com/me/friends?access_token=' .
$cookie['access_token']), true);
if ($include_self == TRUE) {
$friends['data'][] = array(
'name' => 'You',
'id' => $cookie['uid']
);
}
return $friends['data'];
}
function getFriendArray($include_self = TRUE) {
$cookie = $this->get_facebook_cookie();
$friendlist = #json_decode(file_get_contents(
'https://graph.facebook.com/me/friends?access_token=' .
$cookie['access_token']), true);
$friends = array();
foreach ($friendlist['data'] as $friend) {
$friends[$friend['id']] = array(
'name' => $friend['name'],
'picture' => 'http://graph.facebook.com/'.$friend['id'].'/picture'
);
}
if ($include_self == TRUE) {
$friends[$cookie['uid']] = 'You';
}
return $friends;
}
}
?>
The problem is that, in model, it is coming inside 'getFriends()'. From there it is going inside 'get_facebook_cookie()'. But it is not going inside if(isset($_COOKIE['fbs_' . $app_id])))
and hence, it is nor displaying data that I want.
So, if possible, please let me know whats wrong with my code.
Thanks in advance....
i have the following code which i modiefied from a tutorial
but the problem is that, i cant display the result properly in my view page..
if i display the array in the controller then its working properly, but in the view, its throwing the error..
<?php
class test extends CI_Controller {
public function index($start = 0) {
$this->load->model('pages_model');
$data_page=$this->pages_model->get_pages();
$this->load->helper('url');
$this->load->library('pagination');
$config['base_url'] = base_url().'test/index';
$config['total_rows'] = count($data_page);
$config['per_page'] = 5;
$data['user'] = array();
for($i=$start; $i<$start+$config['per_page']; $i++)
{
if (isset($data_page[$i])) {
$data['user'] = $data_page[$i];
}
}
//print_r($data['user']['page_id']); // this line displays 3, since the tupple with id 3 has maximum priority 5
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
if ($this->input->post('ajax')) {
$this->load->view('test', $data);
} else {
$this->load->view('test', $data);
}
}
}
?>
now comes the model
<?php
class pages_model extends CI_Model{
function __construct() {
parent::__construct();
}
function get_pages()
{
return $this->db->query("SELECT * FROM td_pages ORDER BY page_priority ASC")->result_array();
}
}
?>
and finally the view
<!DOCTYPE html>
<html>
<header>
<title>CodeIgniter Ajax Jquery Pagination</title>
<script src="<?php echo base_url(); ?>assets/front_assets/js/jquery-1.7.1.min.js"></script>
<script>
$(function() {
applyPagination();
function applyPagination() {
$("#ajax_paging a").click(function() {
var url = $(this).attr("href");
$.ajax({
type: "POST",
data: "ajax=1",
url: url,
beforeSend: function() {
$("#content").html("");
},
success: function(msg) {
$("#content").html(msg);
applyPagination();
}
});
return false;
});
}
});
</script>
</header>
<body>
<div id="content">
<div id="data">
<?php foreach($page_frag as $ut)
{?>
<div><?php echo $ut['page_slug'];?></div>
<?php } ?>
</data>
<div id="ajax_paging">
<?php echo $pagination; ?>
</div>
</div>
</div>
</body>
</html>
the problem is that the view is displaying the results in wrong way, but if i display in controller, then it shows that the array in the controller is working perfectly...
please help me solving the prob
You are loading the entire view in your ajax request which also contains the body and head text, Create a seperate view with the following content and load it in your ajax request.
<div id="data">
<?php foreach($page_frag as $ut)
{?>
<div><?php echo $ut['page_slug'];?></div>
<?php } ?>
</div>
What you're doing is WRONG
Pagination is meant to off-load processing power/ram from the server not only from the client side.
You're pulling off all your result then paginate them while you should be asking MySQL to paginate it for you & have a second query to give you the number of results to use it.
If you have index.php in your links then you must be using site_url() instead of base_url() & please note that both accepts an argument so you do not need to concatenate it the way you did it would be like this:
site_url('test/index');
codeigniter uses GET method, while in your javascript code you're using POST.
An easier method would be to catch the pagination li > a tags for the pagination & process it inside a container.