Solved

How can I get a query to only echo rows if it has data

Posted on 2011-02-26
7
253 Views
Last Modified: 2012-05-11
How can i get the query to echo rows only if it has data. in the script below I have it echo the results for lineitem1, but in some case if the table has data for lineitem2 I want to echo it as well but I dont want the results to fill the page with empty rows . How would I tell it to echo only rows with data and not the other.
$unit = $_POST['unit'];
$result = mysql_query("SELECT * FROM workorder WHERE wo_unit='$unit' ORDER BY wo_status, wo_hub DESC;");

echo "<table border='1'>
<tr>
<th>W/O Number</th>
<th>Unit</th>
<th>Date</th>
<th>Hub</th>
<th>Description</th>
<th>Notes</th>
<th>Status</th>

</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['wo_number'] . "</td>";
  echo "<td>" . $row['wo_unit'] . "</td>";
  echo "<td>" . $row['wo_date'] . "</td>";
  echo "<td>" . $row['wo_hub'] . "</td>";
  echo "<td>" . $row['lineitem1'] . "</td>";
  echo "<td>" . $row['notes'] . "</td>";
  echo "<td>" . $row['wo_status'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Open in new window

0
Comment
Question by:GD_GRAY
7 Comments
 
LVL 9

Accepted Solution

by:
sshah254 earned 125 total points
ID: 34988877
On line 3, put a check like this

 if ($result->num_rows > 0) {
}

close it at the appropriate place, I think on line 29.

Ss

 
0
 
LVL 19

Assisted Solution

by:Michael701
Michael701 earned 125 total points
ID: 34988883
this should be working fine. you have WHERE wo_unit='$unit' so if there are 30 records for lineitem1 they will display, and if there are one 3 records for lineitem2 they are the only ones that will display.

0
 

Author Comment

by:GD_GRAY
ID: 34988956
the $unit is to pull all the workorders for that one unit, so if I add the echo lineitem2,3 etc if one work order only has one lineitem and the next has 3 it makes the table extra long with blank spots. Could it be made to put the lineitems on a secound row ? like
workorder number  date  hub  notes
Description   |        |        |      |
workorder number  date  hub  notes
Description   |        |        |      |
workorder number  date  hub  notes
Description   |        |        |      |
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 17

Assisted Solution

by:nanharbison
nanharbison earned 125 total points
ID: 34989125
You could do something like this, where you say $row['lineitem1'] is not blank.
while($row = mysql_fetch_array($result) &&($row['lineitem1'] !=""))

Open in new window

0
 
LVL 17

Expert Comment

by:nanharbison
ID: 34989142
Oh wait, I am not sure you can check what is in the field $row['lineitem1'] at the same time you are getting the results. You might have to make it be another if statement inside the while loop
while($row = mysql_fetch_array($result))
  {
  	if ($row['lineitem1'] !="") {
  		echo "<tr>";
  		echo "<td>" . $row['wo_number'] . "</td>";
  		echo "<td>" . $row['wo_unit'] . "</td>";
  		echo "<td>" . $row['wo_date'] . "</td>";
  		echo "<td>" . $row['wo_hub'] . "</td>";
  		echo "<td>" . $row['lineitem1'] . "</td>";
  		echo "<td>" . $row['notes'] . "</td>";
  		echo "<td>" . $row['wo_status'] . "</td>";
  		echo "</tr>";
  }
}

Open in new window

0
 
LVL 3

Assisted Solution

by:sergiobg57
sergiobg57 earned 125 total points
ID: 34989327
SELECT * FROM workorder WHERE wo_unit='$unit'  and len(wo_unit) > 0 ORDER BY wo_status, wo_hub DESC

Open in new window

0
 

Author Closing Comment

by:GD_GRAY
ID: 35216636
I was never able to get any of this to work. but thanks for your help.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Sessions in WordPress 8 31
mysqli insert query problems 4 22
PHP Form Calculate Total Price 10 42
MySQL Query Using Up Memory 6 27
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 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