How add subcategory in Codeigniter - mysql

i need add subcategory after category in this code:
Help pls
Controller:
public function find_diagnoz() {
$key = $this->input->get('key');
$result = $this->Diagnoz_model->find_diagnoz($key);
foreach ($result as $key => $res) {
/* Категория */
$this_cat = $this->Diagnoz_model->get_categories($res['id']);
if (!$this_cat) {
$rec->cat = false;
continue;
}
$result[$key]['cat'] = $this_cat['names'];
}
$html = '';
foreach ($result as $res) {
$html .= '<li>';
$html .= '<a href="javascript:void(0);" data-id="'.$res['id'].'" data-code="'.md5($res['id'].$res['texts']).'" data-title="'.$res['texts'].'" class="select_td">';
$html .= '<p>'.$res['texts'].'</p>';
$html .= '<p>Из категории: '.$res['cat'].'</p>';
$html .= '</a>';
$html .= '</li>';
}
die(json_encode(array('html' => $html)));
}
Model
For get category name:
public function get_category($id) {
$this_category = $this->db->select('*')
->from('zaloby')
->where('id = '.$id)
->get()->result_array();
if (!isset($this_category[0])) {
return false;
}
return $this_category[0];
}
For get result by key:
public function find_diagnoz($key) {
$res = $this->db->select('*')
->from('zaloby')
->like('names', $key)
->get()->result_array();
return $res;
}
Mysql table have structure:
id parid names text
1 0 name1 desc1
2 1 name2 desc2
3 2 name3 desc3
4 2 name4 desc4
When I search result now, my result:
Name2
In category: Name2
I need result with subcategory:
Name2
In category: Name1 / Name2
Thx, sorry for my bad english.

Related

Print all elements in PHP array HTML view

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++;
}

Yii2 Update records resulting from query

