Solved

PHP Variable From A Variable?

Posted on 2009-07-03
21
275 Views
Last Modified: 2013-12-13
Is there any way to make a variable from a variable?  I want it to dynamically create it for me so when I loop through the exact same code, it will get all 365 days for me.  So the desired end result will be:

1, 2, 3, 4....365 for $day365
daily1reading, daily2reading, daily3reading, daily4reading...daily365reading for $dailyreading

The $day365 works great, but the $dailyreading doesn't work, it always echos "$daily1reading" instead of daily1reading

any ideas would be appreciated!
<?php $day365 = 0; ?>
 

<td>

<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>

</td>
 

<td>

<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>

</td>
 

<td>

<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>

</td>
 

<td>

<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>

</td>

Open in new window

0
Comment
Question by:bschwarting
  • 8
  • 5
  • 5
  • +1
21 Comments
 
LVL 2

Expert Comment

by:AphX
ID: 24771924
What's not working with the $dailyreading variable?

It's easier if you attach all of the code.
<?php
 

$day[];
 

for($i = 0; $i <365; $i++){

    echo "<td>";

    $day[$i] = $day[$i] + 1; $dailyreading = $daily . $day[$i] . reading; echo $dailyreading;

    echo $day[$i];

   echo "</td>";

}
 

?>

Open in new window

0
 
LVL 2

Expert Comment

by:AphX
ID: 24771932
<?php

$day[];
 
for($i=0; $i<365; $i++){
       echo "<td>";
       $day[$i] = $day[$i] + 1;
       $dailyreading = $daily . $day[$i] . reading;
       echo $dailyreading;
       echo $day[$i];
       echo "</td>";
}
 
?>
0
 
LVL 2

Expert Comment

by:AphX
ID: 24771939

<?php
 

$day[];

 

for($i=0; $i<365; $i++){

       $day[$i] = $i;

       echo "<td>";

       $day[$i] = $day[$i] + 1;

       $dailyreading = $daily . $day[$i] . reading;

       echo $dailyreading;

       echo $day[$i];

       echo "</td>";

}

 

?>

Open in new window

0
 
LVL 2

Expert Comment

by:AphX
ID: 24771942
Sorry, remove the line 8 like this:
<?php

 

$day[];

 

for($i=0; $i<365; $i++){

       $day[$i] = $i;

       echo "<td>";

       $dailyreading = $daily . $day[$i] . reading;

       echo $dailyreading;

       echo $day[$i];

       echo "</td>";

}

 

?>

Open in new window

0
 
LVL 1

Author Comment

by:bschwarting
ID: 24771947
sorry, forgot to include the variables from the top.  it will echo the variable name instead of the variable result.



$daily1reading = 'Here is your reading for January 1st';
$daily2reading = 'Here is your reading for January 2nd';
$daily3reading = 'Here is your reading for January 3rd';
$daily4reading = 'Here is your reading for January 4th';
...
$daily365reading = 'Here is your reading for January 5th';

<?php $day365 = 0; ?>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>
 
<td>
<?php $day365 = $day365 + 1; $dailyreading = $daily . $day365 . reading; echo $dailyreading; ?><?php echo $day365; ?>
</td>


0
 
LVL 1

Author Comment

by:bschwarting
ID: 24771952
so when you echo $daily1reading it echos "$daily1reading" instead of "Here is your reading for January 1st"
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24771956
sorry, when you echo $dailyreading it echos "$daily1reading" instead of "Here is your reading for January 1st"
0
 
LVL 5

Expert Comment

by:8riaN
ID: 24771988
I'm not sure what you're trying to accomplish.  To produce the output you seem to be looking for above, I'd use this loop:

for ($day=1; $day<=365 ; $day++) {
      echo "<td>daily".$day."reading".$day."</td>";
}

Is there some reason you need these variables later? If so is there a reason you can't use an array?
0
 
LVL 2

Expert Comment

by:AphX
ID: 24772015
Try this code...

It's not complete but it's a start of what I think you need...

Give me feedback please
<?

      function ordinalize($number) {

      if (in_array(($number % 100),range(11,13))){

      return $number.'th';

      }else{

      switch (($number % 10)) {

      case 1:

      return $number.'st';

      break;

      case 2:

      return $number.'nd';

      break;

      case 3:

      return $number.'rd';

      default:

      return $number.'th';

      break;

      }

      }

      }

	  

	  

	  $dailyreading;

	  // Define January

	  for($i=1; $i<32; $i++){

	  $dailyreading[$i] = 'Here is your reading for January '.ordinalize($i);

	  echo $dailyreading[$i];

	  echo "<br />";

	  }

