I have a MySQL table of products. I need to create a .csv export of them.
The products are on the bench 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 set a fixed number of lines per page. When the limit is not reached, 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
}
}
This causes me the following error:
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-> Run (Array) # 1 {main} thrown in C: \ Users \ server \ htdocs \ systems \ modules \ products \ export.php on line 92