I have 3 tables:
profile
id | fname | policy
users
id | fullname | claimnum
and returnprod
claim_id | prodname | description
My SQL is this:
("SELECT *
,CONCAT(fname, ' ' ,lname) AS fullName
,CONCAT(firstname, ' ' ,lastname) AS InsName
,a.address AS ADDRESS
,a.city AS CITY
,a.state AS STATE
,a.zip AS ZIP
,a.phone AS PHONE
,a.fax AS FAX
,a.email AS EMAIL
FROM profile a
INNER JOIN returnprod b ON a.policy = b.claim_id
INNER JOIN users c ON a.adjuster_id = c.id
WHERE date >= '$Start' AND date <= '$End'
Jump ahead a little bit to the loop
`$products[]= $row;`
`foreach ($products as $product)
{
if ($row['policy'] === $product['claim_id']) {
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount,$product['product']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $product['comment']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowcount, $product['anotes']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowcount, $product['price']);
}`
This is working fine if there is only one product. The result will look something like this...
row 1: claim_id | firstname | adjuster name
row 2: productname | description | etc
If I have more than one product this happens...
row 1: claim_id | firstname | adjuster name
row 2: productname | description | etc
row 3: claim_id | firstname | adjuster name
row 4: productname | description | etc
row 5: productname | description | etc
Instead of grouping all the products together and displaying all of them below the profile information it is giving me the first result under the profile then giving me the profile information again with the first product and then the second product.
How do I go about grouping all products underneath the profile the first time?
Example:
row 1: claim_id | firstname | adjuster name
row 2: productname | description | etc
row 3: productname | description | etc
Thank you for taking the time to read through this.
$products[] = $row seems to indicate that this is not the only loop; more context might help. At the least, you will want to sort your DB results by whatever you intend to group by (ORDER BY a.id, a.policy). Then you could keep a reference to the last-seen policy and skip the header if it matches.
$last_id = '';
foreach ($rows as $row) {
if (! $row['policy'] === $last_id ) {
// Write claim_id | firstname | adjuster name
}
// Write productname | description | etc
$last_id = $row['policy'];
}
Assuming that is what you are after...
I went about this a different way.
instead of using FOREACH I ended up using WHILE.
while($row = mysql_fetch_array($result)){
// CODE TO QUERY PROFILE INFORMATION
$productquery=("SELECT * FROM returnprod WHERE claim_id = '$POLICY'");
while($product = mysql_fetch_array($prods)){
//CODE TO QUERY THE PRODUCTS ASSOCIATED WITH THE PROFILE
}
}
Then I added GROUP BY a.policy in the $result
Related
So I've been following this tutorial: https://www.plus2net.com/php_tutorial/chart-line-database.php
I am trying to add a line chart to my website to display number of sales for each month.
This is an example on how my SQL table looks like:
| ID | user | sale_id | date |
| 1 | RVN4372 | 1341234 | 2020-09-22 17:31:32 |
| 2 | OVI6517 | 5452351 | 2020-09-22 15:14:43 |
| 3 | RVN4372 | 8452176 | 2020-09-17 16:23:54 |
| 4 | FOK8905 | 7421312 | 2020-09-17 11:23:11 |
| 5 | DIF9127 | 4236123 | 2020-09-15 15:32:26 |
This is how my current query looks like:
<?php
if($stmt = $link->query("SELECT user,COUNT(*) FROM sales WHERE yearweek(DATE(date), 1) = yearweek(curdate(), 1) GROUP BY user order by COUNT(*) DESC")){
$php_data_array = Array(); // create PHP array
while ($row = $stmt->fetch_row()) {
$php_data_array[] = $row; // Adding to array
}
}else{
echo $link->error;
}
//print_r( $php_data_array);
// You can display the json_encode output here.
echo json_encode($php_data_array);
// Transfor PHP array to JavaScript two dimensional array
echo "<script>
var my_2d = ".json_encode($php_data_array)."
</script>";
?>
<div id="curve_chart"></div>
This is how it looks like on my website:
So this basically groups the users, and count how many sales each user has. On the X axis is display the user's name, and Y axis total number of sales.
I want to change this, so in the X asix is display the month, and Y asis total number of sales. How can I accomplish this?
EDIT: Hava been trying out some, but can't make it work. This is what I've got so far:
if($stmt = $link->query("
SELECT YEAR(date)
as SalesYear,
MONTH(date) as SalesMonth,
COUNT(*) AS TotalSales
FROM sales
GROUP BY YEAR(date), MONTH(date)
ORDER BY YEAR(date), MONTH(date)
AND COUNT(*) DESC
")){
If you have more than one year then you need to group in Year-month combination. Then change this query to this.
SELECT CONCAT(YEAR(date),'-' MONTHNAME(date)) as ym, COUNT(*) FROM sales GROUP BY ym ORDER BY count(*) DESC
My table:
-----------------------
| id: | data: |
|---------------------|
| 1 | a |
| 20 | b |
| 546 | c |
-----------------------
I want to store the data in an array from last inserted row (id is auto incremented) to first.
$sth = $dbh->query('SELECT * FROM mytable');
$data = array();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
array_push($data, $row);
}
return $data;
What can I add to the query in order to loop through the table from bottom to top?
I tried variations of ORDER BY but they didn't work, I'm missing something.
Change MySQL statement to be
SELECT * FROM 'mytable' ORDER BY 'id' DESC
or reverse the array using PHPs reverse array function
return array_reverse($data);
Get records by descending order
use following query
SELECT * FROM mytable order by id desc
where id is the auto increment id of your table
I'm pretty new to databases, so my question is:
Let's say that we have two tables: Users and Orders
Is it possible (and how do I do it) to list all users with their data (First & Last name, birthdate etc) and associated orders in one query? So the result should look something like this:
|------------------------------|
| John Doe | Order 1 details |
| | Order 2 details |
| | Order 3 details |
|------------------------------|
| Janny Hoe | Order x details |
| | Order y details |
|------------------------------|
So you got the point? Each row from table1 has a field which contains multiple rows from table2? I've heard MySQL doesn't return multidimensional arrays, so how does it return something like this?
Any help would be greatly appreciated!
EDIT:
Table structure could be something like this:
TABLE users:
id | name
TABLE orders:
id | user_id | date_ordered | other unrelevant data...
I know this is doable with joins, but what bugs me is what will it return?
Will result have field orders which would be array containing orders row data like $result[0]['orders'][0]['date_ordered'] or something like that ?
Do a LEFT join (if there is a possibility that a user does not have any orders) and then gather the results in an array, based on the user ID:
SELECT Users.id, Users.name, Order.detail
FROM Users
LEFT JOIN Order
ON Users.id= Order.user;
Ex:
$userOrders = array();
$sql = 'SELECT Users.id, Users.name, Order.detail
FROM Users
LEFT JOIN Order
ON Users.id= Order.user';
foreach ($pdo->query($sql) as $row) {
$userOrders[$row['user_id']][] = $row;
}
I'm working on a big project a social networking site but now I'm stuck and i need your advice.
My problem is that I wan't to display everything like posts, videos and statuses into his profile.php under user timeline.
I got more than 40 Tables but let me specify what I wan't, I wan't to get data from these tables and how to display them on the profile.php timeline section ?
Status Table
------------------------------------------
ID | sMessage | sDate | uId
------------------------------------------
1 | Testing status | 2013/07/03 | 1
Videos Table
-----------------------------------------------------
ID | vName | vCode | vDate | uId
-----------------------------------------------------
1 | PHP and MYSQL | 2MvYwz | 2013/07/03 | 1
Users Table
-----------------------------------
ID | uName | JoinDate
-----------------------------------
1 | Tem Henov | 2013/07/03
And here is what i tried:
class userTimeline {
public function loadTimeline(){
dbConn();
$query = "SELECT
sMessage, sDate, vName, vDate, vCode
FROM status
INNER JOIN users
ON (status.uId = users.uId)
INNER JOIN videos
On (videos.uId = users.uId)
WHERE users.uId = '1'";
return $result = mysql_query($query);
}
}
and its loads the data fine but how to display that data in blocks like if its a status display in a separate block and if its a video display it in another block and how to order by date ?! any help is appreciated.
First : To Show data into various <div> first fetch them and show like,
1) Fetch all the record and store into a php array
$rows = array();
while($row = mysql_fetch_array(YOUR_QUERY))
{
$rows[] = $row;
}
2) Now Using foreach() loop use data fetched from query where ever you want
if(is_array($rows)) {
foreach($rows as $row) {
//do with $row or create some if,switch condition here !
}
}
For specific limits and tweaks study the result set we get from mysql_fetch_array() !
Second : To short data by date you can use multiple sorting (click here for
tutorial) but i think you should use MYSQL UNION to merge two
or more MySQL query results to get individual sorting result.
I have two tables. The first table holds simple user data and has the columns
$username, $text, $image
(this is called "USERDATA").
The second table holds information about which users "follow" other users, which is set up with the columns
$username and $usertheyfollow
(this is called "FOLLOWS").
What I need to do is display the data individually to each user so that it is relevant to them. This means that userABC for instance, needs to be able to view the $text and $image inputs for all of the users whom he/she follows. To do this, I believe I need to write a sql query that involves first checking who the logged in user is (in this case userABC), then selecting all instances of $usertheyfollow on table FOLLOWS that has the corresponding value of "userABC." I then need to go back to my USERDATA table and select $text and $image that has a corresponding value of $usertheyfollow. Then I can just display this using echo command or the like...
How would I write this SQL query? And am I even going about the database architecture the right way?
With tables like so:
userdata table
______________________________
| id | username | text | image |
|------------------------------|
| 1 | jam | text | image |
+------------------------------+
| 2 | sarah | text | image |
+------------------------------+
| 3 | tom | text | image |
+------------------------------+
follows table
_____________________
| userId | userFollow |
|---------------------|
| 1 | 2 |
+---------------------+
| 1 | 3 |
+---------------------+
and use the following SQL:
SELECT userdata.text, userdata.image FROM follows LEFT JOIN userdata ON follows.userFollow = userdata.id WHERE follows.userId = 1
will get all the text and images that user with id '1' follows
As it turns out, neither of these answers were right. #jam6459 was closest.
The correct answer is the following:
SELECT userdata.text, userdata.image, follows.userFollow
FROM userdata
LEFT JOIN follows ON follows.userFollow = userdata.username
WHERE follows.userId = $username
I also found it easier to not have a username correspond to an Id as in jam's table example. This is because the same user can have multiple entries in "USERDATA". I instead used username as the Id.
function get_text_image($username)
{
$sql = "SELECT * FROM USERDATA where username='".$username."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row['text'];
echo $row['image'];
}
}
function display_data_of_followers($userid)
{
$sql = "SELECT usertheyfollow FROM follow WHERE userid = ".$userid."";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
get_text_image($row['usertheyfollow']);
}
}
display_data_of_followers($userid);