Advertisement:

Author Topic: [SOLVED] Correct An If Statement?  (Read 141 times)

Rupert

  • Newbie
  • *
  • Posts: 15
[SOLVED] Correct An If Statement?
« on: July 27, 2019, 10:49:56 pm »
Hello All,

(Osclass version 3.8. / bender theme)

I've been trying to write a simple php "if" statement. The results have been...amusing. Even though the below has errors, I think you can see what I'm trying to do in search.php:


<?php
if (osc_category_name($locale = "115")) {echo "<h1>Wording For Category 115</h1>";}
elseif (osc_category_name($locale = "159")) {echo "<h1>Wording For Category 159</h1>";}
else {echo "<h1>all else</h1>";}
?>


And, of course, it's wrong. It outputs "Wording For Cagegory 115" on all the pages. I've been at it a while and looking at a lot of examples on the Web but can't see my mistake(s).

If someone could help me correct the above I would appreciate any assistance.

Thanks.


« Last Edit: July 28, 2019, 07:58:04 am by Rupert »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Correct An If Statement?
« Reply #1 on: July 28, 2019, 12:47:07 am »
osc_category_name($locale = "115")

What is the value of $locale before this line?

Search the project to see how this function is called/used.

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Correct An If Statement?
« Reply #2 on: July 28, 2019, 12:51:28 am »
Also, that function returns something different than false, null or empty string. That is why the first if is always true. You should compare the name with something.

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Correct An If Statement?
« Reply #3 on: July 28, 2019, 01:38:17 am »
Hello!

Function osc_category_name($locale) gets name of the current category in View. $locale parameter is not required and it's used to specify a language.

If you want to check if current category ID matches your ID use
  • if(osc_item_category_id() == YOUR_ID) - for item page
  • if(@osc_search_category_id()[0] == YOUR_ID) - for search page

Regards.

Rupert

  • Newbie
  • *
  • Posts: 15
Re: Correct An If Statement?
« Reply #4 on: July 28, 2019, 02:31:47 am »
Thanks all, I do appreciate the replies.

WEBmods, your answer seemed closest to what I was looking for. I put the below into search.php instead of the <h1> tag that was already there:

<?php
if (if(@osc_search_category_id()[0] == 115)) {echo "<h1>Wording For Category 115</h1>";}
elseif (if(@osc_search_category_id()[0] == 159)) {echo "<h1>Wording For Category 159</h1>";}
else {echo "<h1>all else</h1>";}
?>

But I got a mostly blank page with an error that said
"Parse error: syntax error, unexpected 'if' (T_IF) in xxxxx/public_html/oc-content/themes/bender/search.php on line 77"

Did I put the code in the wrong place?

Thank you.


Rupert

  • Newbie
  • *
  • Posts: 15
Re: Correct An If Statement?
« Reply #5 on: July 28, 2019, 07:14:10 am »
Yikes! Just saw my mistakes in the post above! After removing the mistakes it works! It works great!

Here it is:

<?php
if (@osc_search_category_id()[0] == 115) {echo "<h1>Wording For Category 115</h1>";}
elseif (@osc_search_category_id()[0] == 159) {echo "<h1>Wording For Category 159</h1>";}
else {echo "<h1>all else</h1>";}
?>


Thanks marius-ciclistu for the input, and WEBmods for the solution!

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: [SOLVED] Correct An If Statement?
« Reply #6 on: July 31, 2019, 03:53:52 pm »
You're welcome! :)

I would like to point out why I added @ in front of osc_search_category_id(). It's because osc_search_category_id() returns an array, which is empty if no category is selected and has a key 0 with category ID as value if category is selected. @ will stop warnings if array is empty, altough it's recommended to use array_key_exists($key, $arr), but it complicates things.

Regards.

Rupert

  • Newbie
  • *
  • Posts: 15
Re: [SOLVED] Correct An If Statement?
« Reply #7 on: August 02, 2019, 01:39:30 am »
Thanks for the additional information regarding the @ sign. I'm trying to understand php better, and every bit helps.