/    Sign up×
Community /Pin to ProfileBookmark

Help with a PHP Foreach Loop

Hi All

I am using a foreach loop to display a list of input fields, as below:

[code=php]
<?php

$dateArray = array(“01″,”02″,”03″,”04″,”05″,”06″,”07″,”08″,”09″,”10″,”11″,”12″,”13″,”14″,”15″,”16″,”17″,”18″,”19″,”20″,”21″,”22″,”23″,”24″,”25″,”26″,”27″,”28″,”29″,”30″,”31”);

foreach ($dateArray as $displayDate) {

$textStartMiles = “startMiles” . $displayDate;
echo “<input type=’text’ class=’inputWidths’ id='” . $textStartMiles . “‘ name='” . $textStartMiles . “‘ maxlength=’40’ value=’$startMiles” . $displayDate . “‘ />”;
}

?>
[/code]

The loop works well in that the list produces 31 input fields and when submitted the data is sent to session as $startMiles01, $startMiles02 … $startMiles31.

However, the value being displayed on screen is the $displayDate data i.e. 01, 02 … 31.

Can somebody see where my value=” code is incorrect?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@MarcinAug 11.2008 — and you want to have: startMiles01, startMiles02, startMiles03 ??? insted of 01, 02, 03 ???
Copy linkTweet thisAlerts:
@Mr__E__CrypticAug 11.2008 — Also, swap round " and ' in your echo statement;

[COLOR="#ff0000"]'[/COLOR]<input type=[COLOR="SeaGreen"]"[/COLOR]text[COLOR="#2e8b57"]"[/COLOR] class=[COLOR="#2e8b57"]"[/COLOR]inputWidths[COLOR="#2e8b57"]"[/COLOR] id=[COLOR="#2e8b57"]"[/COLOR][COLOR="Red"]'[/COLOR] . $textStartMiles . [COLOR="#ff0000"]'[/COLOR][COLOR="#2e8b57"]"[/COLOR] etc etc.
Copy linkTweet thisAlerts:
@MarcinAug 11.2008 — I don't know what do you want to achive and why you use in your code

$startMiles - which is undeclared and is empty - that's why you get only numbers. If you want to display value as startMiles01 - for example, just remove $ sign from $startMiles like below:

value='startMiles" . $displayDate . "'
Copy linkTweet thisAlerts:
@peteybauthorAug 11.2008 — Hi

I do not want to display the value as startMiles01, but the value assigned to $startMiles01.
Copy linkTweet thisAlerts:
@peteybauthorAug 12.2008 — Thought i would repost before going off the page! Any ideas anyone?
Copy linkTweet thisAlerts:
@Mr__E__CrypticAug 12.2008 — I do not want to display the value as startMiles01, but the value assigned to $startMiles01.[/QUOTE]

Okay,

where / how are you assigning a value to $startMiles01 ?
Copy linkTweet thisAlerts:
@peteybauthorAug 12.2008 — I am using the following code at the very beginning of the page:

[code=php]
session_start(); // Declaring the session array
foreach($_POST as $key => $value){ // Sending each posted variable to the session array
$_SESSION[$key] = $value;
}

[/code]


I also have the input field within the form:

[code=php]

<form method="post" action="<?php echo $_SESSION['PHP_SELF'] ?>" name="page1">

[/code]
Copy linkTweet thisAlerts:
@the-ferretAug 12.2008 — Looks like you are trying to dsiplay a variable variable.

see http://www.php.net/manual/en/language.variables.variable.php

I've not used it before but looks like you need to do something like;

$textStartMiles = "startMiles" .$displayDate;

echo "<input type='text' class='inputWidths' id='" . $textStartMiles . "' name='" . $textStartMiles . "' maxlength='40' value='$startMiles" . ${$textStartMiles} . "' />";

good luck!
Copy linkTweet thisAlerts:
@peteybauthorAug 12.2008 — Ah, Variable variables!!! what a clever name to give a command!!

It has worked a treat as per your example above.
×

Success!

Help @peteyb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...