/    Sign up×
Community /Pin to ProfileBookmark

I’m hoping a loop can replace several lines of code…

I need to echo out 20 usernames onto a form and I currently have 20 lines like this:

[CODE]<input type=”text” id=”userName1″ class=”userInfo” value=”<?php echo $userName1; ?>” />
<input type=”text” id=”userName2″ class=”userInfo” value=”<?php echo $userName2; ?>” />
.
.
<input type=”text” id=”userName20″ class=”userInfo” value=”<?php echo $userName20; ?>” />[/CODE]

I’m wondering if some kind of loop could replace all those lines. Can someone please share some advice & examples?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@tracknutJan 27.2012 — Here's one way:
<i>
</i>$userName1 = "Bob";
$userName2 = "Jane";
for ($i = 1;$i &lt;= 2;++ $i)
{
$v = "userName$i";
echo "&lt;input type="text" id="$v" class="userInfo" value="${$v}" /&gt;";
}


Dave
Copy linkTweet thisAlerts:
@ssystemsJan 27.2012 — Depends on how you store the original data. Here is a sample if you stored it in an array

[code=php]
foreach($usernames as $key=>$value)
<input type="text" id="username<?php echo $key; ?>" class="userInfo" value="<?php echo $value; ?>" />
[/code]
×

Success!

Help @spowel4 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.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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