tomtimms Posted April 19, 2010 Share Posted April 19, 2010 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/ Share on other sites More sharing options...
dotMoe Posted April 19, 2010 Share Posted April 19, 2010 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/#findComment-1044590 Share on other sites More sharing options...
Psycho Posted April 19, 2010 Share Posted April 19, 2010 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/#findComment-1044603 Share on other sites More sharing options...
tomtimms Posted April 19, 2010 Author Share Posted April 19, 2010 Thanks mjdamato, however the results are displaying the array key as well..so it shows up 1 fname Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/#findComment-1044629 Share on other sites More sharing options...
Psycho Posted April 19, 2010 Share Posted April 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/#findComment-1044633 Share on other sites More sharing options...
tomtimms Posted April 19, 2010 Author Share Posted April 19, 2010 My query is only selecting the fields I want. However it shows the array field number [2] then the column name. Its displaying the column number as well. [0] company [1] fname [2] lname etc. Its displaying the Company Name 1 fname Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/#findComment-1044641 Share on other sites More sharing options...
Psycho Posted April 19, 2010 Share Posted April 19, 2010 Ah, I see. Change mysql_fetch_array() to mysql_fetch_assoc(). I always use mysql_fetch_assoc(). I forgot that mysql_fetch_array() gets two copies of every value, one with the index of the field name and one with a numeric index. Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/#findComment-1044665 Share on other sites More sharing options...
tomtimms Posted April 19, 2010 Author Share Posted April 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/#findComment-1044668 Share on other sites More sharing options...
Psycho Posted April 19, 2010 Share Posted April 19, 2010 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/199009-array-empty-question/#findComment-1044681 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.