0

I am trying to use a for loop to select an incremental value with a MySQL query. I have included sample code below:

<?php

$day_1="sep_28";
$day_2="sep_29";
$day_3="sep_30";

$query = mysql_query("SELECT * FROM table WHERE id = '$id'");

while ($row = mysql_fetch_assoc($query))
{
for ($i = 1; $i <= 3; $i++) 
    {
        $dayVar = "day_".$i;
        //$dayVarCount = $dayVar."_count"; // Don't really need this anymore, so removed.
        $dayVarCount = $row[$$dayVar];
        echo "$$dayVar.': '.$dayVarCount<p>"; // Edited.
    }      
}
?>

I think I am getting close, but when I run the code my page is showing this:

$day_1.': '.0

$day_2.': '.2

$day_3.': '.5

Any additional recommendations? Thanks for the great help!

2
  • 1
    Can you explain a little more? I don't understand what you're trying to accomplish here. Running queries in a for loop can be pretty inefficient. If there's anyway to accomplish it with a single sql query, you will be much better off. Commented Oct 1, 2012 at 6:36
  • What exactly do you try to display? Do you want to count the days of the selected month? Commented Oct 1, 2012 at 6:37

4 Answers 4

1

Try a variable variable:

for ($i = 1; $i <= 3; $i++) 
{
    $dayVar = "day_".$i;
    $dayVarCount = $dayVar."_count";
    $$dayVarCount = $row[$$dayVar];
    echo $$dayVar.': '.$$dayVarCount.'<p>'; // Edited.
}

This basically uses a string to reference a variable by its name.

Just think of it this way:

$variable = 'hello';

$string = 'variable';

echo $$string;
// Is the same thing as:
echo $variable;

// Because you can thing of $$string as ${$string} ---> $variable when {$string} is interpreted into 'variable'

http://php.net/manual/en/language.variables.variable.php

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks stegrex, I looked at some of the documentation for this and thought it might be the right path, I will try this code out. THANK YOU!
@Brandon Sure thing, and good luck! You can let me know if it works or not and I can update my answer.
Thanks for the explanation @Stegrex! I have updated the code above, I think I am getting close.
@Brandon I've made some edits to my answer. Basically, not having the variable variable be interpolated in the double quotes.
Ive implemented the code and think I am getting closer here, please see above. Also, I added an additional double quote at the beginning of the "echo" line as there wasn't one. THANKS!
|
1

Replace this line:

echo "$$dayVar.': '.$dayVarCount<p>";

with this:

echo $$dayVar . ': ' . $dayVarCount . '<br>';

2 Comments

Yep, @Stegrex had given me all that code, but your adjustment made it all click. Thank you very much!
Glad to hear that, yes @Stegrex solution is fitting I guess. I upvote that! :D
0
<?php

$day_1="January 1";
$day_2="January 2";
$day_3="January 3";

$query = mysql_query("SELECT * FROM dates WHERE id = '$id'");

while ($row = mysql_fetch_assoc($query))
{
for ($i = 1; $i <= 3; $i++) 
    {
        $var = $."day_".$i;
        $day_$i_count=$row['$var'];
        echo "$day_$i: $day_$i_count<p>";
    }       
}
?>

You can try this code too.

Comments

0

That all seems overly complicated, but possibly I don't understand the question adequately. Would this work?

$day[1]="sep_28";
$day[2]="sep_29";
$day[3]="sep_30";

$query = mysql_query("SELECT * FROM table WHERE id = '$id'");

while ($row = mysql_fetch_assoc($query))
{
    foreach ($day as $day_str) 
    {
        echo $day_str . ':' . $row[$day_str] . '<p>';
    }      
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.