Saving reservations using available 2 halls - php

Below is the function for saving some reservation details to the database. get_hall() query returns 2 halls(haal1 and hall2). bus when going to the database only hall1 is inserting. I want to insert hall1 next reservation hall2. How to improve below code.
function save_reserve_detail($date,$time,$flag){
$halls = $this->halls_model->get_hall();
foreach ($halls AS $each) {
$hall = $each['hall_id'];
break;
}
$reference_no = date("YmdHis");
if ($flag == 1) {
$hall_id = $hall;
$type = 1;
$date = $date;
} else if ($flag == 2) {
$hall_id = $hall;
$type = 0;
$date = $date;
}
$data = array(
'reference_no' => $reference_no,
'date' => $date,
'type' => $type,
'hall' => $hall
);
$result = $this->db->insert('reserving_details', $data);
if ($result) {
return $reference_no;
} else {
return 0;
}
}

Remove the break from foreach . now the $hall contains an array of 2 values. now use a for loop to insert the value into the table.
<?php
function save_reserve_detail($date,$time,$flag){
$halls = $this->halls_model->get_hall();
foreach ($halls AS $each) {
$hall = $each['hall_id'];
// break;
}
$reference_no = date("YmdHis");
if ($flag == 1) {
$hall_id = $hall;
$type = 1;
$date = $date;
} else if ($flag == 2) {
$hall_id = $hall;
$type = 0;
$date = $date;
}
for($i=0;$i<sizeof($hall);$i++){
$data = array(
'reference_no' => $reference_no,
'date' => $date,
'type' => $type,
'hall' => $hall[$i]
);
$result = $this->db->insert('reserving_details', $data);
if ($result) {
return $reference_no;
} else {
return 0;
}
}
}
?>
Even you can alter your insert('reserving_details', $data); code which can handle array insertion.

Related