I need to update month_no fields of the records resulting from a select query using some variables passed from controller but every field is written with the same value.
These are the variables:
$id_calc = 27
$from_y = "2013-05-01"
$to_y = "2015-11-31"
The table result:
id id_paid year_ref month_no
1 26 2012 12
2 26 2013 12
4 27 2013 12 (should be 8)
5 27 2014 12
6 27 2015 12 (should be 11)
This is model:
public function CorrectTable($id_calc, $from_y, $to_y)
{
$connection = Yii::$app->db;
$query = "SELECT * FROM my_table where id_paid='$id_calc'";
$data = $connection->createCommand($query)->queryAll();
// calculate no. of month first and last year
$month_no_begin = (13 - date("m",strtotime($from_y)));
$month_no_end = (date("m",strtotime($to_y)));
//
foreach($data as $row)
{
$id = $row['id'];
// calculate number of month per year
if ($row['year_ref'] = date("Y",strtotime($from_y)))
{
$month_no = $month_no_begin;
} elseif ($row['year_ref'] = date("Y",strtotime($to_y)))
{
$month_no = $month_no_end;
} else
{
$month_no = 12;
}
Yii::$app->db->createCommand()
->update('my_table', ['month_no' => $month_no], ['id' => $id])
->execute();
}
I tried to put the above code in controller with the same result.
A careless mistake once I had made before. You should use == or === in the if statement to test equal condition, instead of =
public function CorrectTable($id_calc, $from_y, $to_y)
{
$connection = Yii::$app->db;
$query = "SELECT * FROM my_table where id_paid='$id_calc'";
$data = $connection->createCommand($query)->queryAll();
// calculate no. of month first and last year
$month_no_begin = (13 - date("m",strtotime($from_y)));
$month_no_end = (date("m",strtotime($to_y)));
//
foreach($data as $row)
{
$id = $row['id'];
// use `==` instead of `=`
if ($row['year_ref'] == date("Y",strtotime($from_y)))
{
$month_no = $month_no_begin;
}
// use `==` instead of `=`
elseif ($row['year_ref'] == date("Y",strtotime($to_y)))
{
$month_no = $month_no_end;
}
else
{
$month_no = 12;
}
Yii::$app->db->createCommand()
->update('my_table', ['month_no' => $month_no], ['id' => $id])
->execute();
}
}
our if ... esleif ... else sequence seems not logig
as you done the else if is never executed
so you should change the condition this way the e
foreach($data as $row)
{
$id = $row['id'];
// assigne default value
$month_no = 12;
// check for month_begin
if ($row['year_ref'] == date("Y",strtotime($from_y)))
{
$month_no = $month_no_begin;
}
//check for month_end
if ($row['year_ref'] == date("Y",strtotime($to_y)))
{
$month_no = $month_no_end;
}
Yii::$app->db->createCommand()
->update('my_table', ['month_no' => $month_no], ['id' => $id])
->execute();
}

list all records from one custom taxonomy starting with one letter i.e A

I want to list all records in one custom taxonomy start with only A or B. Below code is to list all record with all letters.
Here is the code to list all record with one custom taxonomy with all letters in groups. Example,
A
Aajkacatch (0)
Aajkiitem (0)
Aamzie (0)
Aaneri (0)
Aaramshop (0)
Abaaya (0)
B
B.LAB (0)
Baby-Republic (0)
Babyoye (45)
Bank-Bazaar (1)
<?php
// Template Name: Store Template
// get all the stores
$stores = get_terms(APP_TAX_STORE, array('hide_empty' => 0, 'child_of' => 0, 'pad_counts' => 0, 'app_pad_counts' => 1));
// get ids of all hidden stores
$hidden_stores = clpr_hidden_stores();
$list = '';
$groups = array();
if ($stores && is_array($stores) ) {
// unset child stores
foreach($stores as $key => $value)
if($value->parent != 0)
unset($stores[$key]);
foreach($stores as $store)
$groups[mb_strtoupper(mb_substr($store->name, 0, 1))][] = $store;
if (!empty($groups)) :
foreach($groups as $letter => $stores) {
$old_list = $list;
$letter_items = false;
$list .= "\n\t" . '<h2 class="stores">' . apply_filters( 'the_title', $letter ) . '</h2>';
$list .= "\n\t" . '<ul class="stores">';
foreach($stores as $store) {
if (!in_array($store->term_id, $hidden_stores)) {
$list .= "\n\t\t" . '<li>' . apply_filters('the_title', $store->name). ' (' . intval($store->count) . ')</li>';
$letter_items = true;
}
}
$list .= "\n\t" . '</ul>';
if(!$letter_items)
$list = $old_list;
}
endif;
} else {
$list .= "\n\t" . '<p>' . __('Sorry, but no stores were found.', 'appthemes') .'</p>';
}
?>
While it wouldn't be the most efficient way, you could easily just check the first letter of the store name.
change:
if (!in_array($store->term_id, $hidden_stores)) {
to:
if (!in_array($store->term_id, $hidden_stores) ) {
$first_letter = substr( $store->name, 0, 1 );
if ( $first_letter == 'A' || && $first_letter == 'B' )
That would ensure you're only listing A and B items.

Propel criteria? or is it even possible to create such query?

I have 3 tables: product, product_parameter, product_parameter_item.
Product and product parameter have no foreign references, product_parameter_item has product_id and product_parameter_id.
For example i have 4 products:
1
2
3
4
Product_parametetrs:
1 - Model
2 - Color
Product_parameter_item:
id - product_id - product_paramter_id - value:
1 - 1 - 1 - Toyota
2 - 1 - 2 - white
3 - 2 - 1 - Toyota
4 - 2 - 2 - black
5 - 3 - 1 - Citroen
6 - 3 - 2 - white
7 - 4 - 1 - Citroen
8 - 4 - 1 - black
How can I get all products with Model = Toyota, Color = Black ?
$c->addJoin(ProductPeer::ID, ProductParameterItemPeer::PRODUCT_ID);
$c->addJoin(ProductParameterItemPeer::PRODUCT_PARAMETER_ID, ProductParameterPeer::ID);
foreach ($product_params as $i => $param){
$c1 = $c->getNewCriterion(ProductParameterPeer::ID, $param->getId());
$c2 = $c->getNewCriterion(ProductParameterItemPeer::VALUE, $value);
$c1->addAnd($c2);
$c->addAnd($c1);
}
Doesnt work
$c->addJoin(ProductPeer::ID, ProductParameterItemPeer::PRODUCT_ID);
foreach ($product_params as $i => $param){
$c1 = $c->getNewCriterion(ProductParameterItemPeer::ID, $param->getId());
$c2 = $c->getNewCriterion(ProductParameterItemPeer::VALUE, $value);
$c1->addAnd($c2);
$c->addAnd($c1);
}
Doesnt work either
$c->addJoin(ProductParameterItemPeer::PRODUCT_PARAMETER_ID, ProductParameterPeer::ID);
foreach ($product_params as $i => $param){
$c->add(ProductParameterPeer::ID, $param->getId());
$c->add(ProductParameterItemPeer::VALUE, $value);
}
I have made this function to get ids of products and add them to Criteria. Didnt find another way :(
$product_params = ProductParameterPeer::getParamsForFilter();
$ids = false;
if (count($product_params)){
$sql = '';
$clause = '';
$clauses = array();
$make_query = false;
foreach ($product_params as $i => $param){
if ($values[$param->getAlias()]){
$is_last = end($product_params) == $param;
$sql .= '(SELECT product_id FROM `product_parameter_item` WHERE `value` = "'.$values[$param->getAlias()].'" AND `product_parameter_id` = '.$param->getId().' ) t'.$i.($is_last?' ':', ');
$clauses[] = 't'.$i.'.product_id';
$make_query = 'SELECT t'.$i.'.product_id FROM ';
}
}
if (count($clauses) == 1) $clause = 'WHERE '.$clauses[0];
else if (count($clauses) == 2) $clause = 'WHERE '.$clauses[0].'='.$clauses[1];
else if (count($clauses) > 2){
$clause = 'WHERE ';
for ($i = 1; $i < count($clauses); $i++){
$clause .= '('.$clauses[0].'='.$clauses[$i].')'.($i == count($clauses)-1 ? '' : ' AND ');
}
}
if ($make_query){
$query = $make_query.$sql.$clause;
$con = Propel::getConnection();
$statement = $con->prepare($query);
$statement->execute();
$ids = array();
while($stmt = $statement->fetch(PDO::FETCH_ASSOC)) {
$ids[] = (int)$stmt['product_id'];
}
}
}
return $ids;

Display tree structure in ul li tag from preorder traversal table not working

Based on : Getting a modified preorder tree traversal model (nested set) into a <ul>
I have following table :
id name lft rgt level
1 company 1 22 0
75 Developer 26 31 1
76 Tester 24 27 1
77 Analyst 22 23 1
78 under developer 27 30 2
79 under tster 25 26 2
And following query/code for fetching nested records:
function getstructureInformation() {
$treeArr = array();
$tree = array();
$sql = "SELECT node.name, node.id, node.unit_id,
node.description,node.lft,node.rgt,node.level,
(COUNT(parent.name) - 1) AS depth
FROM tablename AS node
CROSS JOIN tablename AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft";
$query = $this->db->query($sql);
$data = $query->result();
foreach ($data as $datap) {
$treeArr['id'] = $datap->id;
$treeArr['unit_id'] = $datap->unit_id;
$treeArr['lft'] = $datap->lft;
$treeArr['rgt'] = $datap->rgt;
$treeArr['level'] = $datap->level;
$treeArr['name'] = $datap->name;
$treeArr['depth'] = $datap->depth;
$treeArr['description'] = $datap->description;
$tree[] = $treeArr;
}
return $tree;
}
Here the PHP code for displaying it into the view page:
$result = '';
$currDepth = -1; // -1 to get the outer <ul>
while (!empty($tree)) {
$currNode = array_shift($tree);
if ($currNode['depth'] > $currDepth) {
echo '<ul>';
}
if ($currNode['depth'] < $currDepth) {
$result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
}
echo '<li>' . $currNode['name'] . '</li>';
$currDepth = $currNode['depth'];
if (empty($tree)) {
echo str_repeat('</ul>', $currDepth + 1);
}
}
But its not displaying properly like:
It displaying output as:
001 : Company Name
3 : Analyst
2 : Tester
33 : under tster
1 : Developer
44 : under developer
The desired output is like:
001 : Company Name
3 : Analyst
2 : Tester
33 : under tster
1 : Developer
44 : under developer
Is there any solution?
This should work :
function printTree ($tree) {
$last_level = -1;
foreach ($tree as $v) {
$diff = $v['level'] - $last_level;
if ($diff == 0) {
echo '<li>' .$v['name']. '</li>';
}
elseif ($diff > 0) {
for ($i = 0; $i < $diff; $i++)
echo '<ul>';
echo '<li>' .$v['name']. '</li>' ;
}
else {
for ($i = 0; $i > $diff; $i--)
echo '</ul>';
echo '<li>' .$v['name']. '</li>' ;
}
$last_level = $v['level'];
}
}
But with this function, you won't have the depth printed.
Tested and working with
$tree = array (
array (
'name' => 'Line',
'level' => 0
),
array (
'name' => 'Line',
'level' => 1
),
array (
'name' => 'Line',
'level' => 2
),
array (
'name' => 'Line',
'level' => 1
)
);
printTree ($tree);