Populate Flex ComboBox from XML from PHP - php

I'm having trouble populating a combobox from an XML response. Here is the XML that I am receiving:
<distros>
<entry>
<distro>CentOS</distro>
<distro>Debian</distro>
<distro>Other</distro>
<distro>Sabayon</distro>
<distro>Ubuntu</distro>
<distro>VMware</distro>
<distro>Windows</distro>
</entry>
</distros>
So probably the most basic form of XML ever!
and here is the flex code:
private function getDistros():void
{
httpReq = new HTTPService;
httpReq.url = 'http://myserver/xml.php';
httpReq.resultFormat = 'e4x';
httpReq.method = 'GET';
httpReq.addEventListener(ResultEvent.RESULT, popDistros);
httpReq.send( null );
}
private function popDistros(event:ResultEvent):void
{
if(event.result != "")
{
// Set the data to the XMLListCollection for lookup
myXmlCollection= new XMLListCollection(event.result.entry);
// Bind the ListCollection to the comboBox
Alert.show(myXmlCollection.toString());
distroCombo.dataProvider = myXmlCollection.toString();
}
}
and the MXML:
<mx:ControlBar x="139" y="10" width="266" height="358" verticalAlign="top" horizontalAlign="left" direction="vertical">
<mx:ComboBox id="distroCombo" labelField="distro"></mx:ComboBox>
<mx:ComboBox id="imageCombo"></mx:ComboBox>
<mx:Button label="Download"/>
</mx:ControlBar>
The XML comes back fine in the Alert but the comboBox won't populate and I have tried this so many different ways now, anyone got any suggestions? Have I just been staring at it far too long?

if the result (event.result) is XML, then It should wotk like this: (it differs with .distro in the end compared to yours)
myXmlCollection = new XMLListCollection(event.result.entry.distro);
...this should create valid data in myXmlCollection
But then also this row is wrong:
distroCombo.dataProvider = myXmlCollection.toString();
it creates just one item in dataProvider of type string, (Just BTW: if you would have used spark combobox, you would have get compile error at this row).
just use this instead:
distroCombo.dataProvider = myXmlCollection;
And also note, that you can see correct result in the Alert, but it does not say if the data are of correct type, coz Alert evertyhing converts to string :)

Related

How to create solr document with String type using solarium query

I' creating solr document via solarium plugin in php.
But the all document is stored text_general data type except id field. text_general is the default datatype in solr system.
My doubt is why id field is only to stored string type as default.
And If any possible to add document with string type using solarium plugin.
My code part is here,
public function updateQuery() {
$update = $this->client2->createUpdate();
// create a new document for the data
$doc1 = $update->createDocument();
// $doc1->id = 123;
$doc1->name = 'value123';
$doc1->price = 364;
// and a second one
$doc2 = $update->createDocument();
// $doc2->id = 124;
$doc2->name = 'value124';
$doc2->price = 340;
// add the documents and a commit command to the update query
$update->addDocuments(array($doc1, $doc2));
$update->addCommit();
// this executes the query and returns the result
$result = $this->client2->update($update);
echo '<b>Update query executed</b><br/>';
echo 'Query status: ' . $result->getStatus(). '<br/>';
echo 'Query time: ' . $result->getQueryTime();
}
The result document for the above code is here,
{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"q":"*:*",
"_":"1562736411330"}},
"response":{"numFound":2,"start":0,"docs":[
{
"name":["value123"],
"price":[364],
"id":"873dfec0-4f9b-4d16-9579-a4d5be8fee85",
"_version_":1638647891775979520},
{
"name":["value124"],
"price":[340],
"id":"7228e92d-5ee6-4a09-bf12-78e24bdfa52a",
"_version_":1638647892102086656}]
}}
This depends on the field type defined in the schema for your Solr installation. It does not have anything to do with how you're sending data through Solarium.
In the schemaless mode, the id field is always set as a string, since a unique field can't be tokenized (well, it can, but it'll give weird, non-obvious errors).
In your case i'd suggest defining the price field as an integer/long field (if it's integers all the way) and the name field as a string field. Be aware that string fields only generate hits on exact matches, so in your case you'd have to search for value124 with exact casing to get a hit.
You can also adjust the multiValued property of the field when you define the fields explicitly. That way you get only the string back in the JSON structure instead of an array containing the string.

Read POST data in AJAX call

