I am a newbee to CodeIgniter and PHP. I am trying to insert data using a form and display it on the same view page using MVC pattern given by CodeIgniter. My View is:
<html>
<head>
<title>My Form</title>
</head>
<body>
<? echo form_open('books/input'); ?>
<? echo $id; ?>:
<? echo form_input('id'); ?>
</br>
<? echo $title; ?>:
<? echo form_input('title'); ?>
</br>
<? echo $body; ?>:
<? echo form_input('body'); ?>
</br>
<? echo form_submit('mysubmit','Submit!'); ?>
<? echo form_close(); ?>
</body>
</html>
and My Controller is:
<? class Books extends Controller{
function Books(){
parent::Controller();
}
function main(){
$this->load->model('books_model');
$data = $this->books_model->general();
//$this->load->view('books_main',$data);
}
function input(){
$this->load->helper('form');
$this->load->model('books_model');
$data = $this->books_model->general();
$this->load->view('books_input',$data);
}
} ?>
and My model is just returning the data entered by the user as:
<? class books_model extends Model{
function books_model(){
$this->load->helper('url');
}
function general(){
$data['id'] = 'id';
$data['title'] = 'title';
$data['body'] = 'body';
return $data;
}
} ?>
But when I try to access the form, it shows me an error quoting:
load->model('books_model'); $data = $this->books_model->general(); }
function input(){ $this->load->helper('form'); $this->load->model('books_model'); $data = $this->books_model->general();
$this->load->view('books_input',$data); } } ?>
404 Page Not Found
though I am accessing it with the same URL: localhost/CodeIgniter/index.php/books/input
How can I access this form and insert data successfully?
Every Class name should start with capital letter
like
class Books_model extends Model
and do type like this
$this->load->model('Books_model');
this could be the issue that you are getting "404 page not found".
Read some basic things that you have to follow from guide
http://ellislab.com/codeigniter/user-guide/general/models.html
Related
I am creating a sample image test where in I want to display all of the images in a database table in CodeIgniter. The question is, how will I able to display the values of the database table using CodeIgniter?
Here is the code of my model:
<?php
class Image_model extends CI_Model{
/*Sample test function for the image dropdown list*/
public function getImages()
{
$query = $this->db->select('main_image_url'));
$this->db->get('product_master');
return $query->result();
}
/*End sample test function*/
}
?>
Here is the code of my controller:
<?php
if(! defined('BASEPATH')) exit('No direct script access allowed');
class Sample_image_dropdown extends MX_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['main_view'] = 'sample_view/image_dropdown_view';
$this->load->view('sample_view/image_dropdown_view', $data);
}
public function display_all_images()
{
$this->load->model('sample_model/image_model');
$data['images'] = $this->image_model->getImages();
$data['main_view'] = "sample_view/image_dropdown_view";
$this->load->view('sample_view/image_dropdown_view', $data);
}
}
?>
Here is the code of my view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<select name="" id="">
<option value="all">All Image</option>
<option value="with-image">With Image</option>
<option value="no-image">Without Image</option>
</select>
<input type="submit" value="Search">
</div>
<?php if (isset($images)):?>
<?php foreach ($images as $image):?>
<table>
<tr>
<td>
<th>
Images
</th>
</td>
</tr>
<tr>
<td>
<img src="<?php echo "$image->main_image_url";?>">
</td>
</tr>
</table>
<?php endforeach; ?>
<?php endif; ?>
</body>
</html>
You made a mistake in your model and in the controller, I mentioned the changes please note down and try,
Model File
class Image_model extends CI_Model{
/*Sample test function for the image dropdown list*/
public function getImages()
{
$query = $this->db->select('main_image_url');
$this->db->from('product_master');
$query = $this->db->get();
return $query->result();
}
/*End sample test function*/
}
Controller File,
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['main_view'] = 'sample_view/image_dropdown_view';
$this->display_all_images();
}
public function display_all_images()
{
$this->load->model('sample_model/image_model');
$data['images'] = $this->image_model->getImages();
$data['main_view'] = "sample_view/image_dropdown_view";
$this->load->view('sample_view/image_dropdown_view', $data);
}
I assume you are storing images in directory at the time of uploading at
uploads/abc.png
then you can get data from db (which you are already doing)
and in view :
<img src="echo dir_path./$image->main_image_url;" />
or if you are storing whole path in DB then :
<img src="echo $image->main_image_url;" />
I have created a basic login form with validation containing only that the fields are not empty.
In controller the index looks like
$this->load->helper('form');
$this->load->view('auth/login');
and this is the view
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<meta charset="utf-8">
<title>LOGIN
</title>
</head>
<body>
<h1>Simple Login with CodeIgniter</h1>
<?php echo form_open('login/verifylogin'); ?>
<div class="" style="margin:0">
<label for="username">Username:</label>
<div class="" style="display:inline-block;margin:0">
<?php echo form_input(['name'=>'username','class'=>'','placeholder'=>'Your username here','value'=>set_value('username')]); ?>
</div>
<?php echo form_error('username',"<div class='w3-text-red' style='display:inline-block'>","</div>"); ?>
</div>
<br/>
<div class="">
<?php echo form_label('Password:','pass'); ?>
<div class="" style="display:inline-block;margin:0">
<?php echo form_password(['name'=>'password','class'=>'','placeholder'=>'Your password here']); ?>
</div>
<?php echo form_error('password',"<div class='w3-text-red' style='display:inline-block'>","</div>"); ?>
</div>
<br/>
<?php echo form_submit('submit', 'Login'); ?>
<?php echo form_close(); ?>
</form>
</body>
</html>
When a user is successfully validated he is redirected to dashboard view but in address bar still I get http://192.168.1.102/login/verifylogin (192.168.1.102 is the localhost or specifically its my device address on which server is running).
Also if a user leaves a field empty then he is showed the view auth/login with respective errors but the address bar shows http://192.168.1.102/login/verifylogin.
Please guide me how to fix this.
I am a beginner in codeigniter (I also have hands on laravel).
I have removed the index.php file according to the given guide.
I use ubuntu 14.04
EDIT 1:- I don't want to display the method name in address bar if a user fails login
EDIT 2:- Controller:-
<?php
/**
*
*/
class Login extends CI_Controller
{
public function index()
{
$this->load->helper('form');
$this->load->view('auth/login');
}
public function verifylogin()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run()) {
$this->load->helper('url');
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('Loginmodel');
if ($this->Loginmodel->login_valid($username, $password)) {
redirect('dashboard');
}else {
$this->load->view('auth/login');
}
} else {
$this->load->view('auth/login');
}
}
}
?>
I hope someone can help me. How can I best dynamically change page title, desc, or key vs..vs..by extending the original class? Here is what I have so far:
<?php
class siteGlobal
{
public $arr = array('title'=>'Page Default title','desc'=>'Page default description');
public function pageHeader()
{ ?>
<!doctype html><html>
<?php }
public function metaTags()
{ ?>
<head>
<meta charset="utf-8">
<title><?php echo $this->arr['title'];?></title>
<meta name="description" content="<?php echo $this->arr['desc'];?>">
<?php }
public function pageBody()
{ ?>
</head>
<body>
<?php }
public function pageFooter()
{ ?>
</body>
</html>
<?php }
} ?>
OTHER PAGE
require_once('siteGlobal.php');
class pageDetail extends siteGlobal
{
public function __construct()
{
$this->pageHeader();
$this->newMetas();
$this->startBody();
$this->sayfaBilgileri();
$this->pageFooter();
}
public function newMetas()
{
parent::metaTags();
}
public function sayfaBilgileri()
{
//HOW I can write a new title and description in this function to siteGlobal.php (public $metaInfo) ?>
<div style="width:640px; margin:0 auto;">
<h1>Page New Title</h1>
<h2>Page New Subtitle</h2>
<p>Lorem Ipsum, bla bla....</p>
</div>
<?php }
}
$writePage = new pageDetail(); ?>
You can try to create a method in the parent class say:
protected function setTitle($title){
$this->arr['title'] = $title;
}
and use it in the extended class this way:
parent::setTitle("My title");
I dont know whats wrong with my code..
I am trying to display message from database, but i keep on getting error that $results are not defined.
Controller
public function getMessages()
{
$this->load->model('get_message');
$data['results']= $this->get_message->getMessage();
$this->load->view('home_view', $data);
}
Model
<?php
class get_message extends CI_Model{
function getMessage(){
$query = $this->db->query("SELECT * FROM messages");
return $query->result_array();
}
}
View
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<h2>Welcome <?php echo $username; ?>!</h2>
<div id="Main">
<?php
foreach($results->result() as $row){
echo $row ->id;
echo $row ->user_username;
echo $row ->text;
echo $row ->posted_at;
echo "<br/>";
}
?>
</div>
Logout
</body>
</html>
EDIT: here is what my code looks like now
Controller
public function index()
{
$this->load->model('get_message');
$data['results']= $this->get_message->getMessage();
$this->load->view('home_view', $data);
}
Model
<?php
class get_message extends CI_Model{
function getMessage(){
$query = $this->db->query('SELECT * FROM messages');
return $query->result_array();
}
}
view
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<div id="Main">
<?php
foreach($results as $row){
echo $row['id'];
echo $row['user_username'];
echo $row['text'];
echo $row['posted_at'];
echo "<br/>";
}
?>
</div>
</body>
</html>
This code works. if it doesn't you are doing something else wrong.
In the controller you send data as result
$data['result']= $this->get_message->getMessage();
Access in view like this:
foreach($result as $row){
}
You used as results in foreach instead of result
You are passing wrong element to the view.
$data['result']= $this->get_message->getMessage();
^
Should be
$data['results']= $this->get_message->getMessage();
^
Also in Model,
return $query->result();
Should be:
return $query;
And in view,
foreach($results as $row){
Should be
if ($results->num_rows()) { // IN CASE, THE RESULT HAS NO ROWS.
foreach($results->result() as $row) {
View :
Your code
foreach(**$results** as $row){
}
?>
change to
foreach(**$result** as $row){
}
?>
Try to get result in some other variable in your controller function -
public function getMessages()
{
$this->load->model('get_message');
$data['msgesult']= $this->get_message->getMessage();
$this->load->view('home_view', $data);
}
And then in view -
<div id="Main">
<?php
foreach($msgesult as $row){
echo $row['id'];
echo $row['user_username'];
echo $row['text'];
echo $row['posted_at'];
echo "<br/>";
}
?>
</div>
This should work.
I have write a some codeigniter code, when I click button add a data, and the table will ajax reload, not reload the page. How to modify my code? Thanks
This is my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Big_category extends CI_Controller {
function __construct() {
...
}
function index() {
if($this->session->userdata('logged_in')) {
$this->page(0);
} else {
show_404('page');
}
}
function page($page) {
....
$data['pagelist']=$this->pagination->create_links();
$data["main_content"] = "big_category_view";
$this->load->view('template', $data);
}
This is my model
class Big_category_model extends CI_Model
{
...
function get_big_category($limit, $start)
{
$this->db->limit($limit, $start);
$query = $this->db->get_where('category1', array('c1_status' => 1)); //SELECT * FROM category1
return json_encode($query->result());
}
This is my view
...
<? $data = json_decode($all_big_category); ?>
<? foreach($data as $key => $value): ?>
<tr class="modify_tr">
<td><?=($key+1)?></td>
<td id="td_id_<?=$value->c1_id?>" class="modify_td">
<span id="c1_id_<?=$value->c1_id?>"><?=$value->c1_name?></span>
<form class="form-search td_id_<?=$value->c1_id?>">
<input type="text" name="input_c1_id" id="input_c1_id_<?=$value->c1_id?>">
<button class="btn modify" id="button_c1_id_<?=$value->c1_id?>" c1-id="<?=$value->c1_id?>" type="button"><i class="icon-edit"></i></button></td>
</form>
<td><button class="btn btn-danger"><i class="icon-remove-sign"></i></button></td>
</tr>
<? endforeach ?>
</table>
<?=$pagelist?>
...
Here is a simple technique of how you can achieve this.
Layout(Template)
<html>
<body>
<div><!--other stuff here--></div>
<div id = 'form_id'>
<?php echo $content?>
</div>
</body>
</html>
Controller
function index()
{
if($this->input->is_ajax_request()){
//load form here
$this->load->view('form');
}else{
//this will load the form for the first time
//pass third parameter as TRUE so it will returned as string
//assigned to index content which will be displayed in layout
$data['content'] = $this->load->view('form',$data , TRUE);
//load template here
$this->load->view('template', $data);
}
}
JQuery
$.ajax({
type : 'POST',
url : '<?php echo site_url('controllername/index')?>',
data : '' // query string
success : function(formagain){
$('#form_id').html(formagain);
//this will replace the content of div with new form
}
});