Export . csv with complement of lines based on a fixed number

Asked

Viewed 60 times

3

I have a table in Mysql of products. I need to create an export . csv of them.

The products are in the bank as follows:

cod    produto    valor    pagina
123    caderno     1,00    1
456    lápis       1,00    1
789    borracha    1,00    1
1122   régua       1,00    2
1123   cola        1,00    2
1122   caneta      1,00    3

I need to define a fixed number of lines per page. When the limit is not reached, you need to complete with empty spaces. Let’s imagine that the limit is 5.

Expected:

cod    produto    valor    pagina
123    caderno     1,00    1
456    lápis       1,00    1
789    borracha    1,00    1
 -        -          -     -
 -        -          -     -
1122   régua       1,00    2
1122   cola        1,00    2
 -        -          -     -
 -        -          -     -
 -        -          -     -
 1122   caneta      1,00   3
 -        -          -     -
 -        -          -     -
 -        -          -     -
 -        -          -     -
 -        -          -     -

This can be done in Mysql or PHP, but does anyone have any idea how I could do it?

I have the following query:

$pagina = 0;
$quantidade = 24;
$inicio     =  ($quantidade * $pagina) - $quantidade;
$export = $conn->prepare("SELECT 
  f.prod_produto,f.prod_marca,
  f.prod_tipo1,f.prod_tipo2,
  SUBSTRING_INDEX(f.prod_preco,'.',1) AS prod_preco,
  SUBSTRING_INDEX(f.prod_preco,'.',-1) AS prod_centavos,
  REPLACE(f.prod_gramatura,',','.') AS prod_gramatura,  
  (CASE 
  WHEN f.prod_pais = 0 THEN 'BRA' ELSE   p.iso3
  END) AS pais,
  prod_pagina AS pagina
  FROM 
  produto AS f
  LEFT JOIN cep_paises AS p
  ON p.numcode = f.prod_pais
  WHERE
  prod_periodo = '16'
  ORDER BY prod_pagina ASC
  LIMIT  $inicio,$quantidade");
$export->execute(array());


while($dados = $export->fetch()){
   //exibe produtos
  echo $dados[0]."<br>";
}
if($export->rowCount() < $quantidade){
  $conta = $quantidade - $export->rowCount();
  while($conta <= $quantidade){
    echo $conta++;
        //preenche as linhas em branco
  }
}

It causes me the following mistake:

Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near '-24,24' at line 18' in C: Users server htdocs systems modules products export.php:92 Stack trace: #0 C: Users server htdocs systems modules products export.php(92): Pdostatement->execute(Array) #1 {main} thrown in C: Users server htdocs systems modules products export.php on line 92

1 answer

2


$paginaAtual = "";
$limite = 5;
$conta = 0;
while($dados = $export->fetch()){
   //exibe produtos
  if($paginaAtual ==""){ //define a primeira pagina
      $paginaAtual = $dados["pagina"];
      $conta = 0;
  }elseif($paginaAtual != $dados['pagina']){//verifica se mudou de pagina
       while($conta < $limite){ // verifica se ficou algum item a menos
           echo "-        -          -     -"
            $conta++;
       }
       $paginaAtual = $dados["pagina"];
       $conta = 0;
  }
   $conta++
  echo $dados["produto"]."<br>";
}
while($conta < $limite){ // verifica pela ultima vez
       echo "-        -          -     -"
        $conta++;
   }
  • What would be: FIELD == CAMPO2 ?

  • vc can replace with variables q want to use ai $val == $val2

  • returned an error while trying to do, attached the error in your reply, know what could be?

  • I added my question to the original query for you to see how it looked

  • hi, what mistake I’m not seeing here

  • I added my question, I had added in your more not showed even.

  • It seems that there is something wrong with the limit -24,24 it limit is those lines: $quantity = 24; $start = ($quantity * $page) - $quantity;

  • "-24,24" your limit is being passed a negative value, try changing the page value $pagina = 0; to $pagina = 1;

  • Stopped giving error , no longer counting the empty

  • I understood what happened, she is not breaking the pages as in the example above, he calculates the 24 as a whole, more accurate he calculates by the page column as shown in the example

  • See, I increased the quantities and returned: rowcount50 quantidade50 , the number of pages I put it adds to the pages instead of breaking them as I want

  • change the answer, remove the limit from your SQL, and change your while

  • 90% because it worked, but only on the first page, for example, in my case on page 06 it would have to happen this rule but it was not, already I broke the head in the loops but I could not find what could be

  • let me understand only on the last page is not happening filling out the remaining lines ?

  • yes, more is because so, my limit is 24 with that on page 1 has 10 it complete, page 3 has 24, page 3 has 24, and so on, page 6 has 9 it should complete until 24 more not completed

Show 11 more comments

Browser other questions tagged

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