An uncaught Exception was encountered Type: Error Message: Class 'BaseController' not found

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class index extends BaseController
{
public function __construct()
{
parent::__construct();
$this->load->model('P_pmb', 'p_pmb');
}
public function index()
{
$data['title'] = 'Dashboard';
$this->render('index/index', $data);
}
public function pendaftarprodi1()
{
$data['title'] = 'Grafik Berdasarkan Prodi 1';
$prodi = $this->p_pmb->listProdi();
foreach ($prodi as $key => $p) {
$prodi[$key]['jumlah'] = $this->p_pmb->jumlahPendaftarProdi1($p['id_prodi']);
$prodi[$key]['jumlah2'] = $this->p_pmb->jumlahPendaftarProdi2($p['id_prodi']);
$prodi[$key]['size'] = rand(10, 30);
}
//grafik 1
$result = null;
foreach ($prodi as $p => $prod) {
// if ($prod['jumlah'] > $sum) {
// $sum = $prod['jumlah'];
// $sliced = true;
// $selected = true;
// }
$result[$p] = [
"name" => $prod['nama_prodi'],
"jumlah" => $prod['jumlah'],
"y" => $prod['size'],
// "sliced" => $sliced,
// 'selected' => $selected
];
}
$data['pendaftar'] = $prodi;
$data['grafik1'] = json_encode($result);
$this->render('index/grafik1', $data);
}
public function pendaftarprodi2()
{
$data['title'] = 'Grafik Berdasarkan Prodi 2';
$prodi = $this->p_pmb->listProdi();
foreach ($prodi as $key => $p) {
$prodi[$key]['jumlah'] = $this->p_pmb->jumlahPendaftarProdi1($p['id_prodi']);
$prodi[$key]['jumlah2'] = $this->p_pmb->jumlahPendaftarProdi2($p['id_prodi']);
$prodi[$key]['size'] = rand(10, 30);
}
//grafik 2
$hasil = null;
foreach ($prodi as $p => $prod) {
$hasil[$p] = [
"name" => $prod['nama_prodi'],
"jumlah" => $prod['jumlah2'],
"y" => $prod['size'],
// "sliced" => $sliced,
// 'selected' => $selected
];
}
$data['pendaftar'] = $prodi;
$data['grafik2'] = json_encode($hasil);
$this->render('index/grafik2', $data);
}
public function pendaftarprestasi()
{
$pendaftarprestasi = $this->p_pmb->listpendaftarprestasi();
foreach ($pendaftarprestasi as $key => $p) {
$pendaftarprestasi[$key]['jumlah'] = $this->p_pmb->jumlahpendaftarprestasi($p['tingkat_prestasi']);
$pendaftarprestasi[$key]['size'] = rand(10, 30);
}
$result = null;
$sum = 0;
foreach ($pendaftarprestasi as $p => $pendpres) {
//if ($pendpres['jumlah'] > $sum) {
// $sum = $pendpres['jumlah'];
// $sliced = true;
// $selected = true;
// }
$result[$p] = [
"name" => $pendpres['tingkat_prestasi'],
'jumlah' => $pendpres['jumlah'],
'y' => $pendpres['size'],
// $sliced => $sliced,
// $selected => $selected
];
}
$data['pendaftarprestasi'] = $pendaftarprestasi;
$data['grafikprestasi'] = json_encode($result);
$this->render('index/grafikprestasi', $data);
}
public function jalurmasuk()
{
$pendaftar = $this->p_pmb->listjalurpendaftar();
foreach ($pendaftar as $key => $p) {
$pendaftar[$key]['jumlah'] = $this->p_pmb->jumlahjalurpendaftar($p['nama_jalur']);
$pendaftar[$key]['jumlah'] = rand(10, 30);
}
$result = null;
$sum = 0;
foreach ($pendaftar as $p => $jalmask) {
//if ($jalmask['jumlah'] > $sum) {
// $sum = $jalmask['jumlah'];
// $sliced = true;
// $selected = true;
// }
$result[$p] = [
"name" => $jalmask['nama_jalur'],
'jumlah' => $jalmask['jumlah'],
'y' => $jalmask['jumlah'],
// $sliced => $sliced,
// $selected => $selected
];
}
$data['jalur_masuk'] = $jalmask;
$data['grafikmasuk'] = json_encode($result);
$this->render('index/grafikmasuk', $data);
}
public function pendapatan()
{
$data['title'] = 'Grafik Pendapatan Berdasarkan Bank';
$bank = $this->p_pmb->listBank();
$pendaftar = $this->p_pmb->pendaftarBank();
$categories = null;
$lunas = null;
$belum_lunas = null;
$sumTotal = 0;
foreach ($bank as $i => $b) {
$categories[] = $b['bank'];
foreach ($pendaftar as $key => $value) {
if ($b['id_bank'] == $value['id_bank']) {
if ($value['is_bayar'] == '1') {
$sumTotal += intval($value['total']);
$lunas[] = intval($value['total']);
} else {
$belum_lunas[] = intval($value['total']);
}
}
}
}
$result[] = [
'name' => 'Pendapatan',
'data' => $lunas,
];
$data['subtitle'] = 'Total Pendapatan Rp.' . $sumTotal;
$grafik['id_bank'] = json_encode($result);
$grafik['categories'] = json_encode($categories);
$data['grafikpendapatan'] = $grafik;
$this->render('index/grafikpendapatan', $data);
}
public function bank()
{
$data['title'] = 'Grafik Pendapatan Berdasarkan Bank';
$bank = $this->p_pmb->listBank();
$pendaftar = $this->p_pmb->pendaftarBank();
$categories = null;
$lunas = null;
$belum_lunas = null;
$sumTotal = 0;
foreach ($bank as $i => $b) {
$categories[] = $b['bank'];
foreach ($pendaftar as $key => $value) {
if ($b['id_bank'] == $value['id_bank']) {
if ($value['is_bayar'] == '1') {
$sumTotal += intval($value['total']);
$lunas[] = intval($value['total']);
} else {
$sumTotal += $value['jumlah'];
$belum_lunas[] = intval($value['total']);
}
}
}
}
$result[] = [
'name' => 'Lunas',
'data' => $lunas,
];
$result[] = [
'name' => 'Belum Lunas',
'data' => $belum_lunas,
];
$data['subtitle'] = 'Total Pendaftar: ' . $sumTotal;
$data['id_bank'] = $bank;
$data['grafikbank'] = json_encode($result);
$this->render('index/grafikbank', $data);
}
}
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class BaseController extends CI_Controller
{
public function render($filename, $data)
{
$this->load->view('template/app_top', $data);
$this->load->view('template/template_scripts', $data);
$this->load->view($filename, $data);
$this->load->view('template/app_bottom', $data);
}
}

Build grid rows based on columns widths array PHP

