student distribute in exam hall using codeigniter - php

I have three table for student first one is roll_no
second is center details
third is rooms details
$anil = array();
$exam_no_students = array();
$session_usr['user']= $this->session->userdata('user_name');
$total_students['total_stu']=$this->DBfunction->totalCountAll('roll_no');
$exam_centers['no_of_centers']= $this->DBfunction->count_centers();
$exam_centers1= $this->DBfunction->count_centers();
foreach ($exam_centers1 as $center_code => $center_details){
$center_code = array('center_id' =>$center_details->center_id);
$center_table='exam_center';
$center_full_details =$this->DBfunction->getArrayWhereResult($center_table,$center_code);
foreach ($center_full_details as $full => $full_de) {
$fullstudent = array('center_f_id' =>$full_de->center_id,
'center_f_name' =>$full_de->exam_center,
'center_f_addres' =>$full_de->exam_center_address ,
'center_f_city' =>$full_de->village ,
'center_f_count' =>$center_details->count);
}
array_push($exam_no_students, $fullstudent);
$center_f_details =$this->DBfunction->getArrayWhereResult($center_table,$center_code);
foreach ($center_f_details as $roomss => $rooms) {
$room_id = array('exam_center_id' =>$rooms->id);
$center_rooms =$this->DBfunction->getArrayWhereResult('exam_center_rooms',$room_id);
foreach ($center_rooms as $rooms_c => $roomsdetails) {
$center_with_rooms['anil'] = array('center_id'=>$rooms->center_id,
'center_name'=>$rooms->exam_center,
'center_main'=>$roomsdetails->exam_center_id,
'no_of_students'=>$center_details->count,
'room_no'=>$roomsdetails->room_no,
'no_of_seating'=>$roomsdetails->noofseating, 'noofrow'=>$roomsdetails->noofrow,
'noofcol'=>$roomsdetails->noofcol
);
}
}
I want to assign student into rooms for exam like below table
i am so confused to do this.

I got solution for above mentioned query using array_slice(). and get desired result.
public function Student_shift(){
$stateus='Approved';
$statuss = array('status' =>$stateus);
$table ='exam_center';
$get_centers=$this->DBfunction->getArrayWhereResult($table,$statuss);
$new_array = array();
foreach ($get_centers as $centers => $centers_name){
$i=1;
$centerid=$centers_name->center_id;
$main_id = $centers_name->id;
$center_where = array('center_id' =>$centerid);
$roll_table='roll_no';
$all_center_student = $this->DBfunction->getArrayWhereResult($roll_table,$center_where);
$all_center_student_count=count($all_center_student);//die;
$rooms_where = array('exam_center_id' =>$main_id);
$room_table= 'exam_center_rooms';
$rooms_details= $this->DBfunction->getArrayWhereResult($room_table, $rooms_where);
$cout = count($all_center_student);
$diff_diff=$all_center_student;
$start =0;
$shift=1;
$rooms_seating = 0;
while ($diff_diff){
$start =0;
$rooms_seating = 0;
$i=0;
ini_set('max_execution_time', 0);
ini_set('memory_limit','2048M');
foreach ($rooms_details as $roo => $rooms_ava){
$sliced_array = array_slice($diff_diff, $start, $rooms_ava->noofseating-2);
$start = $rooms_ava->noofseating-2;
foreach ($sliced_array as $key1 => $all) {
$students_with_rooms = array('enrollment' =>$all->enrollment,
'center_id' =>$all->center_id,
'roll_no' =>$all->roll_no,
'stu_class' =>$all->stu_class,
'room_no' =>$rooms_ava->room_no,
'shift_id'=>$shift
);
$roll_where = array('roll_no' => $all->roll_no);
$table='roll_no';
$data = array('room_no' => $rooms_ava->room_no,
'shift_id'=>$shift
);
$this->DBfunction->updateArrayWhereResult($table, $roll_where, $data);
array_push($new_array, $students_with_rooms);
}
$rooms_seating = $rooms_seating+$rooms_ava->noofseating-2;
$i++;
}
$diff_diff = array_slice($diff_diff, $rooms_seating);
$shift++;
}
}
redirect('Admin/abcd');
}

Related

creating JSON from while loop and foreach loop in php

I am revamping an application that is using PHP on the serverside which outputs JSON format.
{"by":"Industrie LLC","dead":false,"descendants":396,"id":"396","kids":[1,396],"score":396,"time":"396","title":"Industrie LLC","type":"comment","url":"www.nytimes.com"}
as it is i am getting the last column of mysql data.i know it is something with the loops but i have no idea what in specific.
My PHP code is here
$sql_metro_company_doc_legal = "SELECT * FROM ".$configValues['CONFIG_DB_TBL_PRE']."posts where post_type='company'";
$res_metro_company_doc_legal = $dbSocket->query($sql_metro_company_doc_legal);
while($row_metro_company_doc_legal = $res_metro_company_doc_legal->fetchRow()) {
$notice2[] = $row_metro_company_doc_legal[5];
$notice8[] = strtotime($row_metro_company_doc_legal[0]);
$notice9[] = $row_metro_company_doc_legal[0];
$notice3[] = $row_metro_company_doc_legal[0];
$notice = array("id" => "".$row_metro_company_doc_legal[1]."","title"=>"".$row_metro_company_doc_legal[0]."");
$notice10[] = $row_metro_company_doc_legal[0];
$notice6[] = $row_metro_company_doc_legal[0];
$notice11[] = $row_metro_company_doc_legal[5];
$notice7[] = strtotime($row_metro_company_doc_legal[2]);
$notice12[] = 'www.nytimes.com';
$notice7[] = "comment";
}
foreach ($notice2 as $status2) {
$_page['by'] = $status2;
}
foreach ($notice8 as $status8) {
$_page['dead'] = $status8;
}
foreach ($notice9 as $status9) {
$_page['descendants'] = (int)$status9;
}
foreach ($notice3 as $status3) {
$_page['id'] = $status3;
}
foreach ($notice as $status) {
$_page['kids'][] = (int)$status;
}
foreach ($notice10 as $status10) {
$_page['score'] = (int)$status10;
}
foreach ($notice6 as $status6) {
$_page['time'] = $status6;
}
foreach ($notice11 as $status11) {
$_page['title'] = $status11;
}
foreach ($notice7 as $status7) {
$_page['type'] = $status7;
}
foreach ($notice12 as $status12) {
$_page['url'] = $status12;
}
foreach ($notice4 as $status4) {
$_page['parent'] = (int)$status4;
}
foreach ($notice5 as $status5) {
$_page['text'] = $status5;
}
//sets the response format type
header("Content-Type: application/json");
//converts any PHP type to JSON string
echo json_encode($_page);
You need to make a 2-dimensional array in $_page.
$_page = array();
foreach ($notice2 as $i => $status) {
$_page[] = array(
'by' => $status,
'dead' => $status8[$i],
'descendants' => (int)$status9[$i],
'id' => $status3[$i],
// and so on for the rest
);
}
header ("Content-type: application/json");
echo json_encode($_page);

Using foreach in codeigniter view to generate select options

I ma using codeigniter to generate some html options,but i only get one result,first result from the table.
This is my controller
public function edit_event($id = 0){
$id = $this->uri->segment(3);
$current_id = $this->ion_auth->get_user_id();
$data['data'] = $this->as->the_event_edit($id);
$data['groups'] = $this->as->the_groups_list($current_id);
$this->load->view('editevent',$data);
}
This is my model
public function the_groups_list($current_id){
$query = $this->db->get_where('all_groups', array('group_owner' => $current_id));
foreach ($query->result() as $row)
{
$data = array(
'group_title' => $row->group_title,
'group_name' => $row->group_name,
'group_owner' => $row->group_owner
);
return $data;
}
}
This is the other model
public function as_event_edit($id){
$query = $this->db->get_where('all_scheduled_messages', array('id' => $id));
foreach ($query->result() as $row)
{
$data = array(
'as_title' => $row->as_title,
'as_event_name' => $row->as_event_name,
'as_owner' => $row->as_owner,
'as_type' => $row->as_type,
'as_target_dataset' => $row->as_target_dataset,
'as_timestamp' => $row->as_timestamp,
'as_time' => $row->as_time,
'as_day' => $row->as_day
);
return $data;
}
}
I am then using $groups['group_title'] in view and only the first group title gets displayed even though i have like 4 group titles from the other rows.
How can i return and pass an array that i can then to the view so as to use foreach to iterate and display the group titles?.
In your model you're not creating a multi-dimensional array. You need to add keys either using a counter:
$data = array();
$i=0;
foreach ($query->result() as $row){
$data[$i]['as_title'] = $row->as_title;
$data[$i]['as_event_name'] = $row->as_event_name;
$data[$i]['as_owner'] = $row->as_owner;
$data[$i]['as_type'] = $row->as_type;
$data[$i]['as_target_dataset'] = $row->as_target_dataset;
$data[$i]['as_timestamp'] = $row->as_timestamp;
$data[$i]['as_time'] = $row->as_time;
$data[$i]['as_day'] = $row->as_day;
$i++;
);
return $data;
or use the key of the incoming array
$data = array();
foreach ($query->result() as $id => $row){
$data[$id]['as_title'] = $row->as_title;
$data[$id]['as_event_name'] = $row->as_event_name;
$data[$id]['as_owner'] = $row->as_owner;
$data[$id]['as_type'] = $row->as_type;
$data[$id]['as_target_dataset'] = $row->as_target_dataset;
$data[$id]['as_timestamp'] = $row->as_timestamp;
$data[$id]['as_time'] = $row->as_time;
$data[$id]['as_day'] = $row->as_day;
);
return $data;
Change the model from $data = to $data[] = to insert each row and not only one.
In your view loop over the groups, like so:
foreach ($data as $group) {
echo "Title" . $groups['group_title'];
}
RTM: Adding Dynamic Data to the View
In the controller: rename your $data['data'] to $data['event']
$data['event'] = $this->as->the_event_edit($id);
$data['groups'] = $this->as->the_groups_list($current_id);
$this->load->view('editevent',$data);
In your view:
foreach ($event as $items) {
var_dump($items);
}
foreach ($groups as $group) {
var_dump($group);
}

Create array nested PHP

Hi all' I have a page into PHP where I retrieve XML data from a server and I want to store this data into an array.
This is my code:
foreach ($xml->DATA as $entry){
foreach ($entry->HOTEL_DATA as $entry2){
$id = (string)$entry2->attributes()->HOTEL_CODE;
$hotel_array2 = array();
$hotel_array2['id'] = $entry2->ID;
$hotel_array2['name'] = utf8_decode($entry2->HOTEL_NAME);
$i=0;
foreach($entry2->ROOM_DATA as $room){
$room_array = array();
$room_array['id'] = (string)$room->attributes()->CCHARGES_CODE;
$hotel_array2['rooms'][$i] = array($room_array);
$i++;
}
array_push($hotel_array, $hotel_array2);
}
}
In this mode I have the array hotel_array which all hotel with rooms.
The problem is that: into my XML I can have multiple hotel with same ID (the same hotel) with same information but different rooms.
If I have an hotel that I have already inserted into my hotel_array I don't want to insert a new array inside it but I only want to take its rooms array and insert into the exisiting hotel.
Example now my situation is that:
hotel_array{
[0]{
id = 1,
name = 'test'
rooms{
id = 1
}
}
[0]{
id = 2,
name = 'test2'
rooms{
id = 100
}
}
[0]{
id = 1,
name = 'test'
rooms{
id = 30
}
}
}
I'd like to have this result instead:
hotel_array{
[0]{
id = 1,
name = 'test'
rooms{
[0]{
id = 1
}
[1]{
id = 30
}
}
}
[0]{
id = 2,
name = 'test2'
rooms{
id = 100
}
}
}
How to create an array like this?
Thanks
first thing is it helps to keep the hotel id as the index on hotel_array when your creating it.
foreach ($xml->DATA as $entry){
foreach ($entry->HOTEL_DATA as $entry2){
$id = (string)$entry2->attributes()->HOTEL_CODE;
$hotel_array2 = array();
$hotel_array2['id'] = $entry2->ID;
$hotel_array2['name'] = utf8_decode($entry2->HOTEL_NAME);
$i=0;
foreach($entry2->ROOM_DATA as $room){
$room_array = array();
$room_array['id'] = (string)$room->attributes()->CCHARGES_CODE;
$hotel_array2['rooms'][$i] = array($room_array);
$i++;
}
if (!isset($hotel_array[$hotel_array2['id']])) {
$hotel_array[$hotel_array2['id']] = $hotel_array2;
} else {
$hotel_array[$hotel_array2['id']]['rooms'] = array_merge($hotel_array[$hotel_array2['id']]['rooms'], $hotel_array2['rooms']);
}
}
}
Whilst this is the similar answer to DevZer0 (+1), there is also quite a bit that can be done to simplify your workings... there is no need to use array_merge for one, or be specific about $i within your rooms array.
$hotels = array();
foreach ($xml->DATA as $entry){
foreach ($entry->HOTEL_DATA as $entry2){
$id = (string) $entry2->attributes()->HOTEL_CODE;
if ( empty($hotels[$id]) ) {
$hotels[$id] = array(
'id' => $id,
'name' => utf8_decode($entry2->HOTEL_NAME),
'rooms' => array(),
);
}
foreach($entry2->ROOM_DATA as $room){
$hotels[$id]['rooms'][] = array(
'id' => (string) $room->attributes()->CCHARGES_CODE;
);
}
}
}
Just in case it helps...
And this :)
$hotel_array = array();
foreach ($xml->DATA as $entry)
{
foreach ($entry->HOTEL_DATA as $entry2)
{
$hotel_code = (string) $entry2->attributes()->HOTEL_CODE;
if (false === isset($hotel_array[$hotel_code]))
{
$hotel = array(
'id' => $entry2->ID,
'code' => $hotel_code,
'name' => utf8_decode($entry2->HOTEL_NAME)
);
foreach($entry2->ROOM_DATA as $room)
{
$hotel['rooms'][] = array(
'id' => (string)$room->attributes()->CCHARGES_CODE,
);
}
$hotel_array[$hotel_code] = $hotel;
}
}
}

