Im a designer that i´m taking my first steps on the programing área, mostly on PHP / MySQL. Past week i need to add a blog section on a page made with code igniter.
I make the secction that show all post in order (title and published date). I made a view that takes the id to show the content. But i can`t echo any of the variables.
When i use var_dump or print and it takes all the variables without any problem.
Did i miss somehting? :(
Thank you.
Model:
class blog extends CI_Model {
public function get($id){
$this->db->select('id, title, description, icon, uri, published_at');
$this->db->from('blogs');
$this->db->where('id', $id);
$query = $this->db->get();
$data = $query->result();
return $query->result();
if(!empty($data)){
return $this->make_groups($data);
}
}
}
Controller:
Class Pages extends CI_Controller {
public function blog_detail($id) {
$this->load->model('blog');
if($data = $this->blog->get($id)) {
$this->load->model('blog');
$this->data['blog_menu'] = TRUE;
$this->data['blog'] = $data;
$this->data['og'] = array(
'description' => 'INFO',
'image' => site_url('assets/images/og/blog.png')
);
# layout config
$this->layout->add_css(site_url('assets/css/blog.css'));
$this->layout->title_for_layout = "TITLE";
$this->layout->meta = array(
array('name' => 'description', 'content' => 'DETAIL'),
array('name' => 'keywords', 'content' => 'KEYWORDS'),
);
$this->layout->render('pages/blog_detail/index', $this->data);
}
}
}
View:
<?php var_dump($this->data);
echo $blog->published_at; ?>
On your model you are not returning the result
Check that the filenames and class names only have the first letter upper case.
Blog_model.php
class Blog_model extends CI_Model {
public function get($id) {
$this->db->select('id, title, description, icon, uri, published_at');
$this->db->from('blogs');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->row_array();
}
}
Controller
controllers > Pages.php
class Pages extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('blog_model');
}
public function blog_detail($id) {
$blog_data = $this->blog_model->get($id);
if($blog_data)) {
$this->data['blog_menu'] = TRUE;
$this->data['published_at'] = $blog_data['published_at'];
var_dump($blog_data);
$this->data['og'] = array(
'description' => 'INFO',
'image' => site_url('assets/images/og/blog.png')
);
# layout config
$this->layout->add_css(site_url('assets/css/blog.css'));
$this->layout->title_for_layout = "TITLE";
$this->layout->meta = array(
array('name' => 'description', 'content' => 'DETAIL'),
array('name' => 'keywords', 'content' => 'KEYWORDS'),
);
$this->layout->render('pages/blog_detail/index', $this->data);
}
}
}
View
<?php echo $published_at;?>
Related
I'm quite new to php and there's no error appearing but appereantly, can't update my data in the database.
The controller
public function update_user_view() {
$this->load->helper('form');
$user_id = $this->uri->segment('3');
$query = $this->db->get_where("users",array("user_id"=>$user_id));
$data['records'] = $query->result();
$data['old_user_id'] = $user_id;
$this->load->view('user_edit',$data);
}
public function update_user(){
$this->load->model('user_model');
$data = array(
'user_id' => $this->input->post('user_id'),
'name' => $this->input->post('name'),
'nickname' => $this->input->post('nickname'),
'email' => $this->input->post('email'),
'hadd' => $this->input->post('hadd'),
'gender' => $this->input->post('gender'),
'cpnum' => $this->input->post('cpnum'),
'comment' => $this->input->post('comment')
);
$old_user_id = $this->input->post('old_user_id');
$this->user_model->update($data,$old_user_id);
$query = $this->db->get("users");
$data['records'] = $query->result();
$this->load->view('user_view',$data);
}
the model
<?php
class User_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function insert($data) {
if ($this->db->insert("users", $data)) {
return true;
}
}
public function delete($user_id) {
if ($this->db->delete("users", "user_id = ".$user_id)) {
return true;
}
}
public function update($data,$old_user_id) {
$this->db->set($data);
$this->db->where("user_id", $old_user_id);
$this->db->update("users", $data);
}
}
?>
Just do
$this->db->where("user_id", $old_user_id);
$this->db->update("users",$data);
Remove $this->db->set($data); from update method of model. This is not required as update query data section is used to set table records.
how to use insert_batch to add multiple data in database by using for loop please help me as i am very new to codeigniter.
My controller
class Student extends CI_Controller {
public function _construct()
{
parent::_construct();
//call model
$this->load->model("StudentModel","m");
}
function index()
{
$this->load->view("index");
}
function savedata()
{
$data = array(
array(
'studentname' => 'Reddy' ,
'gender' => 'Male' ,
'phone' => '456879'
),
array(
'studentname' => 'Yalla' ,
'gender' => 'Female' ,
'phone' => '12345678'
)
);
//mean that insert into database table name tblstudent
$this->db->insert_batch('tblstudent',$data);
//mean that when insert already it will go to page index
redirect("Student/index");
}
function edit($id)
{
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view('edit',$data);
}
function update($id)
{
$id=$this->input->post('id');
$data=array(
'studentname' => $this->input->post('studentname'),
'gender' => $this->input->post('gender'),
'phone' => $this->input->post('phone')
);
$this->db->where('id',$id);
$this->db->update('tblstudent',$data);
redirect("Student/index");
}
function delete($id)
{
$id=$this->db->where('id',$id);
$this->db->delete('tblstudent');
redirect("Student/index");
}
}
Model
class StudentModel extends CI_Model{
function _construct()
{
parent::_construct();
}
function gettable()
{
$query=$this->db->get('tblstudent');
return $query->result();
}
function getonerow($id)
{
$this->db->where('id',$id);
$query = $this->db->get('tblstudent');
return $query->row();
}
}
First of all you don't have to add direct access to database in controller. For all database access code you have to create model.
in your above code you added database access code in direct controller,which is logically wrong as per MVC architecture.
Below is code as per MVC and all database access code resides in Model.
Your main question for batch insert is in function insert()
of model.Please go through it.
<?php
class Student extends CI_Controller {
public function _construct()
{
parent::_construct();
//call model
$this->load->model("StudentModel","m");
}
function index()
{
$this->load->view("index");
}
function savedata()
{
$stdarray = array();
$stdarray[] = array('studentname' => 'Reddy' ,'gender' => 'Male' ,'phone' => '456879');
$stdarray[] = array('studentname' => 'Yalla' ,'gender' => 'Female' ,'phone' => '12345678');
//mean that insert into database table name tblstudent
$this->db->insert_batch('tblstudent',$data);
$this->m->insert($stdarray);
//mean that when insert already it will go to page index
redirect("Student/index");
}
function edit($id)
{
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view('edit',$data);
}
function update($id)
{
$updateArray = array();
$updateArray['studentname'] = $this->input->post('studentname');
$updateArray['gender'] = $this->input->post('gender');
$updateArray['phone'] = $this->input->post('phone');
$whereArray = array();
$whereArray['id'] = $id;
$this->m->updateRow($updateArray,$whereArray);
redirect("Student/index");
}
function delete($id)
{
$whereArray = array();
$whereArray['id'] = $id;
$this->m->deleteRow($whereArray);
redirect("Student/index");
}
}
?>
Model:
class StudentModel extends CI_Model{
function _construct()
{
parent::_construct();
}
function gettable()
{
$query=$this->db->get('tblstudent');
return $query->result();
}
function getonerow($id)
{
$this->db->where('id',$id);
$query = $this->db->get('tblstudent');
return $query->row();
}
function insert($insrtArray)
{
$success = $this->db->insert_batch('tblstudent', $insrtArray);
}
function updateRow($updateArray,$whereArray)
{
if(!empty($whereArray))
{
foreach ($whereArray as $key => $value) {
$this->db->where($key,$value);
}
}
$this->db->update('tblstudent',$updateArray);
}
function deleteRow($whereArray)
{
if(!empty($whereArray))
{
foreach ($whereArray as $key => $value) {
$this->db->where($key,$value);
}
}
$this->db->delete('tblstudent');
}
}
?>
Important: morever you have to use escape function for all inputa which are direct send to database for maintain security from SQL injection.
Im trying add data to the database useing CodeIgniter framework. DB method return true, but I have no new data in database.
There is my controller:
class Facebook extends CI_Controller
{
public function index()
{
if($this->input->is_ajax_request())
{
$this->load->model('user_model','user');
$data = array();
$data['u_ip'] = $this->input->ip_address();
$this->user->add_user($data);
}
}
}
And there is my model:
class User_model extends CI_Model
{
public function add_user($data)
{
$this->db->insert('users',$data);
//$res = $this->db->insert('users',$data); (true)
}
}
Update:
There is my database:
id_users (int 255) AI, key
u_ip (varchar 100)
Also, make sure your $data looks like this:
$data = array(
"u_ip" => $this->input->ip_address(),
);
$this->insert("data", $data);
Or if coming from another file, do this:
$this->insert("users", $data[0];)
Remember to load the dataabse:
$this->load->database();
This will go within add_user($data) eg.
class User_model extends CI_Model
{
public function add_user($data)
{
$this->load->database();
$this->db->insert('users',$data);
//$res = $this->db->insert('users',$data); (true)
}
}
However, you can have these automatically, if you go to autoload.php, you can do this:
$autoload['libraries'] = array('database');
And for your model:
$autoload['model'] = array('logic');
You are not mentioning any column names in your query. Try the below code -
class User_model extends CI_Model{
public $db;
public function __construct(){
parent::__construct();
$this->db = $this->load->database('default',true);
}
public function add($data)
{
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
}
$this->input->is_ajax_request()
is this posted via a form ? if so it should be $this->input->post('is_ajax_request');
is_ajax_request being the name of the input your posting,
If its another function within the controller you need to do $this->is_ajax_request();
My small project with CodeIgniter (wich is only to get familiar with it. It's actually like their tutorial's example) it stoped working, giving me no output or errors even that in the index.php is set to developement...
So I tried to debug it myself with some echo's
And I found that here stops logging
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
echo 'This is echoed';
$this->load->model('news_model');
echo 'This wont be echoed';
}
/*(class continues)*/
}
And my news_model.php looks like this:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news'{});
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
}
Any idea what I'm doing wrong?
-EDIT-
public function __construct()
{
echo '__construct()';
$this->load->database();
echo 'after__construct()';
}
None of them are echoed...
find 1
$this->db->get('news'{}); // try to del {}
first parameters is table name
The second and third parameters enable you to set a limit and offset clause
Original codeignitor tutorial has this:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news'); // <----------------------------
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
I wish to list some Categories name on my layout main.php page.
Since the layout doesn't have any associated controller or model, I wish to create a static method like this on Category model:
public static function getHeaderModels()
{
// get all models here
return $models;
}
and then in the main layout
<?php
$models = Category::getHeaderModels();
foreach($models as $model)
{
// ....
}
?>
My question is a very basic one:
How can I retrieve those category names from the model ?
Here is the full model:
class Category extends CActiveRecord {
public static function model($className=__CLASS__) {
return parent::model($className);
}
public function tableName() {
return 'category';
}
public function rules() {
return array(
array('parent_id', 'numerical', 'integerOnly' => true),
array('name', 'length', 'max' => 255),
array('id, parent_id, name', 'safe', 'on' => 'search'),
);
}
public function relations() {
return array(
'users' => array(self::MANY_MANY, 'User', 'categories(category_id, user_id)'),
);
}
public function scopes()
{
return array(
'toplevel'=>array(
'condition' => 'parent_id IS NULL'
),
);
}
public function attributeLabels() {
$id = Yii::t('trans', 'ID');
$parentId = Yii::t('trans', 'Parent');
$name = Yii::t('trans', 'Name');
return array(
'id' => $id,
'parent_id' => $parentId,
'name' => $name,
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('parent_id', $this->parent_id);
$criteria->compare('name', $this->name, true);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
}
public static function getHeaderModels() {
//what sintax should I use to retrieve the models here ?
return $models;
}
May be this answer can help you. First you must create a Widget so you can use it more effectively.
First Create a new widget. Let say the name is CategoryWidget. Put this widget under components directory protected/components.
class CategoryWidget extends CWidget {
public function run() {
$models = Category::model()->findAll();
$this->render('category', array(
'models'=>$models
));
}
}
Then create a view for this widget. The file name is category.php.
Put it under protected/components/views
category.php
<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Then call this widget from your main layout.
main.php
// your code ...
<?php $this->widget('CategoryWidget') ?>
...
If I'm not mistaken, you can also pass any variable available in a view, on to the layout.
You just do it from the view that has your variable.
This is the catch: you need to declare the variable which will receive your value, in the controller, like this:
<?php
class MyController extends Controller
{
public $myvariable;
After this, you will assign your model or whatever to this public variable inside your view,
like this:
$this->myvariable = $modeldata;
After you have assigned your model data to controller's public attribute,
you can easily display it inside your layout e.g.
echo $this->myvariable;
Yii already does this by assigning menu items to column2 sidebar menu, from view, like this:
$this->menu=array(
array('label'=>'List Item', 'url'=>array('index')),
array('label'=>'Manage Item', 'url'=>array('admin')),
);
You can see it in all create/update views that gii crud creates.