Link to home
Start Free TrialLog in
Avatar of jasonkrueger
jasonkruegerFlag for United States of America

asked on

Help with PHP variable display from within loop

I'm trying to dynamically create the content on a page using a loop in PHP and the data elements are pulled from a config.php file.

within the config.php file i define some variables that I use sitewide:
define('NUMDAYS',2);
define ('DAY_1_TIMESLOT','12:00PM-5:00PM');
define ('DAY_1_BANDNAME','FAKE BAND');

define ('DAY_2_TIMESLOT','3:00PM-12:00AM');
define ('DAY_2_BANDNAME','ANOTHER BAND');

from my .php file that I'm calling to do the displaying, I do an include of config.php and I can easily display if I do a simple <?php echo DAY_1_TIMESLOT ?>;   however, I want to loop through the variables defined in config.php, but I can't figure out the syntax.

this is what i have:
<?php
for ( $counter = 1; $counter <= NUMDAYS; $counter += 1) {
      echo "<br />";
      echo "<strong>";
      echo DAY_.$counter._TIMESLOT . "  " . DAY_.$counter._BANDNAME;
      echo "</strong>";
}
?>

What I'd like it to display is:

12:00PM - 5:00PM  FAKE BAND
3:00PM - 12:00AM  ANOTHER BAND

Is there a way to dynamically reference the variable defined in config.php based on the value of $counter ?

Thank you to anyone who takes the time to review this question. Much appreciated as I am very new to PHP syntax.
ASKER CERTIFIED SOLUTION
Avatar of designatedinitializer
designatedinitializer
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave Baldwin
I wouldn't use 'define' for this purpose.  Use an array, they are easy to loop thru, especially with 'foreach'.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Band List</title>
</head>
<body>
<h1>Band List</h1>
<?php 
$bandlist = array(
	'FAKE BAND' => '12:00PM - 5:00PM',
	'ANOTHER BAND' => '3:00PM - 12:00AM'
	);

foreach($bandlist as $key => $value) {
      echo "<br />";
      echo "<strong>";
      echo "$value  $key";
      echo "</strong>";
		}

 ?>

</body>
</html>

Open in new window

Try this:
<?php
for ( $counter = 1; $counter <= NUMDAYS; $counter += 1) {
      echo "<br />";
      echo "<strong>";
      echo constant("DAY_{$counter}_TIMESLOT") . "  " . constant("DAY_{$counter}_BANDNAME");
      echo "</strong>";
}
?>

Open in new window

...acrobatics with constants... that's nice, though I wouldn't recommend it to the unexperienced.
Avatar of rinfo
rinfo

Where is the config.php included in your example code.
Can you check if you have forgotten to mention it here or in the original source code file.
Buy this book and give yourself a couple of weeks to work through the examples.  It will not make you a pro, but it will help you get a sense of the basics of PHP and a little computer science, and it will provide some structured learning.  That will be a lot better than trying to learn PHP from reading someone else's code.
http://www.sitepoint.com/books/phpmysql4/

The advice about arrays that you read here is the correct advice.  The SitePoint book will help you understand why, and how to put that advice into practice in a way that will serve you will as you go forward.  The really right answer is to use a relational data base.  SitePoint will show you why and how.  It's much easier than you think, I promise!
Avatar of jasonkrueger

ASKER

I went with the array approach and it is working nicely for the short term. I will however be moving all that stuff to a database soon as that seems like the most effective solution going forward.

thanks to all that responded. I learned alot.