Link to home
Start Free TrialLog in
Avatar of GD_GRAY
GD_GRAYFlag for United States of America

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of sshah254
sshah254

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GD_GRAY

ASKER

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   |        |        |      |
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GD_GRAY

ASKER

I was never able to get any of this to work. but thanks for your help.