Jump to content

Recommended Posts

I have a foreach loop in a webpage that displays some fields ($c), it looks like this:

foreach($this->fDisplay[5] as $field)
{    
$c = $this->field->showFieldValue($this->content,$field);
if(($c !== "")&&($c !== null)) {
    $title = $this->field->showFieldTitle(@$this->content->catid,$field);
    echo "<span class='f".$field->name."'>";
    if ($title != "")
        echo "<b>".htmlspecialchars($title)."</b>: ";
    echo "$c<br/>";
    echo "</span>";
}


Now I want to put a condition on only one of these fields. I want the field "email" only to show if the user is logged in. If the user is not logged in I want this field to be hidden in frontend. (I already have a variable for logged in users) What is the best way to do this?

On 12/15/2020 at 7:17 PM, Silverman said:

I want the field "email" only to show if the user is logged in.

 

19 hours ago, Silverman said:

The email is required so that will always be there.

And now I'm confused...

I know I need a if-statment. But I don't know how to write this, I'm a beginner with PHP. 

if ($c "contains field email" && $user->guest) {

    "remove field email from $c"

}

Sorry if these are stupid newbie questions.

2 hours ago, maxxd said:

 

And now I'm confused...

The value (the users email) will always be inside that array. But sometimes I want to show it, sometimes not.

Thats why I need some kind of if-statment to remove the users email-field depending on if the user is logged in or not.

17 minutes ago, Silverman said:

if ($c "contains field email" && $user->guest) {

1. Are you sure $c is the field name? Are you sure the field name isn't in the conveniently-named $field variable?
2. Are you sure it has to be as complicated as "contains"? Surely the name of the field is "email"?

 

17 minutes ago, Silverman said:

"remove field email from $c"

Forget "remove". Try phrasing it a different way with different words.

What I was trying to say was "When the foreach finds the email inside $c...do this to it...".  But you are right, the name is email. And it should be inside the $field variable i guess.

Ok, I don't need to remove it. Only not display it. Maybe I can do:

if ($this->content,$field == email && $user->guest)

{"Put this field inside a CSS class"}

And then hide it with CSS? Or is there any better way?

If you don't want to display a thing, and by that I mean it doesn't even need to go on the page, then wouldn't it be best if you completely skipped outputting the HTML for that field entirely?

Here's your code, slightly cleaned up and in such a way that points out how what you posted was incomplete:

foreach($this->fDisplay[5] as $field)
{    
	$c = $this->field->showFieldValue($this->content,$field);
	if(($c !== "")&&($c !== null)) {
		$title = $this->field->showFieldTitle(@$this->content->catid,$field);
		echo "<span class='f".$field->name."'>";
		if ($title != "")
			echo "<b>".htmlspecialchars($title)."</b>: ";
		echo "$c<br/>";
		echo "</span>";
	}

I don't know what else is in the foreach loop - perhaps there's nothing else and the next line is the loop's closing brace - but odds are you can skip everything in that loop as soon as you realize it's trying to process the email field. continue lets you do exactly that.

At the beginning of the loop, add an if statement that checks your criteria: it's the email field and the user is logged in. For its code, call continue; and... I think that should be all you need.

If you have problems, post the code you came up with.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.