FPDF et Tableau mysql, variable de formulaire

Fermé
mannok - Modifié par mannok le 4/01/2011 à 15:14
 freemann - 5 janv. 2011 à 12:12
Bonjour,
je voudrais réaliser un tableau qui se rempli grace à des valeurs d'une base de données et selon des variables de formulaire. Mon problème : les premieres données de ma requete ne s'affiche pas ; le tableau commence à partir des deuxiemes données de ma requete. Aidez-moi s'il vous plait à trouver l'erreur et corriger !
Voici le code pour générer le PDF :

<?php 

if (!isset($_SESSION)) { 
session_start(); 
} 
$MM_authorizedUsers = ""; 
$MM_donotCheckaccess = "true"; 

// *** Restrict Access To Page: Grant or deny access to this page 
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
// For security, start by assuming the visitor is NOT authorized. 
$isValid = False; 

// When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
// Therefore, we know that a user is NOT logged in if that Session variable is blank. 
if (!empty($UserName)) { 
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
// Parse the strings into arrays. 
$arrUsers = Explode(",", $strUsers); 
$arrGroups = Explode(",", $strGroups); 
if (in_array($UserName, $arrUsers)) { 
$isValid = true; 
} 
// Or, you may restrict access to only certain users based on their username. 
if (in_array($UserGroup, $arrGroups)) { 
$isValid = true; 
} 
if (($strUsers == "") && true) { 
$isValid = true; 
} 
} 
return $isValid; 
} 

$MM_restrictGoTo = "connexion.php"; 
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { 
$MM_qsChar = "?"; 
$MM_referrer = $_SERVER['PHP_SELF']; 
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; 
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
$MM_referrer .= "?" . $QUERY_STRING; 
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); 
header("Location: ". $MM_restrictGoTo); 
exit; 
} 

if (isset($_GET['pontbascule'])) { 
$colpontbascule_rsLivraison = (get_magic_quotes_gpc()) ? $_GET['pontbascule'] : addslashes($_GET['pontbascule']); 
} 

if (isset($_GET['debut'])) { 
$coldebut_rsLivraison = (get_magic_quotes_gpc()) ? $_GET['debut'] : addslashes($_GET['debut']); 
} 

if (isset($_GET['producteur'])) { 
$colproducteur_rsLivraison = (get_magic_quotes_gpc()) ? $_GET['producteur'] : addslashes($_GET['producteur']); 
} 

if (isset($_GET['fin'])) { 
$colfin_rsLivraison = (get_magic_quotes_gpc()) ? $_GET['fin'] : addslashes($_GET['fin']); 
} 

if ( $colproducteur_rsLivraison == '%' ) { $colproducteur = '' ;} 
else { $colproducteur = $colproducteur_rsLivraison ;} 

if ( $colpontbascule_rsLivraison == '%' ) { $colpontbascule = '' ;} 
else { $colpontbascule = $colpontbascule_rsLivraison ;} 

if ( $coldebut_rsLivraison == '' ) { $coldebut = '' ;} 
else { $coldebut = changedateusfr($coldebut_rsLivraison) ;} 

if ( $colfin_rsLivraison == '' ) { $colfin = '' ;} 
else { $colfin = changedateusfr($colfin_rsLivraison) ;} 

require('fpdf.php'); 

//Connect to your database 
include("Connections/mabase.php"); 

//Create new pdf file 
$pdf=new FPDF(); 
$pdf->AliasNbPages(); 

//Variables 
$user=$_SESSION['MM_Username']; 
$date=date("d").'/'.date("m").'/'.date("Y"); 
$heure=date("H").'h'.date("i"); 

//Fonction pour changer le format d'affichage de la date du style anglais au français 
function changedateusfr($dateus) 
{ 
$datefr=$dateus{8}.$dateus{9}."-".$dateus{5}.$dateus{6}."-".$dateus{0}.$dateus{1}.$dateus{2}.$dateus{3}; 
return $datefr; 
} 

//Disable automatic page break 
$pdf->SetAutoPageBreak(false); 

//Add first page 
$pdf->AddPage(); 

//set initial y axis position per page 
$y_initial = 50; 
$y_next = 20; 
$y_footer = -20; 

//Set Row Height 
$row_height = 5; 

//print logo 
$pdf->Image('images/slogo.jpg',22,10,20); 

//print date 
$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','',9); 
$pdf->SetXY(165,15); 
$pdf->Cell(20,5,$date.' - '.$heure,0,0,'R'); 

