Silverman Posted December 16, 2020 Share Posted December 16, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/ Share on other sites More sharing options...
requinix Posted December 16, 2020 Share Posted December 16, 2020 In other words, "if the field is email && the user is logged in || the field is not the email" then show it? Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583121 Share on other sites More sharing options...
Silverman Posted December 16, 2020 Author Share Posted December 16, 2020 (edited) Yes, only "if the field is email && the user is logged in". The email is required so that will always be there. Edited December 16, 2020 by Silverman Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583123 Share on other sites More sharing options...
requinix Posted December 16, 2020 Share Posted December 16, 2020 Great. So... problem solved? Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583145 Share on other sites More sharing options...
maxxd Posted December 17, 2020 Share Posted December 17, 2020 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... Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583171 Share on other sites More sharing options...
Silverman Posted December 17, 2020 Author Share Posted December 17, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583173 Share on other sites More sharing options...
Silverman Posted December 17, 2020 Author Share Posted December 17, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583174 Share on other sites More sharing options...
requinix Posted December 17, 2020 Share Posted December 17, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583175 Share on other sites More sharing options...
Silverman Posted December 17, 2020 Author Share Posted December 17, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583176 Share on other sites More sharing options...
requinix Posted December 17, 2020 Share Posted December 17, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583177 Share on other sites More sharing options...
Silverman Posted December 17, 2020 Author Share Posted December 17, 2020 Yes, this works if ($field->name == "email" && $user->guest) { continue; } Thank you 😃 Quote Link to comment https://forums.phpfreaks.com/topic/311871-find-value-inside-foreach-and-use-if-statment-on-that-one/#findComment-1583204 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.