[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP Loop inside function

Posted on 2012-04-13
2
Medium Priority
?
400 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
[X]
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
2 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
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).
Suggested Courses

650 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