views:

112

answers:

2

Hello,

I need some help with an array and a foreach loop,

I have an array that is data returned from a database, basically it is a list of titles and images associated with it, it looks a little like this,

    Array
(
    [0] => Array
        (
            [contentImageId] => 28
            [contentImageName] => yellow.png
            [contentImageType] => .png
            [contentImagePath] => /var/www/bangmarketing.bang/media/uploads/contentImages/7/yellow.png
            [isHeadlineImage] => 1
            [contentImageDateUploaded] => 1265388055
            [contentId] => 7
            [dashboardUserId] => 0
            [contentTitle] => Another Blog
            [contentAbstract] => <p>This is another blog and it is shit becuase this does not work</p>
            [contentBody] => <p>ioasfihfududfhdufhuishdfiudshfiudhsfiuhdsiufhusdhfuids</p>
            [contentOnline] => 1
            [contentAllowComments] => 0
            [contentDateCreated] => 1265388034
            [categoryId] => 1
            [categoryTitle] => blogsss
            [categoryAbstract] => <p>asdsdsadasdsadfdsgdgdsgdsgssssssssssss</p>
            [categorySlug] => blog
            [categoryIsSpecial] => 0
            [categoryOnline] => 1
            [categoryDateCreated] => 1266588327
        )

    [1] => Array
        (
            [contentImageId] => 31
            [contentImageName] => sophie.jpg
            [contentImageType] => .jpg
            [contentImagePath] => /var/www/bangmarketing.bang/media/uploads/contentImages/9/sophie.jpg
            [isHeadlineImage] => 1
            [contentImageDateUploaded] => 1265713423
            [contentId] => 9
            [dashboardUserId] => 0
            [contentTitle] => Bang appoints a new Senior Designer
            [contentAbstract] => <p>Bang has appointed a new Senior Designer in the wake of a number of major client wins.</p>
            [contentBody] => <p>After recently signing up clients including the National Trust and Propaganda, Bang has hired Sophie Smith to join its creative team.</p>

<p>Sophie brings over twelve years of extensive design experience in Bang's core market sectors, including social housing and public sector brands.</p>
<p>Sophie has significant experience of branding and strategic design projects for a broad range of clients, including HBoS, Harvey's, TKMaxx and Places for People.</p>
<p>Warren Watts Creative Director at Bang commented; "Sophie has already proven an invaluable addition to our growing studio. In only her second week, she helped produce the creative that led to the National Trust account win".</p>
            [contentOnline] => 1
            [contentAllowComments] => 0
            [contentDateCreated] => 1265713155
            [categoryId] => 8
            [categoryTitle] => News
            [categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w
            [categorySlug] => news
            [categoryIsSpecial] => 0
            [categoryOnline] => 1
            [categoryDateCreated] => 1265283717
        )

    [2] => Array
        (
            [contentImageId] => 32
            [contentImageName] => selectadna.jpg
            [contentImageType] => .jpg
            [contentImagePath] => /var/www/bangmarketing.bang/media/uploads/contentImages/10/selectadna.jpg
            [isHeadlineImage] => 1
            [contentImageDateUploaded] => 1265713937
            [contentId] => 10
            [dashboardUserId] => 0
            [contentTitle] => Bang client experiences growth
            [contentAbstract] => <p>Selectamark, the UK's leading asset protection specialist recorded its best figures for five years in 2008.</p>
            [contentBody] => <p>The results were released during an extremely challenging economic climate, and just over 12 months into the organisation's relationship with Bang.</p>
<p>Bang has been working on developing Selectamark's range of product brands, which include the UK's leading asset tracking and labelling system Selectamark, and second generation forensic marking system, SelectaDNA. Despite gloomy national economic forecasts, Selectamark has enjoyed an extremely successful start to 2009 &ndash; picking up several major new clients in February, and with discussions taking place to expand operations beyond the UK and Netherlands to Germany and New Zealand.</p>

<p>Chris Garratt, Marketing Director at Bang Marketing Ltd explained "Selectamark adopted a well planned, coordinated and consistent strategy in 2008, and the results speak for themselves.</p>
<p>"Due to the success last year and the forecasts for increased levels of acquisitive crime we're hoping Selectamark's key products will enjoy similar growth in 2009".</p>
<p>Selectamark Security Systems provides DNA Forensic Marking products (SelectaDNA) and Visible Marking solutions (Selectamark and Logomark) to prevent burglaries, theft from void properties, theft of boilers, and theft of valuable metals such as lead from roofs. Selectamark have supplied these Secured By Design crime prevention products to Police, local authorities and housing associations for 25 years.</p>
<p>The Selectamark system has now marked over 25 million items, and is patented and distributed world-wide. Visit www.selectadna.co.uk for product details.</p>
<p>James Brown, Sales Director at Selectamark said "We have been absolutely delighted with the work Bang has done for us. In a short space of time, the Bang team has created a wide range of extremely fresh and professional-looking sales and marketing material for us to use, as well as devising a detailed marketing strategy which has now been implemented. We would have no hesitation in recommending Bang to other organisations."</p>
            [contentOnline] => 1
            [contentAllowComments] => 0
            [contentDateCreated] => 1265713888
            [categoryId] => 8
            [categoryTitle] => News
            [categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w
            [categorySlug] => news
            [categoryIsSpecial] => 0
            [categoryOnline] => 1
            [categoryDateCreated] => 1265283717
        )
)

As you can see the contentTitle changes, everytime but sometime is returns the same content title, I am looking for a way to add a divide between the returned data, if the content Title value changes?

Is this possible? and how? I am really struggling here.

Thanks

+2  A: 

As you loop through, you can have a temporary variable to store the last displayed title

$lastTitle='';
foreach ($array as $record)
{
   if ($lastTitle != $record['contentTitle'])
     echo '<hr>';

    $lastTitle = $record['contentTitle');

    // display content from record

}
Extrakun
+1  A: 

If I understand you correctly, you want to display a divider every time the contentTitle is different from the value it had in the last iteration.
You're making a kind of gallery here, aren't you? If so, I would go about it like this:

$lastTitle = '';
foreach($yourArray as $value)
{
    if(!$lastTitle || $lastTitle != $value['contentTitle'])
    {
        echo '<h2>' . $value['contentTitle'] . '</h2><hr />'; // Display divider
        // Do some other stuff
    }

    $lastTitle = $value['contentTitle'];
    // Display image or whatever you want to do
}
Turbotoast