message: undefined variable in view - codeigniter

I am trying to get the data in my view in codeigniter but it gives me a Message: Undefined variable: topics_array
here is my model :
public function studentTopics($courseId, $periodId)
{
$this->dbi->select('topicitem.TopicItemID, topicItem.Content');
$this->dbi->from('topicitem, topics, periods');
$this->dbi->where('topics.PeriodItemID', $periodId)
->where('topics.TopicItemID = topicitem.TopicItemID')
->where('topics.PeriodItemID = periods.PeriodItemID')
->where('periods.CourseID', $courseId);
$res = $this->dbi->result_array();
return $res;
}
here is my controller
public function student_topic($courseId, $periodId)
{
$this->load->library('table');
$this->load->model('Student_model');
$data['topics_array'] = $this->Student_model->studentTopics($courseId, $periodId);
$this->load->view('Student/StudentView', $data);
}
and my view
<?php
$courseID = $this->uri->segment(4);
$periodID = 1;
foreach ($topics_array as $key =>$value) {
echo "<tr>
<td>".$value['Content']. "</td>
</tr>"; }?>

You seem to be missing a get().
$res = $this->dbi->get()->result_array();
return $res;

Hope this will help you :
change this line of code : $res = $this->dbi->result_array();
$this->dbi->select('topicitem.TopicItemID, topicItem.Content');
$this->dbi->from('topicitem, topics, periods');
$this->dbi->where('topics.PeriodItemID', $periodId)
->where('topics.TopicItemID = topicitem.TopicItemID')
->where('topics.PeriodItemID = periods.PeriodItemID')
->where('periods.CourseID', $courseId);
$query = $this->dbi->get(); // missing this line of code
$res = $query->result_array();
for more : https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data

Related

Call to a member function setPath() on a non-object in laravel

In my Laravel web application, getting error like "Call to a member function setPath() on a non-object" while using pagination.
My code is
In model,
public function scopegetApartments()
{
$list1 = DB::table('properties')->orderBy('id','DESC')->paginate(1);
$list = array();
foreach($list1 as $listeach){
$imgs = DB::table('property_images')->where('property_id',$listeach->id)->get();;
$images = array();
foreach($imgs as $img){
array_push($images, $img->image);
}
$lists = array(
'id'=>$listeach->id,
'name'=>$listeach->name,
'rent'=>$listeach->rent,
'shortdescription'=>$listeach->shortdescription,
'images'=>$images,
);
array_push($list, $lists);
}
if(count($list)>0){
return $list;
}else{
return '[]';
}
}
In view,
$apartmentlist->setPath('serviceapartments');
echo $apartmentlist->render();
How to resolve this problem?

Severity Notice: Undefined offset

With some changes I got the error, This work fine in raw php but in codeigniter I'm trying to get this result_array in a while(list), but I don't know what would be the alternative for fetch_row in codeigniter. Please help me to get this in a way at while (list($d, $sn, $s) = each($IDS-)) line.
Model:
public function raw_attendance_Data() {
$this->db->select('attnDate, TraineeID, Status');
$this->db->from('tbl_attendance');
$this->db->order_by('TraineeID');
$query = $this->db->get();
return $query->result_array();
}
Controller:
public function rawAttendance(){
$data['IDS'] = $this->AttendanceModel->raw_attendance_Data();
$this->load->view('attendance/rawAttendance', $data);
}
View:
$curname='';
$tdata = '';
reset($IDS);
while (list($d, $sn, $s) = each($IDS)) {
if ($curname != $sn) {
if ($curname) {
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
}
$rowdata = $emptyRow;
$curname = $sn;
}
$rowdata[$d] = $s;
}
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
$query->result_array(); returns an array and not an object. Thus the $IDS in $IDS->fetch_row() is an array (a non-object).
I don't understand, where you are getting the function fetch_row()?
If I understand what you are trying to accomplish then this should work.
View:
$tdata = '';
foreach($IDS as $row)
{
$curname = isset($row['TraineeID']) ? $row['TraineeID'] : "";
$date = isset($row['attnDate']) ? $row['attnDate'] : "";
$status = isset($row['Status']) ? $row['Status'] : "";
if(!empty($curname){
$tdata .= "<tr><td>$curname</td><td>$date</td><td>$status</td></tr>";
}
}

Laravel Passing Variable from a controller to another Controller

I'm trying to pass a variable from a controller to another controller I tried using
Redirect::to('dashboard/'.$ssid.'/')->with(compact('wname'))
but does not work any idea how can I achieve this?
here is my code
Route
Route::get('dashboard/{ssid}/', 'HomeController#showDash');
LoginController
public function post_index()
{
if(Auth::attempt($credentials)){
$users = User::where('username','=',$email)->get();
foreach ($users as $value):
$activated = $value['a_status'];
$wname = $value['wholename'];
endforeach;
if($activated == 1):
$red= Redirect::to('dashboard/'.$ssid.'/')->with(compact('wholename'));
else:
$red= View::make('login');
endif;
return $red;
}
}
HomeController
public function showDash($ssid,$wholename)
{
foreach ($wholename as $userVal):
$fn = $userVal['firstname'];
$ln = $userVal['lastname'];
endforeach;
return View::make('dashboard')->with(compact('fn'));
}
The error I'm having is that Missing argument 2 for HomeController::showDash() as per laravel's debugger..
Updated answer based on the comments:
public function post_index()
{
if(Auth::attempt($credentials)){
$users = User::where('username','=',$email)->get();
foreach ($users as $value){
$activated = $value['a_status'];
$wname = $value['wholename'];
}
if($activated == 1) {
Redirect::to('dashboard/'.$ssid.'/')->with(['wholename' => $wholename]);
}
return View::make('login');
}
public function showDash($ssid)
{
$wholename = (Session::has('wholename')) ? Session::get('wholename') : [];
foreach ($wholename as $userVal) {
$fn = $userVal['firstname'];
$ln = $userVal['lastname'];
}
return View::make('dashboard')->with(compact('fn'));
}
Everything else can stay as is.
Update: fixed erroneous space in 'whole name' (autocorrect did that, sorry).

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

codeigniter how to show data in functions

My model:
function get_data($id)
{
$this->db->select('id, Company, JobTitle');
$this->db->from('client_list');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result();
}
I want to get the the data from get_data(), is this the right way?
public function show_data( $id )
{
$data = $this->get_data($id);
echo '<tr>';
echo '<td>'.$data['Company'].'</td>';
echo '<td>'.$data['JobTitle'].'</td>';
echo '<td>'.$data['id'].'</td>';
echo '<td></td>';
echo '</tr>';
}
Use the row_array() function to get the data in the array format.
Reference url
http://ellislab.com/codeigniter/user-guide/database/results.html
you can use foreach loop to print
foreach ($data->result() as $row)
{
echo '<tr>';
echo '<td>'.$row['Company'].'</td>';
echo '<td>'.$row['JobTitle'].'</td>';
echo '<td>'.$row['id'].'</td>';
echo '<td></td>';
echo '</tr>';
}
thank you
just to improve answer, I use "general_model" for all my controllers, there are some exceptions where I need special queries, I just create desired model so "general_model" stays same and I can use it in any project.
e.g.
general_model.php
function _getWhere($table = 'table', $select = 'id, name', $where = array()) {
$this->db->select($select);
$q = $this->db->get_where('`'.$table.'`', $where);
return ($q->num_rows() > 0) ? $q->result() : FALSE;
}
.
.
.
//bunch of another functions
in controller I just call
$this->data['books'] = $this->general_model->_getWhere('book', '*', array('active' => '1'));
$this->render('book_list_view'); // $this->load->view('book_list_view', $this->data);
sidenote: I am extending CI_Controller therefore I use $this->data['books'] instead $data['books'] to pass data into view
in view
//check if there are any data
if ($books === FALSE) {
//some error that there are no books yet
} else {
//load data to table or something
foreach ($books as $book) {
$book->id; // book id
}
}

Resources