I have some Session values that I am constantly changing via Ajax calls. I can't seem to get a handle on the POST data to process it and set the values.
What I am passing to it here is an array of strings like is shown in my code below.
Here is where AJAX calls:
var sessionValues = [];
str = {"PID": "1", "Level": "Main", "MenuName": "Kitchen", "State": "CHECKED"}
sessionValues.push(str);
var postObj = {"sessionData": sessionValues};
$.ajax({
type: 'POST',
data: {'data': postObj},
url: 'setSession.asp'
}).done(function(response){
console.log(response);
})
I have this working fine in a PHP version of the program but my ASP version is not grabbing the data. Here is my PHP ver and the ASP ver as best as I could convert it.
<-- php setSession.php works fine -->
$data = $_POST['data'];
foreach ($data['sessionData'] as $key => $value) {
$projectProduct = "1";
$level = $value["Level"];
$menuName = $value["MenuName"];
$state = $value["State"];
$_SESSION['PID:'.$projectProduct][$level][$menuName]['menu_state'] = $state;
echo "[PID:".$projectProduct."][".$level."][".$menuName."][".$state."]<br>";
}
0 =>>>>> Array<br>[PID:1][Main][Kitchen][CHECKED]
Here I want to do the same thing in ASP
' setSession.asp
data = Request.Form("data")
For Each part In data("sessionData")
projectProduct = part("PID")
level = part("Level")
menuName = part("MenuName")
state = part("State")
Session("PID:" & projectProduct).Item(level).Item(menuName).Remove("menu_state")
Session("PID:" & projectProduct).Item(level).Item(menuName).Add "menu_state", state
response.write("[PID:" & projectProduct&"]["&level&"]["&menuName&"]["&state&"]<br>")
Next
outputs blank
It looks like it never has any data but doesn't throw any errors. Am I reading the POST object correctly?
[edit]
Here is the RAW POST data captured from Fiddler:
data%5BsessionData%5D%5B0%5D%5BPID%5D=1&data%5BsessionData%5D%5B0%5D%5BLevel%5D=Main&data%5BsessionData%5D%5B0%5D%5BMenuName%5D=Kitchen&data%5BsessionData%5D%5B0%5D%5BState%5D=CHECKED
here I used a URL Decode on that string-
data[sessionData][0][PID]=1&data[sessionData][0][Level]=Main Level Plan&data[sessionData][0][MenuName]=Kitchen&data[sessionData][0][State]=CHECKED
This looks like I should be able to loop through the strings now by using
For Each part In Request.Form("data[sessionData]")
but nothing happens. I added a simple loop to look at the request.form and here is what it is seeing:
for each x in Request.Form
Response.Write(x)
Next
' outputs -> data[sessionData][0][PID]data[sessionData][0][Level]data[sessionData][0][MenuName]data[sessionData][0][State]
I guess what this comes down to is just reading through and processing that string correctly, or multiple if more than one is sent. Correct?
The RAW output definitely helps work out what is going on.
What is happening is jQuery is translating the JSON structure into HTTP POST parameters but during the process, it creates some overly complex key names.
If you break down the key value pairs you have something like
data[sessionData][0][PID]=1
data[sessionData][0][Level]=Main Level Plan
data[sessionData][0][MenuName]=Kitchen
data[sessionData][0][State]=CHECKED
As far as Classic ASP is concerned the this is just a collection of string key and value pairs and nothing more.
The correct approach to work out what these keys are is to do what you have done in the question, but with some minor alternations.
For Each x In Request.Form
Response.Write(x) & "=" & Request.Form(x) & "<br />"
Next
Which when outputted as HTML will look similar to the break down shown above.
Armed with the knowledge of what the keys are you should be able to reference them directly from the Request.Form() collection.
Dim pid: pid = Request.Form("data[sessionData][0][PID]")
Response.Write pid
Output:
1

Fullcalendar.js allDay parameter returned from MySql using Codeigniter

