R
R
RomanovAS2013-04-05 09:20:35
PHP
RomanovAS, 2013-04-05 09:20:35

Processing and inserting json data?

Hello dear experts!

There is a problem that cannot be solved. I've already broken my head.

1. There are several containers of the form:

<div id='12'></div><div id='13'></div><div id='14'></div><div id='17'></div>


2. The json string that I receive from the server:

[{"week_num_return":"14","plan_amount_return":"15","fact_amount_return":"18"},{"week_num_return":"17","plan_amount_return":"13","fact_amount_return":"0"}]


It is necessary to process the received json and insert the data into the div so that the week_num_return matches the cell id.

Tried like this:

$('.fc-sum-pfp-number').each(function(i, _cell) {
     $(json).each(function(i2) {
          $(_cell).append(json[i2].plan_amount_return);
      });
});


But this, of course, is not true. All data is inserted into each container.

The solution is on the surface, but I have little experience with JavaScript and jQuery.

Thank you in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Pak, 2013-04-05
@RomanovAS

<script>
  <script>
  $(document).ready(function() {
    $('.article').click(function(){
      var articleView = $(this).parent('.Node').find('.article');
      var viewArticle = $(this).parent('.Node').find('.articleBlock');
      var url = $(this).attr('data');
      $.getJSON(url, function(data){
        $.each(data, function(i, field){
          var productCount = viewArticle.append('<div class="articleBlocPrice">'+field.price+' руб.</div><div class="articleBlockDote"></div><div class="articleBlockName">'+field.name+'</div><div style="clear:both"></div>');
            if ( articleView.attr("data") == productCount ) {
              articleView.removeAttr("data")
              } else {
              articleView.attr("data", productCount);
            }
        });
      });
    });
  });
  </script>

S
Sergey, 2013-04-05
@serega_kaktus

for (var i = 0; i < json.length; i++) {
    var data = "some data" //может быть текст или html
    $('#' + json[i].week_num_return).append(data);

}

There are very good docks in Russian jQuery

M
Maxim Pak, 2013-04-05
@PML

Maybe help?
A small excerpt from my work.

B
Beliyadm, 2013-04-05
@beliyadm

I'll try to insert a related question.
Is it possible to operate with a string with JSON data using MySQL tools?
That is, having the string
{"week_num_return":"17","plan_amount_return":"13","fact_amount_return":"0"}]
only in the query using the muscle to change the value of "week_num_return"?
Writing a regular expression for each request is not an option.
Prepare a line at the input using php - now I do it, but it's inconvenient

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question