I have an array of column widths + content that I need to process and build row based on them.
$content = array(
'100',
'80',
'25',
'25',
'25',
'25',
'50',
'50',
'-1',
'33.333333333333',
'33.333333333333',
'33.333333333333',
'50',
'50',
'100',
'16.666666666667',
'-1'
);
-1 means that this is not a column , but a text or shortcode and it should not be wrapped in to row.
from the above array desired processed array should be
$content = array(
'[row]100[/row]',
'[row]80[/row]',
'[row]25',
'25',
'25',
'25[/row]',
'[row]50',
'50[/row]',
'-1',
'[row]33.333333333333',
'33.333333333333',
'33.333333333333[/row]',
'[row]50',
'50[/row]',
'[row]100[/row]',
'[row]16.666666666667[/row]',
'-1'
);
I have tried a loop with a starting accumulator 0 that I was adding width from loop but is simply buggy.
Any help is appreciated.
Map your array.
$start = [
'100',
'80',
'25',
// ...removed for brevity
];
function process(array $data) {
$output = array_map(function ($row, $index) use ($data) {
if ($row == '-1') {
return '-1';
}
$value = $row;
$previousIndex = $index - 1;
$nextIndex = $index + 1;
// Check previous row
if (
// There's a row and it's different
(isset($data[$previousIndex]) && $data[$previousIndex] != $row)
||
// There's no row, we're on the beggining of the list
!isset($data[$previousIndex])
) {
$value = '[row]' . $value;
}
// Check next row
if (
// There is a next row and it's different
(isset($data[$nextIndex]) && $data[$nextIndex] != $row)
||
// There's no next row, we're on the end of the list
!isset($data[$nextIndex])
) {
$value = $value . '[/row]';
}
return $value;
}, $data, array_keys($data));
return $output;
}
$sum = 100;
$new_content = array();
$prev = false;
for($i=0;$i<count($content);$i++){
$num = $content[$i];
$diff = $sum-$num;
echo $num.'=>'.$diff.',';
if(!$prev && $diff==0){
$new_content[] = "[row]".$content[$i]."[/row]";
}
if($diff==0 && $prev){
$new_content[] = $content[$i]."[/row]";
$sum=100;
$prev = false;
}
if($diff>0 && $diff<100 && $prev && $content[$i]!=-1){
$new_content[] = $content[$i];
$sum = $diff;
$prev = true;
}
if($diff>0 && $diff<100 && !$prev){
$new_content[] = "[row]".$content[$i];
$sum = $diff;
$prev = true;
}
if($diff<0){
$new_content[] = "[/row][row]".$content[$i];
$sum = 100-$content[$i];
$prev = true;
}
if($content[$i]==-1 && $prev){
$new_content[] = "[/row]".$content[$i];
}else if($content[$i]==-1){
$new_content[] = $content[$i];
}
}
print_r($new_content);

how can i create and pass value in multidimensional array in php?

I need to save some data's in php. I am trying this code
$click_free = [];
$ano = 0;
while($res_qry = mysql_fetch_array($sel_qry)){
$arr_cnt = sizeof($click_free);
if($arr_cnt != 0){
$m = 0;
for($k=0;$k<$arr_cnt;$k++)
{
if($click_free[$k][0] == $res_qry['Free_id'])
{
$click_free[$k][0] = ($click_free[$k][0]+$res_qry['Qty']);
$m=1;
}
}if($m==0){
$click_free[$arr_cnt] = [1,];
}
}else{
$click_free[0] = [$res_qry['Free_id'],$res_qry['Qty']];
}
}
But this code has many error. Please help me solve this
while($res_qry = mysql_fetch_array($sel_qry)){
$id_existed = false;
if(!empty($click_free))
foreach($click_free as $key=>$ligne)
if( $ligne[0] == $res_qry['Free_id']){
$qty = $ligne[1] + $res_qry['Qty'];
$click_free[$key] = [ $ligne[0] , $qty ];
$id_existed =true;
}
if(!$id_existed){
$click_free[] = [ $res_qry['Free_id'] , $res_qry['Qty'] ];
}
}
Finally we got a solution
while($res_qry = mysql_fetch_array($sel_qry)){
$arr_cnt = sizeof($click_free);
if($arr_cnt != 0){
$m = 0;
foreach ($click_free as $k => $v) {
if ($v['Free_id']==$res_qry['Free_id']) {
S_qty=$v['Qty']+$res_qry['Qty'];
$click_free[$k]['Qty']=$S_qty;
$m=1;
}
}
if($m==0){
$click_free[] = array("Free_id" => $res_qry['Free_id'],"Qty"=>$res_qry['Qty']);
}
}else{
$click_free[] = array("Free_id" => $res_qry['Free_id'],"Qty"=>$res_qry['Qty']);
}
}
Thanks to bfahmi, Kris Roofe, B. Desai , user3542450, dr_debug

