i have two time values as give below
$row2[0]=00:40:00;
$row1[5]=14:00:00;
$time=14:33:00
$time=$row[1];
i am combining $row2[0],$row1[5] like below
$secs = strtotime($row2[0])-strtotime("00:00:00");
$result = date("H:i:s",strtotime($row1[5])+$secs);
$result=14:40:00
i use if condition as shown
if($row[1]>$row1[5] && $row[1]<$result)
{
$message=array("status"=>$result);
}
else
{
$message=array("status"=>'');
}
but i get "status"=>"" its not correct
i want to get "status"=>"14:40:00"
please help us for getting correct out put
You don't appear to define $row[1] anywhere, so it is treated as NULL, which as an integer is 0, which cannot be greater than the time you have given in $row1[5].
Try using less ambiguous variable names, it might make it easier to spot problems like this.
Personally I don't see why that if is there at all, just remove it and the else block, leaving just $message = array("status"=>$result);.
is this real oder pseudo code ?
because
$row2[0]=00:40:00;
is not real php code !
$row2[0] = 00:40:00; // $row2[0] would be 0, because you are assigning an integer
$row2[0] = "00:40:00"; // $row2[0] would be "00:40:00", but as a string (!)
i would always work with timestamps btw.
I go with Panique on his answer, I want to add also:
You declared two vars:
$row2[0]=00:40:00;
$row1[5]=14:00:00;
But you called different array elements in the if
if($row[1]>$row1[5] && $row[1]<$result)
Solve this and you should have your code working.
Related
I'm still learning PHP, but I needed a way of keeping track of a list of two associated values, a cinema and its postcode. Now before you read any further I must stress that I do not technically need this problem solving as I have since replaced it with a more efficient method. I'm really just wanting to know why it doesn't work as I can't find anything about it elsewhere.
$cinema_locations = array(
array("Odeon", "M4 2BS"),
array("Cineworld", "OL7 0PG"),
array("Vue", "M50 3AG"),
array("AMC", "M3 4EN")
);
for ($i=0; $i<count($cinema_locations); $i++) {
if ($cinema_locations[$i][0] == $_GET['cinema_name']) {
$postcode = $cinema_locations[$i][1];
return;
}
}
As you can probably tell from the code, I am trying to loop through the main array so that I may compare the first value of each child array against a $_GET variable. I have checked over this code multiple times and even showed some of my other coder friends and none of us can find anything wrong, syntax or otherwise. And yet, the browser shows only a white screen. If anyone can shed some light on the issue, I and my friends would be most appreciative; and who knows, it may help someone else with the same issue.
For anyone that may be curious, I replaced the 2D array with an associative array thus:
$cinema_locations = array(
"Odeon" => "M4 2BS",
"Cineworld" => "OL7 0PG",
"Vue" => "M50 3AG",
"AMC" => "M3 4EN"
);
$postcode = $cinema_locations[$_GET['cinema_name']];
EDIT
Thanks rishi, that did it. I never even considered that the return would nullify the result. Using break stopped the loop and the rest of the page loaded fine.
May be you should write break; instead of return;
You needed to break your for loop if you meet the condition. and continue with the below code.
return will instantly returns value from where its called.
For situations in which you cannot change the array structure, this code is a better approach.
// same array as the original post
$cinema_locations = array(
array("Odeon", "M4 2BS"),
array("Cineworld", "OL7 0PG"),
array("Vue", "M50 3AG"),
array("AMC", "M3 4EN")
);
foreach ($cinema_locations as $c_loc) {
$postcode = ($_GET'cinema_name'] == $c_loc[0]) ? $c_loc[1] : null;
}
print $postcode;
Of course, you should never use $_GET directly like this, but you probably already knew that.
I have a bit of php code that I'm not understanding why it is acting as it is. I have a variable called contactId that I want to test to see if it is empty. However even if it is empty it evaluates to true. Code is below. Thanks in advance.
print "*".$contactId."*<br/>";
if($contactId != '')
{
//queryContact($contactId);
print "Contact Present<br/>";
}
result returned to screen is:
**
Contact Present
If you want to see exactly what your string is, simply use var_dump(), like this, for instance:
var_dump($contactId)
instead of
print "*".$contactId."*<br/>";
Couple of things you can try:
if (!empty($contactId)) {
// I have a contact Id
}
// Or
if (strlen($contactId) > 0) {
// I have a contact id
}
In my experience I have often used the latter of the two solutions because there have been instances where I would expect a variable to have the value of 0, which is valid in some contexts. For example, if I have a drink search site and want to indicate if an ingredient is non-alcoholic I would assign it a value of 0 (i.e. IngredientId = 7, Alcoholic = 0).
Do it with if (isset($contactId)) {}.
You likely want:
if (strlen($contactId))
You'll want to learn the difference between '' and null, and between == and ===. See here: http://php.net/manual/en/language.operators.comparison.php
and here: http://us3.php.net/manual/en/language.types.null.php
In future, use if(!empty($str)) { echo "string is not empty"}.
I wanted to allow only specific email domain. Actually I did it. What i wanted to ask why my first code did not work at all.
I am just trying to learn PHP so that the question may seem silly, sorry for that.
Here is my code:
function check_email_address($email) {
$checkmail = print_r (explode("#",$email));
$container = $checkmail[1];
if(strcmp($container, "gmail.com")) {
return true;
}else {
return false;
}
}
Check out the documentation for strcmp() , it will return 0 of the two strings are the same, so that's the check you want to be doing. Also, you're using print_r() when you shouldn't be, as mentioned by the other answerers.
Anyway, here's how I would have done the function - it's much simpler and uses only one line of code:
function check_email_address($email) {
return (strtolower(strstr($email, '#')) == 'gmail.com');
}
It uses the strstr() function and the strtolower() function to get the domain name and change it to lower case, and then it checks if it is gmail.com or not. It then returns the result of that comparison.
It's because you're using print_r. It doesn't do what you seem to expect from it at all. Remove it:
$checkmail = explode("#", $email);
You can find the docs about print_r here:
http://php.net/print_r
Besides that, you can just use the following (it's much shorter):
$parts = explode("#", $email);
return (strcmp($parts[1], "gmail.com") == 0);
The following row doesn't work as you think it does:
$checkmail = print_r (explode("#",$email));
This means that you're trying to assign the return value from print_r() into $checkmail, but it doesn't actually return anything (if you don't supply the second, optional parameter with the value true).
Even then, it would've gotten a string containing the array structure, and your $container would have taken the value r, as it's the second letter in Array.
Bottom line: if your row would've been without the call to print_r(), it would've been working as planned (as long as you made sure to compare the strcmp() versus 0, as it means that the strings are identical).
Edit:
Interesting enough, I just realized that this could be achieved with the use of substr() too:
<?php
//Did we find #gmail.com at the end?
if( strtolower(substr($email, -10)) == '#gmail.com' ) {
//Do something since it's an gmail.com-address
} else {
//Error handling here
}
?>
You want:
if(strcmp($container, "gmail.com")==0)
instead of
if(strcmp($container, "gmail.com"))
Oh! And no inlined print_r() of course.
Even better:
return strcmp($container, "gmail.com")==0;
No need for the print_r; explode returns a list. And in terms of style (at least, my style) no need to assign the Nth element of that list to another variable unless you intend to use it a lot elsewhere. Thus,
$c = explode('#',$mail);
if(strcmp($c[1],'gmail.com') == 0) return true;
return false;
I know how to get the value from the query string if the parameter exists:
$hop = $_GET['hop'];
But I also need to set a default value IF it's not in the query string. I tried this and it didn't work:
$hop = $_GET['hop'];
if ($hop = " ") {
$hop = 'hardvalue';
};
Please help me handle the case where the query string has and does not have the "hop" parameter, and if it's present but not defined:
example.com/?hop=xyz
&
example.com/
&
example.com/?hop=
PS I don't know what I'm doing, so if you explain to me, please also include the exact code for me to add to my PHP page.
use array_key_exists
if (array_key_exists('hop', $_GET))
{
// the key hop was passed on the query string.
// NOTE it still can be empty if it was passed as ?hop=&nextParam=1
}
else
{
//the key hop was not passed on the query string.
}
Thought about it a bit more and decided it should be a bit more robust:
$hop = 'hardvalue';
if (array_key_exists('hop', $_GET)) {
if (!empty($_GET['hop'])) { $hop = $_GET['hop']; }
}
You already got the fiddly solutions. When working with URL or form parameters, you often want to treat the empty string or zeros as absent values too. Then you can use this alternative syntax:
$hop = $_GET["hop"] or $hop = "hardvalue";
It works because of the higher precedence of = over or, and is easier to read with extra spaces.
Starting from PHP 5.3 it's also possible to use:
$hop = $_GET["hop"] ?: "hardvalue";
The advantage here is that this syntax doesn't slurp up php notices, which are useful for debugging.
Actually, I would use
$hop = !empty($_GET['hop']) ? $_GET['hop'] : 'default';
Using empty() instead of isset() takes care of your third scenario, where the parameter is present but not defined.
Also, in if ($hop = " ") the = would need to be changed to ==. = assigns, == tests equality. The way you have it, the if-statement will always run, no matter what $hop equaled.
Let me start by saying I know nothing about Joomla or PHP so i might be comparing apples to oranges here but....
I keep seeing JRequest::getVar($var) and $var in some code i've been looking at. Can someone explain what the difference between these two is?
According to Joomlas' documentation getVar , "Fetches and returns a given variable." If that is the case why would the following code return different results?
echo JRequest::getVar($amount);
echo $amount;
Disclaimer: I don't know the first thing about Joomla.
Looking at the documentation, JRequest::getVar fetches values from GET or POST parameters and is supposed to be used like JRequest::getVar('amount') (note the argument is a string, not a variable). echo $amount and echo JRequest::getVar($amount) are of course nowhere near equivalent, the former means "echo the value of $amount", the latter "echo the value of a GET or POST variable with the name of the value of $amount."
The second parameter $default is also very useful:
$amount = JRequest::getVar('amount', 100);
// is roughly equivalent to:
if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['amount'])) {
$amount = $_GET['amount'];
} else if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['amount'])) {
$amount = $_POST['amount'];
} else {
$amount = 100;
}
Even without the second parameter though, it'll take care of variable cleaning and (un)escaping where necessary, so it's a good idea to use this function to read request parameters.
getVar gets a variable from one of PHPs global arrays depending on the hash argument passed.
Joomla API
Code