Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php - stop echo of empty mysql cells?

Featured Replies

Hi all

 

I have the results of a search of a business directory database echo to a table in a results page. This sort of thing:

 

echo   "<td class='sr_number'>$row[$address1]<br> $row[$address2] <br> $row[$address3] <br> $row[$town] <br> $row[$postcode] <br> $row[$province] <br> $row[$phoneland] <br> $row[$phonemob] <br> $row[$fax] <br>"

 

The problem is, the table looks untidy if the business address has only one line instead of three, or they don't have a fax number etc. The echo statement produces empty lines that look ugly.

 

Is there a way that I can have the statement only echo where there is actual content in a cell?

 

I have searched a lot but maybe using the wrong words. Apologies if it is a common request.

I would make all the variables include the line break.

 

$adline3 = "$row[$address3] <br>";

 

and then use an if statement which wouldnt echo it out if it was blank.

 

if ($adline3 != '') {
echo $adline3;
}

 

Let me know how you get on.

 

There are better ways to do this hopefuly somebody can show you. This method WILL work though.

YuKey code!! lol anyway

 

[important notes]

people have different coding styles, the below code uses the optimum code to merge string and variable {at current standings}, normally i just use double quotes and insert variables right into the string. normally thats ok but here the OP has suggested this is running in a loop where even the slightest speed increase is important, and the echo statements below get compiled quicker this way as the vaiable is converted to a string before being used by the echo.

 

echo "<td class='sr_number'>";
if(isset($row[$address1])){
echo $row[$address1].'<br>';
}
if(isset($row[$address2])){
echo $row[$address2].'<br>';
}
if(isset($row[$address3])){
echo $row[$address3].'<br>';
}

if(isset($row[$town])){
echo $row[$town].'<br>';
}
if(isset($row[$postcode])){
echo $row[$postcode].'<br>';
}
if(isset($row[$province])){
echo $row[$province].'<br>';
}
if(isset($row[$phoneland])){
echo $row[$phoneland].'<br>';
}
if(isset($row[$phonemob])){
echo $row[$phonemob].'<br>';
}
if(isset($row[$fax])){
echo $row[$fax].'<br>';
}

 

that is the same as yours but will display only whats there if you want to add a default then on each if satement mod it to look like this

 

if(isset($row[$fax])){
echo $row[$fax].'<br>';
}else{
echo 'None';
}

 

borellus, allthough that code works [for now] it is php 4 style code and will cause php runtime notice errors... with php moving upto V6 soon i would looking into the learning changes between php 4/5...

 

stuff like setting function argument defaults, __construct(), and increased OOP features

 

hope that helps you both :)

  • Author

many thanks to both of you. I am only a newbie at php, so it may take me a while to figure out your code. But get there I will!! I'll report back when I make some progress.

 

Regards

 

RJ

good luck :), the code sample i did is an exact copy of the line you gave us, if you replaced the old echo with the code above it will work.... if that was just an example and you still have troubles you know where we are :)

YuKey code!! lol anyway

 

echo "<td class='sr_number'>";
if(isset($row[$address1]))

...

 

What a mess!

 

This will do it.

 

<?php

$row['address1'] = '185 Bond Street';
$row['address2'] = 'Bond Way';
$row['address3'] = '007 Estate';
$row['town'] = 'London';
$row['province'] = ''; // Here's the empty one.
$row['phoneland'] = '020 8145 2322';
$row['phonemob'] = '07092 318227';
$row['fax'] = ''; // Another empty

// Build an address array.
$address = array();
$address[] = $row['address1'];
$address[] = $row['address2'];
$address[] = $row['address3'];
$address[] = $row['town'];
$address[] = $row['province'];
$address[] = $row['phoneland'];
$address[] = $row['phonemob'];
$address[] = $row['fax'];

// now we have an array with our address in it, let's do some magical stuff

$addrblock = '';
foreach($address as $k=>$v)
{
if (isset($v) && !empty($v))
	$addrblock .= trim($v) . '|';
}

$addrblock = str_replace('|', '<br />', $addrblock);

echo $addrblock;


?>

 

Also in your code, you are refering to array elements as variables.

 

$row[$province]

 

As you are saying you're a beginner, I would imagine that you have this wrong. It should be $row['province'] with single quotes.

 

You would only use a variable if it's pre defined.

  • Author

OK - I have tried the first two suggested methods. Neither worked in the sense that even if a cell had no entry, echo still produced a blank line. Initially this suggested me that there was something in the database definition for that field which made the query think there was data in that field.

 

EDIT: This seems unlikely given that Avera's suggested code does work (see later post).

 

Right, first, the first suggested method. Here is what I tried:

 

$adline1 = "$row[$address1] <br>";
$adline2 = "$row[$address2] <br>";
$adline3 = "$row[$address3] <br>";

