Solved

Help with PHP variable display from within loop

Posted on 2012-04-03
7
210 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

776 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