Jump to content

Array Empty Question


tomtimms

Recommended Posts

My database has fields id,company,fname, lname, address, phone.  I want to display all the records that have missing fields.  So my results would show Company - (missing field name).  I have started something out below however not sure how to display the name of the row that is missing.

 

while ($row = mysql_fetch_array($result)) {

 

if (empty($row['fname']) OR empty($row['address']) ) {

   

    echo "<li>".$row['company']."</li>";

   

}

else  {

   

    echo "none";

   

}

}

Link to comment
Share on other sites

My database has fields id,company,fname, lname, address, phone.  I want to display all the records that have missing fields.  So my results would show Company - (missing field name).  I have started something out below however not sure how to display the name of the row that is missing.

 

while ($row = mysql_fetch_array($result)) {

 

if (empty($row['fname']) OR empty($row['address']) ) {

   

    echo "<li>".$row['company']."</li>";

   

}

else  {

   

    echo "none";

   

}

}

 

 

while ($row = mysql_fetch_array($result)) {
if ($row['fname'] == '') {   
    echo "<li>".$row['company']." - FName</li>";
} elseif ($row['address'] == '') {
    echo "<li>".$row['company']." - Address</li>";
else  {
    echo "none"; 
}
}

Link to comment
Share on other sites

If you ONLY want to display records with missing information, then the first step is to create the db query such that it only returns records with missing data. How that query looks will depend upon whether the "missing" data is an empty string, a null value, contains only white-space characters, or a combination of the three.

 

not sure how to display the name of the row that is missing

You mean column. dotMoe's example is a start, but it has a flaw. If the first field is empty it will only show that one as missing data. So, any other fields missing data would not display.

 

This code will work without having to explicitly code for each field. Of course, it uses the db field names and not "friendly" names. But, that may be acceptable in your situation.

while ($record = mysql_fetch_array($result))
{
    $emptyFields = array();
    foreach($record as $field => $value)
    {
        $value = trim($value);
        if(empty($value))
        {
            $emptyFields[] = "<li>$field</li>";
        }
    }
    echo "<b>{$record['company']}</b>\n";
    echo "<ul>" . implode('', $emptyFields) . "<\ul>\n";
}

 

Link to comment
Share on other sites

Thanks mjdamato, however the results are displaying the array key as well..so it shows up

  • 1
  • fname

 

 

So, don't include that field in your db query. I assume you are using '*' instead of only specifying the fields you want/need.

 

Or, if you still want to take the lazy method, then add an exception clause the the processing code.

 

EDIT: Wait, that makes no sense. The code only displays field NAMES where there is no value. There's nothing in the code I provided that would display the ID - which is a field VALUE. You must have misimplemented the code - or you have a column name of '1' in your table.

Link to comment
Share on other sites

Yeah I changed it to mysql_fetch_assoc before you replied and that did the trick.  I am however getting the following results

 

Company name

- error 1

 

Company name (showing the second company however no results?)

 

not sure why it is doing this, but it is displaying all the company names even if they don't have missing information.

 

Link to comment
Share on other sites

Company name (showing the second company however no results?)

 

not sure why it is doing this, but it is displaying all the company names even if they don't have missing information.

 

 

As I first stated, if you are only wanting to show the records where there is missing data, then you should build your query to only pull those records. How that is done will depend on what an "empty" field means. So, I did not provide any guidance on that.

 

Or, if you want to be inefficient and just pull all the records and change the code above to not display those records:

 

while ($record = mysql_fetch_assoc($result))
{
    $emptyFields = array();
    foreach($record as $field => $value)
    {
        $value = trim($value);
        if(empty($value))
        {
            $emptyFields[] = "<li>$field</li>";
        }
    }
    if (count($emptyFields)>0)
    {
        echo "<b>{$record['company']}</b>\n";
        echo "<ul>" . implode('', $emptyFields) . "<\ul>\n";
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.