Solved

PHP Loop inside function

Posted on 2012-04-13
2
379 Views
Last Modified: 2012-06-27
I have a MySql table that holds event descriptions and dates. I'm trying to create a HTML table that holds each day's events, and utilizes a function to return the data. The function I created only returns one line and not all the listings. How do I get it to display all the listings for the day?

<?php
function class_listing($classDay) {
      if ($classDay == 'Thurs'){ $timeDay = '2012-08-23';}
      else if ($classDay == 'Fri'){ $timeDay = '2012-08-24';}
      else if ($classDay == 'Sat'){ $timeDay = '2012-08-25';}
      else if ($classDay == 'Sun'){ $timeDay = '2012-08-26';}            
      
      $query = mysql_query("SELECT * FROM `classes` WHERE DATE(`date`) = '$timeDay' ORDER BY `date`");
      while($row = mysql_fetch_assoc($query))
      {

            $listing = '<tr><td valign="middle"><input name="classes'.$timeDay.'[]" type="checkbox" id="classes'.$timeDay.'[]" class="ft" value="' . $row['id'] . '"><label for="' . $row['id'] . '">' . $row['description'] . '</label></td></tr>';
      
      }
                  return $listing;
}
?>      

<table>
<tr><td>Thursday</td></tr>
<?php echo class_listing('Thurs'); ?>
<tr><td>Friday</td></tr>
<?php echo class_listing('Fri'); ?>
</table>
0
Comment
Question by:jej07
2 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 37843090
Add a dot before equal sign within the while loop:

     $query = mysql_query("SELECT * FROM `classes` WHERE DATE(`date`) = '$timeDay' ORDER BY `date`");
      while($row = mysql_fetch_assoc($query))
      {

            $listing .= '<tr><td valign="middle"><input name="classes'.$timeDay.'[]" type="checkbox" id="classes'.$timeDay.'[]" class="ft" value="' . $row['id'] . '"><label for="' . $row['id'] . '">' . $row['description'] . '</label></td></tr>';
     
      }

This add to the existing $listing value the new one: now you are very replaceing the value with the new one.

Cheers
0
 
LVL 1

Author Closing Comment

by:jej07
ID: 37843181
Thank you very much for your help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now