Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Duplicate records during a foreach loop

    I have a query that pulls news stories, then attaches any photos connected with a particular story. This all works fine but I recently added a feature to also pull videos connected with stories and it causes duplicate images or video links to show.

    Example, I have a news story with 3 pictures and 1 video. Currently it posts that one video next to each picture so it shows up 3 times. What I actually want to display is the news story, then any pictures, then any videos. This is the example problem in action http://themeparkfocus.com/news/

    Here is the php

    Code:
    $sLastStory = '';
    foreach ($result AS $row)
    {
        $sStory = $row['headline'] . $row['story'];
        if (strcasecmp($sStory, $sLastStory) != 0)
        {
            if (!empty($sLastStory))
            {
                print('<hr>' . PHP_EOL);
            }
            $sLastStory = $sStory;
    
    
            printf('<h2>%s</h2>' . PHP_EOL, $row['headline']);
    
            printf('<a href="parknews.php?park_id=%s">
            <h3>%s</a> - %s</h3>' . PHP_EOL, $row['park_id'], $row['name'], $row['Displaydate']);
    
            printf('<p>%s</p>' . PHP_EOL, $row['story']);
        }
    
    if(!empty($row['url'])){   
    printf('
    <a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s - Credit - %s" >
    <img src="/images/%s%s-thumb.jpg" style="min-height: 200px; min-width: 200px" alt="%s"/></a>' . PHP_EOL, $row['url'], $row['alt'], $row['headline'],  $row['description'],$row['credit'], $row['url'], $row['alt'], $row['alt'] );
    }
    
    if(!empty($row['location'])){   
    printf('<iframe width="640" height="360" src="%s" frameborder="0" allowfullscreen></iframe>' . PHP_EOL, $row['location'] );
    }
    
    }
    I'm not great with PHP and just copied this part:

    Code:
    if(!empty($row['url'])){   
    printf('
    <a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s - Credit - %s" >
    <img src="/images/%s%s-thumb.jpg" style="min-height: 200px; min-width: 200px" alt="%s"/></a>' . PHP_EOL, $row['url'], $row['alt'], $row['headline'],  $row['description'],$row['credit'], $row['url'], $row['alt'], $row['alt'] );
    }
    then pasted it underneath but changed it for the videos query instead of images.

    This is the query that is pulling from 3 different MySQL tables

    Code:
    try
    {
    $sql = 'SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") AS Displaydate, name, logo, tpf_parks.park_id, url, alt, description, credit, location
    FROM tpf_news
    INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id 
    LEFT JOIN tpf_images ON tpf_news.news_id = tpf_images.news_id 
    LEFT JOIN tpf_videos ON tpf_news.news_id = tpf_videos.news_id 
    ORDER BY date DESC' ;
    $result = $pdo->query($sql);
    }


    Any ideas how to fix it to display correctly? Thanks

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    Quote Originally Posted by danville666 View Post
    Any ideas how to fix it to display correctly?
    sure, set the desired fetch mode, because:
    Quote Originally Posted by php.net/manual/en/pdostatement.fetch.php

    Controls how the next row will be returned to the caller. This value must be one of the PDO::FETCH_* constants, defaulting to value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to PDO::FETCH_BOTH).
    ...
    • PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set

    ...
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

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