Removing empty fields when displaying MySQL database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Charlie
Forum Newbie
Posts: 3
Joined: Mon Jul 07, 2003 5:14 pm
Location: Vancouver Canada

Removing empty fields when displaying MySQL database

Post by Charlie »

How can I remove empty fields when displaying a MySQL database using PHP. My code for doing this is at present is

<field>!=""
as for example

Code: Select all

<?php

if ($_POST[i1]!= " ") {
printf("<li> %s\n", $_POST[i1]);
}

?>
I'm using this code to display an ordered list. Sometimes it works but sometimes I get a whole series of blank lines displayed, as for example

1. Go to refrigerator
2. Take out beer
3. Open beer
4.
5.
6.

I would greatly appreciate an answer to this question

Charlie
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post by jmarcv »

A space is not an empty field.
option 1
<?php

if ($_POST[i1]!= "") printf("<li> %s\n", $_POST[i1]);


?>

option 2
<?php

if ($_POST[i1]) printf("<li> %s\n", $_POST[i1]);


?>
Charlie
Forum Newbie
Posts: 3
Joined: Mon Jul 07, 2003 5:14 pm
Location: Vancouver Canada

Post by Charlie »

Thanks so much for your reply. I really appreciate your time.

I've tried both

Code: Select all

<?php
if ($_POST[i1]!= "") printf("<li> %s\n", $_POST[i1]); 


option 2 
<?php 

if ($_POST[i1]) printf("<li> %s\n", $_POST[i1]); 


?>
And in both cases I still get this problem. It's almost as if some non displaying character is being written into the MySQL database fields.
Charlie
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post by jmarcv »

how about
if (chop($_POST[i1])) printf("<li> %s\n", $_POST[i1]);

and for debugging:
print strlen($_POST[i1]);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you tried

Code: Select all

if (!empty($_POST['i1')) {
    echo '<li>'.$_POST['i1'];
}
You should definitely use this in preference to the

Code: Select all

if ($_POST[i1]) {
It's also a good idea to trim() all of the posted variables before working with them, e.g.:

Code: Select all

foreach ($_POST as $key => $value) {
    $_POST[$key] = trim($value);
}
On a side note, you should really read:
Why is $foo[bar] wrong?

jmarcv - please can you put PHP code into tags.

Mac
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post by jmarcv »

twigletmac
Will do. Just learned about it after about 5,427 incorrect posts.
sorry....
Charlie
Forum Newbie
Posts: 3
Joined: Mon Jul 07, 2003 5:14 pm
Location: Vancouver Canada

Post by Charlie »

Dear twigletmac and jmarcv,

Thanks so much for your input! It's an inspiration for me to see that there are people like yourself who offer their help to others without expectation of gain.

the trim() function worked and my site is looking very clean and professional now.

Yours
Charlie
Post Reply