Click here to Skip to main content
15,063,287 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
home.php
PHP
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {


	public function index()
	{
		$this->load->view('home');
	}
	
	public function course() {
		$course = $this->db->get('course');
		$data['records'] = $course->result_array();
		$this->load->view('course/course', $data);
	}
	
	public function course_add_view() {
		$this->load->view('course/course-add');
	}
	
	public function course_edit_view() {
		$id = $this->uri->segment('3');
		$this->db->where('id', $id);
		$course = $this->db->get('course');
		$data['records'] = $course->result_array();
		$this->load->view('course/course-edit', $data);
	}
	
	public function course_delete() {
		$id = $this->uri->segment('3');
		$this->load->model("course");
		$check = $this->course->delete($id);
		if($check) {
			redirect("home/course");
		} else {
			echo "Record Failed";
		}
	}
	
	public function course_add() {
		/*echo "<pre>";
		print_r($_POST);*/
		$data = $this->input->post();
		unset($data['submit']);
		//print_r($data);
		$this->load->model("course");
		$check = $this->course->insert($data);
		if($check) {
			redirect("home/course");
		} else {
			echo "Record Failed";
		}

	}
	
	public function course_edit() {
		/*echo "<pre>";
		print_r($_POST);*/
		$data = $this->input->post();
		$id = $this->input->post('id');
		unset($data['submit']);
		//print_r($data);
		$this->load->model("course");
		$check = $this->course->update($data, $id);
		if($check) {
			redirect("home/course");
		} else {
			echo "Record Failed";
		}

	}
	
	//students
	
	public function student() {
		$course = $this->db->query("select student.id, student.surname, student.name, student.city, student.gender, student.bdate, course.sname from student, course where 'student'"."'course-id' = course.id");
		$data['records'] = $course->result_array();
		$this->load->view('student/student', $data);
	}
	
}

student.php
PHP
<?php
defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>

	<meta charset="utf-8">
	<title>Welcome to CodeIgniter

	<div class="container">
	<h2>Student</h2>
		<div class="table-responsive">
        	
            	                	<?php
					foreach($records as $row) {
						echo "";
                    	echo "";
                        echo "";
                        echo "";
                        echo "";
						if($row['gender']=="M") {
							$row['gender'] = "Male";
						} else {
							$row['gender'] = "Female";
						}
						echo "";
						echo "";
						echo "";
						echo "";
						echo "";
                    	echo "";
					}
				?>
                <table class="table"><thead>                	<tr>                    	<th>#</th>                        <th>Course</th>                        <th>Name</th>                        <th>Surname</th>                        <th>Gender</th>                        <th>City</th>                        <th>Birth Date</th>                    </tr>                </thead>                <tbody><tr><td>".$row['id']."</td><td>".$row['sname']."</td><td>".$row['name']."</td><td>".$row['surname']."</td><td>".$row['gender']."</td><td>".$row['city']."</td><td>".$row['bdate']."</td><td><a href="".base_url()."index.php/home/course_delete/".$row[">Delete</a></td><td><a href="".base_url()."index.php/home/course_edit_view/".$row[">Edit</a></td></tr></tbody>            </table>
		</div>
	</div>

no error come but its not shown database data....only header shown like this....
Student
# Course Name Surname Gender City Birth Date

What I have tried:

no error come but its not shown database data....only header shown like this....
Student
# Course Name Surname Gender City Birth Date
Posted
Updated 24-Apr-18 2:28am
v3

1 solution

You have to move the output of the table cells into the foreach loop and print them there using echo.

Something like (untested):
HTML
<table class="table"><thead><tr>
<th>#</th><th>Course</th><th>Name</th><th>Surname</th><th>Gender</th><th>City</th><th>Birth Date</th>
</tr></thead><tbody>
<?php foreach ( $records as $row ):?>
    <tr>
    <td><?php echo $row['id'];?></td>
    <td><?php echo $row['sname'];?></td>
    <!-- ... -->
    </tr>
<?php endforeach;?>
</tbody></table>
See also Views — CodeIgniter 3.1.8 documentation[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900