Solved

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

Posted on 2011-02-26
7
244 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now