i want to show all sparepart
where tb_sparepart.id_jenis = id_jenis and where tb_sparepart.id_merk = id_merk, and than where tb_sparepart.id_merk = 1
id_merk 1 = is mean all merk, but if i try in model is always fail.
i try this model:
function GetById($id_kendaraan){
$this->db->select('*');
$this->db->from('tb_kendaraan');
$this->db->where('tb_kendaraan.id_kendaraan',$id_kendaraan);
$query_kendaraan = $this->db->get();
$hasil = false;
if($query_kendaraan->num_rows() > 0)
{
$data_kendaraan = $query_kendaraan->row();
$hasil = [
'tahun_beli' => $data_kendaraan->tahun_beli,
'id_jenis' => $data_kendaraan->id_jenis,
'id_merk' => $data_kendaraan->id_merk,
'spareparts' => [] // empty array, to be filled with spareparts data
];
$this->db->select('*');
$this->db->from('tb_sparepart');
$this->db->where('tb_sparepart.id_jenis', $data_kendaraan->id_jenis);
$query_sparepart = $this->db->get();
foreach ($query_sparepart->result() as $data_sparepart) {
$spareparts = [
'id_sparepart' => $data_sparepart->id_sparepart,
'sparepart' => $data_sparepart->sparepart,
];
array_push($hasil['spareparts'], $spareparts);
}
}
return $hasil;
}
i try this model and the result is like:
result is show sparepart by id jenis.
but i want to show data sparepart where id merk the car and sparepart where id merk is 1 (all merk).
and i try next model..
my model :
function GetById($id_kendaraan){
$this->db->select('*');
$this->db->from('tb_kendaraan');
$this->db->where('tb_kendaraan.id_kendaraan',$id_kendaraan);
$query_kendaraan = $this->db->get();
$hasil = false;
if($query_kendaraan->num_rows() > 0)
{
$data_kendaraan = $query_kendaraan->row();
$hasil = [
'tahun_beli' => $data_kendaraan->tahun_beli,
'id_jenis' => $data_kendaraan->id_jenis,
'id_merk' => $data_kendaraan->id_merk,
'spareparts' => [] // empty array, to be filled with spareparts data
];
$this->db->select('*');
$this->db->from('tb_sparepart');
$this->db->where('tb_sparepart.id_jenis', $data_kendaraan->id_jenis);
$this->db->where('tb_sparepart.id_merk', $data_kendaraan->id_merk);
$query_sparepart = $this->db->get();
foreach ($query_sparepart->result() as $data_sparepart) {
$spareparts = [
'id_sparepart' => $data_sparepart->id_sparepart,
'sparepart' => $data_sparepart->sparepart,
];
array_push($hasil['spareparts'], $spareparts);
}
}
return $hasil;
}
the picture is show data sparepart where id merk = id merk.
i want to show sparepart in misubishi fuso is:
roda gede sekali (id_merk = 1)
punya mishubisi (id_merk = 14)
oli mesin truck (id_merk = 1)
not with sparepart punya hino, because sparepart punya hino is id_merk = 11.
example:
my tb_kendaraan
|id_kendaraan|id_jenis|id_merk|
| 1 | 1 | 11 |
| 2 | 1 | 14 |
| 3 | 2 | 15 |
my tb_sparepart
|id_sparepart|id_jenis|id_merk|sparepart |
| 1 | 1 | 1 |roda gede sekali |
| 2 | 1 | 14 |punya misubisi |
| 3 | 1 | 1 |oli mesin truck |
| 4 | 1 | 11 |punya hino |
| 5 | 2 | 1 |part motorcyle |
| 6 | 2 | 15 |motorcyle engine |
| 7 | 2 | 16 |motorcyle lamp |
i mean id_merk 1 in tb_sparepart is a all merk.
if i call tb_kendaraan.id_kendaraan = 1
the sparepart must show like be:
roda gede sekali
oli mesin truck
punya hino
and if i call tb_kendaraan.id_kendaraan = 2
the sparepart must show like be:
roda gede sekali
punya misubisi
oli mesin truck
and if i call tb_kendaraan.id_kendaraan = 3
the sparepart must show like be:
part motorcyle
motorcyle engine
thx for help me
Not sure if i understand the question but i think you just need to add the id_merk in your last array. You can then use the field "id_merk" in your checkboxes. Like so:
function GetById($id_kendaraan){
$this->db->select('*');
$this->db->from('tb_kendaraan');
$this->db->where('tb_kendaraan.id_kendaraan',$id_kendaraan);
$query_kendaraan = $this->db->get();
$hasil = false;
if($query_kendaraan->num_rows() > 0)
{
$data_kendaraan = $query_kendaraan->row();
$hasil = [
'tahun_beli' => $data_kendaraan->tahun_beli,
'id_jenis' => $data_kendaraan->id_jenis,
'id_merk' => $data_kendaraan->id_merk,
'spareparts' => [] // empty array, to be filled with spareparts data
];
$this->db->select('*');
$this->db->from('tb_sparepart');
$this->db->where('tb_sparepart.id_jenis', $data_kendaraan->id_jenis);
$query_sparepart = $this->db->get();
foreach ($query_sparepart->result() as $data_sparepart) {
$spareparts = [
'id_sparepart' => $data_sparepart->id_sparepart,
'id_merk' => $data_sparepart->id_merk, // Add id_merk
'sparepart' => $data_sparepart->sparepart,
];
array_push($hasil['spareparts'], $spareparts);
}
}
// To debug; uncomment this
//echo '<pre>'; print_r($hasil); exit();
return $hasil;
}
Related
I have a codeigniter project, which describe in simplest form with following tables
tbl_direct_fuel table
+----------------+------------+------------+
| direct_fuel_id | vehicle_id | issue_date |
+----------------+------------+------------+
| 1 | 1000 | 2019-10-01 |
| 2 | 1001 | 2019-10-02 |
+----------------+------------+------------+
tbl_direct_fuel_details table
+-----------------------+----------------+---------+----------+
| direct_fuel_detail_id | direct_fuel_id | item_id | fuel_qty |
+-----------------------+----------------+---------+----------+
| 100 | 1 | 50 | 1.00 |
| 101 | 2 | 60 | 2.00 |
+-----------------------+----------------+---------+----------+
store_item table
+---------+-----------+
| item_id | item_name |
+---------+-----------+
| 50 | DOT 3 |
| 60 | DOT 4 |
| 70 | DOT 5 |
+---------+-----------+
I tried to print fuel_qty in numbers & also in words as follows using my view
1.00 One and Cents Zero Only
2.00 Two and Cents Zero Only
Model
public function directFuelQtyById($id) {
$this->db->select('tbl_direct_fuel_details.fuel_qty');
$this->db->from('tbl_direct_fuel_details');
$this->db->join('tbl_direct_fuel', 'tbl_direct_fuel_details.direct_fuel_id=tbl_direct_fuel.direct_fuel_id', 'inner');
$this->db->where('tbl_direct_fuel.status=1 and tbl_direct_fuel.direct_fuel_id="'.$id.'"');
$this->db->order_by('tbl_direct_fuel.direct_fuel_id','DESC');
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}
Controller
public function convert_number_to_words($number)
{
$hyphen = ' ';
$conjunction = ' and ';
$separator = ' ';
$negative = 'negative ';
$decimal = ' and Cents ';
$dictionary = array(
0 => 'Zero',
1 => 'One',
2 => 'Two',
3 => 'Three',
4 => 'Four',
5 => 'Five',
6 => 'Six',
7 => 'Seven',
8 => 'Eight',
9 => 'Nine',
10 => 'Ten',
11 => 'Eleven',
12 => 'Twelve',
13 => 'Thirteen',
14 => 'Fourteen',
15 => 'Fifteen',
16 => 'Sixteen',
17 => 'Seventeen',
18 => 'Eighteen',
19 => 'Nineteen',
20 => 'Twenty',
30 => 'Thirty',
40 => 'Fourty',
50 => 'Fifty',
60 => 'Sixty',
70 => 'Seventy',
80 => 'Eighty',
90 => 'Ninety',
100 => 'Hundred',
1000 => 'Thousand',
1000000 => 'Million',
);
if (!is_numeric($number)) {
return false;
}
if ($number < 0) {
return $negative . $this->convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int)($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . $this->convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int)($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = $this->convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= $this->convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string)$fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}
/////////////////////
public function printFuel($id){
$this->data['printData']=$this->Fuel_model->directFuelById($id);
$fuel = $this->Fuel_model->directFuelQtyById($id);
foreach($fuel as $obj)
{
$this->amount_word = $this->convert_number_to_words($obj->fuel_qty) . '<br>'." Only";
}
$this->load->view('/template/directFuel/printFuel', $this->data);
}
View (printFuel)
<html>
<head>
<title>Fuel Order</title>
</head>
<body onload="window.print()">
<body>
<div class="col-xs-12 table-responsive">
<table class="table table-bordered" style="font-size: 13px; ">
<tbody>
<?php
if (!empty($printData)) {
$offset=1; // cm
$cnt=0;
foreach ($printData as $item) {
?>
<tr>
<td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 12cm"><?=$item->fuel_qty?> Litres</p></td>
<td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 8cm"><?=$this->amount_word?></p></td>
</tr>
<?php $cnt++;
} // close your foreach HERE
?>
</tbody>
</table>
</div>
<?php
}
?>
</body>
</html>
But when converting to words, the function prints only the last element for <?=$this->amount_word?>, like below:
1.00 Two and Cents Zero Only
2.00 Two and Cents Zero Only
How to get the desired output?
1.00 One and Cents Zero Only
2.00 Two and Cents Zero Only
In your function printFuel($id) you are overwriting $this->amount_word with each new array value. Therefore you only get the very last value.
So just convert each amount you get in $item->fuel_qty into a text string $item->fuel_qty_str using the function convert_number_to_words() in your controller:
public function printFuel($id){
$arr=$this->Fuel_model->directFuelById($id);
foreach($arr as $key=>$obj)
{
$str=$this->convert_number_to_words($obj->fuel_qty) . '<br>'." Only";
$arr[$key]->fuel_qty_str=$str;
}
$data['printData']=$arr;
$this->load->view('/template/directFuel/printFuel', $data);
}
and then output the array in your view:
foreach ($printData as $item) {
?>
<tr>
<td>
<p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 12cm">
<?=$item->fuel_qty?> Litres
</p>
</td>
<td>
<p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 8cm">
<?=$item->fuel_qty_str?>
</p>
</td>
</tr>
<?php $cnt++;
}
I have this table that I use (but not only) for storing friends in a database :
user_1 | user_2 | status
where 'status' can be -1,0 or 1. Here, we will consider only cases where status are '0' (pending for user_1) or '1' (approved by user_2). I have the following query to look for pending/approved friends for a given $user :
SELECT user_1,user_2,status
FROM Friends
WHERE (user_2 = '$user' OR user_1 = '$user') AND status >= 0;
The goal here is to modify the query to also tell if a given $user2 is a common (approved) friend of $user1 and each (approved) friend of $user1.
After some researches, I figured out that the left join would do the trick, by setting another field to either NULL (if no mutual) or $user2. I would want to do it efficiently. I tried several shots, but no success around it.
Thanks by advance for your help
EDIT : For example, let's say we have the following entries :
a | b | 1
c | a | 1
c | b | 1
a | d | 1
I want to list the friends of 'a' and for each friend f of 'a', verify if 'b' is a common friend of f and 'a'. Also, f =/= b for the mutual test. The result of such a query would be :
a | b | 1 | NULL
c | a | 1 | b
a | d | 1 | NULL
Let me know if you need more clarification
As in MySQL query would be so complicated and slow, that I wouldn't use it myself, here's a solution in PHP, with only one query:
<?php
// $db = mysqli_connect(...);
function findMutualFriends($of,$mutual_with){
global $db;
$user_friends = array();
$mutual_friends = array();
$results = array();
$res = mysqli_query($db,"SELECT user_1,user_2,status FROM Friends WHERE ((user_2 = '$of' OR user_1 = '$of') OR (user_2 = '$mutual_with' OR user_1 = '$mutual_with')) AND status >= 0;";
while($row = mysqli_fetch_assoc($res)){
if($row['user_1'] == $of || $row['user_2'] == $of){
$user_friends[] = (($row['user_1'] == $of) ? $row['user_2'] : $row['user_1']);
}
if($row['user_1'] == $mutual_with || $row['user_2'] == $mutual_with){
$mutual_friends[(($row['user_1'] == $mutual_with) ? $row['user_2'] : $row['user_1'])] = 1;
}
}
foreach($user_friends as $friend){
if($mutual_firends[$friend]){
$results[] = $friend;
}
}
return $results;
}
?>
Please notice that it haven't been tested. May contain some minor syntax error, but should return an array of mutual friends.
I modified a bit the Flash Thunder's function post. Just tested with some modifications and it works ! Thanks again.
function findMutualFriends($pdo, $of,$mutual_with){
$user_friends = array();
$mutual_friends = array();
$results = array();
$query = "SELECT user_1,user_2,status FROM Friends WHERE ((user_2 = '$of' OR user_1 = '$of') OR (user_2 = '$mutual_with' OR user_1 = '$mutual_with')) AND status = 1;";
$prep = $pdo->prepare($query);
$res = $prep->execute();
$rows = $prep->fetchAll();
foreach ($rows as $row) {
if($row['user_1'] == $of || $row['user_2'] == $of) {
$user_friends[] = ($row['user_1'] == $of ? $row['user_2'] :$row['user_1']);
}
if($row['user_1'] == $mutual_with || $row['user_2'] == $mutual_with) {
$mutual_friends[($row['user_1'] == $mutual_with ? $row['user_2'] :$row['user_1'])] = true;
}
}
foreach($user_friends as $friend) {
$results[$friend] = $mutual_friends[$friend] == true ? true : false;
}
return $results;
}
I have a Reservation model which has date_from and date_to fields.
How can reservations be grouped by one day interval, so it is possible to know how many of them are active on a particular date?
Example
Reservation::groupBy('...')->get()
Output:
reservations
==================
qty | date
------------------
3 | 2000-01-01
5 | 2000-01-02
2 | 2000-01-03
...
First add "date_from" and "date_to" to $dates array on model
protected $dates = ['date_from', 'date_to'];
Then
$min_date = Reservation::select(DB::raw('MIN(date_from) as min'))->first();
$max_date = Reservation::select(DB::raw('MIN(date_to) as max'))->first();
$data = [];
if (!is_null($min_date) && !is_null($max_date)) {
$daterange = new DatePeriod(DateTime::createFromFormat('Y-m-d H:i:s', $min_date->min), new DateInterval('P1D') ,DateTime::createFromFormat('Y-m-d H:i:s', $max_date->max));
$reservations = Reservation::all();
foreach ($daterange as $date) {
$data[$date->format('Y-m-d')] = $reservations->filter(function($reservation) use($date) {
$carbon_date = Carbon::instance($date)->endOfDay();
return ($reservation->date_from->startOfDay()->lte($carbon_date) && $reservation->date_to->endOfDay()->gte($carbon_date));
})
->count();
}
}
MYSQL TABLE
|id|parent_id|
|1 | 0| <- depth 1
|2 | 1| <- depth 2
|3 | 1| <- depth 2
|4 | 2| <- depth 3
|5 | 3| <- depth 3
|6 | 4| <- depth 4
I need to find out the depth of id 4 which in the above case it's 3
Is it possible to do it with one mysqli query?
If not, how can I do that with multiple mysqli queries?
Many many thanks.
EDITED
function find_depth($id, $depth = 0){
global $con;
$query = "SELECT * from table WHERE `id` = $id";
if ($result = $con->query($query)){
if (mysqli_num_rows($result) == 1){
$row = $result->fetch_array();
$depth = $depth + 1;
find_level($row['parent_id'], $depth);
} else {
return $depth;
}
}
}
find_depth('4')
will give me 3
However, I am still looking for a better way to achieve that
If the table is not huge, you can do only one query to DB
function find_depth($id, $depth = 0) {
global $con;
$temp = array();
$query = "SELECT * from table";
if ($result = $con->query($query)) {
while($row = $result->fetch_array())
$temp[$row['id']] = $row['parent_id'];
do { $depth = $depth + 1; } while($id = $temp[$id]);
}
return $depth;
}
find_depth('4');
I'm using Codeigniter V2.1.3 and I have a calendar template that have a scheduling stuff and I want to count the events in a particular date. Here is my database:
----Table: schedule -----
id | materialID | borrowerID | date_reserve |
------------------------------------------------
1 | 7 | 7 | 2013-08-16 |
2 | 10 | 10 | 2013-08-16 |
1 | 12 | 13 | 2013-08-18 |
What I want is in my calendar template the total event for the date=2013-08-16 will be 2 events. Here is my code which is not working coz it keeps on sending me only 1 event maybe you could figure out where is my mistake in here:
$query = $this->db->select('*')->from('schedule')->like('date_reserve', "$year-$month")->get();
$cal_data = array();
foreach ($query->result() as $row) {
$index = ltrim(substr($row->date_reserve,8,2), '0');
$cal_data[$index] = count($row->borrowerID). 'event(s)';
}
return $cal_data;
Any help?
If you want number of events for exact date: YYYY-MM-DD you can replace like with where:
$r = $this->db->select('id, materialID, borrowerID, date_reserve')
->where('date_reverse', $year."-".$month."-".$day)
->get('schedule')
->result();
Also you can print_r the result so you will see what the result is. Also you can echo last query: echo $this->db->last_query();
Edit:
We had to run query per each date to fetch the number of events.
$cal_data = array();
for($i=1;$i<=31;$i++)
{
$this->db->select('COUNT(id) as count,date_reserve,borrowerID');
$this->db->from('schedule');
$this->db->where('date_reserve',"$year-$month-$i");
//$this->db->group_by('date_reserve');
$query = $this->db->get();
foreach ($query->result() as $row) {
$cal_data[$index] = $row->count. 'event(s)';
}
try using group_by
public function get_results($date) {
$this->db->select('COUNT(id),date_reserve');
$this->db->from('schedule');
$this->db->like('date_reserve',$date);
$this->group_by('date_reserve');
$result = $this->db->get();
return $result->result_array();
}