Solved

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

Posted on 2011-02-26
7
261 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to pass JavaScript generated HTML text form values to PHP? 5 38
Wifi connection to laptop without router windows 10 11 37
mysql query for sum() 3 28
Ahax pagination 9 32
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
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.

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