create a sequence for rows with similar ids using php - 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.

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.

Number auto increase with other while function (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>

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.

PHP and MySQL, array associated with 2 keys

I have this scenario.
I input $groupid="1";
main table
----------------------
| groupid | postid |
|---------------------|
| 1 | 1 |
| 2 | 2 |
| 1 | 3 |
$query = "SELECT postid FROM `mainl` WHERE groupid='$groupid'";
$result = mysql_query($query);
// a group of postids belonging to that groupid which should hold [1, 3] for groupid=1
while($row = mysql_fetch_array($result)) {
$postids[] = $row["postid"];
}
second table
-------------------------------------------
| postid | commentid | comment |
-------------------------------------------
| 1 | 1 | testing 1 |
| 1 | 2 | testing 2 |
| 1 | 3 | what? |
| 2 | 1 | hello |
| 2 | 2 | hello world |
| 3 | 1 | test 3 |
| 3 | 2 | begin |
| 3 | 3 | why? |
| 3 | 4 | shows |
$query = "SELECT * FROM `second`";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
if (in_array($row["postid"], $postids)) {
$comments[$row["postid"]] = $row["comment"];
But how should I take care of commented
I want the postid array to be [1,3] and my comment array to be
[commentid: comment] [1:testing1, 2: testing2, 3: what?] for postid=1
and
[1:test3, 2:begin, 3: why? 4:shows] for postid=3
how should be arrange everything such comment are associated with commentid and postid?
First I would follow rokdd suggestion and make 1 query
SELECT m.groupid , s.postid, s.commentid, s.comment FROM `main1` m JOIN `second` s USING (postid) where m.groupid = 1
Then I would make a multi-dimensional array
while ($row = mysql_fetch_array($result))
$groups[$row['groupid'][$row['postid']][$row['commentid']=$row['comment'];
then to iterate through the array
foreach($groups as $group)
foreach($group as $post)
foreach($post as $comment)
echo $comment;
This will keep track of groups also (if you ever want to select by more than 1 group.
If you don't care about groups just drop off the first part of the array.
while ($row = mysql_fetch_array($result))
$posts[$row['postid']][$row['commentid']=$row['comment'];
foreach($posts as $post)
foreach($post as $comment)
echo $comment;
I guess to use the join in sql so that you will have one statement:
SELECT * FROM second as second_tab LEFT join main as main_table ON main_table.post_id=second_table.post_id WHERE main_table.group_id="3"
Well not tested now but thats a way to solve some of your problems!

How to select column in MySQL table and get value with PHP mysql_result?

I have tables illustrated below
//reference type table
+---+-----------+---------+
|ID |Article_ID |Ref_Types|
+---+-----------+---------+
| 1 | 1 | article |
| 2 | 1 | book |
| 3 | 1 | article |
| 4 | 1 | article |
| 5 | 2 | book |
+---+-----------+---------+
//book references table
+---+-----------+--------+
|ID |Article_ID |Title |
+---+-----------+--------+
| 1 | 1 | book1 |
| 2 | 1 | book2 |
| 3 | 2 | book3 |
| 4 | 2 | book4 |
| 5 | 2 | book5 |
+---+-----------+--------+
//article references table
+---+-----------+-----------+
|ID |Article_ID |Title |
+---+-----------+-----------+
| 1 | 1 | article1 |
| 2 | 1 | article2 |
| 3 | 2 | article3 |
| 4 | 2 | article4 |
| 5 | 2 | article5 |
+---+-----------+-----------+
I have to look into first table and check the reference, of which type it is;
for each reference type, I have get reference table from related table
I have to output in order, as shown in table one.
1:
$data=array();
$sql=mysql_query("SELECT * FROM reftypes
WHERE Article_ID=1 ORDER BY ID ASC");
while($row = mysql_fetch_array($sql)){
$data[]=$row[2]; // i store in an array so that i can use later..
}
2:
foreach ($data as $ref) {
$counter=1;
switch ($ref) {
case "article":
$sqlarticle= mysql_query("SELECT Title
FROM book WHERE Article_ID=1 ORDER BY ID ASC");
echo mysql_result($sqlarticle, $counter); //i want to get only one out of book table
$counter++;
break;
...
...
But $sqlarticle does not seem to work.
I want to display as:
+-----------+----------+
|Article_ID |Reference |
+-----------+----------+
| 1 | article1 |
| 1 | book1 |
| 1 | article2 |
| 1 | article3 |
+-----------+----------+
I know it is a long question and for experts or experienced people it is very trivial, but that is where I'm stuck.
SELECT
*
FROM
reftypes R
WHERE
Article_ID=your_id
LEFT JOIN books B ON (B.Article_ID = R.Article_ID AND R.Ref_Types = 'book')
LEFT JOIN articles A ON (A.Article_ID = R.Article_ID AND R.Ref_Types = 'article')
ORDER BY
R.id ASC;
Even if the database is wrongly modeled, I think.
What about the followin model instead?
""although especially question owners should respect any kind of effort and input, -i am thankful- i can not understand why some people try to think of question's holder as well-informed or experienced as themselves, or worse comment from higher level. ""
anyway, my question was about to get values one by one, here is how i did it;
$data=array();
$sql=mysql_query("SELECT * FROM reftypes
WHERE Article_ID=1 ORDER BY ID ASC");
while($row = mysql_fetch_array($sql)){
$data[]=$row[2]; // i store in an array so that i can use later..
}
$articlecount=0;
$bookcount=0;
foreach ($data as $value) {
switch ($value) {
case "article":
$sqlarticle=mysql_query("SELECT RefArticleTitle
FROM ref_article
WHERE $article_ID=Article_ID
ORDER BY ID ASC");
$articles= mysql_result($sqlarticle, $articlecount);
echo $articles;
echo "\n";
$articlecount++;
break;
case "book":
$sqlbook=mysql_query("SELECT RefBookName
FROM ref_book
WHERE $article_ID=Article_ID
ORDER BY ID ASC");
$books= mysql_result($sqlbook, $bookcount);
echo $books;
echo "\n";
$bookcount++;
break;
...
...
as a result, i got what i required..
+-----------+----------+
|Article_ID |Reference |
+-----------+----------+
| 1 | article1 |
| 1 | book1 |
| 1 | article2 |
| 1 | article3 |
+-----------+----------+
thanks to whoever interested in the topic..
$result=mysqli_query("select ref_types from reference type");
while($row=mysqli_fetch_array($result))
{
$table=$row[0];
$result1=mysqli_query("select * from $table");
while($row1=mysqli_fetch_array($result1))
{
var_dump($row1);
}
}

Categories