Add condition to PHP Function

The function below is from the SmartJobBoard script and its main job is to send an email notification whenever a job seeker applies to a job.
What I want is to put a condition that will make it run depending on whether an $application_email is present. If no $application_email is present, then the function should not run.
public static function sendApplyNow($info, $file = '', $data_resume = array(), $current_user_sid = false, $notRegisterUserData = false, $score = false)
{
if ($current_user_sid) {
$user_info = SJB_UserManager::getUserInfoBySID($current_user_sid);
$sender_email_address = $user_info['email'];
} else {
$sender_email_address = $notRegisterUserData['email'];
}
$application_email = SJB_Applications::getApplicationEmailbyListingId($info['listing']['id']);
$email_address = !empty($application_email) ? $application_email : $info['listing']['user']['email'];
$questionnaire = !empty($info['submitted_data']['questionnaire'])?unserialize($info['submitted_data']['questionnaire']):'';
$questionnaireInfo = array();
if ($questionnaire) {
$listingInfo = SJB_ListingManager::getListingInfoBySID($info['listing']['id']);
$questSID = isset($listingInfo['screening_questionnaire'])?$listingInfo['screening_questionnaire']:0;
$questionnaireInfo = SJB_ScreeningQuestionnaires::getInfoBySID($questSID);
$passing_score = 0;
switch ($questionnaireInfo['passing_score']) {
case 'acceptable':
$passing_score = 1;
break;
case 'good':
$passing_score = 2;
break;
case 'very_good':
$passing_score = 3;
break;
case 'excellent':
$passing_score = 4;
break;
}
if ($score >= $passing_score) {
$questionnaireInfo['passing_score'] = 'Passed';
}
else {
$questionnaireInfo['passing_score'] = 'Not passed';
}
}
if (!empty($info['listing']['subuser']['sid'])) {
$subUserInfo = SJB_UserManager::getUserInfoBySID($info['listing']['subuser']['sid']);
if (!empty($subUserInfo)) {
$email_address = $subUserInfo['email'];
}
}
$data = array(
'user' => SJB_Array::getPath($info, 'listing/user'),
'listing' => $info['listing'],
'applicant_request' => $info['submitted_data'],
'data_resume' => $data_resume,
'questionnaire' => $questionnaire,
'score' => $score,
'questionnaire_info' => $questionnaireInfo);
$email = SJB_EmailTemplateEditor::getEmail($email_address, self::SEND_APPLY_NOW_SID, $data);
$email->setReplyTo($sender_email_address);
if ($file != '') {
$email->setFile($file);
}
return $email->send('Apply Now');
}
Appreciate your help.
Thanks.
<?php
$application_email = SJB_Applications::getApplicationEmailbyListingId($info['listing']['id']);
if(empty($application_email)) return; # New line
$email_address = !empty($application_email) ? $application_email : $info['listing']['user']['email'];

Why isn't my sorting algorithm working?

