Page 1 of 1
Removing empty fields when displaying MySQL database
Posted: Mon Jul 07, 2003 5:14 pm
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
Posted: Tue Jul 29, 2003 8:44 pm
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]);
?>
Posted: Tue Jul 29, 2003 9:28 pm
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
Posted: Tue Jul 29, 2003 10:20 pm
by jmarcv
how about
if (chop($_POST[i1])) printf("<li> %s\n", $_POST[i1]);
and for debugging:
print strlen($_POST[i1]);
Posted: Wed Jul 30, 2003 4:32 am
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
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
Posted: Wed Jul 30, 2003 8:55 am
by jmarcv
twigletmac
Will do. Just learned about it after about 5,427 incorrect posts.
sorry....
Posted: Fri Aug 01, 2003 6:17 pm
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