Using three elements condition in PHP - php

I have a weird bug.
I have A class names Lessons with 3 elements: video, audio & written.
A functios called IsReadOnly supposed to check if a lesson has something only in the 'written' cell while the 'audio' and the 'video' are empty.
This is how it looks:
public function IsReadOnly() {
if ($this->Audio=="" && $this->Video=="" && $this->WrittenLesson!="")
return true;
else
return false;
}
In the PHP page I'm calling the function:
if (!$Less->IsReadOnly()) {
// ...
}
else {
// ...
}
as far as i get - while video/audio will return false, the full condition will return false also and the IF (not the ELSE) will be executed.
But, thats not working. If the video is empty and the audio isn't - everythig is fine. but if the audio is empty and the video isn't - the ELSE is executed. (I've tried replacing between them, nothing...).
What am I doing wrong?
Thanks.

Perhaps you need to make the if more forgiving:
if ($this->WrittenLesson && !$this->Audio && !$this->Video)

Apparently the solution was to make the SSCCE...
While testing I've found the problem. The video wasn't the only cell I had to check because in the database there were two fields that represent video.
Thanks!

Related

WordPress - PHP conditional logic

I have a really simple question about PHP conditional logic. I have some code where I want to load a different header file based on the post category type. The code below works:
if(in_category('news')) {
get_header('tertiary');
}
elseif(in_category('events')) {
get_header('tertiary');
}
else {
get_header('secondary');
}
But when I try to simplify the code to:
if(in_category('news' || 'events)) {
get_header('tertiary');
}
else {
get_header('secondary');
}
The tertiary header file for the events is not loading, it is showing the secondary header file. I'm using similar code elsewhere in my theme and it is working with no issues. So I'm not sure why it is not working here. I'm not getting an errors in my PHP console.
There may be two ways you can do this. Check in_category individually (this will always work)
if(in_category('news') || in_category('events')) {
Or in_category can take an array, in some versions of Wordpress:
if(in_category(['news', 'events'])) {
You need to read the documentation and instead of
if(in_category('news' || 'events)) {
...
do
if(in_category(['news', 'events'])) {
...
You need to read the documentation
if ( in_category( Array( 'news', 'events' ) ) ) {
get_header('tertiary');
} else {
get_header('secondary');
}

Disable span tag in PHP for GLPI

all.
I am trying to disable no-mandatory fields in GLPI. As the application doesn't offer this option, I am trying to change the source code to do it.
This is the piece of code that is related to the mandatory fields:
function getMandatoryMark($field, $force=false) {
if ($force || $this->isMandatoryField($field)) {
return "<span class='red'>*</span>";
}
return '';
}
And this is what I am trying to do:
function getMandatoryMark($field, $force=false) {
if ($force || $this->isMandatoryField($field)) {
return "<span class='red'>*</span>";
}
else{
return "<span onclick='return false;'>*</span>";
}
return '';
}
But when I make this change, create tickets page doesn't load. I am not familiar with PHP so I have no idea what is going on...
No need to edit the files.
Just create a ticket template, name it (e.g. EasyTicket), and make mandatory, add or hide whatever fields you need. I would suggest using Simplified Interface for end users. Less fuss ;)
Manage Templates
Then go to your end users profile (post-only or self-service probably), and choose your Default ticket template (EasyTicket).

if title contains string 'aaa','bbb' or ,'ccc' replace field {category[1}

Hello gods of Stackoverflow,
Now i hate to be "that guy" who didnt search properly but i am running into a problem that i need a fix for and can't find a solution i can work with because of my lack in coding skills, my knowledge barely tickles the surface.
Here's the thing.
I am using a tool to import feeds into my website (WP all Import) as woocommerceproducts. But in the categorization the feed suppliers made errors which i want to tackle without emailing them everytime i stumble upon one.
i.e: the title contains words like satchel, bag, clutch etc but the product is categorized as 'jewellery > earrings' in the CSV or XML.
The import tool will ask me where to find the product category, i point it to the node {category[1]}
But when the category is missing or faulty i want it to check the title for certain words, and if present change the value of it to that found word.
something like:
[if ({title[1]}contains "satchel") {
category = "bags > satchel",
} else if ({title[1]} contains clutch) {
category = "bags > clutch",
} else {
category = {sub_category[1]} #the normal value if nothing was found
}]
I just can't find the pieces to put the formatting together. I might need to work towards a function that i could expand to generate categories based solely out of presence of certain words in the title but maybe when i get better that would be an option.
I hope i was able to provide a clear view on the problem. The "[ ]" is there because thats how the plugin wants code to be entered instead of a {fieldname[1]}, another example below:
The following was an example of a problem i was able to fix:
i needed to replace values like "0/3months/1/2months" to "0-3 months/1-2months" before i replaced the slash"/" with a pipe"|" for wordpress to recognize it as a seperate value.
[str_replace("/","|",
str_replace("0/3","0-3",
str_replace("1/2","1-2",
str_replace("3/6","3-6",{size[1]}))))]
The fields can also be used to call functions but only in the 'pro' version of the plugin.
Any help is very much appreciated, thanks in advance.
You could use strpos.
Example:
if (strpos($title, "satchel") !== false) {
$category = "bags > satchel";
}
So after an afternoon of poking around, testing stuff, and not knowing alot about php i came up with a solution with help from a friend.
Wp All Import does not allow custom php directly from the field itself, it does however support custom php functions and provides an editor for these on the bottom of the import configuration page. I did the following:
<?php
//Checks Title for keywords and
//uses those to create categories
//We are using our own Main categories so only
//sub and subsub categroies need to be created.
//Call function
[get_subcat_from_title({title[1]},{category[1]})]
//Function
function get_subcat_from_title($title,$defaultcat)
{
if (strpos($title,"Satchel") !== false) {
$cat = "Tassen";
} elseif (strpos($title,"Travel") !== false) {
$cat = "Tassen";
} elseif (strpos($title,"Gusset") !== false) {
$cat = "Tassen";
} else {
$cat = $defaultcat;
}
return $cat;
}
//Checks Title for keywords and uses those to create subcategories
//Call Function
[get_subsubcat_from_title({title[1]},{sub_category[1]})]
//Function
function get_subsubcat_from_title($title,$defaultcat)
{
if (strpos($title,"Satchel") !== false) {
$cat = "Satchel";
} elseif (strpos($title,"Travel") !== false) {
$cat = "Travel";
} elseif (strpos($title,"Gusset") !== false) {
$cat = "Gusset";
} else {
$cat = $defaultcat;
}
return $cat;
}
?>
On the Taxonomies, Tags, Categories option we can create our own hierarchical order like this:
[main category]
+[sub category]
++[sub sub category]
The main category field is given a name we use as our main category.
The sub category is filled with function SUBCAT
The subsub category is filled with function SUBSUBCAT
this way it will create a parent by our own name, a child that has the named 'Tassen' if any keyword is present, and a grandchild with the specific keyword as it's name.
This way i can expand the function using all kinds of statements when the right category isn't present in de provided feed.
thanks #sebastianForsberg for responding.
I hope this helps anyone who comes across a similar problem in the future..

Codeigniter--add “active” css class, how to apply on a link?

i have a url like this
http://localhost/bestbookfinder.com/viewallbooks/books/pgn/grid/6
where:
bestbookfinder.com: Project name
viewallbooks : class name
books :function
pgn :Constant Parameter
grid :view type
6 :pagination current page number
so now i am trying to read the above shown url and to apply the CSS class with the following code, but unfortunately my code is not working.
I think i am making mistake when i tried to read the url.
Please help me to solve this problem
<?php
if ( $this->uri->uri_string() == '/books/pgn/grid' )//i think mistake is here
{
echo "".anchor(base_url().'viewallbooks/books/pgn/grid/'.$this->uri->segment(5),' Grid', array('id' => 'gridactive'))."";
}
?>
From the codeigniter manual - uri_string() returns a string with the complete URI. So in your example that would be "viewallbooks/books/pgn/grid/6".
Therefore "viewallbooks/books/pgn/grid/6" != '/books/pgn/grid'
Just do something like this:
if ($this->uri->segment(4) == "grid")
{
// do something
}
if($this->uri->segment(4) == 'grid'){
//carry on

PHP else statement not working as it should.. is_numeric and isset

In the following If.. else statement, I am trying to see if first, there IS a 4th breadcrumb (URI array 4), and then to see if it is a number..
but both always return true, number, letters, or empty..
can anyone tell me why?
// checks the third breadcrumb contained within the breadcrumbs class to see
// if it is an article. Then uses a recursive function to search the first
// and second breadcrumbs to see if they exist within their respective menu
// arrays. finally, if that validates, then the fourth breadcrumb is checked
// to see if it exists (not empty) AND if it is a number.
else
if (($this->breadcrumbs->getCrumb(3)=='article' &&
$array_helper->recValueReturn($this->breadcrumbs->getCrumb(2),
$this->getNavTwo()) &&
$array_helper->recValueReturn($this->breadcrumbs->getCrumb(1),
$this->getNavOne())) || ($this->breadcrumbs->getCrumb(4)) &&
is_numeric($this->breadcrumbs->getCrumb(4))) {
...
the following always validates to False:
else
if (($this->breadcrumbs->getCrumb(3)=='article'&&
$array_helper->recValueReturn($this->breadcrumbs->getCrumb(2),
$this->getNavTwo()) &&
$array_helper->recValueReturn($this->breadcrumbs->getCrumb(1),
$this->getNavOne())) &&
(array_key_exists($this->breadcrumbs->getCrumb(4),
$this->breadcrumbs->getCrumbs())&&
is_numeric($this->breadcrumbs->getCrumb(4)))) {
...
For future reference, and for anyone else reading this..
The problem was solved in the BreadCrumbs class itself.
Previous, breadcrumbs had a private array with the exploded URI values. And I retreived a single url index using:
public function getCrumb($x) { return $this->breadcrumbs[$x] }
But if i modify this getter directly to include an isset:
public function getCrumb($x) {
if (isset($this->breadcrumbs[$x])) {
return $this->breadcrumbs[$x];
} else {
return null;
}
}
and then use the first else..if in the OP.. problem solved. It works.

Categories