I'm trying to get FullCalendar working on my Codeigniter app, but coming across a problem with the events showing as all day from my JSON feed.
I've found that setting "allDay":false fixes the problem (tested with a static json file) but when i store that value in a mysql DB, then return the data and run the JSON encode, it converts the False into a string, which cause the event to show as all day!
Does anybody know how i can store true or false in my DB (i'm currently storing as text) and return it to my json feed without it being a string? I've tried casting and converting but can't get anything to work!
Examples of my model/controller/json feed below;
Thanks in advance;
Model function
function show_installs()
{
return $this->db->query(
"SELECT id, client_name as title, start, end, concat('/planner/view_install/',id) as url
FROM installs WHERE completed != 2"
)->result();
}
Controller function
function json_installs()
{
$this->load->model('installs_model');
$data = array();
if($query = $this->installs_model->show_installs())
{
$data = $query;
}
$json = json_encode($data);
echo $json;
}
JSON Feed (That doesnt work because of the " around false)
[
{
"id":"18",
"title":"John",
"allDay":"false",
"start":1339585200,
"end":1339592400,
"url":"\/planner\/view_install\/18"
},
{
"id":"19",
"title":"Mike",
"allDay":"false",
"start":"1339585200",
"end":"1339592400",
"url":"\/planner\/view_install\/19"
}
]
Any help would be really appreciated - i just need to get those bloomin quote marks off from false/true and it would work perfectly!
Thanks
Why don't you change the column type in your DB to be boolean instead of text? You could also parse your $json object to convert all booleans_as_text to booleans before you set it in the calendar - quick googling found me this:
for(var i=0; i<$json.length; i++)
$json[i]=/^true$/i.test($json[i]);
You could also set the allDayDefault to false to avoid set allday: false in each event.

Code for communcation between PHP and AS2?

I have a Flash developer I'm working with. This person is building a tool in AS2 that will provide an interface that will send voting data (firstname, lastname, email address, votes (there are 100 items in categories and users will be able to choose some subset to declare "best").
All fair enough, Flash dev will POST data to a PHP app I will develop, and I will store the data in MySQL. This Flash dev has not done a great deal of work with databases or web apps.
I want to return data back to the Flash application. I want to be able to return "email address invalid" or "problem connecting to database" or "vote information accepted" messages. My instinct is to want to send back JSON or XML data. But I'm wondering if there are tools in AS2 to easily consume such responses.
I would like to see some "Hello World" type examples of AS2 code that consumes JSON or XML data so I can get the Flash app and the PHP app interacting well. My understanding is AMF is not on the table because it's AS2, but I'm open to what will work on both ends given the constraint of it being AS2.
Below should give you an example.
XML:
<alldots>
<dotname id="bigDot" color="0xff0000" url="http://www.fletchermartin.com/" photos="8" />
<dotname id="otherDot" color="0x000066" url="http://www.ajc.com/" photos="8" />
<dotname id="thirdDot" color="0xCC0099" url="http://www.tiffanybbrown.com/" photos="0" />
</alldots>
AS2 Code
var dots:XML = new XML();
dots.load('bigdot.xml');
dots.onLoad = function(success:Boolean){
if(success){
if(dots.status == 0){
var dotsToXMLString:String = new String(); // initializes a new string variable
dotsToXMLString = dots.toString(); // converts dots XML object to a string and stores it in dotsToXMLString.
var dotsXML:XML = new XML(dotsToXMLString);// creates new XML object with the string contents from above.
dotsXML.parseXML(dotsToXMLString); // parses the string from above.
var dotsNodes:Object = dotsXML.firstChild; // Saves the firstChild (in this case, the outermost element) as an object
var dotsNodesChildren:Object = dotsNodes.childNodes; // Saves the childNodes of firstChild as an object
for(i=0;i<dotsNodesChildren.length;i++){
var newObj:Object = dotsNodes.childNodes[i].attributes.id; // creates a new object out of the child node's id.
var newObjColor:Color = new Color(newObj); // creates a new color object with newObj as its target
var theColor:Number = dotsNodes.childNodes[i].attributes.color; //retrieves the hex code value (number) of the attribute color
newObjColor.setRGB(theColor); // sets the RGB value of newObjColor.
}
} else {
trace("Problem parsing XML.");
}
} else{
trace("Could not load XML");
}
}

How do I POST XML data to a URL via JavaScript in an Adobe AIR app?

I'm writing an application that downloads an XML string from a URL and POSTs that to another URL (that's set up to handle an incoming "XML" field). I've got the first part right - it downloads the XML and I can alert() it and all that, but I'm unable to figure out how to POST that data to a server.
function pull() {
var myLoader = new air.URLLoader();
var myRequest = new air.URLRequest('http://something/something.xml');
myLoader.addEventListener(air.Event.COMPLETE, pulled);
myLoader.load(myRequest);
}
function pulled(evt) {
if (evt.target.bytesTotal>0) {
// alerting shows the full string just fine
alert(evt.target.data);
var myLoader = new air.URLLoader();
var myRequest = new air.URLRequest('http://someplace/push.php');
myRequest.method = air.URLRequestMethod.POST;
// myVars = new air.URLVariables("xml="+evt.target.data); //
// alert(evt.target.data.toUpperCase());
myRequest.data = "xml="+evt.target.data; // myVars;
myLoader.dataFormat = air.URLLoaderDataFormat.TEXT;
myLoader.addEventListener(air.Event.COMPLETE, pushed);
myLoader.load(myRequest);
}
}
I made the 2nd server PHP echo the contents of the xml variable, but I'm just unable to get the exact contents of the XML string. There is something I'm doing wring with the myRequest.data and/or dataFormat bit.
Can someone just figure this out? I know it's probably a simple thing, but I'm at my wit's end right now.
This is my first AIR app.
Another related question (or sub-question) is that...
alert(evt.target.data); // shows an alert box with the XML
alert(typeof evt.target.data); // shows String
alert(evt.target.data.toUpperCase()); // shows the xml converted to upper case
alert(encodeURI(evt.target.data)); // shows up blank.
alert(escape(evt.target.data)); // shows up blank.
Why??
The error seems to be the way you are assigning the parameters to 'data' ... Use URLVariables.
var params:URLVariables = new URLVariables();
params.[name of parameter] = [value];
--- so like params.xml = (YOUR XML) ... from your example:
// uses the dynamic object to add the 'xml' property to 'params' at runtime.
params.xml = evt.target.data
Then Change you request.data to request.data = params;
-- The URLVariables guy is dynamic - so you can add properties as I describe above.
For a basic example - much more complete that what I have here: http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_2.html

Categories