Number auto increase with other while function (PHP) - php

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>

Related

Query SUM for two fields in two different tables PHP

I am having a problem generating the result I need. I want to sum the other table gg_hp base on gg_id
+--------------+----------+-------+------------+
| sg_name | sg_grade | gg_id | subject_id |
+--------------+----------+-------+------------+
| Quiz 1 | 20 | 14 | 68 |
| Midterm Exam | 50 | 15 | 68 |
| Quiz 2 | 50 | 14 | 68 |
+-------+--------------+----------+-------+----+
tbl_gradesubcateg
+-------+--------------+------------+------------+------------+------------+-------+
| gg_id | categname | percentage | gradecount | teacher_id | subject_id | gg_hp |
+-------+--------------+------------+------------+------------+------------+-------+
| 14 | Quiz | 10 | NULL | 4 | 68 | 0 |
| 15 | Midterm Exam | 20 | NULL | 4 | 68 | 0 |
+-------+--------------+------------+------------+------------+------------+-------+
This is my expected output
+-------+--------------+------------+------------+------------+------------+-------+
| gg_id | categname | percentage | gradecount | teacher_id | subject_id | gg_hp |
+-------+--------------+------------+------------+------------+------------+-------+
| 14 | Quiz | 10 | NULL | 4 | 68 | 70 |
| 15 | Midterm Exam | 20 | NULL | 4 | 68 | 50 |
+-------+--------------+------------+------------+------------+------------+-------+
This is the query I made but.. Im not getting accurate result.
$querycount = "SELECT * FROM tbl_gradesubcateg order by gg_id asc ";
$query_run = mysqli_query($con,$querycount);
$sums= 0;
$ctr = 0;
$id1 = "";
while ($num = mysqli_fetch_assoc ($query_run)) {
$sums += $num['sg_grade'];
$id= $num['gg_id'];
if($id == $id1)
{
$queryhp = mysqli_query($con,"UPDATE tbl_gradecateg SET gg_hp = '".$sums."' where gg_id='".$id."'") or die(mysqli_error($con));
}
else
{
$queryhp = mysqli_query($con,"UPDATE tbl_gradecateg SET gg_hp = '".$sums."' where gg_id='".$id."'") or die(mysqli_error($con));
$sums= 0;
}
$id1= $num['gg_id'];
}
```
Any thoughts would be great.
A correlated subquery is a simple approach:
update tbl_gradesubcateg gs
set sg_grade = (select sum(sg.g_grade)
from gg_hp g
where g.gg_id = gs.gg_id
);
I don't recommend doing this calculation, though. It is simple enough to aggregate when you query the table. Summarizing the results just means that they are out-of-date the next time rows are inserted, updated, or deleted in the tables.

How to update other rows by getting number from the previous updated rows

I have the following table, named "leave" :
ID | Employee | Numb1 | Numb2 | Numb3
1 | 4 | 12 | 2 | 10
2 | 4 | 10 | 3 | 7
3 | 4 | 7 | 2 | 5
The process is when I update the first row the Numb1 and Numb3 in another row will also updated by the value of Numb3 of the first row.
The output I was hoping for Is like i this table :
the case is when I change the Numb 2 to 3 of the first row
ID | Employee | Numb1 | Numb2 | Numb3
1 | 4 | 12 | 3 | 9
2 | 4 | 9 | 3 | 6
3 | 4 | 6 | 2 | 4
Here is my code :
//Form Input
$amount = $_POST['amount'];
//Form Input END
$query_leave = mysql_query("SELECT
Numb1,
Numb2,
Numb3
FROM leave
WHERE
ID = '$id_edit'");
$data = mysql_fetch_array($query_leave);
$remains = $data[0] - $amount;
$query_update = mysql_query("UPDATE leave SET
ID = '$idkaryawan',
Numb2 = '$amount',
Numb3 = '$remains',
WHERE ID = '$id_edit'");
$query_leave2 = mysql_query("SELECT
Numb1,
Numb2,
Numb3
FROM leave
WHERE
ID <> '$id_edit' AND
Employee = 4");
$data2 = mysql_fetch_array($query_leave2);
$remains2 = $data2[0] - $data2[1];
$query_update2 = mysql_query("UPDATE leave SET
Numb1 = '$remains'
WHERE
ID <> '$id_edit' AND
Employee = 4");
The output when I use the above code is :
ID | Employee | Numb1 | Numb2 | Numb3
1 | 4 | 12 | 3 | 9
2 | 4 | 9 | 3 | 6
3 | 4 | 9 | 2 | 6
My Problem is I don't know how to loop it
Currently, I'm using PHP native to develop this program
Thanks a lot for any help

sql query is taking long time to execute

I wrote this sql query and it works but it is taking long time to be executed
SELECT trans_files.id, trans_files.game_name, trans_files.file_name,
COUNT(en_txt.id) as txt_num, COUNT(ar_txt.id) as n_txt_num
FROM trans_files
LEFT JOIN en_txt ON en_txt.file_id = trans_files.id
LEFT JOIN ar_txt ON ar_txt.en_text_id = en_txt.id
&& ar_txt.date = (SELECT MAX(ar_txt.date)
FROM ar_txt
WHERE en_text_id = en_txt.id)
GROUP BY trans_files.id
ORDER BY trans_files.id DESC, trans_files.game_name ASC, trans_files.file_name ASC
..
mysql> select * from ar_txt limit 3;
+----+------------+---------+----------------+---------------------+
| id | en_text_id | user_id | text | date |
+----+------------+---------+----------------+---------------------+
| 1 | 16 | 3 | Ϻ┘äÏ▓Ï╣┘è┘à | 2017-01-24 19:10:19 |
| 2 | 18 | 3 | Ϻ┘äϡϻϺϻ | 2017-01-24 19:13:36 |
| 3 | 3 | 3 | Ϻ┘äÏÀÏ¿┘èÏ¿┘è | 2017-01-24 19:15:48 |
+----+------------+---------+----------------+---------------------+
mysql> select * from en_txt limit 3;
+----+---------+------------------+
| id | file_id | text |
+----+---------+------------------+
| 1 | 2 | Apothecary Cheng |
| 2 | 2 | Blacksmith Ho Li |
| 3 | 2 | Apothecary Sung |
+----+---------+------------------+
mysql> select * from trans_files limit 3;
+----+-----------+-----------+
| id | game_name | file_name |
+----+-----------+-----------+
| 1 | drb | skills |
| 2 | drb | npcs |
| 3 | drb | test |
+----+-----------+-----------+
in this case i am using PDO and query to execute it
PHP code
so why does it take a long time?

create a sequence for rows with similar ids using php

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.

Need help ordering data from mysql_fetch_array()

I'm having a hard time organizing the data that I get from mysql_fetch_array().
I have a DB table with that looks something like this:
+---------------------+------------------+---------------+--------+---------+
| date | name | indexed_pages | nameID | entryID |
+---------------------+------------------+---------------+--------+---------+
| 2012-06-15 21:18:06 | site1.com | 200 | 1 | 1 |
| 2012-06-15 21:18:10 | site2.com | 25 | 2 | 1 |
| 2012-06-15 21:18:13 | site3.com | 12 | 3 | 1 |
| 2012-06-15 21:18:16 | site4.com | 8 | 4 | 1 |
| 2012-06-15 21:18:19 | site5.com | 2 | 5 | 1 |
| 2012-06-16 00:11:12 | site1.com | 191 | 1 | 2 |
| 2012-06-16 00:11:21 | site2.com | 25 | 2 | 2 |
| 2012-06-16 00:11:30 | site3.com | 12 | 3 | 2 |
| 2012-06-16 00:11:44 | site4.com | 8 | 4 | 2 |
| 2012-06-16 00:11:51 | site5.com | 2 | 5 | 2 |
| 2012-06-18 10:20:47 | site1.com | 191 | 1 | 3 |
| 2012-06-18 10:20:52 | site2.com | 25 | 2 | 3 |
| 2012-06-18 10:20:56 | site3.com | 12 | 3 | 3 |
| 2012-06-18 10:21:00 | site4.com | 8 | 4 | 3 |
| 2012-06-18 10:21:04 | site5.com | 2 | 5 | 3 |
+---------------------+------------------+---------------+--------+---------+
I need to order the results in a Google Line Graph in the following manner:
['date', 'site1_entryID=1', 'site2_entryID=2', 'site3_entryID=3', (...)],";
The thing is that I'm having trouble managing the arrays that I generate. I'm using the following code:
mysql_connect("host_here", "username_here", "pass_here") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$query = "SELECT * FROM pages";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
After this I need to echo the number of indexed_pages for each site where entryID = 1.
I don't know if this description is confusing or not, but I've tried pretty much everything and can't get the organize the data from the arrays to serve what I need to do. Help, please!
Thanks in advance!
Don't use select *, that's lazy, and you're stuck accepting the fields in the order the DB decides to produce them in.
Specify the fields you want, in the order you want:
SELECT date, name, indexed_pages, etc...
I think the simplest query is :
$result= mysql_query("SELECT name, index_pages, entryID from table_name WHERE entryID =
1");
while($row=mysql_fetch_array($result)){
echo "$row[name]";
echo "$row[index_pages]";
echo "$row[entryID]";
}
Try this. There might be some mistakes. Because i developed it fast. And replace table_name with yours.
Or you can display it in a table:
echo "<table>";
echo "<tr><td>Sit Name</td>";
echo "<td>Page Name</td>";
echo "<td>EntryID</td>";
echo "</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>$row[name]</td>";
echo "<td>$row[index_pages]</td>";
echo "<td>$row[entryID]</td>";
echo "</tr>";
}
echo "</table>";
SELECT date, name, indexed_pages
FROM pages
where entryID=1
order by date asc ,name asc
Not sure if this will help
mysql_connect("host_here", "username_here", "pass_here") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$query = "SELECT * FROM pages";
$result = mysql_query($query);
$data[]='date';
while($row = mysql_fetch_assoc($result)){
$name=substr($row['name'], -4);
$data[]= $name."_entryID=".$row['entryID'];
}
A little of a brute force method.

Categories