On my view I need to be able to get the hidden banner_image_id and use that in my $this->db->where('banner_image_id', $id) on my model function
When I update the image. It does not update to the correct row. Instead it update all rows with the same filename.
Question: On my model how can I make sure that my $this->db->where('banner_image_id', $id) updates correct rows.
I have done a vardump($id) and result is string(2) "63" which is correct but still updates every row the same. I tried update_batch also no luck.
Model Function
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
if (isset($banner_image_id)) {
foreach ($banner_image_id as $id) {
//var_dump($id);
//exit;
$data = array('banner_id' => $this->uri->segment(4),'banner_image' => $file_name);
$this->db->where('banner_image_id', $id);
//$this->db->where_in('banner_image_id', $id); // Tried No Luck
//$this->db->or_where_in('banner_image_id', $id); // Tried No Luck
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
}
View Table
<table>
<tbody>
<?php foreach ($banner_images as $img) { ?>
<tr>
<td>
<?php echo $img['banner_image_id'];?>
<input type="hidden" name="banner_image_id[]" value="<?php echo $img['banner_image_id'];?>">
</td>
<td>
<input type="file" name="banner_image[]" multiple size="20">
</td>
<td>
<img src="<?php echo base_url() . 'uploads/' . $img['banner_image'];?>" />
<input type="hidden" name="banner_image" value="<?php echo $img['banner_image'];?>">
</td>
</tr>
<?php }?>
<tbody>
</table>
Try to concatenate the id numbers in foreach loop and pass it where function at once.
Assuming that your id numbers are array(1,2,5,6).
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
if (isset($banner_image_id)) {
$bid = 'IN(';
foreach ($banner_image_id as $id) {
$bid .= $id . ',';
}
$bid = substr($bid, 0, -1) . ')'; //remove last comma and add closing paranthesis.
//The content of the variable $bid would be like
//IN(1,2,5,6)
$data = array('banner_id' => $this->uri->segment(4),'banner_image' => $file_name);
$this->db->where('banner_image_id', $id);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
Another Suggestion:
Convert $banner_image_id to a normal array in case of it is an associative array.
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
$bid = array();
if (isset($banner_image_id)) {
foreach ($banner_image_id as $id) {
$bid[] = $id;
}
$data = array('banner_id' => $this->uri->segment(4),
'banner_image' => $file_name);
$this->db->where_in('banner_image_id', $bid);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
I suggest you to make convertion in the controller section, not in model. The lines ending with // should be in the controller. You can easily pass $bid to edit_banner_image($file_name, $bid) function then.
public function edit_banner_image($file_name, $bid) {
$banner_image_id = $this->input->post('banner_image_id'); //
$bid = array();//
if (isset($banner_image_id)) {//
foreach ($banner_image_id as $id) { //
$bid[] = $id;//
}
$data = array('banner_id' => $this->uri->segment(4),
'banner_image' => $file_name);
$this->db->where_in('banner_image_id', $bid);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
Thus, you will have more readable code.
Related
I have this model function below which lets me join both tables. Printed results are at bottom of question.
<?php
class Forum_model extends CI_Model {
public function get_forums() {
$this->db->select('f.*, fc.*');
$this->db->from('forum_category fc', 'left');
$this->db->join('forum f', 'f.forum_id = fc.category_forum_id', 'left');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
}
How ever when I go to look at in on my view it displays it like image below
The two General categories news, lounge should be displayed on the one panel. For some reason it displays the two general categories in own panel.
Question: How is it possiable to display the two categories together? I have tried $this->db->group_by('fc.category_forum_id')
Image
Controller
<?php
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->model('reports/thread_analytics_model');
$this->load->model('forum/forum_model');
}
public function index() {
$results = $this->forum_model->get_forums();
echo "<pre>";
print_r($results);
echo "</pre>";
$data['forums'] = array();
foreach ($results as $result) {
$data['forums'][] = array(
'forum_name' => $result['forum_name'],
'category_name' => $result['category_name']
);
}
$data['content'] = 'common/home_view';
$this->load->view('theme/default/template_view', $data);
}
}
View
<?php foreach ($forums as $forum) {?>
<div class="panel panel-home">
<div class="panel-heading"><h1 class="panel-title"><?php echo $forum['forum_name'];?></h1></div>
<div class="panel-body">
<table class="table">
<tbody>
<tr>
<td><?php echo $forum['category_name'];?></td>
</tr>
</tbody>
</table>
</div>
</div><!-- Panel -->
<?php }?>
Printed Results Image
A possible solution with your approach would be:
class Forum_model extends CI_Model {
public function get_forums() {
$arrGroupedData = array();
$this->db->select('f.*, fc.*');
$this->db->from('forum_category fc');
$this->db->join('forum f', 'f.forum_id = fc.category_forum_id', 'left');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach($query->result_array() AS $arrCategory)
{
$arrGroupedData[$arrCategory['forum_id']][] = $arrCategory;
}
return $arrGroupedData;
} else {
return false;
}
}
}
But in my opinion - you should probably split up your queries
1 query to get the forums and 1 query to get all categories from your forums
and stick it together in a tree structure
example for you
$forums = array();
$results = array(
array(
'forum_name' => 'general',
'category_name' => 'news1'
),
array(
'forum_name' => 'general',
'category_name' => 'news2'
),
array(
'forum_name' => 'latter',
'category_name' => 'news3'
),
);
foreach ($results as $result)
{
if(in_array($result['forum_name'],$forums))
{
$array = array($result['category_name']);
array_push($forums[$result['forum_name']],$array);
}
else
{
$forums[$result['forum_name']][] = array(
'forum_name' => $result['forum_name'],
'category_name' => $result['category_name']
);
}
}
echo '<pre>';
print_r($forums);
In this codeigniter model i have this query testing if value entered in input exists in database..
function get_search_form() {
$match = $this->input->post('search1');
$this->db->where('numero',$match);
$this->db->where('inscris','non');
$q = $this->db->get('transaction');
if($q->num_rows()>0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
Behold the controller i'd like to display value grabbed in input in view inscription.php
function search()
{
$data['row'] = $this->site_model->get_search_form();
$this->load->view('acceuil/aside');
$this->load->view('acceuil/inscription', $data);
}
My issue is how to display in that view input value and a form if this value exists in database ?
I have tried like this but i need help :
inscription view:
<?=form_open('navigation/search');?>
<input type="text" name="search1" id="search1" required />
<input type='submit' value='Display' />
<?=form_close();?>
I try to display the form like this but i don't know how to display as well the input value entered
<?php
if( $row > 0 )
{
?>
les champs du formulaire ici....
<?php
}else
{ }
?>
In your view you can check if the variable $row is not null (because it will be null when no rows are found):
if ($row !== null) {
// do stuff
}
You can modify the model function to return some other value if no rows are found, for example, by setting the $data to an empty array:
function get_search_form() {
$match = $this->input->post('search1');
$this->db->where('numero',$match);
$this->db->where('inscris','non');
$q = $this->db->get('transaction');
$data = array(); // <--- here
if($q->num_rows()>0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
And then in the view you can simply loop the data:
foreach($row as $r) {
// do stuff
}
or you can implode the array to use as the input value:
<input type="text"
name="search1"
id="search1" required
value="<?php echo implode(' ', $row); ?>" />
You could also use html entities here, in case double quotes are possible to appear in what your model function returns (I have no idea what it returns).
you can access $data global object like this check
<?php
if( isset($row))
{
foreach($row as $v){
//Do the operations here
}
}else
{
//Do the operations here
}
?>
My model is:
function get_all_pages(){
$query= $this->db->query("SELECT Title FROM news");
return $query->result_array();
}
My view is:
<table cellpadding="0" cellspacing="0">
<tr>
<th>Title</th>
</tr>
<?php
$count=1;
if(!empty($content)) {
foreach ($content as $content1)
{
?>
<tr>
<td align="left"><?php echo $content1['Title']; ?></td>
</tr>
<?php
}
} else{?>
<tr>
<td align="center" colspan="4"><?php echo "No Record Added Yet!!!";?></td>
</tr>
<?php }?>
</table>
My controller is:
public function manage_news()
{
$this->load->model('users_model');
$result_nb = $this->users_model->get_all_pages();
$data['content'] = $result_nb;
$data['page_title']="Manage Pages";
$this->load->view("manage_news",$data);
}
I am using codeigniter. my exact error is:
Call to undefined method users_model::get_all_pages()
how can i fix this error? i want the title row data to appear in a table (from the db)
Q: use Correct naming structure for CI? Which version do u use?
Filename
models/user_model.php
Examples (for demo purpose)
<?php
class User_model extends CI_Model {
// example one
function get_all_pages() {
$query = $this->db->query("SELECT Title FROM news")->result_array();
return ( is_array($query) && sizeof($query) > 0 ? $query : FALSE);
}
// example second
function get_all_pages_second() {
$data = FALSE;
$query = $this->db->query("SELECT Title FROM news");
if ($query->num_rows() > 0){
$data = $query->result_array();
}
return $data;
}
// example third
function get_all_pages_third() {
$data = FALSE;
$query = $this->db->select('Title')->get('news');
if ($query->num_rows() > 0){
$data = $query->result_array();
}
return $data;
}
}
I'm new to codeigniter and followed the guides but seem to be missing something. I have a simple database with customer records in it. My first goal in codeigniter is to simple list all my customers.
here is my controller:
public function index()
{
$this->load->model('HomeModel'); //loads the HomeModel model
$data = $this->HomeModel->function1(); //loads the function (function1) from the model
$this->load->view('index', $data);
}
Here is my model:
public function function1()
{
$query = $this->db->get('work_orders');
return $query->result();
}
here is my view:
<table class="table table-bordered table-striped table-highlight" id="invoice-details">
<thead>
<tr>
<th>Work Order ID</th>
<th>Status</th>
<th>Description</th>
</tr>
</thead>
<?php
foreach ($data->result() as $row);?>
<tr class="even gradeC">
<td><a href="/WorkOrders/viewWo/<?php echo $row['id']; ?>">
<?php echo $row['id']; ?></a></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo Logic\System\Lib\Helper::trunc(htmlentities($row['description']), 8); ?></td>
</tr>
<?php endforeach; ?>
</table>
Change on model :
public function function1()
{
$query = $this->db->get('work_orders');
//return $query->result();
return $query->result_array();
}
Change on controller :
public function index()
{
$this->load->model('HomeModel');
//$data = $this->HomeModel->function1();
$data['result'] = $this->HomeModel->function1();
$this->load->view('index', $data);
}
Change on view :
//foreach ($data->result() as $row);
if(is_array($result)&&!empty($result))
foreach ($result as $row);
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
Views : Codeigniter User Guide
And Since you are using array on your view like $row['id'], you got to use result_array on model to return result set array:
This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
Generating Query Results : Codeigniter
You can use $this->db->join();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
Produces:
SELECT * FROM blogs
JOIN comments ON comments.id = blogs.id
Go through active record class guide .
Active Record Class
In ur controller,u must pass the values as subarray of data array$data['result'].After which u can simply call it in view as $result .
Controller
$data['result'] = $this->HomeModel->function1();
And In view,
foreach ($result as $row){
<?php echo $row->id; ?>
..
}
I have this following code in my controller. I want to print the data i have my array.. should it be double forloop or foreach?
CONTROLLER:
public function index()
{
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
$cart['list'] = $in_cart;
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$this->load->view('layout/default', $data);
}
VIEW:
<?php if(is_array($list)): ?>
<?php foreach($list as $row):?>
<tr>
<td><?=$row->name?></td>
</tr>
<?php endforeach ?>
<?php endif;?>
but i have this following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: shop/cart.php
Line Number: 18
anyhelp guys? :(
I see you are using Code igniter (nice!)
I would do the following to ensure the $key is taking 'name'
public function index() {
foreach ($_SESSION['cartProducts'] as $key => $value) {
echo $key;
} }
Then
public function index() {
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$data['list'] = $in_cart;
$this->load->view('layout/default', $data); }
PHP is telling you exactly what is wrong.
You are trying to access the name property of the $row array when the -> syntax is specifically reserved for objects.
To access array values, you use square brackets and keys: <?=$row['name']?>
Helpful hint to understand - What you're doing is quasi-equivalent to: 7->name, insofar as the left value (7) has no idea how to use the arrow syntax. It's reserved for when the left value is an object. Not an integer, or in your case, an array.
Update after your comment:
You would get at the data like this:
<? foreach($row['list'] as $r):?>
<tr>
<td><?=$r[0]->name;?></td>
</tr>
<? endforeach;?>