2
foreach ($arrQuestionId as $key=>$question) {

?>

<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"></span>:</strong></p>
</div>


<?php

}

?>

在上面的代码中,我有一个 while 循环,它显示QUESTION :. 现在我想做的是介于两者之间QUESTION:我想包含一个计数,以便每次QUESTION出现时,它旁边都包含一个计数,如下所示:

QUESTION 1:


QUESTION 2:


QUESTION 3:

...

如何才能做到这一点?

4

6 回答 6

0

你也可以试试下面的方法,

$countArray = array();
foreach ($arrQuestionId as $key=>$question) {
$countArray[] = $key;
}
count($countArray) or sizeOf()

(或)直接你可以得到没有 for 循环本身,如下所示

count($arrQuestionId)
于 2013-02-07T04:35:06.957 回答
0

取一个变量并增加它

$i=1;
foreach ($arrQuestionId as $key=>$question) {

?>

<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"><? echo $i; ?></span>:</strong></p>
</div>


<?php
$i++;
}

?>
于 2013-02-07T04:29:25.697 回答
0
$count = 1;
foreach ($arrQuestionId as $key=>$question) {

?>

<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"><?php echo $count++; $></span>:</strong></p>
</div>


<?php

}

?>
于 2013-02-07T04:29:33.500 回答
0
$i=0;
foreach ($arrQuestionId as $key=>$question) {
$i=$i+1;
?>
<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"></span>:<?php echo $i;?></strong></p>
</div>
于 2013-02-07T04:29:56.180 回答
0
$qno = 1;
foreach ($arrQuestionId as $key=>$question) {

?>

<div class='lt-container'>
    <p><strong>QUESTION <span id="quesnum"></span>:</strong><?php echo $qno?></p>
</div>


<?php
$qno++;
}

?>
于 2013-02-07T04:30:49.037 回答
0

创建一个变量并在 foreach 中递增它。

喜欢:

$s = 1;
foreach($value as $text) {
echo "Question #".$s." :".$text;
$s++;
}
于 2013-02-07T04:28:20.520 回答