Gossamer Forum
Home : Products : DBMan : Customization :

One last question: Displaying fields only if they're a certain value

Quote Reply
One last question: Displaying fields only if they're a certain value
Okay... so I have a radio select field named "Sale Status" that has two options: Not for sale and For sale. After the record has been added, I only want that part to be visible if the Sale Status is For Sale. I tried doing it on my own, but it didn't work. I dunno what to do. Any help is appreciated!!!
Quote Reply
Re: [Reyhan] One last question: Displaying fields only if they're a certain value In reply to
If I understand your question, the best way to explain it is to give an example. Let's say that below is part of your html_record:

Code:
print qq|<table>
<tr><td>Field 1:</td><td>$rec{'Field1'}</td></tr>
<tr><td>Field 2:</td><td>$rec{'Field2'}</td></tr>
<tr><td>Field 3:</td><td>$rec{'Field3'}</td></tr>
<tr><td>Sale Status:</td><td>$rec{'Sale Status'}</td></tr>
<tr><td>Field 4:</td><td>$rec{'Field4'}</td></tr>
<tr><td>Field 5:</td><td>$rec{'Field5'}</td></tr>
<tr><td>Field 6:</td><td>$rec{'Field6'}</td></tr>
|;


Let's say we don't want the Sale Status field and Field 4 to print out if the Sale Status is "Not for sale." Notice the changes:
Code:
print qq|<table>
<tr><td>Field 1:</td><td>$rec{'Field1'}</td></tr>
<tr><td>Field 2:</td><td>$rec{'Field2'}</td></tr>
<tr><td>Field 3:</td><td>$rec{'Field3'}</td></tr>|;
if ($rec{'Sale Status'} eq "For sale") {
print qq|
<tr><td>Sale Status:</td><td>$rec{'Sale Status'}</td></tr>
<tr><td>Field 4:</td><td>$rec{'Field4'}</td></tr> |;
}
print qq|
<tr><td>Field 5:</td><td>$rec{'Field5'}</td></tr>
<tr><td>Field 6:</td><td>$rec{'Field6'}</td></tr>
|;


Does this help? :-)


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] One last question: Displaying fields only if they're a certain value In reply to
Ah! I had that same thing except I had an = instead of "eq" Now I just feel silly, lol. Thanks so much, JPDeni! Your other advice on the initial value of a field worked too. Thanks!!!
Quote Reply
Re: [Reyhan] One last question: Displaying fields only if they're a certain value In reply to
No need to feel silly. I still make that same error. I have to consciously think about the fact that = means assignment, but comparisons are different. I have memorized that == means "is equal to" for numbers and eq is the same thing with strings. But it's something I have to think about every time I use it.

I'm glad the codes worked. :-)


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] One last question: Displaying fields only if they're a certain value In reply to
Okay, I'm trying to incorperate this same idea (the if/then thingy) into something else... but it's telling me that I have an internal error. I'm gonna paste all my stuff and see if you can tell me what is wrong... Cause i think it should be right... It's possible that I can't use this same idea, too, so if it's that, let me know.

