Solved

PHP Variable From A Variable?

Posted on 2009-07-03
21
311 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
[X]
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
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is this? I've never seen this... 2 30
php error 27 61
assigning javascript variable to php variable 8 43
blocking the posted data from outside of my domains 8 25
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 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