Solved

Help with PHP variable display from within loop

Posted on 2012-04-03
7
212 Views
Last Modified: 2012-04-04
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.
0
Comment
Question by:jasonkrueger
[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
7 Comments
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 500 total points
ID: 37804366
First things first:
what you have there are constants, not variables.

What you need is an array.
You define an array like this:

$one_array = array();

Open in new window

or like this:

$timeslots = array( '12:00PM-5:00PM', '3:00PM-12:00AM');
$bands = array( 'FAKE BAND', 'ANOTHER BAND');

Open in new window

...and then, you get the arrays' elements' values like this:

echo $timeslots[0];  // this will output '12:00PM-5:00PM'
echo $timeslots[1];  // this will output '3:00PM-12:00AM'

Open in new window


You can loop thru the arrays like this:

for($i=0; $i<count($timeslots); $i++){
    echo $timeslots[$i];
    echo $bands[$i];
}

Open in new window


for more info read this:
http://www.php.net/manual/en/language.types.array.php
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37804378
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

0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37804468
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

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37805380
...acrobatics with constants... that's nice, though I wouldn't recommend it to the unexperienced.
0
 
LVL 9

Expert Comment

by:rinfo
ID: 37805502
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.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37805647
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!
0
 

Author Closing Comment

by:jasonkrueger
ID: 37807011
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.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 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