Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP add +1 in while loop

Tags:

php

while-loop

I have while loop and in that there's forum posts.I want to show number of posts (1,2..).

Let me show you what i think

while($some = mysql_fetch_array($forum_posts)){
echo 'Number of post is $num++';
}

and show like

------ Thread -------

------ Posts -------

Text of post            1.
Text of post            2.
Text of post            3.

Thanks.Sorry for bad english

like image 565
harisdev Avatar asked Dec 13 '22 05:12

harisdev


2 Answers

$num must be initialized!

$num = 1;
while($some = mysql_fetch_array($forum_posts)){
echo 'Number of post is '.($num++);
}
like image 162
core1024 Avatar answered Dec 28 '22 23:12

core1024


$num = 0;

while($some = mysql_fetch_array($forum_posts)){
    echo 'Number of post is '.++$num;
}
like image 25
Vytautas Avatar answered Dec 28 '22 22:12

Vytautas