Viewed   105 times

I have made a small application using jQuery's datepicker. I am setting unavailable dates to it from a JSON file which looks like this:

{ "dates": ["2013-12-11", "2013-12-10", "2013-12-07", "2013-12-04"] }

I would like to check if a date given is already in this list and remove it if so. My current code looks like this:

if (isset($_GET['date'])) //the date given
{
    if ($_GET['roomType'] == 2)
    {
        $myFile = "bookedDates2.json";
        $date = $_GET['date'];
        if (file_exists($myFile))
        {
            $arr = json_decode(file_get_contents($myFile), true);
            if (!in_array($date, $arr['dates']))
            {
                $arr['dates'][] = $_GET['date']; //adds the date into the file if it is not there already
            }
            else
            {
                foreach ($arr['dates'] as $key => $value)
                {
                    if (in_array($date, $arr['dates']))
                    {
                        unset($arr['dates'][$key]);
                        array_values($arr['dates']);
                    }
                }
            }
        }

        $arr = json_encode($arr);
        file_put_contents($myFile, $arr);
    }
}

My problem here is that after I encode the array again, it looks like this:

{ "dates": ["1":"2013-12-11", "2":"2013-12-10", "3":"2013-12-07", "4":"2013-12-04"] }

Is there a way to find the date match in the JSON file and remove it, without the keys appearing after the encode?

 Answers

5

Use array_values() for your issue:

$arr['dates'] = array_values($arr['dates']);
//..
$arr = json_encode($arr);

Why? Because you're unsetting array's key without re-ordering it. So after this the only way to keep that in JSON will be encode keys too. After applying array_values(), however, you'll get ordered keys (starting from 0) which can be encoded properly without including keys.

Wednesday, November 30, 2022
3

I notice that you've used an absolute URL rather than a relative one. If your page isn't also being served from http://www.site.com, you're running into the Same Origin Policy. The SOP is a security mechanism implemented by browsers.

You have a couple of options for working around this. If you're in control of the server and you don't need to support IE6 or IE7, you can implement Cross-Origin Resource Sharing. In most modern browsers, if the server is CORS-enabled, your ajax calls will just start working (the browser handles it under-the-covers). IE6 and IE7 don't have any support for CORS, though, and IE8's requires that the client-side code do something special.

Another option is JSONP, which makes use of the fact that although you can't do a cross-origin ajax call (unless you have CORS), it's perfectly okay for a page to load a script from a remote host. So you load the script, which includes the data and which calls you back to let you know it's there. The advantage of JSONP is that it works with all major browsers, right now. And jQuery has JSONP support built into its ajax call.

Sunday, November 27, 2022
 
1

Set the proper header in the php

header('Content-Type: application/json');
$data = json_encode(mysql_fetch_assoc($query));

jquery will take care of the parsing

to add the data to your page use append

      $('body').append('<form><p>'+data.Question+'<input value="'+data.QP_Name+'"></from>');

var data = {
  "Q_Id": "1",
  "QP_Name": "test1",
  "Question": "Which is indian capital.?"
};
$('#ques').val(data.Question);
$('#QPt').val(data.QP_Name);
$('body').append('<form><p>' + data.Question + '<input value="' + data.QP_Name + '"></from>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="btn btn-primary col-xs-12 col-lg-2" style="float: right; margin:10px;" id="savenext" name="savenext" onclick="return dataPass()">Save & Next</a></span>
<input type="text" style="width: 410px;text-align: center; margin: 0px;" class="onlyNumber form-control pull-left" id="ques" value="1" name="ques" />
<input type="text" style="width: 410px;text-align: center; margin: 0px;" class="onlyNumber form-control pull-left" id="QPt" value="1" name="QPt" />
Tuesday, September 6, 2022
 
1

You need set display.multi_sparse to False:

#if need temporary use option
with pd.option_context('display.multi_sparse', False):
    print (df)

                  0
Jim Betty          
20  N/A    0.201643
50  N/A    0.201643
20  10     0.201643
20  30     0.201643

If this display option is required throughout a notebook, the option can be set once and for all as follows:

# if permanent use
import pandas as pd
pd.options.display.multi_sparse = False

Documentation:

display.multi_sparse
True
“Sparsify” MultiIndex display (don’t display repeated elements in outer levels within groups)

Monday, September 5, 2022
 
brunis
 
5

It looks like LocalFoo is treated somehow like a non-static class. That's why it claims no instance of Test is in scope.

From the tutorial:

Local classes are non-static because they have access to instance members of the enclosing block. Consequently, they cannot contain most kinds of static declarations.

https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html

The method foo() or the class LocalFoo must be static for this to work. But a class inside a method can't be declared as static. So you'd have to move it out of the method if foo() should remain nonstatic (as an inner, static class). Another option is to just use this:
ls.stream().map(s -> new LocalFoo(s));

There should be a way to just say Test.this.LocalFoo, but that doesn't work. And if it did the compiler should also just accept LocalFoo::new.

There is a bug report now: https://bugs.openjdk.java.net/browse/JDK-8144673
(See comment by Brian Goetz)

Thursday, August 18, 2022
 
joschua
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :