TCPDF how to redirect the page if the code is invalid?

Asked

Viewed 157 times

1

I am making a report for printing with TCPDF but I am facing some difficulties in redirecting if the code is invalid. see the following code:

if(isset($_GET['CODIGO']) AND $_GET['CODIGO'] != ''){

        $html   = '';
        $item   = 1;
        $dados = filter_var($_GET['CODIGO'], FILTER_SANITIZE_NUMBER_INT);

        $sql    = "SELECT * FROM TESTE_TABELA WHERE CODIGO = $dados";
        $query  = sqlsrv_query($conexion,$sql);

        while($row = sqlsrv_fetch_array($query)){
            $cod = 0;
            $manufacturero = $row['NOME'];
            $imagen = $row['IMG'];

            if($row['CODIGO']){

                $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
                $pdf->SetTitle('Imprimir Imagem'); //Titlo del pdf
                $pdf->setPrintHeader(false); //No se imprime cabecera
                $pdf->setPrintFooter(false); //No se imprime pie de pagina
                $pdf->SetMargins(20, 20, 20, false); //Se define margenes izquierdo, alto, derecho
                $pdf->Cell(200, 30, 'Relatório', 1, '', 'center', '', '', '', '');
                $pdf->SetAutoPageBreak(true, 20); //Se define un salto de pagina con un limite de pie de pagina   
                $pdf->addPage();

                //$barcode = '';
                //$barcode = $pdf->serializeTCPDFtagParameters(array($barcode, 'C128', '', '', 72, 25, 0.5, array('position'=>'S', 'border'=>false, 'padding'=>2, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>7, 'stretchtext'=>6), 'N'));

                $html .= '
                        <h3 style="text-align:center; font-family:helvetica;">Nome: '.$manufacturero.'</h3><br><br>      
                        <img src="images/'.$imagen.'" width="520px"><br>';

                $item = $item+1;

                $pdf->SetFont('Helvetica', '', 10);
                $pdf->writeHTML($html, true, 0, true, 0);

                $pdf->lastPage();
                $pdf->output('Reporte.pdf', 'I');

            }else if(!$row['CODIGO']){
                echo "<script>window.location.href='index.php'</script>";
            }                
        }
    }else{
        echo "<script>window.location.href='index.php'</script>";
    } 

The first if is working fine, if there is no get, and if the url code is 0 it redirects. The big problem is when it enters while, then it enters if makes the comparison but if the code does not exist it does not redirect. Example: I have a report with code 7, if I type in the url 6 is a report that does not exist, it should enter the first if but be barred in the second, but it does not do anything, does not redirect, the page is empty, anyone has any idea what it is? Thanks in advance.

1 answer

0

if(isset($_GET['CODIGO']) AND $_GET['CODIGO'] != ''){
        $dados = filter_var($_GET['CODIGO'], FILTER_SANITIZE_NUMBER_INT);

        $sql    = "SELECT CODIGO FROM TESTE_TABELA WHERE CODIGO = $dados";
        $query  = sqlsrv_query($conexion,$sql);

        if($row = sqlsrv_fetch_array($query) > 0){

        }else{
             echo "<script>window.location.href='index.php'</script>";
        } 
}

After a long time, I made this condition if if($Row = sqlsrv_fetch_array($query) > 0) is true or greater than 0 it executes the code and opens the pdf, if false is less than or equal to 0 it redirects. All the rest of the code I posted before goes inside if($Row = sqlsrv_fetch_array($query) > 0

Browser other questions tagged

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