//print title 
$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','BU',12); 
$pdf->SetXY(55,30); 
$pdf->Cell(100,7,'SITUATION DE LIVRAISON',0,0,'C'); 

//print column titles 
$pdf->SetFillColor(190,190,190); 
$pdf->SetFont('Arial','B',9); 
$pdf->SetXY(25,$y_initial); 
$pdf->Cell(18,5,'DATE',1,0,'L',1); 
$pdf->Cell(55,5,'PRODUCTEUR',1,0,'L',1); 
$pdf->Cell(27,5,'PONT BASCULE',1,0,'L',1); 
$pdf->Cell(15,5,'TRANSP',1,0,'L',1); 
$pdf->Cell(20,5,'MODE PAIE',1,0,'L',1); 
$pdf->Cell(25,5,'QUANTITE',1,0,'R',1); 

$y_axis = $y_initial + $row_height; 

//print footer 
$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','',9); 
$pdf->SetXY(165,-20); 
//$pdf->Cell(100,5,'Edité par '.$user,0,0,'L'); 
$pdf->Cell(20,5,'Page '.$pdf->PageNo().'/{nb}',0,0,'R'); 

//Select data you want to show in your PDF file 

$query_val_producteur = "SELECT id_producteur, code, nomprenom FROM producteur WHERE id_producteur LIKE '$colproducteur' " ; 
$val_producteur = mysql_query($query_val_producteur, $mabase) or die(mysql_error()); 
$row_val_producteur = mysql_fetch_assoc($val_producteur); 
$val_producteur = $row_val_producteur['nomprenom']; 

$query_val_pontbascule = "SELECT id_pontbascule, nom_pbascule FROM pontbascule WHERE id_pontbascule LIKE '$colpontbascule' " ; 
$val_pontbascule = mysql_query($query_val_pontbascule, $mabase) or die(mysql_error()); 
$row_val_pontbascule = mysql_fetch_assoc($val_pontbascule); 
$val_pontbascule = $row_val_pontbascule['nom_pbascule']; 

$query_rsLivraison = sprintf("SELECT * FROM livraison, pontbascule, producteur WHERE livraison.pontbascule LIKE '%s' AND livraison.producteur LIKE '%s' AND livraison.pontbascule=pontbascule.id_pontbascule AND livraison.producteur=producteur.id_producteur AND (date_livraison BETWEEN '%s' AND '%s') ORDER BY livraison.date_livraison ASC, producteur.nomprenom ASC", $colpontbascule_rsLivraison, $colproducteur_rsLivraison, $coldebut_rsLivraison, $colfin_rsLivraison); 

$rsLivraison = mysql_query($query_rsLivraison, $mabase) or die(mysql_error()); 
$row_rsLivraison = mysql_fetch_array($rsLivraison); 
$totalRows_rsLivraison = mysql_num_rows($rsLivraison); 

$row_height = 10; 
$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','',9); 
$pdf->SetXY(25,42); 
$pdf->Cell(73,5,$val_producteur,0,0,'L'); 
$pdf->Cell(24,5,$val_pontbascule,0,0,'L'); 
$pdf->Cell(23,5,'Période : ',0,0,'R'); 
$pdf->Cell(40,5,$coldebut.' au '.$colfin,0,0,'R'); 

//initialize counter 
$i = 0; 

//Set maximum rows for first page 
$max = 43; 

$row_height = 10; 
$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','B',9); 
$pdf->SetXY(25,55); 
$pdf->Cell(18,210,'',1,0,'C',1); 
$pdf->Cell(55,210,'',1,0,'C',1); 
$pdf->Cell(27,210,'',1,0,'C',1); 
$pdf->Cell(15,210,'',1,0,'C',1); 
$pdf->Cell(20,210,'',1,0,'C',1); 
$pdf->Cell(25,210,'',1,0,'R',1); 

$total_qte=0; 

