Yii Framework Forum: Send two dimensional array to javascript - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Send two dimensional array to javascript Rate Topic: -----

#1 User is offline   tHr 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 22-May 12

Posted 06 August 2012 - 07:40 PM

Hi
in controller i want query database and send 2 dimensional array data to view and traverse it every 2 second with javascript! when i send data like below command
$this->render("index", array('2dimentionalArray'=>$data));

I can't traverse it every 2 seconds!
How can i traverse this array with javascript every 2seconds and show it's data?
Is json solution of my problem?If yes please show me an example.
thanks
0

#2 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 547
  • Joined: 27-December 10
  • Location:china

Posted 06 August 2012 - 10:31 PM

//  in  your view file :
<script type="javascript/text">
var myJsArray = <?php echo CJSON::encode($2dimentionalArray); ?>; // but why it's starts witch "2"

//here use myJsArray as  a normal js  variable . js timer is ok 


</script>

 tHr, on 06 August 2012 - 07:40 PM, said:

Hi
in controller i want query database and send 2 dimensional array data to view and traverse it every 2 second with javascript! when i send data like below command
$this->render("index", array('2dimentionalArray'=>$data));

I can't traverse it every 2 seconds!
How can i traverse this array with javascript every 2seconds and show it's data?
Is json solution of my problem?If yes please show me an example.
thanks

1

#3 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 547
  • Joined: 27-December 10
  • Location:china

Posted 06 August 2012 - 10:39 PM

and additional i think you want use ajax to query the 2dimentionalArray every 2 seconds ?
   //js side 
   function repeatRequest(){
    var   url = "<?php  echo  $this->createUrl('someActionId'); ?>";
    $.post(url,function(response){
        // handle the response here !
        here use js timer to repeat this function again !(2 seconds latter )
    });

  }
  repeatRequest(); // start it 

  // server side :
  public function someAction(){
    // query db
    echo CJavaScript::encode($resultSetFromDb);
    die();

  }
  // you may need to put this action to accessRules or check if it is ajax request

1

#4 User is offline   tHr 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 22-May 12

Posted 07 August 2012 - 10:11 AM

 yiqing95, on 06 August 2012 - 10:39 PM, said:

and additional i think you want use ajax to query the 2dimentionalArray every 2 seconds ?
   //js side 
   function repeatRequest(){
    var   url = "<?php  echo  $this->createUrl('someActionId'); ?>";
    $.post(url,function(response){
        // handle the response here !
        here use js timer to repeat this function again !(2 seconds latter )
    });

  }
  repeatRequest(); // start it 

  // server side :
  public function someAction(){
    // query db
    echo CJavaScript::encode($resultSetFromDb);
    die();

  }
  // you may need to put this action to accessRules or check if it is ajax request


Thanks for your reply and help
I query the database in my php code and save it in 2dimensional array and send this array to view file.In view file i get data from this 2dimesional array with setTimeout function of java script to get vehicle location every 2 seconds and create route of vehicle.
Is this approach true?I don't want to query database every 2 seconds and send request to server!.Is this approach true for speed up application?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users