Jump to content

Dynamically naming a variable


stevepatd
Go to solution Solved by stevepatd,

Recommended Posts

I have a database table with 52 values (one for every week of the year).  The column names are Week1, Week2, etc. When processing this I would like to use a 'for' loop from 1 to 52 and create something like this:

  for ($n=1; $n<=52; $n++) {
    $variable = '$Week' . $n;
    if ($variable == something) {
      do the thing;
      }
    }// for

 

then if the value in that weeks cell matches the expected value I do my thing.  How can I dynamically create my "variable" that is the name of the variable so I can test the value of it?  I don't want to write a little snipet of code 52 times to see if $Week1 == something, $Week2 == something......

 

Link to comment
Share on other sites

  • Solution

Sorry to have bothered you all.  I figured it out.  Instead of using a variable for each column of the database I can use the $row array after the mysqli_fetch_array.  That makes it really sweet.

 

I really appreciate this site and all the effort you experts put into answering our questions.

Link to comment
Share on other sites

2 hours ago, stevepatd said:

Instead of using a variable for each column of the database I can use the $row array after the mysqli_fetch_array.  That makes it really sweet.

That is also very, very much the more appropriate solution than creating dynamically-named variables. So that's great 👍

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.