?
Solved

Help with PHP variable display from within loop

Posted on 2012-04-03
7
Medium Priority
?
214 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 2000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 111

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

801 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