foreach/while?

the code below works fine, but isn’t it possible to accomplish the same thing with a foreach loop?
thanks in advance for any guidance.

php:
[php]<?PHP
$hours= $_POST[‘hour0’] . $_POST[‘hour1’] . $_POST[‘hour2’] . $_POST[‘hour3’] . $_POST[‘hour4’] . $_POST[‘hour5’];
print ($hours);
?>[/php]

html:
[php]

[/php]

Foreach is probably not best choice here… it would be good if you had an array of text boxes, like this:

<td><input type="text" name="hour[0]" /></td> <td><input type="text" name="hour[1]" /></td> <td><input type="text" name="hour[2]" /></td> <td><input type="text" name="hour[3]" /></td> <td><input type="text" name="hour[4]" /></td> <td><input type="text" name="hour[5]" /></td>

Then, your php code with foreach loop could be:
[php]<?php
$hours = ‘’;
if(is_array($_POST[‘hour’])) foreach($_POST[‘hour’] as $key=>$val){
$hours.= $val;
}
print ($hours);
?>[/php]

Or, even shorter without the foreach loop:
[php]<?php
$hours = implode(’’, $_POST[‘hour’]);
print ($hours);
?>[/php]

And with your current html code, you can use the for loop:
[php]<?php
$hours = ‘’;
for($i=0; $i<=5; $i++) $hours.=$_POST[‘hour’.$i];
print ($hours);
?>[/php]

excellent. precisely what I wanted.

I’ve just begun using various help sites such as this, (ubuntu config, apache config, html usage, etc.) and I have begun posing a short question after each response that succeeds in solving my problem. Something of an informal survey, if you will.

I wonder if you’d take the time time to respond.

Why do you offer this sort of help? Don’t you have better things to do? What’s the benefit of assisting someone in this way when there is no obvious ‘payback’?

My questions are posed in all good cordiality. I’m just quite frankly surprised at the quality of help and generosity of people who frequent lists such as this.

Thanks in advance for your response. And, if I’ve exceeded the bounds of this group, please forgive me.

Cordially,

Amalafrida

Well, your question was simple and I just answered because I thought it will help. But when I have time to response to a more difficult questions, I often learn something new.

Sponsor our Newsletter | Privacy Policy | Terms of Service