Combining 2 arrays into one

I got 2 arrays:
$ping_array = array();
$ping_array[] = '400';
$ping_array[] = '200';
$ping_array[] = '600';
$ping_array[] = '100';
$timestamp_array = array();
$timestamp_array[] = '2013-03-25 16:30:07';
$timestamp_array[] = '2013-03-25 16:30:39';
$timestamp_array[] = '2013-03-25 18:30:06';
$timestamp_array[] = '2013-03-25 18:45:49';
I want to make something like this (i dont know how its called):
$combined_array = array(
'time' => $timestamp_array,
'ping' => $ping_array
);
so later on i could use the 2 arrays combined like this:
foreach ($combined_array as $ca=> $values) {
echo $values['ping'];
echo $values['time'];
}
Thx guys this combine_array is amazing
Try this:
$combined_array = array();
foreach ($ping_array as $key => $value){
$combined_array[] = array(
'time' => $timestamp_array[$key],
'ping' => $value
);
}
What about this?
for ($i=0; $i<count($timestamp_array); $i++) {
$combined_array[$i]["ping"] = $ping_array[$i];
$combined_array[$i]["time"] = $timestamp_array[$i];
}
PHPs array_combine: "Creates an array by using one array for keys and another for its values"
$combined_array = array_combine($timestamp_array, $ping_array);
Then just repeat through similar to what you included:
foreach($combined_array as $time => $ping) {
echo $ping;
echo $time;
}
How about php 'array_merge_recursive'? It does exactly what you are looking for. It is found on php 4 and up.
What you could do is make array of objects, like this:
class PingInfo {
public $ping;
public $time;
public function PingInfo($ping, $time) {
$this->ping = $ping;
$this->time = $time;
}
}
$pingInfos = array();
$pingInfos[] = new PingInfo('400', '2013-03-25 16:30:07');
$pingInfos[] = new PingInfo('300', '2013-03-25 16:50:13');
You could build it from two array like this:
$pingInfos = array();
for ($i = 0; $i < count($ping_array); $i++)
$pingInfos[] = new PingInfo($ping_array[$i], $timestamp_array[$i]);
Now you can access it like this:
foreach ($pingInfos as $pingInfo) {
echo $pingInfo->ping;
echo $pingInfo->time;
}

