Solved

PHP Loop inside function

Posted on 2012-04-13
2
388 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 31

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

809 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