Count lines with PHP and JS (Different)

Asked

Viewed 161 times

1

I need a function to help me count certain Ivs in my php. I will explain better, I have a product checker, it works as follows, I insert the product codes in the textarea, put to search and it does a check in the database. The products that already have, it returns as failed, those that does not have it returns as approved.

    <script type="text/javascript">
        function buscarProdutosRepro(str) {
            document.getElementById(\'listProdutosRepro\').innerHTML += \'<div>\' + str + \'</div>\';
        }

        function buscarProdutosApro(str) {
            document.getElementById(\'listProdutosApro\').innerHTML += \'<div>\' + str + \'</div>\';
        }

        function FormatoInvalido(str) {
            document.getElementById(\'FormatoInvalido\').innerHTML += \'<div>\' + str + \'</div>\';
        }
    </script>

This function, it is applied here

<center>
    <p style="background: #54bd0e; padding: 6px 20px; width: 180px; font-size: 13px; border-radius: 15px; color: #F0F0F0;">Produtos Carregados ('.$conta.') </p>
</center>
<div class="panel panel-green">
    </span>
    <div class="alert alert-success"><small>✔ Produtos Aprovados</small></div>
    <i class="fa fa-spinner fa-spin" style="font-size:24px"></i> Verificando...
    <div class="panel-body">
        <br/><div id="listProdutosApro"></div>
    </div>
</div>                              
<div class="panel panel-danger">
    <div class="panel-heading"><small>✘ Produtos Reprovados</small></div>
    <div class="panel-body">
        <br/><div id="listProdutosRepro"></div>
    </div>
</div>
<div class="panel panel-orange">
    <div class="panel-heading">Invalidas</div>
    <div class="panel-body">
        <br/><div id="FormatoInvalido"></div>
    </div>
</div>

It will print on the screen the results, div under div.

<br/><div id="listProdutosApro">
<br/><div id="listProdutosApro">
<br/><div id="listProdutosApro">

Upshot: http://image.prntscr.com/image/ebb07cbb7bf547d98406cad16ef39e2f.png

and I want to count these Ivs, as if they were lines, so:

Approved Products (80) <- this sum I need.

Disapproved Products (15) <- this sum I need.

If anyone can help me, since thank you.

  • is more or less that http://jsfiddle.net/t6Z5s/ only that this function counts the line break of a textarea

1 answer

0

From what I understand, in Javascript would be like this:

$(function() {
  var quantidade1 = document.getElementById('reprovados').children.length;
  
  var quantidade2 = document.getElementById('aprovados').children.length;
  
  $('.resultado').text(
      quantidade1/2 + " foram reprovados e " +
      quantidade2/2 + " foram aprovados"
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="panel-body" id="reprovados">
  <br/><div id="listProdutosRepro"></div>
  <br/><div id="listProdutosRepro"></div>
  <br/><div id="listProdutosRepro"></div>
</div>

<div class="panel-body" id="aprovados">
   <br/><div id="listProdutosAprov"></div>
   <br/><div id="listProdutosAprov"></div>
   <br/><div id="listProdutosAprov"></div>
   <br/><div id="listProdutosAprov"></div>
   <br/><div id="listProdutosAprov"></div>
</div>

<div class="resultado">
</div>

Where I have a list of approved and disapproved, I have to count how many of each I have, so I take the element with the id 'disapproved', which would be the list of results, and count how many children that element has, in case I divide by two because each element has a div and a br. At the end I put the text on the screen.

  • give a look http://image.prntscr.com/image/350f0f519acb4836a402666bb2bab7c7.png - http://image.prntscr.com/ee4f3e5688324b3c831d9f3e3cb90d61.png

  • The first code is javascript with Jquery and not PHP

  • i put it this way http://prntscr.com/cwy4nt and I get this error http://prntscr.com/cwy4s9

  • so to attach, my file is a php that echo to print html, types like http://prntscr.com/cwyj47

  • You’re mixing PHP with Javascript/HTML, it’s trying to read this code as if it were PHP. You probably didn’t close the php tag with ?>

  • I closed friend, the page is working normally if I take this code you sent. the error happens on the line I put it. But it doesn’t do me much good to take out the code, the page back to work and I haven’t solved the initial problem :

  • Edit the question by placing all the code together with the code I put for me to take a look, without print, the same code of the whole page.

  • I thought about doing it before, but the store owner wouldn’t let me. I noticed that when I change single quotes from your code to double quotes, it skips the error to another line that has single quotes. I’ve had this problem before, I remember that to solve it was only a matter of interpretation. But I’ve tried everything here.

  • I had to delete some things to be allowed to post. That’s how it works, buddy? http://pastebin.com/psgWV61f

  • I had to delete some things to be allowed to post. So it’s okay, buddy? Pastebin.com/psgWV61f

  • There are several string breaks in the code recommended is to put the HTML of the page out of the tag <?php ?>, whole echo more and one line that does not have a PHP variable should be written outside the PHP tag, because this way you avoid this confusion of putting all your HTML as a string.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.