Jump to content

[SOLVED] while(!empty($scorer)) {echo "$scorer"} - Need a solution


Recommended Posts

Hello Guys,

 

I am having a problem...

 

Heres the HTML code:

 

<input type="text" name="scorer" /><br />
<input type="text" name="scorer" /><br />
<input type="text" name="scorer" /><br />
<input type="text" name="scorer" /><br />

 

Heres the PHP code:

 

<?php

$scorer = $_POST["scorer"];

while(!empty($scorer)) { echo "$scorer"; }

?>

 

Now I made this script to take values from fields with the same name so a javascript code can add more fields in the event of more than 4 being needed.

 

So why, if the last field entry equals Bob for example then why does the script keep echoing

 

Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob
Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob
Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob 
Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob Bob 

 

I need help with this desperately, as the site is due to go live soon.

Then why doesn't this work either?

 

while(!empty($_POST["scorer"])) { echo $_POST["scorer"]; }

 

 

I am good enough at PHP to understand many errors, but this is the first time I have tried to do anything like this.... So how would I fix this issue?

because that is essentially the same exact thing as the first code...

 

your basically doing

 

hey set this variable to this value

 

while this variable has a value do echo that value

 

 

since you never do anything but echo the value, the while will always be true and thus it becomes an infinite loop

because that is essentially the same exact thing as the first code...

 

your basically doing

 

hey set this variable to this value

 

while this variable has a value do echo that value

 

 

since you never do anything but echo the value, the while will always be true and thus it becomes an infinite loop

 

So what would you recommend?

I want it to take all information from each field and insert each item individually into a database called scorers. The whole script will be a match report if you like, for a football club. So the user can add additional scorers if needed and no matter how many form fields, the script will use the data regardless.

well, the way your doing it won't really work. I'm not entirely sure, but having a bunch of fields with the same name will result in the last field being the value from $_POST[whatever] (or maybe the first).

 

However, if you change it from

<input type="text" name="scorer" /><br />

to

<input type="text" name="scorer[]" /><br />

 

then the scorer post var will be its own array (note the [] at the end of the name)

 

then you simply iterate through the array

 

$scorers = $_POST['scorer'];

foreach($scorers as $scorer){
//database insert code here
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.