Looping Rows

[php]while($row = mysqli_fetch_array($result))
{
if ($i<=2)
{
$title = $row[‘title’];
$image = $row[‘imagelarge’];
$owner = $row[‘owner’];

echo '<div id="craftlarge">';

echo '<div id="craftlargeimage">';
echo "<img src=\"".$image."\" width=300 height=300>";
echo "</div>";

echo '<div id="crafttitle">';
echo "$title";
echo "</div>";

echo '<div id="craftowner">';
echo "$owner";
echo "</div>";

echo nl2br($body);

echo "</div>";
$i++;
}

else if ($i<=8)
{
$title = $row['title'];
$image = $row['imagesmall'];
$owner = $row['owner'];

echo '<div id="craftsmall">';

echo '<div id="craftsmallimage">';
echo "<img src=\"".$image."\">";
echo "</div>";

echo '<div id="crafttitle">';
echo "$title";
echo "</div>";

echo '<div id="craftowner">';
echo "$owner";
echo "</div>";

echo nl2br($body);

echo "</div>";
$i++;
}

else{
$i=1;
}

}[/php]

I want this first two items loaded to format one way then the next 6 to format another. After this I would like them to repeat. It all seems to work, but it will not repeat. Any suggestions?

I assume you have initiated the variable $i somewhere else in the code??? here is you code, hope this is what you intended for???

[php]<?php

while($row = mysqli_fetch_array($result))

{

if ($i <= 2)
{
    $title = $row['title'];
    $image = $row['imagelarge'];
    $owner = $row['owner'];

    echo '<div id="craftlarge">';

    echo '<div id="craftlargeimage">';
    echo "<img src=\"".$image."\" width=300 height=300>";
    echo "</div>";

    echo '<div id="crafttitle">';
    echo "$title";
    echo "</div>";

    echo '<div id="craftowner">';
    echo "$owner";
    echo "</div>";

    echo nl2br($body);

    echo "</div>";
    $i++;
}

elseif ($i > 2 && $i <= 8)
{
    $title = $row['title'];
    $image = $row['imagesmall'];
    $owner = $row['owner'];

    echo '<div id="craftsmall">';

    echo '<div id="craftsmallimage">';
    echo "<img src=\"".$image."\">";
    echo "</div>";

    echo '<div id="crafttitle">';
    echo "$title";
    echo "</div>";

    echo '<div id="craftowner">';
    echo "$owner";
    echo "</div>";

    echo nl2br($body);

    echo "</div>";
    $i++;
    
}

elseif ($i > 8 && $i % 2 == 1) {

    $title = $row['title'];
    $image = $row['imagelarge'];
    $owner = $row['owner'];

    echo '<div id="craftlarge">';

    echo '<div id="craftlargeimage">';
    echo "<img src=\"".$image."\" width=300 height=300>";
    echo "</div>";

    echo '<div id="crafttitle">';
    echo "$title";
    echo "</div>";

    echo '<div id="craftowner">';
    echo "$owner";
    echo "</div>";

    echo nl2br($body);

    echo "</div>";
    $i++;
}

elseif ($i > 8 && $i % 2 == 0){

    $title = $row['title'];
    $image = $row['imagesmall'];
    $owner = $row['owner'];

    echo '<div id="craftsmall">';

    echo '<div id="craftsmallimage">';
    echo "<img src=\"".$image."\">";
    echo "</div>";

    echo '<div id="crafttitle">';
    echo "$title";
    echo "</div>";

    echo '<div id="craftowner">';
    echo "$owner";
    echo "</div>";

    echo nl2br($body);

    echo "</div>";
    $i++;
}

else {
        $i=1;
}  

}
[/php]

I mean I want the whole loop to repeat. Not just alternate styles afterwards. Sorry for the confusion.

Never mind, I solved my own problem. I just needed to say else if(%i >11) {$i=1}.

Sponsor our Newsletter | Privacy Policy | Terms of Service