1

I am trying to make a looped variable variable accessible outside of the loop. Everything works, but I am having trouble with this piece of the code:

$dayVar."_count" = $dayVarCount;

The full code is 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 = $row[$$dayVar];
        echo $$dayVar . ': ' . $dayVarCount . '<br>';

        $dayVar."_count". = $dayVarCount;
    }      
}

echo "$day_3_count";

?>
3
  • 1
    Please describe what is your trouble. IE: what you actually have, and what your expect to have. By the way, the expression $dayVar."_count". = $dayVarCount; cannot be a variable assignation. Commented Oct 1, 2012 at 7:48
  • 1
    Could you please provide us with the error message you are getting? Commented Oct 1, 2012 at 7:48
  • What is the significance of $day_1="sep_28";? is this important to what you're trying to do - I think that the answers might change if so. Commented Oct 1, 2012 at 8:10

4 Answers 4

2

i believe that your variable assignment of of $dayVar."_count" is incorrect or illegal. i would try ${$dayVar."_count"} instead.

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

1 Comment

Wow, I didn't know you could do that in php.
0

As Skrol29 rightly says, that line can not work:

An assignment (assigning a value to a veriable) always has the form

<variablename> <assignmentoperator> <value>

in php that means:

$day_1 = "123"

as you have already used.

Variable names are not usually not generic. If you need such functionality, use arrays. PHP arrays

Comments

0

You're almost in the rightplace.. but.

 for ($i = 1; $i <= 3; $i++)  
    { 
        $dayVar = "day_".$i; 
        $dayVarCount = $dayVar."_count";
    $$dayVarCount =  $row[$$dayVar];   
    }       


echo "$day_3_count"; 

Comments

0

Try this,

 <?php
 // Connect to server and select database.
$host   =   "host here";
$username="username here";
$password="pass here";
$db_name="db here";
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
   if(mysql_select_db("$db_name")){echo "Success";}else{echo "cannot select DB";}



   $day_1="sep_28";
   $day_2="sep_29";
   $day_3="sep_30";
   $id=1;
   $query = mysql_query("SELECT * FROM `tab1` WHERE id = '$id'");

   while ($row = mysql_fetch_assoc($query))
   {

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

    ${$dayVar."_count"} = $dayVarCount;
  }      
  }

  echo "$day_3_count";

 ?>


  <?php
   // close connection
  mysql_close();
  ?>

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.