if ($adline1 != '') {
echo "<td class='sr_number'>$adline1";
}else{echo "NONE";}

if ($adline2 != '') {
echo "$adline2";
}else{echo "NONE";}

if ($adline3 != '') {
	echo "$adline3";
}else{echo "NONE";}

</td>

 

Note that, just to be sure, I added an else statement which I presume should echo "None" if the cell was empty. This was not the case. Cells with data were echoed correctly, but empty cells gave a blank line. The word None was not echoed if a cell was empty.

 

Now - the second suggested code:

 

echo "<td class='sr_number'>";
if(isset($row[$address1])){
echo $row[$address1].'<br>';
}else{
echo 'None';
}

if(isset($row[$address2])){
echo $row[$address2].'<br>';
}else{
echo 'None';
}
if(isset($row[$address3])){
echo $row[$address3].'<br>';
}else{
echo 'None';
}

if(isset($row[$town])){
echo $row[$town].'<br>';
}else{
echo 'None';
}

if(isset($row[$postcode])){
echo $row[$postcode].'<br>';
}else{
echo 'None';
}
if(isset($row[$province])){
echo $row[$province].'<br>';
}else{
echo 'None';
}

if(isset($row[$phoneland])){
echo $row[$phoneland].'<br>';
}else{
echo 'None';
}

if(isset($row[$phonemob])){
echo $row[$phonemob].'<br>';
}else{
echo 'None';
}

if(isset($row[$fax])){
echo $row[$fax].'<br>';
}else{
echo 'None';
}
</td>

 

This echoes the data correctly where there are entries in the fields but still gives a blank line rather than "None" if a cell is empty.

 

The setup for the relevant variables follow this format:

 

Field: address1

Type: varchar(255)

Default: None

Null: (off - i.e. unchecked)

 

I have tried choosing null but same result.

 

I have tried entering a new row manually (the data would normally come by way of a processed form) and deliberately leaving one of the address lines blank - still get blank lines and the word "None" never appears.

 

Not sure where to go from here. Can anyone help me troubleshoot this one?

 

Many thanks.

  • Author

Avera - sorry I posted my latest without reading yours. I am going to have a go with your suggestion (when i have figured out what it all means! I like to try and understand if at all possible rather than just knock it out blindly...)

 

 

EDIT:

Thanks for the reminder about single quotes where it is not a variable. MY BAD. I did not explain that the code I am using is to echo the results of a search of the database to a "Results Page". The fields are defined as variables in another file that is called as part of the search exercise - hence no quotes.

 

Now - good news. I have tried your code by just constructing a new php page and it ran fine. I then cut and pasted just this block from your code:

 

// Build an address array.
$address = array();
$address[] = $row['address1'];
$address[] = $row['address2'];
$address[] = $row['address3'];
$address[] = $row['town'];
$address[] = $row['province'];
$address[] = $row['phoneland'];
$address[] = $row['phonemob'];
$address[] = $row['fax'];

// now we have an array with our address in it, let's do some magical stuff

$addrblock = '';
foreach($address as $k=>$v)
{
if (isset($v) && !empty($v))
	$addrblock .= trim($v) . '|';
}

$addrblock = str_replace('|', '<br />', $addrblock);

echo $addrblock;

 

into my Results Page. The echo process now works brilliantly - no more blank lines. I have just about figured out how you did it. You are a very clever chap. Hat's off to you.

 

And many thanks of course.

 

RJ

 

BTW - if the other posters want me to try variations of their scripts in order to track down why they did not work - I'll help of course. But I now have a solution that works so all is well for me.

aj, ok my example failed because your null values arnt null(false) but a empty string (' ') so isset needs tobe changed to !empty() (note: empty returns true if empty, which is diffrent to isset(), so we need to add ! meaning if the following statement is false)

 

as for the "What a mess!" comment, i wrote it without arrays and looping to help the OP learn the code so the original poster would understand the code he is implementing as its usless not knowing how your own code works!

 

also what is the point of this code... its an extra unneeded step..

 

$addrblock = str_replace('|', '<br />', $addrblock);

 

so OP if your using that code you can change this line

 

$addrblock .= trim($v) . '|';

 

to be

 

$addrblock .= trim($v) . '<br/>';

 

and remove this line

$addrblock = str_replace('|', '<br />', $addrblock);

 

 

also the code could work directly off the $row array and dosnt need to make a $address array

as for the "What a mess!" comment, i wrote it without arrays and looping to help the OP learn the code so the original poster would understand the code he is implementing as its usless not knowing how your own code works!

 

also what is the point of this code... its an extra unneeded step..

 

Alright PAL, this isn't a competition - I was only trying to have a laugh with you.

 

YuKey code!! lol anyway

 

You said your code was clunky anyway...

 

Everyone on here knows how sarcastic I am...

 

We're both trying to help him...

also the code could work directly off the $row array and dosnt need to make a $address array

 

