Jump to content

setting vars with recurring numbers within the vars


pakenney38

Recommended Posts

For example, I got this to work:

[code]<?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>';
}

?>[/code]

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?
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.
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!
Before posting code that "looks right," please test it first to see if it is right.

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

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.