Solved

Loop through 3 times

Posted on 2014-09-29
10
209 Views
Last Modified: 2014-09-29
I'm trying to automate our patients meal choices for our galley.
When a patient comes in, a nurse will get their meal choice for the next three meals and enter them into the computer.
It will show up like this:

name    Room    meal1   meal2   meal3

What I need help with is if there is only one meal for a person (might be waiting for approval to change diet or they may be going home before the 2nd meal), I need the system to put two blank cells in the table so everything lines up right.

The code I have is this which works great if there are three meals entered.

$today_time = date ("H:i");
$food_query = "SELECT fo.*, me.*, mc.* FROM food AS fo INNER JOIN mealchoice AS mc ON fo.Choice=mc.MCID INNER JOIN meal AS me ON me.MID=fo.Meal WHERE Patient = '$nurse_id' AND FDate >= '$current_date' Order By FDate, MID ASC"; 
							$food_query_result = mysql_query($food_query);

							if (mysql_num_rows($food_query_result) > 0)
							{
                                                           $count = 0;
								while ($food_row = mysql_fetch_assoc($food_query_result))

								{
									$food_id = $food_row['FID'];
							                $Meal = $food_row['MealChoice'];
									$Choice = $food_row['MealChoice1'];
									$Comments = $food_row['Comments'];
							
                                                                             echo"<td>$Choice </td>\n";
							
								$count++;
    if ($count == 3)
      break;
}								
							}

Open in new window

0
Comment
Question by:BHUC
  • 5
  • 5
10 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40350894
I can't see from the code how you would know the number of meals.  Would it be determinable by the number of rows returned from the query?

Also, you have a database conversion coming up.
0
 

Author Comment

by:BHUC
ID: 40350904
I only want it to show the next three... so if one meal is entered, it would show
Choice   Add Meal   Add Meal

If two were entered, it would show
Choice     Choice     Add Meal

They can only enter up to three in advance.

Does that make sense.

I'll read up on the data conversion
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40350917
Not quite...  Let me ask the question another way.  Regarding this:

$food_query_result = mysql_query($food_query);

What would you expect to find in mysql_num_rows($food_query_result) ?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:BHUC
ID: 40350931
If any meals have been entered, I would find the number of meals that have been entered and what they are.. if no meals have been entered for that patient, there wouldn't be anything...

BTW - thanks for always making me think.. I learn stuff by seeing it work and making changes.. you always make me understand what I am doing... I know enough to be dangerous but don't always understand why something is done a certain way.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40350960
I can't test this so you will have to.  I think it's correct or pretty close.

Since we expect no more than 3 rows, we set the query LIMIT to three.  And we have a three position array to hold the meal choices.  We initialize all three of the array positions to NULL.  As we retrieve the rows, we overwrite the NULL positions in the $meals array.  Finally, we use the three $meals array positions to create our <td> statements.
$today_time = date ("H:i");
$food_query = "SELECT fo.*, me.*, mc.* FROM food AS fo INNER JOIN mealchoice AS mc ON fo.Choice=mc.MCID INNER JOIN meal AS me ON me.MID=fo.Meal WHERE Patient = '$nurse_id' AND FDate >= '$current_date' Order By FDate, MID ASC LIMIT 3"; 
$food_query_result = mysql_query($food_query);

// SET THE MEAL CHOICES TO NULLS
$meals[1] = $meals[2] = $meals[3] = NULL;

// SET THE COUNTER TO ZERO
$cnt = 0;
while ($food_row = mysql_fetch_assoc($food_query_result))
{
    // INCREMENT THE COUNTER TO PROVIDE AN INDEX INTO THE MEAL CHOICES
    $cnt++;
    
    $food_id = $food_row['FID'];
    $Meal = $food_row['MealChoice'];
    $Choice = $food_row['MealChoice1'];
    $Comments = $food_row['Comments'];
    
    // SAVE THIS MEAL INFORMATION
    $meals[$cnt] = $Choice;
}

// CREATE THE TABLE DATA ELEMENTS
$out = NULL;
$out .= '<td>' . $meals[1] . '</td>';
$out .= '<td>' . $meals[2] . '</td>';
$out .= '<td>' . $meals[3] . '</td>';

// WRITE THE TABLE DATA TO THE BROWSER
echo $out;

Open in new window

0
 

Author Comment

by:BHUC
ID: 40350977
Ray...

1) I had the Limit 3 in there and took it out because I thought it was duplicating the count. Now I know!
2) Code works great - but how do I set the value to "Add Meal" so if nothing is there, it will print out the "Add meal" and the user can click on it to add the meal to the database?  Is it in the $meals[1] = $meals[2] = $meals[3] = NULL; line?

3) is there a difference in writing it out using the echo stuff I usually use vs how you did it? Or is it just a preference for a coder?
0
 

Author Comment

by:BHUC
ID: 40351005
Ray, I answered my own question in #2, but was wondering about preference in writing code with echo vs how you did it.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40351026
As long as you produce the correct output, either way is OK.  I like to write scripts that produce all of my variables before there is any browser output.  Then I can use templates (learn about HEREDOC) to generate the HTML documents.  I find that this makes it easier to change templates and style the output.  For a really good explanation of the ideas, get Matt Zandstra's book.  It's not 100% up-to-date, nor is it light reading, but it has a wealth of good information for programmers.
http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/
0
 

Author Closing Comment

by:BHUC
ID: 40351031
Thank you so much!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40351034
Glad to be able to help.  Thanks for the points and thanks for using E-E, ~Ray
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maybe no no httpd.conf 6 47
How would I do this...? 2 28
PHP Syntax Error 4 27
How do uses indexes to maximize MySQL Searches 14 31
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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