dynamic variables from array

I store the field names within an array, in hopes to dynamically create the variables.
I receive a illegal offset type error for the if and else, these two lines:
$data[$tmp_field] = $tmp_field[$id];
$data[$tmp_field] = 0;
I checked the post data and it is posting with the appropriate data, but I am not sure what the problem is.
$student_id stores all the students ids., for example: $student_id = array(8,9,11,23,30,42,55);
function updateStudentInfo() {
$student_id = $this->input->post('student_id');
$internet_student = $this->input->post('internet_student');
$dismissed = $this->input->post('dismissed');
$non_matriculated_student = $this->input->post('non_matriculated_student');
$felony = $this->input->post('felony');
$probation = $this->input->post('probation');
$h_number = $this->input->post('h_number');
$office_direct_to = $this->input->post('office_direct_to');
$holds = $this->input->post('holds');
$fields = array('internet_student', 'non_matriculated_student', 'h_number', 'felony', 'probation', 'dismissed');
foreach($student_id as $id):
$data = array();
foreach($fields as $field_name):
$tmp_field = ${$field_name};
if(empty($tmp_field[$id])) {
$data[$tmp_field] = 0;
} else {
$data[$tmp_field] = $tmp_field[$id];
}
endforeach;
print '<pre style="color:#fff;">';
print_r($data);
print '</pre>';
endforeach;
}
This is the array format I desire:
Array
(
[internet_student] => 1
[non_matriculated_student] => 1
[h_number] => 0
[felony] => 0
[probation] => 1
[dismissed] => 0
)
Added screenshot to give you a visual of the form the data is being posted from
foreach($student_id as $id):
$data = array();
foreach($fields as $field_name):
$tmp_field = ${$field_name};
if(empty($tmp_field[$id])) {
$data[$field_name] = 0;
} else {
$data[$field_name] = $tmp_field[$id];
}
endforeach;
print '<pre style="color:#fff;">';
print_r($data);
print '</pre>';
endforeach;
I am assuming that all these fields are arrays, as otherwise you wouldn't need any loops.
function updateStudentInfo()
{
$student_id = $this->input->post('student_id');
$internet_student = $this->input->post('internet_student');
$dismissed = $this->input->post('dismissed');
$non_matriculated_student = $this->input->post('non_matriculated_student');
$felony = $this->input->post('felony');
$probation = $this->input->post('probation');
$h_number = $this->input->post('h_number');
$office_direct_to = $this->input->post('office_direct_to');
$holds = $this->input->post('holds');
$fields = array('internet_student', 'non_matriculated_student', 'h_number', 'felony', 'probation', 'dismissed');
$student_count = count($student_id);
foreach($student_id as $id)
{
$data = array();
foreach($fields as $field)
{
if(array_key_exists($id, $$field))
$data[$field] = ${$field}[$id];
}
}
}
You are trying to use the student id as an array key for the other fields but the HTML form is just a standard indexed array, not keyed to any student data.

Categories