Solved

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

Posted on 2011-02-26
7
268 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
[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
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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