while($row_rsLivraison = mysql_fetch_assoc($rsLivraison)) 
{ 
$total_qte += $row_rsLivraison['quantite']; 

//If the current row is the last one, create new page and print column title 
if ( $i == $max ) 
{ 
$pdf->AddPage(); 

//print column titles for the current page 
$row_height = 5; 

$pdf->SetFillColor(190,190,190); 
$pdf->SetFont('Arial','B',9); 
$pdf->SetXY(25,$y_next); 
$pdf->Cell(18,5,'DATE LIVR',1,0,'L',1); 
$pdf->Cell(55,5,'PRODUCTEUR',1,0,'L',1); 
$pdf->Cell(27,5,'PONT BASCULE',1,0,'L',1); 
$pdf->Cell(15,5,'TRANSP.',1,0,'L',1); 
$pdf->Cell(20,5,'MODE PAIE',1,0,'L',1); 
$pdf->Cell(25,5,'QUANTITE',1,0,'R',1); 

//Go to next row 
$i = 0; 
$y_axis = $y_next + $row_height; 

//print footer 
$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','',9); 
$pdf->SetXY(25,-20); 
$pdf->Cell(160,5,'Page '.$pdf->PageNo().'/{nb}',0,0,'R'); 

//Set $i variable to 0 (first row) 
$i = 0; 

//Set maximum rows per page 
$max = 48; 

//print total 
$row_height = 10; 

$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','B',9); 
$pdf->SetXY(25,25); 
$pdf->Cell(18,240,'',1,0,'C',1); 
$pdf->Cell(55,240,'',1,0,'C',1); 
$pdf->Cell(27,240,'',1,0,'C',1); 
$pdf->Cell(15,240,'',1,0,'C',1); 
$pdf->Cell(20,240,'',1,0,'C',1); 
$pdf->Cell(25,240,'',1,0,'R',1); 

//print total 
if ( $pdf->PageNo()*$max >= $totalRows_rsLivraison ) 
{ $row_height = 10; 
$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','B',9); 
$pdf->SetXY(25,265); 
$pdf->Cell(135,7,'TOTAL',1,0,'C',1); 
$pdf->Cell(25,7,number_format($total_qte, 0, ',', ' ').' kg',1,0,'R',1); 
} 

} 

//data 
$date = changedateusfr($row_rsLivraison['date_livraison']); 
$producteur = $row_rsLivraison['nomprenom']; 
$pontbascule = $row_rsLivraison['nom_pbascule']; 
$transport = $row_rsLivraison['mode_transport']; 
$paiement = $row_rsLivraison['mode_paiement']; 
$quantite = number_format($row_rsLivraison['quantite'], 0, ',', ' ').' kg'; 

$row_height = 5; 

$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','',9); 
$pdf->SetXY(25,$y_axis); 
$pdf->Cell(18,5,$date,1,0,'L',1); 
$pdf->Cell(55,5,$producteur,1,0,'L',1); 
$pdf->Cell(27,5,$pontbascule,1,0,'L',1); 
$pdf->Cell(15,5,$transport,1,0,'L',1); 
$pdf->Cell(20,5,$paiement,1,0,'L',1); 
$pdf->Cell(25,5,$quantite,1,0,'R',1); 

//Go to next row 
$y_axis = $y_axis + $row_height; 
$i = $i + 1; 

//print total 
if ( $max >= $totalRows_rsLivraison ) 
{ 
$row_height = 10; 
$pdf->SetFillColor(255,255,255); 
$pdf->SetFont('Arial','B',9); 
$pdf->SetXY(25,265); 
$pdf->Cell(135,7,'TOTAL',1,0,'C',1); 
$pdf->Cell(25,7,number_format($total_qte, 0, ',', ' ').' kg',1,0,'R',1); 
} 

} 

mysql_close($mabase); 

//Send file 
$pdf->Output('situation_livraison.pdf', 'I'); 
?>


A voir également:

1 réponse

Reivax962 Messages postés 3672 Date d'inscription jeudi 16 juin 2005 Statut Membre Dernière intervention 11 février 2021 1 011
4 janv. 2011 à 16:19
Bonjour,

C'est très simple.

Dans ton code, à cet endroit :
$rsLivraison = mysql_query($query_rsLivraison, $mabase) or die(mysql_error()); 
$row_rsLivraison = mysql_fetch_array($rsLivraison); 
$totalRows_rsLivraison = mysql_num_rows($rsLivraison); 

Tu extraits la première ligne avec ta fonction mysql_fetch_array. Puis tu n'en fais plus rien.
Plus loin, au moment de ta boucle, tu fais :
while($row_rsLivraison = mysql_fetch_assoc($rsLivraison)) 
{ 

Là, tu reprends ton résultat à partir de la deuxième ligne, car la première a déjà été récupérée !

Donc pour corriger le problème, il suffit d'effacer la ligne
$row_rsLivraison = mysql_fetch_array($rsLivraison);

Xavier
2
Merci. J'essaie et vous tiens informé !
0