Jump to content


Photo

setting vars with recurring numbers within the vars


  • Please log in to reply
6 replies to this topic

#1 pakenney38

pakenney38
  • Members
  • PipPipPip
  • Advanced Member
  • 90 posts

Posted 10 July 2006 - 04:40 AM

For example, I got this to work:

<?PHP

for ($counter = 1; $counter <= 200; $counter++)
{
  $T[$counter] = 'T' . $counter;
  echo $T[$counter] . '<br>';
}

if ($T[1])
{
echo 'yes<br>';
}

else
{
echo 'no<br>';
}

if ($T[2])
{
echo 'yes<br>';
}

else
{
echo 'no<br>';
}

?>

But what if I didn't want my variables to come out named $T[1], $T[2], $T[3]....etc?
What if I wanted $T1, $T2, $T3....etc?
How do I do this without setting each variable $T1 through $T200 independently?


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 05:22 AM

Why would you want individual variables instead of an array? Arrays are so much easier to use.

BTW, please change the tags around your code from [quote][/quote] to [code][/code]

Ken

#3 pakenney38

pakenney38
  • Members
  • PipPipPip
  • Advanced Member
  • 90 posts

Posted 10 July 2006 - 01:06 PM

Well, for this project I am dealing with some 220 HTML form fields whose values must find their way to a MySQL database. For the sake of my sanity, I am trying to keep the form fields the same name as the PHP variables and the MySQL fields. Also, time is the primary concern for this project, as are all of my projects at work, so generally while coding I like any amount of code that I can copy and paste, even if it's just field names.

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 10 July 2006 - 01:24 PM

<?PHP

for ($counter = 1; $counter <= 200; $counter++)
{
  $T[$counter] = 'T' . $counter;
  echo $T[$counter] . '<br>';
}

$t1=$T[1];
$t2=$T[2];

if ($t1)
{
echo 'yes<br>';
}

else
{
echo 'no<br>';
}

if ($t2)
{
echo 'yes<br>';
}

else
{
echo 'no<br>';
}

?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 just-j

just-j
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts

Posted 10 July 2006 - 01:44 PM

for the

$t1 = $t[1];
ect....

instead of typeing all those out couldnt you just do a loop.  ive been studing php for about a week now so i couldnt really type an example but ill try  ;)

for ($i = 1; $i <=200; $i++)
{
$t.$i = $t[.$i.];
}

looks like it would work..  i could be way off though..  HAH!

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 02:02 PM

Before posting code that "looks right," please test it first to see if it is right.

Your code:
<?php
for ($i = 1; $i <=200; $i++)
{
$t.$i = $t[.$i.];
}
?>
Is incorrect. The correct way of doing this is:
<?php
for ($i = 1; $i <=200; $i++)
{
${t.$i} = $t[$i];
}
?>

Ken

#7 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 10 July 2006 - 02:10 PM

${t.$i}

Never come across that before. Does it allow you to concatenate two strings to create a variable name?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users