data comparision fails even though the value is same - cakephp

I am working on a code for a quiz test, here
function run($id){
//Is this the first question ?
if($this->data){
$question_no = $this->Session->read('Test.qno'); //0
$last_answer = $this->Session->read('Test.last_answer');
$question_no = $question_no + 1; //1
$this->Session->write('Test.qno',$question_no); //Test.qno = 1
$this->Session->setFlash('last_answer'.$this->data['Test']['answer']);
$this->redirect($this->referer());
if($this->data['Test']['answer']==$last_answer){
$score = $this->Session->read('Test.score');
$score = $score + 1 ;
$this->Session->write('Test.score',$score);
$this->Session->setFlash('Correct answer');
}
}
$question_no = $this->Session->read('Test.qno'); //question_no =
if(!$question_no){
$question_no = 0;
$this->Session->write('Test.qno',$question_no);
$this->Session->write('Test.score',0);
}
$question = $this->Test->Question->find('first',array('conditions'=>array('Question.test_id ='=>$id),'offset'=>$question_no));
$answer = $question['Question']['answer'];
$this->Session->write('Test.last_answer',$answer);
if(empty($question)){
$score = $this->Session->read('Test.score');
$this->Session->setFlash('Your Score is '.$score);
$this->Session->write('Test.qno',0);
$this->redirect(array('controller'=>'States','action'=>'index'));
}
else{
$this->set(compact('question'));
}
}
here data comparison fails even though they both hold the same value can some one tell me why,here $last_answer is retrieved from session and holds a number.
$this->data['Test']['answer'] is taken from a form with radio button's

Add
debug( '"'.$this->data['Test']['answer'].'"' );
debug( '"'.$last_answer.'"' );
to confirm they really are the same.
Also make sure it's not a problem with sessions (i.e. the condition is true, but the code inside doesn't do anything): add debug( "true" ); inside the if block and confirm that you see the message.
UPDATE after seeing the whole code:
You have a $this->redirect() right before the if block! Of course the code you initially posted doesn't do anything because you redirect the user to another page before the execution even reaches it.

Related

When i running the multiple execute statement service i got the error "Cannot read property "parameters" from null "