?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24772154
You can create variable names dynamically, and use double $$ to resolve it, like this:

$daily1reading = 'Here is your reading for January 1st';
$day365 = 1;
$var = "daily{$day365}reading";
$dailyreading = $$var;
echo $dailyreading;

...but it would probably be better to use a loop, similar to what AphX and 8riaN have suggested. Could you explain in more detail what you want to achieve?
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24772179
You don't need the intermediate variable $var:

$dailyreading = "daily{$day365}reading";
echo $$dailyreading;
0
 
LVL 5

Expert Comment

by:8riaN
ID: 24772286
I'd do this:
<table>

<?php

$thisyear = date('Y',strtotime('Now'));

if (cal_days_in_month(CAL_GREGORIAN, 2, $thisyear) == 29) {$numdays=366;} else {$numdays=365;}

for ($daynum=1; $daynum <= $numdays ; ++$daynum ) {

	$newday = mktime(0,0,0,1,$daynum,$thisyear);

	echo "<tr><td>Here is your reading for ".date('F jS',$newday)."</td></tr>";

}

?>

</table>

Open in new window

0
 
LVL 5

Expert Comment

by:8riaN
ID: 24772291
And just remove all the $daily1reading variables.
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24772324
reading through all this now, just a minute guys and thanks for all this!
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24772394
ok, what cxr said did what I wanted perfectly.  now i just need a simple loop of this code for 365 days.  how is best to do that for this code?

<td>
<?php $day365 = $day365 + 1; $dailyreading = "daily{$day365}reading"; echo $$dailyreading; ?>"><?php echo $day365; ?>
</td>
$loop365 = 0;

if $loop365 < 365

$loop365 = $loop365 + 1;

echo "<td>

<?php $day365 = $day365 + 1; $dailyreading = "daily{$day365}reading"; echo $$dailyreading; ?>"><?php echo $day365; ?>

</td>"

} else {

echo "<input type="submit" value="Update All 365 Days Now!"><input type="Reset">";

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24772495
Something like this, maybe... though it would be easier to read if you split it on multiple lines. You would need to insert <tr></tr> tags for every x days. Again, if you explain what you want to achieve, it would be easier to give you good advice.
for($loop365=1;$loop365<=365;$loop365++) {

  $dailyreading = "daily{$day365}reading"; 

  echo '<td>'.$$dailyreading.'</td>';

}

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24772502
sorry, wrong variable in the loop:
for($loop365=1;$loop365<=365;$loop365++) {

  $dailyreading = "daily{$loop365}reading"; 

  echo '<td>'.$$dailyreading.'</td>';

}

Open in new window

0
 
LVL 1

Author Comment

by:bschwarting
ID: 24772590
i am writing out a table that is 13x29, with a checkbox for the user to select if they have completed that days task.  beside each checkbox is the day, 1-365. (see attached image) i will also need to put in the </tr><tr> like you said every 13 runs of the loop to start the new row.  right now I have the attached code copy/pasted 365 times!  this loop will help when i figure it out.
<td align="center">

<span title="<?php $day365 = $day365 + 1; $dailyreading = "daily{$day365}reading"; echo $$dailyreading; ?>"><?php echo $day365; ?></span>

</td>

<td align="center">

<input type='checkbox' value='1' name='<?php echo answer . $day365; ?>'>

</td>

Open in new window

365-days.png
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24772604
also, when you hover over each number the title will pop up with the $daily1reading - $daily365reading text.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24772704
How does this work:
<?php

echo '<table><tr>';

for($loop365=1;$loop365<=365;$loop365++) {

  if(($loop365-1)%13==0) echo '</tr><tr>'; ?>

  <td align="center"><span title="<?php 

  echo "Daily reading for ".date('F jS',mktime(0,0,0,1,$loop365)); 

?>"><?php echo $loop365; ?></span></td>

<td align="center">

<input type='checkbox' value='1' name="answer<?php echo $loop365; ?>" />

</td><?php

}

echo '</tr></table>';

?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:bschwarting
ID: 31599539
perfect!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now