Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
Charlie
Forum Newbie
Posts: 3 Joined: Mon Jul 07, 2003 5:14 pm
Location: Vancouver Canada
Post
by Charlie » Mon Jul 07, 2003 5:14 pm
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 » Tue Jul 29, 2003 8:44 pm
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 » Tue Jul 29, 2003 9:28 pm
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 » Tue Jul 29, 2003 10:20 pm
how about
if (chop($_POST[i1])) printf("<li> %s\n", $_POST[i1]);
and for debugging:
print strlen($_POST[i1]);
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Wed Jul 30, 2003 4:32 am
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
jmarcv
Forum Contributor
Posts: 131 Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado
Post
by jmarcv » Wed Jul 30, 2003 8:55 am
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 » Fri Aug 01, 2003 6:17 pm
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