Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

referance a variable from a string and a variable 1

Status
Not open for further replies.

Sylv4n

Technical User
Feb 27, 2002
83
GB
What I want to do is loop through a variang number of controls that all have the same prefix "select" but with a incrementing number on the end. and if it is there it will be = to "true" here is what I have so far:

for ($i = 0; $i < $howmanyfields; $i++) {
if ( &quot;select&quot; . $i == &quot;true&quot; ) {
print ( &quot;Number $i is avalable&quot;);
}
}

but it wont recogine the (&quot;select&quot; . $i) as ($select1) I haev tried to put a \$ @ the front but still the same problem, any ideas?
 
for ($i = 0; $i < $howmanyfields; $i++) {
if ( &quot;select&quot; . $i == &quot;true&quot; ) {
print ( &quot;Number $i is avalable $i&quot;);
}
else{
print ( &quot;Number $i is NOT available $i&quot;);
}

}
 
same problem,
Waht I need to be able to di is type

print ( &quot;\$select&quot; . $i . &quot;<br />&quot; );

or something like that and it will print

true

rather than waht it is soing at the moment which is:

$select0
 
Just for clarification, I have the following code:

for ($i = 0; $i < $howmanyfields; $i++) {
if ( &quot;select&quot; . $i == &quot;true&quot; ) {
print ( &quot;Number $i is avalable $i<br />&quot;);
} else {
print ( &quot;Number $i is NOT available $i<br />&quot;);
}
}

and the following URL (without initial bit)
URL/SelectStatement2.php?select1=true&select2=true&select4=true

and I am getting the following output:

Number 0 is NOT available 0
Number 1 is NOT available 1
Number 2 is NOT available 2
Number 3 is NOT available 3
Number 4 is NOT available 4

When clearly 1,2 & 4 are selected
 
Try this out:

<SCRIPT language=&quot;php&quot;>
for ($i = 0; $i < $howmanyfields; $i++)
{
$select = &quot;select&quot;.$i;
if ( $$select == true )
{
print ( &quot;Number $i is avalable $i<br />&quot;);
}
else
{
print ( &quot;Number $i is NOT available $i<br />&quot;);
}
}
</SCRIPT>

I've tested it, and it works beautifully. Note the &quot;$$&quot; in the if-clause... very key.

Mark
 
If these are HTML form inputs, why not just loop through $_GET as appropriate, building the array key name as needed?

Code:
<?php
for ($i = 0; $i < $howmanyfields; $i++) {
  if ( $_GET['select' . $i] == &quot;true&quot; ) {
    print ( &quot;Number $i is avalable $i<br />&quot;);
  } else {
    print ( &quot;Number $i is NOT available $i<br />&quot;);
  }
}

Want the best answers? Ask the best questions: TANSTAAFL!!
 
BRILLIANT,

works fine, thank you all very much
but I'm not sure why this works.

$select = &quot;select&quot;.$i;
if ( $$select == true )

are you saying the variable $select = (variable name) &quot;select0&quot;
but why the $$select?
 
Take a look at the PHP online manual section on variable variables:

However, I and the PHP manual both recommend that you reference $_GET rather than depend on PHP's registration of the variables.
Want the best answers? Ask the best questions: TANSTAAFL!!
 
Oops. Hit &quot;submit&quot; too soon.

In addition to the reasons given in the PHP manual page on register_globals, referencing $_GET and $_POST also produces better-documented code. Six months from now you may not remember how the value got into $foo, but $_POST['foo'] is self-evident.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
interesting,
I have changed my code accordingly, thanks sleipnir214
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top