You're right here though, there is no real need to create a second array - just did it for example.

yeah totally wasn't trying to compete but re-reading it i was a little on the ball shall i say, sorry for that, its not an excuse but im dyslexic... was just saying why my code was like that, if i wrote that code at work i would of been killed/sacked lol

so was just clearing my paranoia's really lol

 

and

 

also what is the point of this code... its an extra unneeded step..

 

i really dint mean that in a sarcastic way :)

 

peace :)

  • Author
aj, ok my example failed because your null values aren't null(false) but a empty string (' ') so isset needs to be changed to !empty() (note: empty returns true if empty, which is different to isset(), so we need to add ! meaning if the following statement is false)

 

Thanks for clarifying this for me. Just so I understand - how would your code look different with this new knowledge? Here is a clip of it:

 

if(isset($row[$address2])){
echo $row[$address2].'<br>';
}else{
echo 'None';
}

 

 

Cheers

 

RJ

  • Author
You're right here though, there is no real need to create a second array - just did it for example.

 

Avera

[or SniderDK if it comes to that]

Could either of you just amplify this for me ("also the code could work directly off the $row array and doesnt need to make a $address array"). I am keen to understand this area and arrays are next on the list for my studies! This is what I used in my php from your suggestion:

 

// Build an address array.
$address = array();
$address[] = $row['address1'];
$address[] = $row['address2'];
$address[] = $row['address3'];
$address[] = $row['town'];
$address[] = $row['postcode'];
$address[] = $row['province'];
$address[] = $row['phoneland'];
$address[] = $row['phonemob'];
$address[] = $row['fax'];

// now we have an array with our address in it, let's do some magical stuff

$addrblock = '';
foreach($address as $k=>$v)
{
if (isset($v) && !empty($v))
	$addrblock .= trim($v) . '<br/>';
}

echo "<td class='sr_number'>$addrblock", '<br>';

 

Note I have made the changes and removed the redundant line as per SniderDK's suggestion.

 

Thanks to both of you. I think it is marvellous that experts like yourselves are prepared to give so generously of your time to people you don't even know and are unlikely to ever meet. Luckily I am a retired psychologist and am designing a database-driven website just as a way of teaching myself something new. Not sure I would want to do this for a living <_<

 

What a great forum this is.

dont use mine, if your about to learn arrays anyway stick with what your using, but so you can see the edit in my example here ya go:

 

if(!empty($row[$address2])){
echo $row[$address2].'<br>';
}else{
echo 'None';
}

 

to array that up you would do somthing like this

 

foreach($row as $key => $value){

if(!empty($value)){ // NOTE: i edited this line since posting fixing a bug
echo $value.'<br>';
}else{
echo 'None'; // could change that to, echo 'No '.ucwords($key);
}

}

 

which would cycle through the $row array spliting the array into 2 bits, key and value... the key as it sounds is the identifier for the data eg key = postcode, value = ta1 2dd

 

so first we check that the key has a value -->> if(!empty($value)){

if its not (!) empty echo the value else echo None

Actually, SniderDK is spot-on!

 

You CAN just do this:

 

foreach($row as $key => $value)
{
if(!empty($value) && !is_null($value))
{
	echo stripslashes($value) . '<br />';
}
}

 

I always do it the long way then look again and think - what was I thinking.

 

Try the above!

  • Author

Thanks for the examples everyone. I'll keep them in my files for when I get to that stage in my learning about php/mysql. :unsure:

i allways forget to sort the output out in my code examples lol... its cus i use a class i made called prep when not using flexy for templating as that does it for you :)

 

ah yeah, for info sakes the is_null check is covered in empty()

 

Return Values

 

Returns FALSE if var has a non-empty and non-zero value.

 

The following things are considered to be empty:

 

* "" (an empty string)

* 0 (0 as an integer)

* "0" (0 as a string)

* NULL

* FALSE

* array() (an empty array)

* var $var; (a variable declared, but without a value in a class)

 

 

:)

 

bit off subject but got the right people looking here, has anyone tried the variable casters(not sure on the right name)

 

basicly lets you force a data type

 

$var = (int)'dfjkdfjksdfjk'; // gets set to null

$var= (int)'123456789'; // works as normal

 

you can also force object, array, string etc

Do you know what - I did not know that "empty" also covered "!is_null" in that way!

 

Every day's a school day!

actually i remembered while riding the motorbike home that the only thing it wont cover tho is a mysql null as that comes through as a string value of 'NULL'...

 

so depending on the database settings i.e if can values be null(good plan with big databases), the checks we did wont sort that...

 

so adding that to your code Avera, which the OP should update the code used

foreach($row as $key => $value)
{
if(!empty($value) && strcasecmp(trim($value),'null') == 0) // case insensitive check 
{
	echo stripslashes($value) . '<br />';
}
}

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.