0

I'm running a while loop which is taking rows from mySQL and echo'ing them out. Pretty standard. However, there needs to be different HTML markup for the first item, next two items, then it continues as normal into the while loop.

Currently, my while loop looking something like this:

while( ( $row = mysql_fetch_object( $result ) ) !== false )
    {   

    // Places ad based of predefined variable frequency
    if ($ad == $adFrequency){

     echo '<div class="one-advertisement-article clearfix">';
     echo '<div class="grid_9 suffix_2"><img src="http://placehold.it/263x75/000000/ffffff"></div>';
     echo '<div class="grid_1"><a class="navigate-right-icon ss-icon" href="#">navigateright</a></div>';
     echo '</div>';

     $ad = 0;
    }

    echo '<div class="one-news-article clearfix">';
    if( $row->imagelibid )
            {
                $imageid = intval( $row->imagelibid );
                $image_result = mysql_query( "SELECT * FROM imagelib WHERE id = {$imageid}", $db );

                $image_row = mysql_fetch_object( $image_result );

                $image_url = "http://www.#.com/slir/w280/imagelib/" . $image_row->id . "_" . $image_row->filename;

                echo '<div class="grid_4"><a href="#"><img src="'.$image_url.'"></a></div>';        
            }
    else {
                echo '<div class="grid_4"><div class="news-placeholder"><span class="ss-icon">ellipsischat</span></div></div>';
        }

    echo '<div class="grid_7">';
    echo '<h2><a href="item-news.php?id='.$row->id.'">'.$row->title.'</a></h2>';

    $published_date = date('D, d M Y', strtotime($row->datein));
    echo '<p class="published-date">'.$published_date.'</p>';

    echo '</div>';
    echo '<div class="grid_1">';
    echo '<div class="news-item-vertical-sep">&nbsp;</div>';
    echo '<p></p>';
    echo '</div>';
    echo '</div>';

    $ad++;

    }

This works fine, but I need to take the first three news items and use this:

echo ' <div class="one-news-featured-article clearfix">';
    echo '  <div class="grid_12">';

    if( $row->imagelibid )
            {
                $imageid = intval( $row->imagelibid );
                $image_result = mysql_query( "SELECT * FROM imagelib WHERE id = {$imageid}", $db );

                $image_row = mysql_fetch_object( $image_result );

                $image_url = "http://www.#.com/slir/w280/imagelib/" . $image_row->id . "_" . $image_row->filename;

                echo '<div class="large-featured-image-container"><img src="'.$image_url.'">/div>'; 
            }   

    echo '      <div>';
    echo '        <h2><a href="item-news.php?id='.$row->id.'">'.$row->title.'</a></h2>';
    echo '      </div>';
    echo '    </div>';
    echo '  </div>';
    echo '  <div class="grid_6">';

Any help? Essentially, it needs to apply the second code to the first three items in the query, then follow through with the code included in the current while loop - like an offset I guess.

Thanks, R

2
  • 1
    Add a counter and use the modulus operator. Commented Dec 12, 2012 at 11:31
  • Use templates to separate the logic and the view! Commented Dec 12, 2012 at 11:35

3 Answers 3

2

This is quite simple. I hope I understood your question correctly:

$i = 0;
while ( ( $row = mysql_fetch_object( $result ) ) !== false ) {
    if ($i < 3) {
        // First code
    } else {
        // Second code
    }
    $i += 1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much. This makes perfect sense.
Sure, although usually when 'if/else' blocks start to get complicated you should consider using a switch statement instead:
2

Firstly, you should avoid using any mysql_ functions as they are all deprecated and will be removed from future versions of PHP. I'd recommend using PDO instead. See this tutorial to get started.

Then, it'll simply be a case of doing something like this:

foreach($db->query($query) as $index => $row) {
    if ($index < 3) {
        // first 3 items
    } else {
        // other items
    }
}

Comments

0

You can do it easaly with an simple counter and an if statement:

$count = 0;

while(fetching) {
    if($count < 3) {
        // items 1, 2 and 3
    } else {
        // do normal
    }

    $count++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.