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:".

5
  • 4
    Why the heck do you need to start out at one? Especially if you want the first row?? Commented Feb 25, 2011 at 3:39
  • don't try to ix something that is not broken. Commented Feb 25, 2011 at 3: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. Commented Feb 25, 2011 at 4: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 ? Commented Feb 25, 2011 at 4:39
  • Is there an example of this, russel? I'm still learning. Commented Feb 25, 2011 at 4:47

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

@Brandon: doesn't work is not an explanation of a problem. On such question the only answer I can give is: "so fix it somehow"
It just made it not echo anything at all.
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];

Comments

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.

4 Comments

Hmm after adding this I get Parse error: syntax error, unexpected '"', expecting ',' or ';' in /home/organik/public_html/beta/test.php on line 238
Line 238: $question=$_POST['question'][$i]."<br />"; echo "<b>Question ".$i+1.":</b> $question";
if(isset($_POST['question'])) { foreach (($_POST['question']) as $key=>$value) { $key +=1; echo "<b>Question ".$key.":</b> $value"; } }
Or try $question_no = $i + 1; $question=$_POST['question'][$i]."<br />"; echo "<b>Question $question_no :</b> $question"; hope it's work
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!

1 Comment

Oh, just add +1 at your $i Like : echo "<b>Question".$i+1.":</b>"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.