I have a data when i do
var_dump ($data);
here is how my data is displayed ($data is not a table balise):
+-------+----------+-----------+---------------+
| adan | Afla | avo | 0 |
+-------+----------+-----------+---------------+
| adan | ken | joly | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
however, I would like the table display to become :
+-------+----------+-----------+---------------+
| adan | Afla | avo | 1 |
+-------+----------+-----------+---------------+
| adan | ken | joly | 1 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 3 |
+-------+----------+-----------+---------------+
here is my code :
foreach($data as $liste){
$town[]=$liste->getLocalite()->getName();
$counties[]=$liste->getCountie()->getName();
$districts[]=$liste->getDistrict()->getName();;
}
$a= count($listforages)-1;
$numberDoc=0;
$tab1=$tab2=$tab3=$tab4=array();
for ($i=0; $i<count($data);$i++){
$tab[$i]= $town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'.$numberDoc[$i];
if( $i == $a ) {
if ($counties [$a-1]==$counties [$a] && $districts[$a-1]==$districts[$a] && $numberDoc[$a-1]==$numberDoc[$a] ){
$numberDoc[$a]++; // counter
$tab1[$a-1]=$town[$a-1].'--'.$counties [$a-1].'--'.$districts[$a-1].'--'. $numberDoc[$a-1];
}
if ($counties [$a-1] !=$counties [$a] && $districts[$a-1] !=$districts[$a] && $numberDoc[$a-1] !=$numberDoc[$a] ){
$numberDoc[$a]++; // counter
$tab2[$a-1]=$town[$a-1].'--'.$counties [$a-1].'--'.$districts[$a-1].'--'. $numberDoc[$a-1];
}
}
else{
if ($counties [$i]==$counties [$i+1] && $districts[$i]==$districts[$i+1] && $numberDoc[$i]==$numberDoc[$i+1] ){
$numberDoc[$i]++; // counter
$tab3[$i]=$town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'. $numberDoc[$i];
}
if ($counties [$i] !=$counties [$i+1] && $districts[$i] !=$districts[$i+1] && $numberDoc[$i] !=$numberDoc[$i+1] ){
$numberDoc[$i]++; // counter
$tab4[$i]=$town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'. $numberDoc[$i];
}
}
}
when you see my data (see table) :
town is column 0,
counties is column 1,
district is column 2,
numberDoc is column 3 ,
my code don't work ,How resolve this issue ?
NB: excuse my english,
thank you in advance
Related
I'd like to select all period of current term (as now FY80) + 3 period only of last term (FY79) from m_period table
Here's the table m_period looks :
+----+------+-------------+--------+
| id | term | period | status |
+----+------+-------------+--------+
| 1 | FY79 | 79::2020/01 | close |
| 2 | FY79 | 79::2020/02 | close |
| 3 | FY79 | 79::2020/03 | close |
| 4 | FY79 | 79::2020/04 | close |
| 5 | FY79 | 79::2020/05 | close |
| 6 | FY79 | 79::2020/06 | close |
| 7 | FY80 | 80::2020/07 | open |
| 8 | FY80 | 80::2020/08 | open |
| 9 | FY80 | 80::2020/09 | open |
| 10 | FY80 | 80::2020/10 | open |
| 11 | FY80 | 80::2020/11 | open |
| 12 | FY80 | 80::2020/12 | open |
| 13 | FY80 | 80::2021/01 | open |
| 14 | FY80 | 80::2021/02 | open |
| 15 | FY80 | 80::2021/03 | open |
| 16 | FY80 | 80::2021/04 | open |
| 17 | FY80 | 80::2021/05 | open |
| 18 | FY80 | 80::2021/06 | open |
+----+------+-------------+--------+
my php script stuck from here :
<?php
function getPeriod()
{
$prev_term = (int)substr($this->session->userdata('term'),2,2) - 1;
$this->db->select('period');
$this->db->where('term', $this->session->userdata('term'));
$this->db->where('term', $prev_term);
$this->db->order_by('period', 'ASC');
return $this->db->get('m_period')->result_array();
}
I've read CI documentation and couldn't find how to use CASE in codeigniter, or there is another way instead of using CASE ?
can someone help me , how to do this ?
Note : I store current term on session
You can use limit() method.
Then, You have to have two separate functions.
function getPrevPeriod()
{
$prev_term = (int)substr($this->session->userdata('term'),2,2) - 1;
$this->db->select('period');
$this->db->where('term', $prev_term);
$this->db->order_by('period', 'ASC');
$this->db->limit(3);
return $this->db->get('m_period')->result_array();
}
function getCurrentPeriod()
{
$this->db->select('period');
$this->db->where('term', $this->session->userdata('term'));
$this->db->order_by('period', 'ASC');
return $this->db->get('m_period')->result_array();
}
Then you can use array_merge() to combine arrays.
$periods = array_merge($prev, $current);
Edit: With one query
function getPeriod()
{
$prev_term = (int)substr($this->session->userdata('term'),2,2) - 1;
$this->db->select('term','period');
$this->db->or_where_in('term',[$this->session->userdata('term'),$prev_term])
$this->db->order_by('period', 'ASC');
$result = $this->db->get('m_period')->result_array();
$output = [];
$n = 0;
foreach($result as $row){
if($row["term"] == $prev_term){
if($n <= 3){
$output[] = $row;
$n++;
}
}else{
$output[] = $row;
}
}
return $output;
}
I need to export only those records whose column satisfies defined value.
Eg.
if(null !== ($this->f3->get('SESSION.userStatus')))
{
$userStatus = $this->f3->get('SESSION.userStatus');
}
This is how I tried to set cell values depending on set value:
$rowID = 5;
foreach ($results as $result)
{
if($result['status'] == $userStatus)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$rowID, $result['fullname']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$rowID, $result['checkin_date']);
}
else
{
$objPHPExcel->getActiveSheet()->removeRow($rowID);
}
$rowID++;
}
The file is exported with only those records that have defined "status" value.
But the problem is that rows whose criteria doesn't meet are filled with spaces and still occupies rows.
Here is how output looks like:
A | B | C | D
1 | | |
.
.
174 | | |
175 | | |
176 | | |
177 | | |
178 John | 2014| xyz | dfdf
179 Jack | 2015| jkl | dfdf
180 | | |
.
.
How can I fix it to get records starting with top row?
Eg.
1 | John | 2014 | xyz | dfdf
2 | Jack | 2015 | jkl | dfds
between
}
$rowID++;
put this
else
{
$objPHPExcel->getActiveSheet()->removeRow($rowID);
}
That will delete your blank rows.
Note, that you start with row 5 for some reason and do not test for max rows. Your code will still need some fixing up
i have a datastructure similar to this
+---------+---------+
| id | value |
+---------+---------+
| 1 | value |
1 | value |
| 1 | value |
| 1 | value |
| 1 | value |
| 2 | value |
| 2 | value |
| 2 | value |
| 3 | value |
| 3 | value |
| 3 | value |
| | |
+---------+---------+
I am trying to update this table to look something like this
+---------+---------+
| id | value |
+---------+---------+
| 1 | value 0 |
1 | value 1 |
| 1 | value 2 |
| 1 | value 3 |
| 1 | value 4 |
| 2 | value 0 |
| 2 | value 1 |
| 2 | value 2 |
| 3 | value 0 |
| 3 | value 1 |
| 3 | value 2 |
| | |
+---------+---------+
To achieve this, i have written php script that looks like this
$query = "select count(*) as count,id, value from foo group by id";
$sql=$con->prepare($query);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
while($row=$sql->fetch()){
$id[] = $row['id'];
$count[] = $row['count'];
$value[] = $row['value'];
echo "<pre>";
}
$c=array_combine($id, $count);
foreach ($c as $key=>$value){
for($i=0;$i<=$value;$i++){
$postid = $key;
if($i==0){
$multiple = "multiple";
$newvalue= $value;
}
else{
$x=$i-1;
$multiple = "multiple_".$x;
echo $multiple . "<br>";
$query2 = "update foo set value = :multiple";
$sql2=$con->prepare($query2);
$sql2->bindValue(':multiple', $multiple);
$sql2->execute();
}
}
}
The problem is that the code returns the following results
+---------+---------+
| id | value |
+---------+---------+
| 1 | value_1 |
1 | value_1 |
| 1 | value_1 |
| 1 | value_1 |
| 1 | value_1 |
| 2 | value_1 |
| 2 | value_1 |
| 2 | value_1 |
| 3 | value_1 |
| 3 | value_1 |
| 3 | value_1 |
| | |
+---------+---------+
What can i be possibly be doing wrong?
Thanks #Shadow
Your query runs fine but returns the following results
+------+-----------------------------------------------+
| id | value |
+------+-----------------------------------------------+
| 1 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 1 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 1 | multiple_1_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2 |
| 1 | multiple_1_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3 |
| 2 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 2 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 2 | multiple_1_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2 |
| 2 | multiple_1_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3 |
| 3 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 3 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 3 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
+------+-----------------------------------------------+
You can do the update iterating and creating data in such a way:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM foo");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$response = array();
foreach ($data as $dataIndex => $dataValue) {
if (!isset($response[$dataValue["id"]]["count"])) {
$response[$dataValue["id"]]["count"] = 0;
} else {
$response[$dataValue["id"]]["count"] ++;
}
$response[$dataValue["id"]]["values"][$dataValue["pid"]] = "value_" . $response[$dataValue["id"]]["count"];
$sth = $pdo->prepare("UPDATE foo SET value = '{$response[$dataValue["id"]]["values"][$dataValue["pid"]]}' WHERE pid = {$dataValue["pid"]}");
$sth->execute();
}
?>
But try to do an update using the least iteration not to create as many database queries , example:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM foo");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$response = array();
$update = array();
foreach ($data as $dataIndex => $dataValue) {
$response[$dataValue["id"]]["id"] = $dataValue["id"];
if (!isset($response[$dataValue["id"]]["count"])) {
$response[$dataValue["id"]]["count"] = 0;
} else {
$response[$dataValue["id"]]["count"] ++;
}
$response[$dataValue["id"]]["values"][$dataValue["pid"]] = "value_" . $response[$dataValue["id"]]["count"];
$update[] = "UPDATE foo SET value = '{$response[$dataValue["id"]]["values"][$dataValue["pid"]]}' WHERE pid = {$dataValue["pid"]};";
}
$update = implode("",$update);
$sth = $pdo->prepare($update);
$sth->execute();
?>
Your update query
$query2 = "update foo set value = :multiple";
does not contain any where criteria, each time you call this query it updates the value field's value in all records.
Honestly, I would not really involve php in this update, would do it purely in sql using user defined variables and multi-table join syntax in the update:
update foo inner join (select #i:=0, #previd:=-1) as a
set foo.value=concat(foo.value,'_',#i:=if(id=#previd,#i+1,0),if(#previd:=id,'',''))
The subquery in the inner join initialises #i and #previd user defined variables. The 3rd parameter of the concat function determines the value #i to be concatenated to the value field. The 4th parameter of concat sets the #previd variable and returns an empty string not to affect the overall concatenation. Unfortunately, I do not have access to MySQL to test the query, but it should be a good starting point anyway.
UPDATE
The OP claims in the updated question that the query I provided creates the below resultset:
+------+-----------------------------------------------+
| id | value |
+------+-----------------------------------------------+
| 1 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 1 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 1 | multiple_1_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2 |
| 1 | multiple_1_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3 |
| 2 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 2 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 2 | multiple_1_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2 |
| 2 | multiple_1_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3 |
| 3 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 3 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 3 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
+------+-----------------------------------------------+
Tested my solution in sqlfiddle. I had to remove the order by clause, otherwise the query produced the results in line with the requirements stated in the question. See sqlfiddle for details.
The results in the updated question are likely the result of running the query in a loop multiple times. In simple words: you just copy pasted the query into your code and did not remove the loop, even when I pointed out, that this may be the reason of the results you received.
I want the queqe id auto increase start from 1
I have an mysql table call t1
mysql table t1 Data as below:
+----------+------------------+-------------+
| ID | Name | Status |
+----------+------------------+-------------+
| 1 | ABBCCC | 1 |
| 2 | BASDASD | 1 |
| 3 | ABBCCC | 1 |
| 4 | ABBCCC | 2 |
+-------------------------------------------+
I loop data in php like this:
$quserCA = DB::query("SELECT * FROM ".DB::table('jnbook_book')." WHERE Name = 'ABBCCC' ORDER BY id DESC LIMIT 20");
$nqCA = mysql_num_rows($quserCA);
while($ruserCA = DB::fetch($quserCA)){
$CAlist[] = $ruserCA;
}
$x = 1;
while($x <= $nqCA) {
//echo "The number is: $x <br>";
$x++;
}
I loop this in my htm like this:
<table>
<tr>
<td>Queqe ID</td><td>ID</td><td>Status</td>
</tr>
<!--{loop $CAlist $value}-->
<tr>
<td>{$x}</td><td>{$value[id]}</td><td>{$value[status]}</td>
</tr>
<!--{/loop}-->
</table>
But after that my table output as below show
+---------------+-------------------+----------------+
| Queqe ID | ID | Status |
+---------------+-------------------+----------------+
| 1 | 1 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 2 |
+----------------------------------------------------+
Actually what I want the table output as below
(I want the queqe id auto increase start from 1):
+----------+-----------------+-----------------+
| Queqe ID | ID | Status |
+----------+-----------------+-----------------+
| 1 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
+----------------------------------------------+
Thank you.
This should be done something like:
$x = 1;
while($ruserCA = DB::fetch($quserCA)){
// add a field, say `x` with number of a record:
$ruserCA['x'] = $x++;
$CAlist[] = $ruserCA;
}
In a template:
<td>{$value[x]}</td><td>{$value[id]}</td><td>{$value[status]}</td>
I need If Statement (php) to :
if (Modem = ModemNotReceived) and (CheckModem = Done)
Total + 50
And
if (Modem = ModemReceived) and (CheckModem = Done)
Total - 50
How i can do that in one if statement?
My Table :
| ID | Modem | CheckModem | Total
------------------------------------------------
| 1 | ModemReceived | Done | 120
------------------------------------------------
| 2 | ModemNotReceived | Null | 90
------------------------------------------------
| 3 | ModemReceived | Null | 100
If I understand what you're saying correctly, this code is what you mean:
<?php
if ($Modem == 'ModemNotReceived' && $CheckModem == 'Done') {
$Total += 50;
} else if ($Modem == 'ModemReceived') && $CheckModem == 'Done' {
$Total -= 50;
}
?>