Reputation: 117
I am pulling information from a mysql db as an Associative Array. Some variables will be NULL if there is no information (i.e. a phone number or a second address line). I extract the information and echo it to a webpage just fine. However, the positions which hold return Null there is a blank. I would like to remove the blank for those records which are NULL.
I have tried to create a foreach statement to loop through the array, but it repeats the records 14 times in a row excluding one variable at a time.
Example:
A database has 10 columns:
id = auto increment
fname
lname
address1
address2 = NULL
city
state
zip
officephone = NULL
mobilephone = NULL
Class::members();
After running my PDO, I begin a while loop and create the array $member:
while($return = $stmt->fetch(PDO::FETCH_ASSOC)) {
$member = array(
$return['fname'],
$return['lname'],
$return['address1'],
$return['address2'],
$return['city'],
$return['state'],
$return['zip'],
$return['ophone'],
$return['mphone']);
I echo this to the webpage with html and css styling
echo
$member[1] . $member[2].'<br />' .
$member[3] . '<br />'.
$member[4] . '<br />'.
$member[5] . ', ' . $member[6].$member[7].'<br />'.
$member[8] . '<br />' .
$member[9] . '<br />'.
What is looks like on the web page
Instantiating - this runs through the records until the end then stops.
<?php $abs = array(Class::members()); foreach($mbs as $mb) { echo $mb;}?>
What you see
John Smith
222 One Drive
(GAP BECAUSE NULL)
Somewhere, USA 00000
(GAP BECAUSE NULL)
555555555
I want to write some php code to skip the GAP above when it is NULL, but include it when it is not NULL.
So that it looks like:
John Smith
222 One Drive
Somewhere, USA 00000
555555555
I have tried creating a method to loop through the results and skip the NULL variable with no success.
foreach($member as $key => $value)
/* variants I have tried in if() statment ($value != NULL),
($value === NULL),($value == 'NULL'), ($value == ""),
(empty($value)), ($member[$value] == NULL)*/
if($value == NULL) {
continue; }
}
This returns 14 identical results of each record, and all of them have a gap. I have also tried to use array_filter($members).
Any recommendations?
Upvotes: 1
Views: 106
Reputation: 14259
Just do not output <BR>
when not needed:
$output = $member[1] . $member[2];
$output.= ($output!='' ? '<br/>' : '') . $member[3];
$output.= ($output!='' ? '<br/>' : '') . $member[4];
$output.= ($output!='' ? '<br/>' : '') . $member[5];
$output.= ($output!='' AND ($member[6]!='' OR $member[7]!='') ? ', ' : '') . $member[6] . $member[7];
$output.= ($output!='' ? '<br/>' : '') . $member[8];
$output.= ($output!='' ? '<br/>' : '') . $member[9];
echo $output;
I think this is a better solution to your challenge:
$output = $member[1] . $member[2];
$output.= ($output!='' ? "\n" : '') . $member[3];
$output.= ($output!='' ? "\n" : '') . $member[4];
$output.= ($output!='' ? "\n" : '') . $member[5];
$output.= ($output!='' AND ($member[6]!='' OR $member[7]!='') ? ', ' : '') . $member[6] . $member[7];
$output.= ($output!='' ? "\n" : '') . $member[8];
$output.= ($output!='' ? "\n" : '') . $member[9];
// we replace multiple NewLines with a single NewLine
$output = preg_replace('/\n+/', "\n", $output);
// we trim our string to remove the NewLines on left and write, if any
$output = trim($output);
// we then simply replace all NewLines with <BR>
echo nl2br($output);
Upvotes: 1
Reputation: 38502
Just try array_filter()
to remove the Null's from your $member
array like this-
$member = array_filter($member);
It'll remove boolean false
, null, ''
from your array value if exists. SEE. If you want to restrict the value you can use any callback function.
Upvotes: 0