Jump to content

[SOLVED] setting variable names and values from an array


drumhrd

Recommended Posts

ok I am not sure if this can be done or not.

 

let's say I have a table

 

Key  |  something_1 | something_2 | something_3

 

auto |  value_1        | value_2      | value_3

 

 

 

basically I do an SQL query to the table and get * where key = "uniq number"

 

I'd like to dynamically generate variables

 

$something_1 = value_1

 

$something_2 = value_2

 

$something_2 = value_3

 

 

So it would look something like

 

$result_row = $result->fetchRow(DB_FETCHMODE_ASSOC);

foreach ($result_row as $i =>$output)
if ($output != ""){

 

THEN

 

somehow generate variable with $i as the variable name and make it = $output.

 

 

 

DOES THIS MAKE SENSE??

 

any help is greatly appriciated.

 

thanks

 

There are generally better ways to work with data returned from a database query. You should know exactly what columns exist in each table, for example, so using dynamic variables like this is generally not needed.

 

However:

$num= "1";

while (true) {

  $somethingVar = 'something_'.$num;

  if (!isset($$somethingVar)) { break; }

  echo $$somethingVar;

  $coolVar = 'cool_'.$num;

  echo $$coolVar;

num++;

}

[code=php:0]

That's the point of having it in an array in the first place - so that you can use a foreach loop or something.  Which you used...to extract and make individual variables...only to turn around and want to automatically cycle through...

Maybe I am going about this the wrong way.

 

I have a table that has the following columns example.

 

tddate1 | tddate2 | tddate3 - tddate12      tdlocation1 | tdlocation2 | tdlocation3 - tdlocation 12  tdvenue1 | tdvendue2 | tdvenue3 - tdvenue 12

 

 

the "like" numbers go together..so tddate10, tdlocation10, and tdvenue10 all have info that relate together.

 

the user can enter up to 12 "tourdates"

 

So I want to be able to check if a key column has an entry. Like if tddate3 has an entry in it.  If so I want to echo out the date,location, and venue..then loop to the next set.

 

 

$num = 1;
While(tddate"$num" != ""){

echo $tddate"$num";
echo $tdlocation"$num";
echo $tdvenue"$num";

$num++
}

 

 

 

If there is a way I could do this and reference the fields while they are in the array that would be great, I know that pushing this stuff out to variables is redundant and taxing.

 

 

But seeing as I am just a week out of "learning php and mysql" BOOK!!  I am trying to jump straight in and start coding.

 

 

 

$result_row = $result->fetchRow(DB_FETCHMODE_ASSOC);

for ($i = 1; $i <= 12; $i++) {
    if ($result_row['tddate'.$i] != '') {
        echo $result_row['tddate'.$i];
        echo $result_row['tdlocation'.$i];
        echo $result_row['tdvenue'.$i];
    }
}

 

This only checks if there is a value in the tddate column.

 

 

There are better ways to store the data in the database.

 

For example, create a tour table with id, date, location, and venue as the columns.  You would link the tour with the user in a second table with the userId as one column and the tourId as the other.  If you want to limit the number of tours a user can create, do the checks in your php script.

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.