Avatar of EICT
EICT
Flag for United Kingdom of Great Britain and Northern Ireland asked on

dynamically changing php variable name based on the number of rows in a table

Hi,
I would like to be able to create the following PHP code dynamically based on the number of rows in a table.

$DataSet->AddPoint(array($r1,$r2,$r3,$r4,$r5,$r6,$r7,$r8,$r9,$r10),$Series);

Open in new window


If the table has 3 rows the code should be
$DataSet->AddPoint(array($r1,$r2,$r3),$Series);

Open in new window


If the table has 4 rows the code should be
$DataSet->AddPoint(array($r1,$r2,$r3,$r4),$Series);

Open in new window


If it has 5 rows the code should be
$DataSet->AddPoint(array($r1,$r2,$r3,$r4,$r5),$Series);

Open in new window


etc...............................

$numbers holds the number of rows in the table.

Hope this makes sense?

Thank you
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
mankowitz

This is probably not what you really want to do. How do you know how many rows there are in the table? How will you know their names? Are they always named $rx, where x is a sequence? Is there any limit?

However, this should get you started. You want a variable variable, liike this:

$a = array();

for ($k=1; $k<=$number; $k++) {
      if (isset (${"r" . $k})) {
            $a[] = ${"r" . $k};
      }
}

print_r($a);
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
EICT

ASKER
Chris,
You are right. I think my cold was clouding my judgement and it would have been daft to try an list out of series of variables by some how creating the variable name on the fly when I can just pull them straight from the database as an array.

In the end this is what I did using the $names array. Seems a bit odd to have to convert from a multidimention array $rowheadings to another array $names in the way I have though!

$resultquestions = mysqli_query($dbci,  "SELECT shortQuestion FROM survey_questions WHERE surveyid = '$question'"); // Run the query.
$names = array();
while($rowheadings = mysqli_fetch_array($resultquestions, MYSQLI_ASSOC))
  {
    $name = $rowheadings['shortQuestion'];  
    $names[] = $name;
  }

 // Dataset definition
 $DataSet = new pData;
 $DataSet->AddPoint($names,"Label");
Ray Paseur

This won't matter to performance in a small data set, but unless you have a need the $name variable, the while loop might better be written this way.  Variable proliferation is an anti-practice to be avoided.
while($rowheadings = mysqli_fetch_array($resultquestions, MYSQLI_ASSOC))
{
    $names[] = $rowheadings['shortQuestion'];
}

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Chris Stanyon

You're not really converting from one array to another. You're looping through each record of you queryset and adding a field to the $names array. You don't actually need to assign the value to $name before assigning $name to your array:

$names = array();
$result = $dbci->query("SELECT shortQuestion FROM survey_questions WHERE surveyid = '$question'");

while( $name = $result->fetch_object() ):
	$names[] = $name->shortQuestion;
endwhile;

$dataSet = new pData;
$dataSet->AddPoint($names, "Label");

Open in new window

Ray Paseur

Agree with Chris -- $rowheadings is an associative array.  It has one element with the index name shortQuestion.  This structure is an artifact of the query and fetch method.  One instance of this array is returned from the query results set every time the while() iterator executes.  With PDO there is a convenient way to get an array of objects from the results set (I find arrays of objects to be very, very useful data structures).  See more on MySQL, PDO, etc, in this article.

The problem with variable proliferation takes a few forms.  First, your colleague (or maybe you, if it's been a while) may look at the code or the symbol table and ask, "What's $name for?"  Only after an exhaustive search will it be revealed that $name is not for anything - an unnecessary fifth wheel that just wasted your time!  Or you may use $name in another place in the code, perhaps in a different namespace, where it has a different meaning.  Understanding of the purpose of the variable may take a while, since $name is a very generic term.  Or you may use $name in the same namespace, accidentally relying on the assumption that it's either set or not set because of something external to this block of code, and this can result in a run-time failure.  So a good code-review practice is to explain the purpose and meaning of anything that gets assigned to the left of the equal sign.  This will usually lead to code with fewer unwanted variables, and more specific variable names.