Raffy T Lawrence Raffy T Lawrence - 3 months ago 14
PHP Question

PHP: How to duplicate the first count of loop

I have a fecth while loop and i need to duplicate the first count 1.

$count=1;
while($soc_sql2 = mysql_fetch_assoc($sql2)){
echo'<span class="icon_remove_sub" id="'.$count.'"></span>';
$count++;
}
//this output is
<span class="icon_remove_sub" id="1"></span>
<span class="icon_remove_sub" id="2"></span>
<span class="icon_remove_sub" id="3"></span>
<span class="icon_remove_sub" id="4"></span>
<span class="icon_remove_sub" id="5"></span>
<span class="icon_remove_sub" id="6"></span>


I need the output of this

<span class="icon_remove_sub" id="1"></span> //<--How do i duplicate
<span class="icon_remove_sub" id="1"></span> //<--this first count 1
<span class="icon_remove_sub" id="2"></span>
<span class="icon_remove_sub" id="3"></span>
<span class="icon_remove_sub" id="4"></span>
<span class="icon_remove_sub" id="5"></span>

Answer

Check $count value and repeat output:

$count=1;
while($soc_sql2 = mysql_fetch_assoc($sql2)){
    echo'<span class="icon_remove_sub" id="'.$count.'"></span>';
    if ($count == 1) {
        echo'<span class="icon_remove_sub" id="'.$count.'"></span>';
    }

    $count++;
}
Comments