Hello, I need help figuring out how to generate a loop using foreach with queries from the database to get an output result like below.
{
"emails": [
{
"group": "Today April 23",
"list": [{
"id": 1,
"subject": "Pages - Multi-Purpose Admin Template Revolution Begins here!",
"to": ["David Nester", "Jane Smith"],
"body": "<p>First email body</p> ",
"time": "5 Mins ago",
"datetime": "Today at 1:33pm",
"from": "David Nester",
"dp": "assets/img/profiles/avatar.jpg",
"dpRetina": "assets/img/profiles/avatar2x.jpg"
}, {
"id": 2,
"subject": "Your site has some very imaginative animation /movement! ",
"to": ["Anne Simons"],
"body": "<p>Second email body</p> ",
"time": "45 mins ago",
"datetime": "Today at 1:33pm",
"from": "Anne Simons",
"dp": "assets/img/profiles/5.jpg",
"dpRetina": "assets/img/profiles/5x.jpg"
}]
}, {
"group": "Yesterday April 22",
"list": [{
"id": 3,
"subject": "Good design is obvious. Great design is transparent",
"to": ["John Doe", "Anne Simons"],
"body": "<p>Third email body</p> ",
"time": "1:33pm",
"datetime": "Today at 1:33pm",
"from": "David Nester",
"dp": "assets/img/profiles/b.jpg",
"dpRetina": "assets/img/profiles/b2x.jpg"
}]
}
}
]
Currently I am getting this output. Which ha repeated items in the list array inside the email array.
[code]{
“emails”: [
{
“group”: “Today, Jun 05”,
“list”: [
{
“id”: “7”,
“subject”: “Subject Text”,
“to”: [
“admin”
],
“body”: "This is sample text!
",
“time”: “15:02:46”,
“datetime”: “2016-06-05”,
“from”: “admin”,
“dp”: “…/assets/img/profiles/3.jpg”,
“dpRetina”: “…/assets/img/profiles/3x.jpg”
},
{
“id”: “7”,
“subject”: “Subject Text”,
“to”: [
“edem”
],
“body”: "This is sample text!
",
“time”: “15:02:46”,
“datetime”: “2016-06-05”,
“from”: “edem”,
“dp”: “…/assets/img/profiles/3.jpg”,
“dpRetina”: “…/assets/img/profiles/3x.jpg”
},
{
“id”: “8”,
“subject”: “Subject Text 2”,
“to”: [
“admin”
],
“body”: “This is sample text! 2
”,
“time”: “15:02:55”,
“datetime”: “2016-06-05”,
“from”: “admin”,
“dp”: “…/assets/img/profiles/3.jpg”,
“dpRetina”: “…/assets/img/profiles/3x.jpg”
},
{
“id”: “8”,
“subject”: “Subject Text 2”,
“to”: [
“edem”
],
“body”: “This is sample text! 2
”,
“time”: “15:02:55”,
“datetime”: “2016-06-05”,
“from”: “edem”,
“dp”: “…/assets/img/profiles/3.jpg”,
“dpRetina”: “…/assets/img/profiles/3x.jpg”
},
{
“id”: “9”,
“subject”: “Subject Text 3”,
“to”: [
“admin”
],
“body”: “This is sample text! 3
”,
“time”: “15:03:01”,
“datetime”: “2016-06-05”,
“from”: “admin”,
“dp”: “…/assets/img/profiles/3.jpg”,
“dpRetina”: “…/assets/img/profiles/3x.jpg”
},
{
“id”: “9”,
“subject”: “Subject Text 3”,
“to”: [
“edem”
],
“body”: “This is sample text! 3
”,
“time”: “15:03:01”,
“datetime”: “2016-06-05”,
“from”: “edem”,
“dp”: “…/assets/img/profiles/3.jpg”,
“dpRetina”: “…/assets/img/profiles/3x.jpg”
}
]
}
]
}
[/code]
This is my php code generating the wrong putput above.
[code]<?php
header(‘Content-type: application/json’);
// Start session and get session values.
session_start();
// Connect to database.
require('../../config/database.php');
$sql = "SELECT DISTINCT(`datetime`) FROM `memo`";
$dates = mysqli_query($conn, $sql);
foreach ($dates as $key => $date) {
$sql = "SELECT `memo`.*, `users`.`username` FROM `memo` JOIN `users` ON `memo`.`to` = '" . $_SESSION['user_id'] . "' WHERE `datetime` = '" . $date['datetime'] . "'";
$results = mysqli_query($conn, $sql);
// Clean array for each new loop.
unset($lists);
// Loop and create lists.
foreach ($results as $key => $result) {
$lists[] = array(
'id' => $result['id'],
'subject' => $result['subject'],
'to' => array(
$result['username']
),
'body' => $result['body'],
'time' => $result['time'],
'datetime' => $result['datetime'],
'from' => $result['username'],
'dp' => "../assets/img/profiles/3.jpg",
'dpRetina' => "../assets/img/profiles/3x.jpg"
);
}
// Set group variable to date.
$group = date("M d", strtotime($date['datetime']));
// Current date.
$current_date = date("M d");
// User interface recent date into simple text like "Today, April 24".
if ($group == $current_date) {
$group = "Today, " . date("M d");
}
// Add group and list to email array.
$json[] = array(
'group' => $group,
'list' => $lists
);
}
$memos = array(
'emails' => $json
);
echo json_encode($memos);
// Close connection.
mysqli_close($conn);
?>
[/code]
Thanks for the opportunity.
I really need help with this one.