Solved

PHP Loop inside function

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

Suggested Solutions

Title # Comments Views Activity
$q.all is slower or not  in  Angularjs 2 21
PHP Curl to output a url 7 47
Ahax pagination 9 32
PHP 7 issue seeing runtime MS SQL driver in PHP info page 5 11
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

856 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