0

Why doesnt this work? I need it to start out at 1 instead of 0. And if I change $i to = 1 then it doesnt grab the first row.
http://www.mcregister.com/beta/test.php

<?php  
if(isset($_POST['question'])) {  
   for ($i=0; $i<count($_POST['question']);$i++) {  
      $question=$_POST['question'][$i]."<br />"; echo "<b>Question $i:</b> $question";  
   }    
}  
?>

EDIT: Instead of starting out at 1.. I just need it to echo starting with "Question 1:" instead of "Question 0:".

Brandon
  • 13
  • 1
  • 3
  • 4
    Why the heck do you need to start out at one? Especially if you want the first row?? – Peter C Feb 25 '11 at 03:39
  • don't try to ix something that is not broken. –  Feb 25 '11 at 03:55
  • I should rephrase.. the count works fine.. but then I do the last echo "Question $i" that says Question 0... needs to say question 1. – Brandon Feb 25 '11 at 04:01
  • 1
    Don't change the keys of the array for something this trivial. Either, `$i + 1` or, alternatively initialize another variable to keep count of the questions ? – Russell Dias Feb 25 '11 at 04:39
  • Is there an example of this, russel? I'm still learning. – Brandon Feb 25 '11 at 04:47

5 Answers5

3

All array keys, by default, begin at index 0 (unless explicitly stated), therefore starting at 1 will not include the first result.

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
1

If you need posted data started with 1 (which is pointless actually) you have to change your html from

<input type="text" name="question[]" class="text">

to

<input type="text" name="question[1]" class="text">

etc

zerkms
  • 249,484
  • 69
  • 436
  • 539
1

If all you want to do is output each question (without any validation!) you don't need the question variable at all. Just do:

echo 'Question #' . ($i + 1) . ': ' . $_POST['question'][$i];
mesch
  • 195
  • 9
0

For all the answers here.. and i'm sure they know it.. for logical and simple reason, start your array at zero. Because.. like Russel Dias says: all array start at 0. If you don't want to make it complicated, start it at 0.

A high recommandation of all programmers :D Good Luck!

ThorDozer
  • 104
  • 2
  • 12
0

It's seem like you would like to get output

<b>Question 1:</b> Blah Blah

But. The array key is start from 0 by default

There have 2 way to get it. If you really want an array start with key number 1 . you can do by following code.

$new_array = array();
for ($i=0; $i<count($_POST['question']);$i++) {  
      $new_array[$i+1] = $_POST['question'][$i];  
} 

but if you want to have just 1. you can do like

if(isset($_POST['question'])) {  
   for ($i=0; $i<count($_POST['question']);$i++) {  
      $question=$_POST['question'][$i]."<br />"; echo "<b>Question ".$i+1.":</b> $question";  
   }    
}

Hope it's helpful.

Thurein Soe
  • 178
  • 3
  • 8
  • Hmm after adding this I get `Parse error: syntax error, unexpected '"', expecting ',' or ';' in /home/organik/public_html/beta/test.php on line 238` – Brandon Feb 25 '11 at 04:18
  • Line 238: `$question=$_POST['question'][$i]."
    "; echo "Question ".$i+1.": $question";`
    – Brandon Feb 25 '11 at 04:18
  • `if(isset($_POST['question'])) { foreach (($_POST['question']) as $key=>$value) { $key +=1; echo "Question ".$key.": $value"; } }` – Thurein Soe Feb 25 '11 at 05:22
  • Or try `$question_no = $i + 1; $question=$_POST['question'][$i]."
    "; echo "Question $question_no : $question";` hope it's work
    – Thurein Soe Feb 25 '11 at 05:24