I have 2 tables, Contacts and Contact_Info
I can relate the two with no problems and get the data I want based on the ID of the Contact. My issue is I want to show the Contact Once and the Contact Info as many times as I have that information.
I want this.
Contact Name: John T.
Contact Notes: Date, Entered contact into Database
Date, Called John T to say hello
=-=-=-=-=-=-=-=-
What I get is,
John T. Date, Entered contact into Database
John T. Date, Called John T to say hello
=-=-=-=-=-=-=-=-
My code is based on PHP MySQL Novice to Ninja
{
$sql = ‘SELECT contacts.id, first, middle, last, contactdate, note FROM contacts INNER JOIN contact_notes ON contact_id = contacts.id WHERE contacts.id ="’ . $_GET[‘id’] . ‘"’;
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching contacts: ’ . $e->getMessage();
include $_SERVER[‘DOCUMENT_ROOT’] . ‘/tsfamily/includes/error.html.php’;
exit();
}
while ($row = $result->fetch())
{
$contacts = array (‘id’ => $row[‘id’], ‘first’ => $row[‘first’], ‘middle’ => $row[‘middle’], ‘last’ => $row[‘last’], ‘contactdate’ => $row[‘contactdate’], ‘note’ => $row[‘note’]);
}
?>
<!DOCTYPE html>
<html lang=“en”>
<head>
<meta charset=“utf-8”>
<title>List of Contacts</title>
</head>
<body>
<?php foreach ($contacts as $contact): ?>
<blockquote>
<h1>
<?php
echo htmlspecialchars($contact[‘first’], ENT_QUOTES, ‘UTF-8’) . ’ ';
echo htmlspecialchars($contact[‘middle’], ENT_QUOTES, ‘UTF-8’) . ’ ';
echo htmlspecialchars($contact[‘last’], ENT_QUOTES, ‘UTF-8’) . ’ ';
echo htmlspecialchars($contact[‘contactdate’], ENT_QUOTES, ‘UTF-8’) . ’ ';
echo htmlspecialchars($contact[‘note’], ENT_QUOTES, ‘UTF-8’) . ’ ';
?>
</h1>
</blockquote>
<?php endforeach; ?>
</body>
</html>
I’m sure I need to move First, Middle and Last out of the For each loop in the HTML but I can’t get it to work outside of that loop.