I'm developing a system for a client that creates a csv of packing labels which is sent to a printer. The client has six different items. Customers order products in bulk from my client. Two items (product A and product B) share the same packing line. In order to make packing more efficient my client wants to alternate between packing product A and packing product B first.
For example, if John, Sally, and James all ordered both products, the system needs to write John's orders to the csv starting with product A, Sally's orders starting with product B, and James' orders starting with product A again.
I've pasted my non-working code below, but this is really screwing with my head and I'm having a really tough time with it.
foreach($orders as $order) {
$name = null;
$phone = null;
$account = DAO_ContactPerson::get($order->account_id);
$delivery = false;
if($account->is_agency) {
$name = $order->getAttribute('name');
$phone = $order->getAttribute('phone');
} else {
$name = sprintf("%s %s",
$account->getPrimaryAddress()->first_name,
$account->getPrimaryAddress()->last_name
);
$phone = $account->phone;
}
$name = trim($name);
$phone = trim($phone);
$items = $order->getItems();
if($order->getAttribute('delivery')) {
$type = 'deliveries';
$destination = 'Delivery';
$address = sprintf("%s %s %s",
$order->getAttribute('delivery_address.line1'),
$order->getAttribute('delivery_address.line2'),
$order->getAttribute('delivery_address.postal')
);
} else {
$type = 'pickups';
$agency = DAO_ContactPerson::getAgency($order->getAttribute('pickup'));
$destination = $agency->name;
// Override account id so orders are grouped by agency
$order->account_id = $agency->id;
$address = null;
}
// var_dump($order->id);
// Init account array
if(!isset($rows[$type][$order->account_id]))
$rows[$type][$order->account_id] = array('combined' => array(), 'separate' => array());
foreach($items as $item) {
$packing = 'separated';
if($item->product_id == 3 || $item->product_id == 4)
$packing = 'combined';
if(!isset($rows[$type][$order->account_id][$packing][$item->product_id]))
$rows[$type][$order->account_id][$packing][$item->product_id] = array();
$i = 0;
while($i < $item->quantity) {
$rows[$type][$order->account_id][$packing][$item->product_id][] = array(
'number' => $order->id,
'destination' => $destination,
'size' => $item->product_id,
'name' => $name,
'address' => $address,
'phone' => $phone
);
$i++;
}
}
// if($order->id == 176) {
// var_dump($rows[$type][$order->account_id][$packing]);
// }
}
$this->weight = 1;
$pickups = count($rows['pickups']);
for($i = 0; $i < $pickups; $i++) {
$account =& $rows['pickups'][$i];
$account['output'] = array();
if(isset($account['combined'])) {
$combined_products =& $account['combined'];
if(!empty($combined_products)) {
foreach($combined_products as $prod_id => $combined) {
usort($combined_products[$prod_id], array($this, "_compareBoxes"));
}
// Flip weights once we finish with this account
$last_box = end($combined_products);
$last_box = array_pop($last_box);
reset($combined_products);
if($this->weight == 1) {
$this->weight = -1;
if($last_box['size'] == 3) {
asort($combined_products);
}
} else {
if($last_box['size'] == 4) {
arsort($combined_products);
}
$this->weight = 1;
}
foreach($combined_products as $combined) {
$account['output'][] = $combined;
}
foreach($account['separated'] as $separate) {
$account['output'][] = $separate;
}
}
} else {
if(isset($account['separated']))
$account['output'] = $account['separated'];
}
}
$deliveries = count($rows['deliveries']);
for($i = 0; $i < $deliveries; $i++) {
$account =& $rows['deliveries'][$i];
$account['output'] = array();
if(isset($account['combined'])) {
$combined_products =& $account['combined'];
if(!empty($combined_products)) {
foreach($combined_products as $prod_id => $combined) {
usort($combined_products[$prod_id], array($this, "_compareBoxes"));
}
// Flip weights once we finish with this account
$last_box = end($combined_products);
$last_box = array_pop($last_box);
reset($combined_products);
if($this->weight == 1) {
$this->weight = -1;
if($last_box['size'] == 3) {
asort($combined_products);
}
} else {
if($last_box['size'] == 4) {
arsort($combined_products);
}
$this->weight = 1;
}
foreach($combined_products as $combined) {
$account['output'][] = $combined;
}
foreach($account['separated'] as $separate) {
$account['output'][] = $separate;
}
}
} else {
if(isset($account['separated']))
$account['output'] = $account['separated'];
}
}
$rows['output'] = $rows['pickups'];
array_push($rows['output'], $rows['deliveries']);
$output = '';
foreach($rows['output'] as $account_id => $boxes) {
if(!empty($boxes['output'])) {
foreach($boxes['output'] as $labels) {
if(!empty($labels)) {
foreach($labels as $label) {
$output .= implode(',', $label) . "<br>";
}
}
}
}
}
The _compareBoxes method looks like this:
private function _compareBoxes($a, $b) {
if($a['size'] == $b['size']) {
return 0;
}
if($this->weight == 1) {
// Medium first, then Large
return ($a['size'] < $b['size']) ? -1 : 1;
}
if($this->weight == -1) {
// Large first, then Medium
return ($a['size'] > $b['size']) ? -1 : 1;
}
}

Categories