SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: For Loop Issues

  1. #1
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    For Loop Issues

    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.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You've basically got to 2 ways to go.

    a) Do 2 selects. Get the contact, then get the rows of contact info. Display the contact, loop through the array of contact info records.

    Pseudocodish ...
    PHP Code:
    $qry1 "select name from users where id = 23";
    $qry2 "select datetime, message from messages where user_ref=23";

    echo 
    $qry1['name'];

    foreach(
    $qry2 as $row){
    echo 
    $row['datetime'] . ' ' $row['message'] . PHP_EOL ;

    b) Do 1 select. Get the contact multiple times, once for each of the rows of contact info -- then have PHP loop through all the rows but display only one row for the contact and then display all of the contact info records.

    Pseudocodish also ...
    PHP Code:
    $qry "select name, datetime, message from messages left joins users on users.id=messages.user_ref AND user_ref=23";

    $count 0;

    foreach(
    $qry as $row){

    if( 
    $count === 0){
    echo 
    $row['name'] . PHP_EOL ;
    }
    echo 
    $row['datetime'] . ' ' $row['message'] . PHP_EOL ;
    $count++;



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •