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.
Related
Controller:
public function latestnews()
{
$data['news'] = $this->New_model->getById($id);
$this->load->view('news',$data);
}
Model:
public function getById($id)
{
return $this->db->get_where($this->_table, ["new_id" => $id])->row();
}
View:
<?php
if (isset($news) and $news) {
foreach($news as $new) {
?>
<div class="col-sm-12">
<div class="section">
<img src="<?php echo site_url('uploads/'.$new->image); ?>" />
</div>
<p><?php echo $new->description;?> </p>
</div>
<?php
}
}
?>
How and where to define the variable id?
While clicking a dynamic image,it should be opened in another page containing details but instead showing Undefined variable: id
In the latestnews() function you have no $id defined.
Try to call the latestnews() function with the right $id parameter.
you can write function like this,
function latestnews($id)
{
Controller:
function single_news($id) { // This $id variable comes from URL Ex: example.com/single_news/15. So $id = 15
$this->data['news_data'] = $this->new_model->getNewsByID($id);
$this->load->view('single-news', $this->data); // Create New View file named single-news.php
}
Model:
function getNewsByID($id) {
$this->db->where('id', $id);
$q = $this->db->get('news');
if($q->num_rows() > 0) {
return $q->row();
}
return false;
}
View: (Single News Page)
<div>
<img src="<?php echo site_url('uploads/'.$news_data->image); ?>" />
</div>
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 want to do a simple search with pagination in codeigniter...
I dont really know how to do it..
My Model
public function testSearch()
{
$this->db->like('Profile_for', 'Myself');
$query = $this->db->get('users');
return $query->result();
}
My Controller
public function searcher()
{
$config['base_url']='http://localhost/index.php/controller/searcher';
$config['total_rows'] = $this->Model->testSearch()->num_rows();
$config['per_page'] = 5;
$config['num_links'] = 2;
$this->pagination->initialize($config);
$data['records'] = $this->Model->testSearch($config['per_page'], $this->uri->segment(3));
$this->load->view('pagination', $data);
}
I THINK MY CONTROLLER CODES ARE WRONG....
Please help me guyz....
Thanks in advance.....
1.Open routes.php under Config folder
2.Now create a file called pagination.php under controllers folder and paste the following code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pagination extends CI_Controller
{
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Countries_Model");
$this->load->library("pagination");
}
public function index() {
$config["base_url"] = base_url() . "pagination";
$config["total_rows"] = $this->Countries_Model->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2))? $this->uri->segment(2) : 0;
$data["results"] = $this->Countries_Model
->get_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("pagination", $data);
}
}
3.Create a file called countries_model.php under models folder and paste the following code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Countries_Model extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("tblcountries");
}
public function get_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("tblcountries");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
4.Lastly, create a file called pagination.php under views folder and paste the following code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Pagination with CodeIgniter</title>
</head>
<body>
<div id="container">
<h1 style="text-align:center">Page Navigation in PHP</h1>
<table width="400" border="1" align="center">
<tr>
<td width="100" bgcolor="#CCCCCC"><p>Country ID</p></td>
<td width="300" bgcolor="#CCCCCC">Country</td>
</tr>
<h1 style="text-align:center">Countries</h1>
<div id="body">
<?php
foreach($results as $data) {
?>
<tr>
<td><?php echo $data->CountryID ?></td>
<td><?php echo $data->Country ?></td>
</tr>
<?php
}
?>
</table>
<div style="text-align:center"><?php echo $links; ?></div>
</div>
</div>
</body>
</html>
This is code concept is same as your code.I hope this will help you
lot Thank you
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
I want to create an element in XML file using CodeIgniter, but every time I'm getting a fatal error: "Call to undefined method SimpleXMLElement::createElement()". Here is my code in Controller, View & Model class, what am I doing wrong?
Controller (xml_insert.php):
<?php
class Xml_insert extends CI_Controller {
function index() {
$this->load->model('xml_insert_model');
$data['rows'] = $this->xml_insert_model->getAll();
$this->load->view('xml_insert_view', $data);
}
function insert() {
$this->load->model('xml_insert_model');
$data['rows'] = $this->xml_insert_model->getAll();
foreach ($data['rows'] as $r) {
$path1 = $r->xml_file_path;
$xml = simplexml_load_file($path1);
$newAct = $_POST['activity'];
$root = $xml->firstChild;
$newElement = $xml->createElement('activity');
$root->appendChild($newElement);
$newText = $xml->createTextNode($newAct);
$newElement->appendChild($newText);
$xml->save('$path1');
$this->index();
}
}
}
View (xml_insert_view.php):
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php foreach ($rows as $r): ?>
<?php
$path1 = $r->xml_file_path;
$xml = simplexml_load_file($path1);
?>
<?php foreach ($xml->children() as $activity) : ?>
<?php echo "Activity : " . $activity . " <br />"; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<form name="input" action="index.php/xml_insert/insert" method="post">
insert activity:
<input type="text" name="activity"/>
<input type="submit" value="send"/>
</form>
</body>
</html>
Model (xml_insert_model.php):
<?php
class Xml_insert_model extends CI_Model
{
function getAll()
{
$q = $this->db->get("XML");
if ($q->num_rows > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
XML file(sample.xml)
<?xml version="1.0"?>
<list>
<activity>swimming</activity>
<activity>running</activity>
<activity>Jogging</activity>
<activity>Theatre</activity>
<activity>Programming</activity>
<activity>driving</activity>
<activity>eating</activity>
</list>
I think you are trying to use functions of DomDocument
instead of
$xml = simplexml_load_file($path1);
Try
$xml = new DomDocument();
$xml->load($path1);
You can use addChild method of simpleXML if you prefer.