Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP Variable From A Variable?

Posted on 2009-07-03
21
304 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 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