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
Solved

Help with PHP variable display from within loop

Posted on 2012-04-03
7
211 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cookie not unsetting 7 27
Why is my wamp get_include_path() wrong? 2 21
parse url to form? 7 25
How do I correctly structure this?   PHP / PDO / CSV download 1 20
This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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.

860 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