[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

PHP Loop inside function

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
jej07
Asked:
jej07
1 Solution
 
Marco GasiFreelancerCommented:
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
 
jej07Author Commented:
Thank you very much for your help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now