<?php
$realtime = date("2016-09-22 12:55:24");
$mongotime = New DateTime($realtime);
$mt = $mongotime->getTimeStamp();
var_dump($mt);
//database Connection
$server= mongodb://localhost:27017";
$c = new Mongo($server);
$db = $c->dbname;
echo "Database selected";
//collection selection
$collection = $db->collection;
echo "Collection selected";
$cursor = $collection->find();
//If Condition here i'm getting diifficulty
if (($realtime-timestamp)<=5) {
if (channel<=11) {
if (channel>=36) {
echo ādual bandā;
}
}
if (channel>=36) {
if (channel<=11) {
echo ādual bandā;
}
} else {
echo āSingle bandā;
}
foreach ($cursor as $doc) {
var_dump($doc);
}
}
?>
Hi @chopraarpita92 and welcome to SitePoint Forums.
Please enclose your scripts starting and ending with three backticks on their own lines:
<?php
$realtime = date("2016-09-22 12:55:24");
$mongotime = New DateTime($realtime);
$mt = $mongotime->getTimeStamp();
var_dump($mt);
# DATABASE CONNECTION
$server= mongodb://localhost:27017";
$c = new Mongo($server);
$db = $c->dbname;
echo "Database selected";
# COLLECTION SELECTION
echo "Collection selected";
$collection = $db->collection;
$cursor = $collection->find();
# If Condition
if (($realtime-timestamp) <= 5) {
if (channel <= 11) {
if (channel>=36) {
echo ādual bandā;
}
}
if (channel >= 36) {
if (channel<=11) {
echo ādual bandā;
}
}else{
echo āSingle bandā;
}
foreach ($cursor as $doc) {
var_dump($doc);
}
}
I find it best to indent code to see where the condition starts and ends.
Can you spot the problem where certain conditions are never met?
Thanks John,
Iām using first time mongo with php. So iām not familiar that how to use if condition with find query. iām able to fetch the data if iām not using if condition but what i need is putting time value & checking the value with database time value if they hv difference btw less than or equal to 5 then iām checking of the channel value.
Here is the dataformat -
{ ā_idā : ObjectId(ā57e36429e4b0b720a9196d8eā), ātimestampā : NumberLong(1474520101), āageā : NumberLong(2713), ādata_rateā : NumberLong(4), ātypeā : NumberLong(2), āchannelā : NumberLong(6), āapā : ā36.222.198.197.171.30ā, āassociatedā : NumberLong(1), ārssiā : NumberLong(-67) }
Can you see that if the channel <= 11
it will never ever meet the following condition?
Same applies for the next if(channel >= 36)
I have a bunch of data & want to check that in the whole database do i have both type of channel data. i mean some channels are less than 11 & some are greater than 36 so want to check wheather i have both type of data. when iām checking the time. pre define time with database time also it is not comparing.
Putting aside the conditional logic errors, have you tried running the āqueryā with hard-coded values to see if at least that works?
Off-Topic
if ((age < 21) AND (age > 65)) {
echo āagelessā;
}
yes i have checked with hard core value.
Please read and try to understand Post #4.
The PHP logic is incorrect and some conditions will never be met.
Try adding these lines to the top of your script:
<?php
error_reporting(-1);
ini_set( 'display_errors', 'true');
$realtime = date("2016-09-22 12:55:24");
Unless channel is a predefined constant an error will be shown.
Also to ensure the if condition is correct I would add the following:
echo '<br>' .__LINE__ .' | ' .$realtime-timestamp;
if (($realtime-timestamp)<=5) {
echo '<br>' .__LINE__ .' | ' .$realtime-timestamp;
die;
Thanks. As long as that code is working it leaves the conditional as the problem area.
Iām guessing that the database is returning a āfull set of resultsā and you are using PHP to work with those results instead of using PHP to put together queries that would select only what youāre interested in.
The latter would likely be more efficient, but as long as the database is small the former is probably good enough and any difference between the two would be negligible.
I see ā$realtime-timestamp)<=5ā so Iām assuming if itās > 5 you do nothing with the results.
Then you have āchannel<=11ā and āchannel>=36ā
I donāt know which is dual band and which is single band, but I think if you donāt nest the contradicting condition (and make sure theyāre variable$) you just may see what youāre hoping to see.
EDIT
I just noticed your code example has ācurly quotesā. These will cause problems. If you are using Word as a code editor please donāt
Try this
if (($realtime-timestamp) <= 5) {
if ($channel <= 11) {
echo "channel less than 11";
}
if ($channel >= 36) {
echo "channel greater than 36";
}
}else{
echo "timestamp greater than 5";
}
is ā$realtime-timestampā a defined variable?
Thanks John! I have tried this one & it is not working. i canāt put $channel if iām putting $ then it is giving error. āchannelā is working but the thing is i have channel 40 which is greater than 36 so it should like channel greater than 36 but it is showing channel less than 11.
Errors are preventing your script from working.
What are the errors displayed?
Try using my debug function:
<?php
error_reporting(-1);
ini_set( 'display_errors', 'true');
//===========================
usage fred( $var );
//===========================
function fred($val)
{
echo '<pre>';
print_r( $val );
// var_dump( $val );
echo '</pre>';
}//
// try this:
fred(cursor);
Iām afraid that because Iām unfamiliar with mongoDB you likely know more than I do, and that I can only guess.
So āchannelā is a mongo global or a field name and not a PHP variable?
Could you eliminate all the PHP if conditional code by using a different āfindā line? eg.
$cursor = $collection->find( { channel { $lt: 11, $gt: 36 } } );
Off-topic
I thought Rubyās ActiveRecord syntax was different, but seeing operators that look like variables !
yes its not a variable, Its a field name. Find query what you have suggested i canāt use that because it will give the data only which has channel btw 11 to 36. I have used the same query with another piece of code.
Try using the following breakpoint and ensure you have no errors then gradually move the breakpoint until you discover an error.
echo '<br>' .__LINE__; kill;
// when an error is encountered use the following:
fred( $variable-giving-the-problem );
echo '<br>' .__LINE__; kill;
// or
var_dump( $variable-giving-the-problem );
echo '<br>' .__LINE__; kill;
If you cannot solve the problem then copy and paste the errors here.
Edit:
Added missing text.
Hmmm. confusing indeed.
I must say I would find it aggravating to have my
āless than 11 or greater than 36ā
interpreted as
āgreater than 11 and less than 36ā
There have been times Iāve thought about trying mongo, but not anymore.
What is a complete list of all the possible āchannelā values?
for less than 11 it can be 3,6,11 & greater than equal to 36 can be 36,40,44 something like that
yes bit confusing. Can ewsily find the values btw 11 to 36 but diificult to find less than 11 & greater than 36.
Very interesting discussion glad that I came across such informative post.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.