Forum Moderators: coopster

Message Too Old, No Replies

Hide text if Database field is empty

Hide Text if field in database is empty and dont contain any data

         

borisz

4:22 pm on Apr 2, 2010 (gmt 0)

10+ Year Member



Hi all,

this is my first time Im posting at WebmasterWorld and I'm sorry for my English but I hope you will understand what Im trying to tell. Im fresh starter with PHP and MySQL and I need small help with fallowing:

how can I hide certain text on page if certain field in database if empty and don't contain any data. For example I have repeatable table which show the list of products from database. One field in database called "ProductSavingsCode" sometimes I have code to add for product and sometimes I dont so the problem is that on the php page next to "ProductSavingsCode" I haev fallowing text "Coupon Code:" so when the field in databse is empty I want o hide that text too and when I have code in database it will show e.g.

"Coupon Code: SAVE123"

where "SAVE123" is "ProductSavingsCode" from database.

with this beginning knowledge I have right now I tried with "IF ELSE" but always get errors. Can any1 give me suggestion, link of webpage or maybe a code for it. Hope you understand what Im asking. Thanks a lot!

Anyango

4:34 pm on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmasterworl Borisz.

So lets assume you are currently doing it like this

echo "Coupon Code: $code"; // $code you get from db

And when the code is empty it must be showing like this

Coupon Code:

What we need to do is tell php that if there is no value in your variable, then dont echo that line. Which in PHP we do like this (just one of many methods)

if($code!="") // Two times Doule Quotation marks that is, and != means "not equal to". So we mean to say if $code is not equal to empty
{
echo "Coupon Code: $code";
}
else
{
echo "No Code";

}

You can comment the echo in else, incase you dont want to echo something. If confusion is still there, post here those couple lines of your code which echo.

Cheers

borisz

5:13 pm on Apr 2, 2010 (gmt 0)

10+ Year Member



Thanks Anyango for reply, it is working beatufilly, just one more minor thing. This is how the code looks like:

<?php
if($row_coupons['ProductSavingsCode']!="")
{
echo $row_coupons['ProductSavingsCode'];
}
else
{
echo '';
}
?>


and it shows the code for field that have it added in database and dont show nothing for empty fields (just as I want it). Now for first echo I want to add it like this

echo '<span id="redtext">Coupon Code:</span> <span id="redtextbox"> $row_coupons['ProductSavingsCode']</span>';

but I get error. How can I include those <span> and $row_coupons['ProductSavingsCode'] in same time? Thanks again!

Readie

5:20 pm on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're doing nothing when the database cell is empty you don't need the "else" clause.

To include HTML you need to use concatonations ( . character) like so:

<?php

if($row_coupons['ProductSavingsCode']!== "") {
echo '<span id="redtext">Coupon Code:</span><span id="redtextbox"> ' . $row_coupons['ProductSavingsCode'] . '</span>';
}

?>

[edited by: Readie at 5:23 pm (utc) on Apr 2, 2010]

Anyango

5:23 pm on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok lets talk about this.

In php you can echo any string using either Double quotes, or single quotes.
like this

echo 'testing';
echo "testing";

they both work fine, right ? you can test them. Now if you have to echo a variable, it either has to be outside quotation marks, or in Double quotes. Any variable in single quotes wont work.

for example

$code=123;
echo $code; //works
echo "$code"; // works
echo '$code'; //doesnt show 123


Now that was if we were echo'ing a string and a variable seperately, your question was what to do when you have to echo them together.
You have two options here, Either enclose the variable name along with the string within double quotes or use the concat operator (.)

for example

echo "Coupon Code: $code"; //works
echo "Coupon Code: ".$code; //works

Last thing, since this
$row_coupons['ProductSavingsCode'] is not a simple variable, its an array element, so we cannot just put array element in quotation marks and expect that to work. for that We either use the second option, that is to put them outside quotation marks and use the concat operator, like this

echo "Coupon Code: ".$row_coupons['ProductSavingsCode'];


Or if you want to echo an array element within quotes, we use { } around the variable, like this

echo "Coupon Code: {$row_coupons['ProductSavingsCode']}";


I hope that all makes sense, now finally the answer to your question is


Either

echo '<span id="redtext">Coupon Code:</span> <span id="redtextbox">'.$row_coupons['ProductSavingsCode'].'</span>';

Or

echo "<span id='redtext'>Coupon Code:</span> <span id='redtextbox'>{$row_coupons['ProductSavingsCode']}</span>";

borisz

5:34 pm on Apr 2, 2010 (gmt 0)

10+ Year Member



thanks both of you, and Anyango thanks for this short guide, I learned few useful things from you today :) . All work as charm now

rocknbil

6:39 pm on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, maybe I'm an odd duck. (Fire away. :-) ) Planning your database with default values will help a lot. That way you can always expect something, or expect NULL.


$out=NULL;
// presume values of active=1/0,coupon=somenumber or
// NULL,last name is optional so last name or NULL.
while ($row=mysql_fetch_array) {
$active=($row['active']==1)?'Yes':'No';
$lname=(empty($row['last_name']))?'-':$row['last_name'];
$coupon = (empty($row['coupon']))?'None':$row['coupon'];
$out .= "<tr><td>$active</td><td>$lname</td><td>$coupon</td></tr>\n";
}
//
if ($out) {
echo "
<table>
<tr><td>ACTIVE</td><LAST NAME</td><td>COUPON</td></tr>
$out>
</table>
";
}
else {
echo "<p>No results found</p>";
}


This approach demonstrates extensive expandability.. Let's say you have access levels.

var $access_levels = Array(
'Free Member',
'Registered Member',
'Editor',
'Administrator';
);

Then a field for access level where numeric values are stored.

$access = $access_levels[$row['acc_lvl']];

Or, using my "active" example above,

$YesNos=Array('No','Yes');

$active = $YesNos[$row['active']];