[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

How to check if a field is empty?

I have a form that submits the data to mysql. Then on the page where it displays all the rows in the table (items) I want it to only show the form fields (or columns) that have been filled out. So I'm trying to check if it is empty. I'm using a code that does that now -- but it only works for the first entry in the database, and then applies that to each and every entry after it (even though it isn't true)**. Should I be using a for loop so it checks every database table entry, and not just the first entry?

** When I say it applies to each and every entry I mean if the first entries "Category" is empty, it thinks the rest of the table entries "Category" is empty, when in fact it isn't.

Hope that makes sense...

$result = mysql_query("SELECT * FROM `Equipment Listings` WHERE id = $id");
 
while($row = mysql_fetch_assoc($result)){
 
echo '<table>';
 
//CATEGORY
if (isset($_POST['category'])) 
{
echo '<tr><td><span class="equipmentheader">Category:</span> ';
echo $row['category'];
echo '</td></tr>';
}
else
{
echo '';
}
 
 
//YEAR
if (isset($_POST['year'])) 
{
echo '<tr><td><span class="equipmentheader">Year:</span> ';
echo $row['year'];
echo '</td></tr>';
}
else
{
echo '';
}
 
 
//SERIAL NUMBER
if (isset($_POST['serialnum'])) 
{
echo '<tr><td><span class="equipmentheader">Serial Number:</span> ';
echo $row['serialnum'];
echo '</td></tr>';
}
else
{
echo '';
}

Open in new window

0
natie769
Asked:
natie769
  • 3
  • 3
1 Solution
 
allmerCommented:
As long as $_POST['category'] was not passed from the sending script
you will only see '' for all category entries.
The same for year and serial num.

If you would rather like to show the values in the database (independent of the setting of the values)
it would be more like the below.

It is however not entirely clear to me what you are trying to achieve.
$result = mysql_query("SELECT * FROM `Equipment Listings` WHERE id = $id");
 
while($row = mysql_fetch_assoc($result)){
 
echo '<table>';
 
//CATEGORY
if (isset($row['category'])) 
{
echo '<tr><td><span class="equipmentheader">Category:</span> ';
echo $row['category'];
echo '</td></tr>';
}
else
{
echo '';
}
 
 
//YEAR
if (isset( $row['year'])) 
{
echo '<tr><td><span class="equipmentheader">Year:</span> ';
echo $row['year'];
echo '</td></tr>';
}
else
{
echo '';
}
 
 
//SERIAL NUMBER
if (isset($row['serialnum'])) 
{
echo '<tr><td><span class="equipmentheader">Serial Number:</span> ';
echo $row['serialnum'];
echo '</td></tr>';
}
else
{
echo '';
}

Open in new window

0
 
natie769Author Commented:
Thank you, I will try that solution.

Sorry I will try to explain this.

I have a page listing all of the items in the database. These items were entered there through a form. Each of these items has a "Read More" link that goes to its own page that just displays this one item (that is where the above code is from -- ex: $result = mysql_query("SELECT * FROM `Equipment Listings` WHERE id = $id");)

The database table has numerous columns (category, year and serial number are just a few) and all the fields aren't required, so some of them are empty. I only want to display the fields that have been filled out.

I hope that makes sense...let me know if it doesn't though.

Thank you for your help!
0
 
allmerCommented:
Well as a user I would also like to see the fields that were not filled.
But that is a design choice.

When you say id wouldn't that suggest that just one row is being returned?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
natie769Author Commented:
There is over 30 fields, so it doesn't look good if they all show.

I have only one equipment details page - and it displays the details page based on which listing they choose "Read More" in the list (carries the ID over).
0
 
allmerCommented:
Alright then,
for each attribute you should simply test whether it contains data.
If so you should display it if not don't.

The code below should achieve that (at least for the Category field ;).


$result = mysql_query("SELECT * FROM `Equipment Listings` WHERE id = $id"); 
$row = mysql_fetch_assoc($result); //Since you just expect one result row no while loop needed.
echo"<table>";
if (isset($row['category']) && $row['category'] != null && $row['category'] != "") 
{
    echo '<tr><td><span class="equipmentheader">Category:</span> '; 
    echo $row['category'];
    echo '</td></tr>';
 
}
 
//maybe change the above if statement to:
//if ($row['category'] != "") //may be enough please check that.
//    echo "<tr><th>Category</th><td>$row['category']</td></tr>";
 
 
//...
echo"</table>";
//Repeat the above for all 30 attributes in the table.

Open in new window

0
 
natie769Author Commented:
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now