1

So I have an array $papers, that comes from a database. The size of the array can change at the will of whoever changes the database.

I thought that if I did something like:

foreach($papers as $paper) {
    echo '<option value="' . $paper . '">' . $paper . '</option>';
}

I could get a dynamic list in my form. However, for some reason when I try this, the webpage just comes up blank. Obviouslly I could do something like:

for($i = 0; $i < SOME_NUMBER; $i++) {
    echo '<option value="' . $papers[$i] . '">' . $papers[$i] . '</option>';
}

But with the size of the array being variable, it doesn't seem like a viable option. Also, when I use the second method of a simple for loop, the page loads, and the values do actually come up. I'm very confused as to why that is though. Any help would be awesome, thanks.

8
  • 1
    Do print_r($papers) and see what you get. Commented Jun 20, 2012 at 15:47
  • 1
    Option 1 is fine. What does print_r($papers); give you? Blank page means you need to turn on error reporting. Commented Jun 20, 2012 at 15:47
  • 1
    heres a couple of things that might help get it sorted. firstly try counting out how many results are in $papers 'echo count($papers);' also echo "<pre>"; print_r($papers); echo "</pre>"; your foreach should work fine. Commented Jun 20, 2012 at 15:47
  • Also, php -l filename.php to check it for parse errors. Commented Jun 20, 2012 at 15:48
  • "The webpage comes up blank" sounds like you have error reporting disabled. You could try ini_set("display_errors", 1); Remember to disable it again after you are done degugging Commented Jun 20, 2012 at 15:49

3 Answers 3

2

you can modify your foreach loop like this

foreach($papers as $key => $value) {
    echo '<option value="' . $key . '">' . $value . '</option>';
}

where $key is holding the index and $value is holding actual value of array

i am not sure what data u are getting in your $papper array but i am guessing u can also do like this

foreach($papers as $paper) {
    echo '<option value="' . $paper['your_db_field_name'] . '">' . $paper['your_db_field_name'] . '</option>';
}

with simple for loop you can use sizeof() like below

for($i = 1 ; $i <= sizeof($pappers) ; $i++){
//your logic
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to use the array referencing method, try this:

for($i = 0; $i < count($papers); $i++) {
    echo '<option value="' . $papers[$i] . '">' . $papers[$i] . '</option>';
}

The count() function will return the length of the array which, as you say, may be different each time you run it.

Comments

0

turn on the error reporting

error_reporting(E_ALL | E_STRICT);
init_set('display_errors', 'on');

and try to debug with :

print_r($papers);

and look the structure of the array $papers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.