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};
}
}
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.
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");
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.
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);