when the following else statement will be executed? if the $system_path is invalid, of cause ,it will go to the else statement. however, in this case, the else statement seems to be meaningless.
https://github.com/bcit-ci/CodeIgniter/blob/develop/index.php line 196
if (($_temp = realpath($system_path)) !== FALSE)
{
$system_path = $_temp.DIRECTORY_SEPARATOR;
}
else
{
// Ensure there's a trailing slash
$system_path = strtr(
rtrim($system_path, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
).DIRECTORY_SEPARATOR;
}
The point of that if/else block is to construct a string that makes any kind of sense for use in the if block that follows. That block starts with
if( ! is_dir($system_path))
If realpath() returns FALSE then $system_path as written cannot be resolved. But that might not mean the path does not exist. Perhaps the developer knows something that 'realpath()' cannot figure out. The else part gives the developer the benefit of the doubt. It takes the supplied value of $system_path and formats it for use in is_dir($system_path).
Off the top of my head I cannot think of an example where realpath() would fail but where $system_path does exist. But developers are a creative bunch.
Related
I'm trying to create a function, it exists and does not exist in the search feature. Condition does not exist, will display Error 404. Condition, exists. Then it will display from search. But in the code I wrote, it only shows Error 404.
This is my model:
function search_album($keyword){
$result=$this->db->query("SELECT tbl_album.*,DATE_FORMAT(album_date,'%d %M %Y') AS date FROM tbl_album WHERE album_title LIKE '%$keyword%'");
return $result;
}
This is my controller:
function search(){
$keyword=str_replace("'", "", $this->input->post('xfilter',TRUE));
$x['data']=$this->model_album->search_album($keyword);
if(empty($x) && empty($keyword)){
$this->load->view('view_contents',$x);
}
else if (!empty($x) && !empty($keyword)){
$this->load->view('view_error_404');
}
}
I've tried from this source, but it doesn't work. Can you help me?
There are several errors in your code:
ONE: You have interchanged your if statements. Like, if the search did not return any data and the keyword was not supplied, it is supposed to display the "view_error_404", otherwise, it is supposed to load data into the view "view_contents".You did the vice versa I have corrected that for you in the code below.
TWO: You are checking if $x is empty which will never be empty as you have initialized $x['data']. Note that the model can return empty data, such that $x['data'] is empty. Instead, check if the search result is empty by replacing empty($x) with empty($x['data'])
THREE: In your model, you are returning the query builder class, but not the actual data from the query statement. instead, replace return $result; with return $result->result();
FOUR:
From your if statements, you need to add an else statement so that if the 2 conditions are never met, it can execute. With your current implementation, there is a state which will not meet first or second conditions and will lead to a blank screen.
if(empty($x['data']) && empty($keyword)){
$this->load->view('view_error_404');
}else if (!empty($x['data']) && !empty($keyword)){
$this->load->view('view_contents',$x);
}else{
$this->load->view('view_error_404'); // replace this code with your preference
}
you need to mention search function in routes.php file like this:-
$routes['search'] ='your_controller_name/search';
without knowing your function model_album->search_album($keyword) and assuming it returns a string or array, you don't need to worry about checking for the keyword, since without a keyword the function should return false or empty.
anyway you mixed up your if/else conditions, since you are checking for not empty results to return a 404, instead of empty result returning the 404.
this should do it:
if(empty($x)){
$this->load->view('view_error_404');
}
else {
$this->load->view('view_contents',$x);
}
I want to add multiple conditions to a breakpoint in IntelliJ.
Something like:
stringA.equals("test") && objectA.equals(objectB);
How can I do this?
IntelliJ IDEA breakpoint condition can be any boolean expression:
Select to specify a condition for hitting a breakpoint. A condition is
a Java Boolean expression including a method returning true or false,
for example, str1.equals(str2).
This expression must be valid at the line where the breakpoint is set,
and it is evaluated each time the breakpoint is hit. If the evaluation
result is true, the selected actions are performed.
You can enter multi-line expressions, for example:
if (myvar == expectedVariable) {
System.out.println(myvar);
anotherVariable = true;
}
return true;
stringA.equals("test") && objectA.equals(objectB) appears to be a valid expression returning true or false, so it should work right out of the box.
Proof of work:
The following condition statement will also work:
return stringA.equals("test") && objectA.equals(objectB);
Please note that there is a known issue which will show a red underline after the condition indicating that semicolon is expected. If you add the semicolon, the condition will become invalid and you will have to also add a return statement to make it valid again. It's a cosmetic issue and you can either use the condition without semicolon and ignore the error or you can add the semicolon and return to make it a valid statement:
So adding a return statment in front of the statment solved the problem.
this is my code:
if node['app']['source']
src = "#{node['app']['source']}\\#{node['app']['file_name']}"
else
src = "second_source"
end
I want to add a log.warn at the end of my statement in case of any source isn't valid,
something like:
if node['app']['source']
src = "#{node['app']['source']}\\#{node['app']['file_name']}"
else
src = "second_source"
whatever
Chef::Log.warn "This path #{src} is not supported, check attributes again"
return
end
I will glad if somebody has an any idea,
Thank you...
That isn't how code works, a condition can either be true or false, and the two branches of if/else cover both cases. You would have to come up with a way to check if a source is valid and use if/elsif/else or similar.
I'm getting some unexpected behaviour in the most recent Dart editor (version 0.4.0_r18915).
I have this minimal command line app that was intended to either take a command line argument or not and print a hello -somenoe- message. The application works just fine. But the debuggins fails to stop at the breakpoints set inside each of the if statement bodies. (I wanted to look at the state of the application weather the options.arguments.isEmpty was true or false)
var person;
main(){
var options = new Options();
if(options.arguments.isEmpty){
person = "someone who forgot to pass a command-line argument";
} else {
person = options.arguments[0];
}
print("Hello, $person!");
}
Debugger will stop at breakpoints in other lines but not in:
person = "someone who forgot to pass a command-line argument";
or in:
person = options.arguments[0];
Yes, file a bug. My suspicion is that the debugger can only stop at what's called a "safepoint" and that the assignment of a constant to a variable doesn't create one. Adding some line above it, like
print("breakpoint");
should help if that's the case. But I've also seen other problems with breakpoints not firing.
How do I create an if statement saying something like this?
Basically, how do you use the URI class to determine if there is a value in any segment?
$segment = value_of_any_segment;
if($segment == 1{
do stuff
}
I know this is pretty elementary, but I don't totally understand the URI class...
Your question is a little unclear to me, but I'll try to help. Are you wondering how to determine if a particular segment exists or if it contains a specific value?
As you are probably aware, you can use the URI class to access the specific URI segments. Using yoursite.com/blog/article/123 as an example, blog is the 1st segment, article is the 2nd segment, and 123 is the 3rd segment. You access each using $this->uri->segment(n)
You then can construct if statements like this:
// if segment 2 exists ("articles" in the above example), do stuff
if ($this->uri->segment(2)) {
// do stuff
}
// if segment 3 ("123" in the above example) is equal to some value, do stuff
if ($this->uri->segment(3) == $myValue) {
// do stuff
}
Hope that helps! Let me know if not and I can elaborate or provide additional information.
Edit:
If you need to determine if a particular string appears in any segment of the URI, you can do something like this:
// get the entire URI (using our example above, this is "/blog/article/123")
$myURI = $this->uri->uri_string()
// the string we want to check the URI for
$myString = "article";
// use strpos() to search the entire URI for $myString
// also, notice we're using the "!==" operator here; see note below
if (strpos($myURI, $myString) !== FALSE) {
// "article" exists in the URI
} else {
// "article" does not exist in the URI
}
A note regarding strpos() (from the PHP documentation):
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "". Please read
the section on Booleans for more
information. Use the === operator for
testing the return value of this
function.
I hope my edit helps. Let me know if I can elaborate.