/    Sign up×
Community /Pin to ProfileBookmark

Font class in concatenated variable

I originally posted this on StackOverflow, but it ended up getting completely ignored there.
Original text:

I am trying to concatenate a couple of things into 1 variable, to make MySQL’s life easier.
However, this only works as it’s supposed to if people have a custom name colour applied.
People who don’t will appear as the default “a” (for URLs) class as specified in CSS.

For example, I have the following thing in PHP:

[code=php]$name = “<a href=’user.php?page=profile&uid=$uid’>”;

if (empty($custom_name_colour)) {
$name .= “<font style=’color: #7C60B0;’>”;
}
else {
$name .= “<span style=’$custom_name_colour’>”;
}

if (empty($display_name)) {
$name .= $username;
}
else {
$name .= $display_name;
}

if (empty($custom_name_colour)) {
$name .= “</font>”;
}
else {
$name .= “</span>”;
}

$name .= “</a>”;[/code]

The font style is actually being determined though a function based on the users’ gender and power level, but I chose one of them to make this question a bit more testable.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 03.2016 — Question: what is the format of the [FONT=Courier New][COLOR="#B22222"][B]$custom_name_colour[/B][/COLOR][/FONT] string? By any chance do you need to prepend [B][FONT=Courier New][COLOR="#B22222"]color:[/COLOR][/FONT][/B] to it?
Copy linkTweet thisAlerts:
@BlaveloperauthorDec 03.2016 — The "$custom_name_colour" variable is an CSS string fetched from MySQL, it can be anything actually.

But don't worry too much about that part, that one works as it should.

The problem exists in the "font style" part of things.

In the actual application, it's a "font class", but I replaced it by "font style" in this topic so it can be tested more easily.
Copy linkTweet thisAlerts:
@NogDogDec 03.2016 — Just eye-balling your code, it looks logical to me, though the <font> HTML tag has been deprecated for a long time. In any case, I'd be tempted to use sprintf() to tighten things up a bit structurally (untested):
[code=php]
$name = sprintf(
"<a href='user.php?page=profile&uid=%d'><span style='%s'>%s</span></a>",
$uid,
empty($custom_name_colour) ? 'color: #7C60B0' : $custom_name_colour,
empty($display_name) ? $username : $display_name
);
[/code]
Copy linkTweet thisAlerts:
@NogDogDec 03.2016 — PS, for that matter, the <span> seems superfluous. You could just do:
[code=php]
$name = sprintf(
"<a href='user.php?page=profile&uid=%d' style='%s'>%s</a>",
$uid,
empty($custom_name_colour) ? 'color: #7C60B0' : $custom_name_colour,
empty($display_name) ? $username : $display_name
);
[/code]
(still untested)
Copy linkTweet thisAlerts:
@BlaveloperauthorDec 03.2016 — Thanks, it works! ?
×

Success!

Help @Blaveloper spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...