default.cfg
%db_def = (
'Owner' => [ 0, 'alpha', 20, 255, 0, '', ''],
'Owner Email' => [ 1, 'alpha', 20, 255, 0, '', '.+@.+..+'],
'Horse' => [ 2, 'alpha', 20, 255, 0, '', ''],
'Breed' => [ 3, 'alpha', 20, 255, 0, '', ''],
'Year of birth' => [ 4, 'numer', 20, 255, 0, '', ''],
'Gender' => [ 5, 'alpha', 0, 8, 0, 'Mare', ''],
'Color' => [ 6, 'alpha', 20, 255, 0, '', ''],
'Markings' => [ 7, 'alpha', 20, 255, 0, '', ''],
'Sire' => [ 8, 'alpha', 20, 255, 0, '', ''],
'Dam' => [ 9, 'alpha', 20, 255, 0, '', ''],
'Height' => [10, 'numer', 20, 255, 0, '', ''],
'Training' => [11, 'alpha', '40x5', 1000, 0, '', ''],
'Imported Points' => [12, 'numer', 20, 255, 0, '', ''],
'Total Points' => [13, 'numer', 20, 255, 0, '', ''],
'Title Status' => [14, 'alpha', 20, 255, 0, '', ''],
'Stabled at' => [15, 'alpha', 0, 19, 0, 'Black Storm Stables', ''],
'Last Vet' => [16, 'date', 20, 255, 0, &get_date(), ''],
'Last Farrier' => [17, 'date', 20, 255, 0, &get_date(), ''],
'Organizations' => [18, 'alpha', '40x5', 1000, 0, '', ''],
'Owner History' => [19, 'alpha', '40x5', 1000, 0, '', ''],
'Registration' => [20, 'numer', 20, 255, 1, '', '\d{3}'],
'Sale Status' => [21, 'alpha', 0, 13, 0, 'Not for sale', ''],
'Picture' => [22, 'alpha', 20, 255, 0, '', '']
);
...
%db_select_fields = (
'Gender' => 'Mare,Stallion,Gelding'
'Stabled at' => 'Black Storm Stables,Harmony Holsteiners,Revelation Farms,Signiture Farms,Wintershore Gardens'
);


