Ben L
Ben L

Reputation: 127

How to call on a PHP dynamic variable name?

I have 24 pieces of data on an SQL server. When my webpage starts it pulls all this data one at a time from the server and displays it. Looks like this:

$sql = "SELECT `$dateName` FROM `$user` WHERE hour=2";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     while ($row = $result->fetch_assoc()) {
         $hour2 = $row[$dateName];
     }
}
$sql = "SELECT `$dateName` FROM `$user` WHERE hour=3";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {
         $hour3 = $row[$dateName];
     }
}

etc... until hour 24.

But I think it is better with a for loop. Like this:

for ($x = 1; $x <= 24; $x++) {

    $sql = "SELECT `$dateName` FROM `$user` WHERE hour=`$x`";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
             $hour.$x = $row[$dateName];
         }
    }                          
}

I want to call $hour1, $hour2, $hour3. By adding $x to $hour. How do I do this? Thank you so much!

Upvotes: 2

Views: 101

Answers (5)

Amit Ray
Amit Ray

Reputation: 3485

function pullRequest($dateName,$user,$x){
    //Define $conn here
    $sql = "SELECT `$dateName` FROM `$user` WHERE hour=`$x`";
    $result = $conn->query($sql);
    $hour = array();
    if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
             $hour[$x] = $row[$dateName];
         }
    }
    return $hour[$x];

} 

Then you can call it like this
$dateName ="DATENAME";
$user="USER";
 for ($x = 1; $x <= 24; $x++) {
    pullRequest($dateName,$user,$x);
 }

Upvotes: 1

Alan Tan
Alan Tan

Reputation: 347

You can create $hour as an array, use $x as the keys and setting the appropriate values to the keys.

EG:

$hours = array();

for ($x = 1; $x <= 24; $x++) {

    $sql = "SELECT `$dateName` FROM `$user` WHERE hour=`$x`";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
             $hour[$x] = $row[$dateName];
         }
    }
}

echo $hour[1];

Note:

You should consider using a more efficient way, where you do not have to connect to the database 24 separate times for each hour.

EG:

$sql = "SELECT `$dateName` FROM `$user` ORDER BY hour ASC";

This will return all 24 rows in one query, then you can manipulate the data with PHP.

Upvotes: 1

sevavietl
sevavietl

Reputation: 3802

You can do this with Variable variables:

${"hour" . $x} = $row[$dateName];

Upvotes: 1

Andrei Todorut
Andrei Todorut

Reputation: 4526

Wrap them into ${'hour'.$x} = $row[$dateName]

Upvotes: 2

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5049

Instead of generating dynamic variables, I would suggest to create array of $hours.

$hour = array();
for ($x = 1; $x <= 24; $x++) {

    $sql = "SELECT `$dateName` FROM `$user` WHERE hour=`$x`";
    $result = $conn->query($sql);


    if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
             $hour[$x][] = $row[$dateName];
         }
    }
 }

So that you can access like $hour[1],$hour[2]...etc...

Upvotes: 3

Related Questions