0

This is a part of my controller,i need to pass the data in the variable $coinPacks into my view page


else {
            $coinPacks=$packs->getPacks();
            // print_r($coinPacks);
            echo $this->_helper->json(array('error'=>array('error_code'=>300,'error_message'=>'No enough coins to do correction.','pack'=>$coinPacks)));
        }

here is the view page part


alert(JSON.stringify(response.error));
         // foreach($pack as $newpacks)
         // {

            answerContent = answerContent + '<p class="buy-coins" value="1" coins="100">200 &nbsp; &nbsp; &nbsp; &nbsp; $1</p><p>';
            answerContent = answerContent + '<p class="buy-coins" value="3" coins="500">500 &nbsp; &nbsp; &nbsp; &nbsp; $3</p><p>';
            answerContent = answerContent + '<p class="buy-coins" value="5" coins="1000">1000 &nbsp; &nbsp; &nbsp; &nbsp; $5</p><p>';
            answerContent = answerContent + '<p class="buy-coins" value="15" coins="5000">5000 &nbsp; &nbsp; &nbsp; &nbsp; $15</p><p>';
          // } 

i need to get the values that passes from the view and use the value to make loop and display the corresponding value from the database.i really don't have much knowledge in json.please give me some advice.


am getting an alert like this


{"error":{"error_code":300,"error_message":"No enough coins to do correction.","pack":{}}}
4
  • first, check the value of $coinPacks Zend_Debug::dump($coinPacks); it seems its not valid value for json response. Commented Mar 26, 2014 at 7:10
  • @konradwww when using that it showing response like this <pre>object(Zend_Db_Table_Rowset)#147 (10) { [&quot;_data&quot;:protected] =&gt; array(4) { [0] =&gt; array(3) { [&quot;id&quot;] =&gt; string(1) &quot;1&quot; [&quot;number_of_coins&quot;] =&gt; string(3) &quot;100&quot; [&quot;price&quot;] =&gt; string(4) &quot;1.00&quot; Commented Mar 27, 2014 at 4:57
  • try first convert it to array with $coinPacks->toArray() Commented Mar 27, 2014 at 8:24
  • great! i've paste it as an answer below. Commented Mar 27, 2014 at 10:02

2 Answers 2

1

try first convert it to array with $coinPacks->toArray()

Sign up to request clarification or add additional context in comments.

Comments

0

converted it into an array


   $coinPacks=$packs->getPacks();
                $coinpacks = array();
                foreach($coinPacks as $coin){
                    $coinpacks['id'] = $coin->id;
                    $coinpacks['number_of_coins'] = $coin->number_of_coins;
                    $coinpacks['price'] = $coin->price;
                    $coins[] = $coinpacks;
                }

and call it in the view page

  var coin = response.pack;                                                                                                                 

  for(var i=0;i<coin.length;i++)                                                                                               
  {                                   
      answerContent = answerContent + '<p class="buy-coins" value="'+coin[i].price+'" coins='+coin[i].number_of_coins+'>'+coin[i].number_of_coins+' &nbsp; &nbsp; &nbsp; &nbsp; $'+coin[i].price+'</p><p>';                  
  }  

Now its working good

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.