html.pl
<tr><TD ALIGN="Right" VALIGN="TOP" WIDTH="150"><$font_color>Stabled at:</FONT></TD>
<TD VALIGN="TOP" WIDTH="475">&nbsp;|; print &build_select_field("Stabled at",$rec{'Stabled at'}); print qq|</TD></tr>
...
<tr>
<td width="432" colspan="2"><$font_color>$rec{'Horse'} is currently stabled at
|;
if ($rec{'Stabled at'} eq "Black Storm Stables") { print qq|
<a href="http://www.geocities.com/blackstorm_stables/" target="_blank">Black Storm Stables</a>|;
if ($rec{'Stabled at'} eq "Harmony Holsteiners") { print qq|
<a href="http://harmony.onthebitsim.com/" target="_blank">Harmony Holsteiners</a>|;
if ($rec{'Stabled at'} eq "Revelation Farms") { print qq|
<a href="http://" target="_blank">Revelation Farms</a>|;
if ($rec{'Stabled at'} eq "Signiture Farms") { print qq|
<a href="http://www.froggie.shimotsuki.net/cs/" target="_blank">Signiture Farms</a>|;
if ($rec{'Stabled at'} eq "Wintershore Gardens") { print qq|
<a href="hhttp://www.eagle.half-halt.net/citation/" target="_blank">Wintershore Gardens</a>|;
{
print qq|.</td>
</tr>

I think that's all the relevant stuff. Anywho, any help is great!!!! You guys are such a blessing, cause I'd be lost without this forum (and my databases would be rather plain Tongue )

Last edited by:

Reyhan: Feb 21, 2006, 7:18 PM
Quote Reply
Re: [Reyhan] One last question: Displaying fields only if they're a certain value In reply to
I see the problem. Here's the code, with the additions that are needed:

Code:

if ($rec{'Stabled at'} eq "Black Storm Stables") { print qq|
<a href="
http://www.geocities.com/blackstorm_stables/" target="_blank">Black Storm Stables</a>|;
}
if ($rec{'Stabled at'} eq "Harmony Holsteiners") { print qq|
<a href="
http://harmony.onthebitsim.com/" target="_blank">Harmony Holsteiners</a>|;
}
if ($rec{'Stabled at'} eq "Revelation Farms") { print qq|
<a href="http://" target="_blank">Revelation Farms</a>|;

}
if ($rec{'Stabled at'} eq "Signiture Farms") { print qq|
<a href="
http://www.froggie.shimotsuki.net/cs/" target="_blank">Signiture Farms</a>|;
}
if ($rec{'Stabled at'} eq "Wintershore Gardens") { print qq|
<a href="hhttp://www.eagle.half-halt.net/citation/" target="_blank">Wintershore Gardens</a>|;
}


Each "if" statement has to have its own closing curly bracket.

You could also use "elsif" statements on all but the first one:

Code:

if ($rec{'Stabled at'} eq "Black Storm Stables") { print qq|
<a href="
http://www.geocities.com/blackstorm_stables/" target="_blank">Black Storm Stables</a>|;
}
elsif ($rec{'Stabled at'} eq "Harmony Holsteiners") { print qq|
<a href="
http://harmony.onthebitsim.com/" target="_blank">Harmony Holsteiners</a>|;
}
elsif ($rec{'Stabled at'} eq "Revelation Farms") { print qq|
<a href="http://" target="_blank">Revelation Farms</a>|;

}
elsif ($rec{'Stabled at'} eq "Signiture Farms") { print qq|
<a href="
http://www.froggie.shimotsuki.net/cs/" target="_blank">Signiture Farms</a>|;
}
elsif ($rec{'Stabled at'} eq "Wintershore Gardens") { print qq|
<a href="hhttp://www.eagle.half-halt.net/citation/" target="_blank">Wintershore Gardens</a>|;
}

I'm not sure it makes much of a difference in this case, but we programmers like to use elsif statements.

You wanna see something really cool, though?

Define your urls in your .cfg file like this:

$url{'Black Storm Stables'} = 'http://www.geocities.com/blackstorm_stables/';
$url{'Harmony Holsteiners'} = 'http://harmony.onthebitsim.com/';
$url{'Signiture Farms'} = 'http://www.froggie.shimotsuki.net/cs/';
$url{'Wintershore Gardens'} = 'http://www.eagle.half-halt.net/citation/';

(Notice that I left out the one for Revelation Farms, because there was no url associated with it.)

Then, where you want it to print out, use

Code:

if ($url{$rec{'Stabled at'}}) {
print qq|<a href="$url{$rec{'Stabled at'}}" target="blank">$rec{'Stabled at'}</a>|;
}
else {
print qq|$rec{'Stabled at'}|;
}


That way all of your URLs are in one place and you don't have to go hunting for them in the html.pl file if they change. It's easier to add more if you should need to. Just add them to the list for the select field and create another $url line. Also, if there isn't a URL associated with a stable, you don't have a non-working link for it. Besides, I just think that kind of structure is cool. Cool

I always think of things like parentheses, brackets and curly brackets rather like doors. If you open one, you have to close it. For every ( there must be a corresponding ). Same with [ and ] and with { and }. Those will get you into more trouble than anything else.

I'm really glad we're able to help. I just like to solve the puzzles. LOL


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] One last question: Displaying fields only if they're a certain value In reply to
Okay, that was great. I got it to work.

+blush+ One more thing...

A while ago I remember posting about something where I wanted a certain code to print if the value in a field was within a certain range of numbers. i.e. CH print if 200 > $rec{'Points'} > 99 or XH print if $rec{'Points'} > 5000.
Someone had written an idea on how to do it, but I can't find that post anymore. And I'm not sure that what they said worked, because when I tried it, I couldn't get it to. Of course, that was probably an error on my part, but still, lol. Anywho, yet again... any help? Thanks!
Quote Reply
Re: [Reyhan] One last question: Displaying fields only if they're a certain value In reply to
No need to be embarrassed. :smile:

I'm not sure I understand the "CH" and "XH" in your example, but this might help. I just pulled some numbers at random.

Code:

if ($rec{'Points'} < 50) {
print qq|whatever you want for scores that are 49 and below|;
}
elsif ($rec{'Points'} < 100) {
print qq|whatever you want for scores that are 50 through 99|;
}
elsif ($rec{'Points'} < 500) {
print qq|whatever you want for scores that are 100 through 499|;
}
else {
print qq|whatever you want for scores that are 500 or greater|;
}



If you have the statements in numerical order like above and use the "elsif" statements, you only have to define one condition for each statement. (Did that make any sense at all? :-)

Then again, I may be completely misunderstanding what you had in mind.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.

Last edited by:

JPDeni: Feb 22, 2006, 11:32 PM
Quote Reply
Re: [JPDeni] One last question: Displaying fields only if they're a certain value In reply to
Yep, that's exactly what I wanted! Thanks yet again! You're a life saver!!!