I want to insert the below default values when i am running the service i got this below error any one please tell me how to resolve.
Runtime error in script ("Process: 'CustomPersonalGS Practice' ProcessItem: 'Initialize' Type: 'ITEM'" -1:-1).TypeError: Cannot read property "parameters" from null
//Initialise SQL Query List
tw.local.sqlQueries = new tw.object.listOf.SQLStatement();
tw.local.sql = "";
tw.local.customerPD = new tw.object.customerPD1BO();
tw.local.customerPD.customerPersonalDetailsList = new tw.object.listOf.customerSpecificPersonalDetailsListBO();
var custPersonalDetails = new tw.object.customerSpecificPersonalDetailsListBO();
custPersonalDetails.customerId = "8467";
custPersonalDetails.custPersonalDetailsId = "8";
custPersonalDetails.isBPMEnabled = true;
custPersonalDetails.isCCPEnabled = true;
custPersonalDetails.isCCPMandatory = true;
custPersonalDetails.isLatestVersion = true
tw.local.customerPD.customerPersonalDetailsList.insertIntoList(tw.local.customerPD.customerPersonalDetailsList.listLength, custPersonalDetails);
tw.local.sql = "INSERT INTO CUSTOMPERSONALDETAILSQUESTION(CUSTOMERID,CUSTPERSONLADETAILSID,ISBPMENABLED,ISCCPENABLED,ISCCPMANDATORY,ISLATESTVERSION) VALUES (?,?,?,?,?,?) ";
function addSQLStatement() {
tw.local.sqlQueries[tw.local.sqlQueries.listLength] = new tw.object.SQLStatement();
}
function addParam(value,type,mode) {
log.info("VALUE :" + value);
var newParam = new tw.object.SQLParameter();
newParam.value = value;
newParam.type = type;
newParam.mode = mode;
if( tw.local.sqlQueries == null){
tw.local.sqlQueries = new tw.object.listOf.SQLStatement();
}
if( tw.local.sqlQueries[tw.local.sqlQueries.listLength] == null ){
tw.local.sqlQueries.insertIntoList(tw.local.sqlQueries.listLength, new tw.object.SQLStatement());
}
if(tw.local.sqlQueries[tw.local.sqlQueries.listLength].parameters == null ){
tw.local.sqlQueries[tw.local.sqlQueries.listLength].parameters = new tw.object.listOf.SQLParameter();
}
var paramsLength = tw.local.sqlQueries[tw.local.sqlQueries.listLength].parameters.listLength;
tw.local.sqlQueries[tw.local.sqlQueries.listLength].parameters[paramsLength] = newParam;
}
for(var i=0;i<tw.local.customerPD.customerPersonalDetailsList.listLength;i++){
addSQLStatement(tw.local.sql);
addParam(tw.local.customerPD.customerPersonalDetailsList[i].customerId,"VARCHAR","IN");
addParam(tw.local.customerPD.customerPersonalDetailsList[i].custPersonalDetailsId,"VARCHAR","IN");
var yesNoFlag = "N";
if(tw.local.customerPD.customerPersonalDetailsList[i].isBPMEnabled){
yesNoFlag="Y";
addParam(yesNoFlag,"CHAR","IN");
}
yesNoFlag = "N";
if(tw.local.customerPD.customerPersonalDetailsList[i].isCCPEnabled){
yesNoFlag="Y";
addParam(yesNoFlag,"CHAR","IN");
}
yesNoFlag = "N";
if(tw.local.customerPD.customerPersonalDetailsList[i].isCCPMandatory){
yesNoFlag="Y";
addParam(yesNoFlag,"CHAR","IN");
}
yesNoFlag = "N";
if(tw.local.customerPD.customerPersonalDetailsList[i].isLatestVersion){
yesNoFlag="Y";
addParam(yesNoFlag,"CHAR","IN");
}
}
You didn't initialize the parameter list in your SQL as far as I can tell. That is on line 38 you call -
var paramsLength = tw.local.sqlQueries[tw.local.sqlQueries.listLength].parameters.listLength;
However when you create the entry in the tw.local.sqlQueries, you didn't initialize the parameter array. I'll also note that your addSQLStatement() function ignore the sql input (and that value is hard coded, so really you don't need to pass it in). I think if you change addSQLStatement to be something like -
function addSQLStatement(query) {
var targetQuery = new tw.object.SQLStatement();
targetQuery.sql = query;
tagetQuery.params = new tw.object.listOf.SQLParameter();
tw.local.sqlQueries[tw.local.sqlQueries.listLength] = targetQuery;
}
then your code will work. Additionally you could actually return targetQuery from this function and then pass it to the "addParams" method eliminating the need to find the last one in the array. Alternatively insert it in the beginning of the array instead and just update the 0th item instead of the last.
-AP
This comparison will never work properly. array[array.length] will always be null (line 35).
if (tw.local.sqlQueries[tw.local.sqlQueries.listLength].parameters == null ){
In addition, in the next lines, if you want to work with the last element of the list, you might want to use something like array[array.length - 1]. Personally, I'd use some temporary variable, do some stuff with it and insert it into the list at the end (similar to #Drux's answer).

Cloning a record in controller CakePHP

I have a case where I have to clone a record in controller, modify the cloned and original records slightly and then save them both. I have tried implemeting in many different ways but always end up with one recod not being updated or being update wrong. Here is the code that gets me closest:
public function postpone( $id = null ){
$this->request->allowMethod( ['post'] );
$originalTask = $this->Tasks->get( $id );
//information that has to be updated
$meetingId = 200;
//set original to removed and update it
$originalTask->removed = 1;
if( $this->Tasks->save( $originalTask ) ){
//Logic for storing a clone task
$cloneTask = $originalTask;
$cloneTask->id = NULL;
$cloneTask->removed = 0;
$cloneTask->meeting_id = $meetingId;
$this->Tasks->save( $cloneTask );
}
}
What happens in this case is that when storing $cloneTask simply $originalTask is being updated and that is it, where instead a need to get a new record.
I have no custom before or after save logic that could influence it.
You should create a new entity before you can save a "cloneTask".
if( $this->Tasks->save( $originalTask ) ){
//Logic for storing a clone task
$cloneTask = $this->Tasks->newEntity();
$cloneTask->removed = 0;
$cloneTask->meeting_id = $meetingId;
$this->Tasks->save( $cloneTask );
}

ReferenceError: Error #1069 Actionscript 3

So i have been stuck on this for about 2 weeks and i have no idea how to progress.
I have an array of movie clips called "_main.speederArray" and i'm trying to make it so that if they collide with each other then they are both destroyed. Here is my code in the "Speeder class" to detect collision.
private function detectionHandler():void{
//trace("array length", _main.speederArray.length);
detectionID = _main.gameCounter;
for ( var i:int = _main.speederArray.length -1; i >= 0; i--){
var speeder:Speeder = _main.speederArray[i];
if(speeder.destroyMe) continue;
if(speeder.detectionID == this.detectionID) continue;
if (boxIntersect(this, speeder)){
trace("collision");
destroyMe = true;
speeder.destroyMe = true;
}
}
}
Here is the boxIntersect function this code refers to. It's in the same class
private function boxIntersect ( speeder1:Speeder, speeder2:Speeder):Boolean{
if(speeder1.x + speeder1.distRight < speeder2.x + speeder2.distLeft) return false; //checking for overlap on X axis
if(speeder1.x + speeder1.distLeft > speeder2.x + speeder2.distRight) return false;
if(speeder1.y + speeder1.distBot < speeder2.y + speeder2.distTop) return false; // checking for overlap on Y axis
if(speeder1.y + speeder1.distTop > speeder2.y + speeder2.distBot) return false;
return true;
}
And then here is where i think the problem is. I have a class called "spawner" and this is where i was going to handle the objects being created and destroyed. Here is the code where i am trying to splice objects from the array depending on whether the destroyMe bool is set to true. At this stage i have confused the shit out of myself so any help would be greatly appreciated!
private function updateArray(e:Event):void{
for(var i:int = _main.speederArray.length - 1; i>=0; i--){
var speeder:Speeder = _main.speederArray[i];
if(speeder.destroyMe){
//trace("hello");
removeChild(speeder[i]); // take it off the stage
_main.speederArray[i] = null;
_main.speederArray.splice(i, 1); //remove it from the array
}
}
}
Now, the game runs however as soon as the 2 objects within the same array collide, i get the collision trace in the output window but straight after i get this :
ReferenceError: Error #1069: Property 1 not found on com.game.Speeder and there is no default value.
at com.game::Spawner/updateArray()
No idea what it means :(
Any help appreciated thanks guys!
The problem comes from the line
removeChild(speeder[i]); inside your update function.
Speeder has no properties that are called 1 and the 1 comes obviously from your for loop.
So, to solve this problem, you should simply call
removeChild(speeder);
speeder already is the object at the position i of your array. Putting [] behind an object is the same like accessing a property from it. essentially you were doing this:
removeChild(speeder.1);

Angularfire $save array

I'm trying to save a collection of data after i have updated an entry in the array.
// Edit a post
$scope.editMe = function(message) {
if($scope.textBoxMessage = "What good did you do today?"){
$scope.textBoxMessage = "Here you can edit your post by entering a new message and pressing edit on the affected post" + "\n \n" + "Your post:" + "\n" + message.post;
}
else{
$scope.message.post="hello"; //$scope.newMessage
$scope.messages.$save(2);
}
}
Ones a user have entered some text in a textfield i want to replace that with the already stored data. This by overwriting message.data with sometext. Since i read in data like this:
var list = fbutil.syncArray('posts/'+user.uid);
i also tried to simply say:
message.post = $scope.newMessage;
list.$save()
Neither of these two methods work but i'm sure it's a minor mistake.
ED:
According to angularFire api, visit: https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-save-recordorindex
list[2].post = "hello";
list.$save(2);
should work but i have had no luck.
Okay here is how i solved it:
The problem had more to do with the if else statement then with the server code itself. The $scope.textBoxMessage is always true no matter what and this is what bothered me. Even if i say if($scope.textBoxMessage = "hi") it will be true.
I'm not sure why this is the case but feel free to comment if you have any suggestions.
This is how i solved it:
// Edit a post
$scope.editMe = function(message) {
if($scope.newMessage == null){
$scope.textBoxMessage = "Here you can edit your post by entering a new message and pressing edit on the affected post" + "\n \n" + "Your post:" + "\n" + message.post;
}
else{
message.post = $scope.newMessage;
list.$save(message);
$scope.textBoxMessage = chatmessage;
$scope.newMessage = null;
}
}

Arrays and if statements. How to check if value is in array? *javascript*

So I have a program that sends emails. The user has a list of emails that cannot be sent to. These are in arrays and I need to use a if statement to determine if what the user entered in is in the array of emails. I tried the in function which didnt work but Im probably just using it wrong. I tried for loops and if statements inside. But that didnt work either. Here is a snapshot of the code Im using to help you get the idea of what im trying to do.
function test2(){
var safe = [1]
safe[1] = "lol"
safe[2] = "yay"
var entry = "lol"
Logger.log("entry: " + entry)
for(i = 0; i < safe.length; i++){
if(entry == safe[i]){
Logger.log("positive")
}else{
Logger.log("negative")
}
}
}
Here is what I tried with the in function to show you if I did it wrong
function test(){
var safe = [1]
safe[1] = "lol"
safe[2] = "yay"
var entry = "losl"
Logger.log("entry: " + entry)
if(entry in safe){
Logger.log("came positive")
}else{
Logger.log("came negative")
}
Logger.log(safe)
}
array.indexOf(element) > -1 usually does the trick for these situations!
To expand upon this:
if (array.indexOf(emailToSendTo) < 0) {
// send
}
Alternatively, check this cool thing out:
emailsToSend = emailsToSend.filter(function(x) {
// basically, this returns "yes" if it's not found in that other array.
return arrayOfBadEmails.indexOf(x) < 0;
})
What this does is it filters the list of emailsToSend, making sure that it's not a bad email. There's probably an even more elegant solution, but this is neat.

Resources