0

Hello I have this query

$upit = "SELECT id from usluga";
$rezultat = mysql_query($upit);
while($red = mysql_fetch_object($rezultat))
{
    echo "<li>{$red->id}</li>";
}

I want on exit to get this:

<li>1,2,3,4,5,6</li>
<li>7,8,9,10,11,12</li>
<li>13,14,15,16,17,18</li>
<li>19,20,21,22,23,24</li>

and so on..

Any help?

Dead Man
  • 2,880
  • 23
  • 37
Tanja Pakovic
  • 193
  • 1
  • 3
  • 14
  • 1
    [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Apr 04 '13 at 10:22
  • This is just simple example, first i got in mind, i need good exit, this can be done with and array, not matter – Tanja Pakovic Apr 04 '13 at 10:24
  • what code have you tried? – Mayukh Roy Apr 04 '13 at 10:24
  • I tried with If statments, but not good :( – Tanja Pakovic Apr 04 '13 at 10:29

2 Answers2

5

You can use the mod operator(%) or use array functions to achive this

$upit = "SELECT id from usluga";
$rezultat = mysql_query($upit);

while ($red=mysql_fetch_object ($rezultat)) {
    $t[] = $red->id;
}

$chunks = array_chunk($t, 6);
foreach ($chunks as $chunk) {
    echo "<li>" . join(",", $chunk) . "</li>";
}
Philipp
  • 15,377
  • 4
  • 35
  • 52
0

try this..

$upit="SELECT id from usluga";
$rezultat=mysql_query($upit);
$i=0;
$liflag=0;
while ($red=mysql_fetch_assoc($rezultat))
{
    if($i<6)
    {
        $liflag=1;
        if($i==0)
        {
            echo "<li>$red->id";
            $i++;
            continue;
        }
        else 
        {
            echo ",$red->id";
            $i++;
            continue;
        }
    }
    else
    {
        echo ",$red->id</li>";
        $i=0;
        $liflag=0;
        continue;
    }
}
if($liflag==1)
{
    echo "</li>";
}
MKV
  • 913
  • 7
  • 6