Reputation: 51
I have two tables; 'client' and 'sys_notes' in the same DB.
(SELECT client.*, sys_notes.* FROM client LEFT JOIN sys_notes ON sys_notes.client_id = client.id ORDER BY create_date DESC
)
I need to combine the two tables echo in the same <div>content</div>
, like this:
echo all from client = first.name + last.name (e.g. Scott)
echo all from client = role
echo all from client = whatever else
echo all from sys_notes sent by Robert Scott = note1, note2, note3, note(etc.)...
echo all from client = whatever else
echo all from client = whatever else
example output:
<div class="content">
Robert Scott
Supervisor
Robert works at the order office at the firm.
Robert´s notes:
Robert´s Phone: 000 000 000
Robert´s E-mail: etc.
Other information about Robert: ............
</div>
<div class="content">
Lisa Johansson
Supervisor
Lisa works at the marketing dep.
Lisas´s notes:
Lisa´s Phone: 000 000 000
Lisa´s E-mail: etc.
Other information about Lisa: ............
</div>
When i echo results today Robert is being "echoed" as many times he has notes,
with one note in each <div class="content">
. (In example over Robert has 3 notes & 3 different Robert-div is echoed.)
Please help.
--- UPDATE ---
-- Table structure for table `sys_notes`
CREATE TABLE IF NOT EXISTS `sys_notes` (
`note_id` int(11) NOT NULL AUTO_INCREMENT,
`client_id` int(11) NOT NULL,
`note_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`owning_user` varchar(50) NOT NULL,
`note` varchar(500) NOT NULL,
PRIMARY KEY (`note_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;
-- Table structure for table `client`
CREATE TABLE IF NOT EXISTS `client` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_date` datetime NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`phone` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
`issue` varchar(50) NOT NULL,
`other` longtext NOT NULL,
`owning_user` int(11) NOT NULL,
`status` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1081 ;
Upvotes: 1
Views: 778
Reputation: 399
use:
SELECT DISTINCT
instead of:
SELECT
or GROUP BY statement.
-- EDIT --
Example echo:
$result = $db->query("SELECT client.*, GROUP_CONCAT(sys_notes.note SEPARATOR "|") as note, sys_notes.* FROM client LEFT JOIN sys_notes ON sys_notes.client_id = client.id GROUP BY sys_notes.client_id ORDER BY create_date DESC");
ob_start();
?>
<div class="content">
<?php
foreach($result as $row) {
sprintf("<p>%s %s<br />%s<br />%s</p> %s <ul>", $row['firstname'], $row['lastname'], $row['role'], $row['desc'](example));
foreach(explode("|", $row['note']) as $note) {
sprintf("<li>%s</li>", $note);
}
sprintf("</ul>");
}
?>
</div>
<?
$result = ob_end_clean();
echo $result;
-- FINAL UPDATE --
<?php
try {
$db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$result = $db->query('SELECT client . * , GROUP_CONCAT( sys_notes.note SEPARATOR "|" ) AS n, sys_notes.*
FROM client
LEFT JOIN sys_notes ON sys_notes.client_id = client.id
GROUP BY sys_notes.client_id
ORDER BY create_date DESC');
ob_start();
?>
<div class="content">
<?php
foreach($result as $row) {
printf("<p>%s %s<br />%s</p> %s <ul>", $row['first_name'], $row['last_name'], $row['issue'], $row['other']);
foreach(explode("|", $row['n']) as $note) {
printf("<li>%s</li>", $note);
}
printf("</ul>");
}
?>
</div>
<?php
ob_end_flush();
Upvotes: 1
Reputation: 142
Steps:
1.Make Array
2.you use while loop for iterating complete if first+lastname is same then use same array index
3. within loop make array like $a['notes'][]=$row['NOTES'];
then echo above array
Upvotes: 0