execute function

2

I would like to run the following functions , excel() that generates the excel file, and the pdf() function, these files are generated within a search, and how they are generated automatically, I decided to create function to be able to be executed, but it did not work very well, could anyone help me?

<?php   

define('FPDF_FONTPATH', 'font/');
require('fpdf.php');
$pdf=new FPDF('p', 'cm', 'A4');
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);


include "conexao.php";




$busca = $_POST['palavra'];// palavra que o usuario digitou

$busca_query = mysql_query("SELECT * FROM carreteiro WHERE nome LIKE '%$busca%' or cidade like '%$busca%' or estado like '%$busca%' or bairro like '%$busca%' or rua like '%$busca%'")or die(mysql_error());//faz a busca com as palavras enviadas


if (empty($busca_query)) { 
    echo "Nenhum registro encontrado.";
}

// quando existir algo em '$busca_query' ele realizará o script abaixo.
while ($dados = mysql_fetch_array($busca_query)) {

    echo "Nome : $dados[nome]<br />"; 
    echo "Cidade: $dados[cidade] <br />";
    echo "Estado: $dados[estado]<br />";
    echo "Rua: $dados[rua]<br />";
    echo "Bairro: $dados[bairro]<br />";
    echo "<hr>";

}



//inicio pdf /////////////////////////////////

function pdf(){

$exe = mysql_query("SELECT * FROM carreteiro WHERE nome LIKE '%$busca%' or cidade like '%$busca%' or estado like '%$busca%' or bairro like '%$busca%' or rua like '%$busca%'")or die(mysql_error());


while ($dados = mysql_fetch_array($exe))
 {
    $pdf->Cell(3,1,$dados['nome'],1,0,'L');
    $pdf->Cell(4,1,$dados['cidade'],1,0,'L');
    $pdf->Cell(2,1,$dados['estado'],1,0,'L');
    $pdf->Cell(5,1,$dados['rua'],1,0,'L');
    $pdf->Cell(5,1,$dados['bairro'],1,0,'L');

 }
    ob_start ();
$pdf->Output();

}


?>

<form action="pdf()"><input type="submit" value="Gerar PDF" /></form>
    
asked by anonymous 20.05.2015 / 17:21

2 answers

0

The php does not work that way, with javascript you could do, but with php you need to reload the page or use jQuery AJAX

Your form has to send a POST or GET with the information that php needs to execute the function, eg

<form>
<input type="hidden" name="comando" value="pdf" />
<input type="submit" value="Gerar PDF" />
</form>

In the php vc you would do the following:

<?php
$comando = $_GET["comando"];

if ($comando == "pdf") {
   pdf(); // chama a função aqui
}

?>
    
20.05.2015 / 17:34
0

This does not really work.

The problem is that PHP is a language run inside the server, and HTML, as well as Javascript, are "languages" of the client. Therefore, it is not possible to call PHP methods directly from the client.

What you should do is to action put the address of the script that will perform the report generation, and specify the desired format. In% PHP% of report generation, you then decide and execute the appropriate method (to generate Excel or PDF).

Simply put, it would look something like:

index.php

<form action="relatorio-pdf.php" method="post">
    <input type="text" name="palavra" />
    <input type="submit" value="Gerar PDF" />
</form>

report-pdf.php

<?php
    // tentei adaptar seu código para tornar o exemplo mais concreto.
    define('FPDF_FONTPATH', 'font/');
    require('fpdf.php');
    $pdf=new FPDF('p', 'cm', 'A4');
    $pdf->Open();
    $pdf->AddPage();
    $pdf->SetFont('Arial','',12);

    include "conexao.php";

    $busca = $_POST['palavra'];// palavra que o usuario digitou
    $exe = mysql_query("SELECT * FROM carreteiro WHERE nome LIKE '%$busca%' or cidade like '%$busca%' or estado like '%$busca%' or bairro like '%$busca%' or rua like '%$busca%'")or die(mysql_error());

    while ($dados = mysql_fetch_array($exe))
    {
        $pdf->Cell(3,1,$dados['nome'],1,0,'L');
        $pdf->Cell(4,1,$dados['cidade'],1,0,'L');
        $pdf->Cell(2,1,$dados['estado'],1,0,'L');
        $pdf->Cell(5,1,$dados['rua'],1,0,'L');
        $pdf->Cell(5,1,$dados['bairro'],1,0,'L');
    }
    ob_start ();
    $pdf->Output();

Note that this code simply generates the PDF. Generate HTML and XLS can be separate codes or you can modify this example to support all this in a single code.

I hope I have helped to lighten up your ideas a little bit.

Editing

PHP needs some way to know the word to perform the search. In the example I've mounted, I've followed your code that expects a script parameter with the name POST .

The flow of a normal application is as follows:

  • Displays a page asking the user to type the word for the search;
  • Displays a page with the search results, and buttons to export the results to PDF (or any other format) you want.
  • I'll illustrate how you could build this:

    item 1 would be built into the file palavra ; the search would be performed and displayed in the file busca.php ; export to PDF in file resultado.php .

    Note, I'll show only the interesting parts of HTML.

    search.php

    <form action="resultado.php" method="post">
        <input type="text" name="palavra" />
        <input type="submit" value="Buscar" />
    </form>
    

    result.php

    This file follows what you have developed, but without the PDF part:

    <?php   
        define('FPDF_FONTPATH', 'font/');
    
        include "conexao.php";
    
        $busca = $_POST['palavra'];// palavra que o usuario digitou
    
        $busca_query = mysql_query("SELECT * FROM carreteiro WHERE nome LIKE '%$busca%' or cidade like '%$busca%' or estado like '%$busca%' or bairro like '%$busca%' or rua like '%$busca%'")or die(mysql_error());//faz a busca com as palavras enviadas
    
        if (empty($busca_query)) { 
            echo "Nenhum registro encontrado.";
        }
    
        // quando existir algo em '$busca_query' ele realizará o script abaixo.
        while ($dados = mysql_fetch_array($busca_query)) {
            echo "Nome : $dados[nome]<br />"; 
            echo "Cidade: $dados[cidade] <br />";
            echo "Estado: $dados[estado]<br />";
            echo "Rua: $dados[rua]<br />";
            echo "Bairro: $dados[bairro]<br />";
            echo "<hr>";
        }
    ?>
    <form action="exportar-pdf.php">
        <input type="hidden" name="palavra" value="<?php echo $busca; ?>" />
        <input type="submit" value="Gerar PDF" />
    </form>
    

    Notice that within exportar-pdf.php , I added a hidden field with the name form , initialized with the search word previously entered. In this way, we can pass it to the script that will export the search data into PDF.

    export-pdf.php

    <?php
        // tentei adaptar seu código para tornar o exemplo mais concreto.
        define('FPDF_FONTPATH', 'font/');
        require('fpdf.php');
        $pdf=new FPDF('p', 'cm', 'A4');
        $pdf->Open();
        $pdf->AddPage();
        $pdf->SetFont('Arial','',12);
    
        include "conexao.php";
    
        $busca = $_POST['palavra'];// palavra que o usuario digitou
        $exe = mysql_query("SELECT * FROM carreteiro WHERE nome LIKE '%$busca%' or cidade like '%$busca%' or estado like '%$busca%' or bairro like '%$busca%' or rua like '%$busca%'")or die(mysql_error());
    
        while ($dados = mysql_fetch_array($exe))
        {
            $pdf->Cell(3,1,$dados['nome'],1,0,'L');
            $pdf->Cell(4,1,$dados['cidade'],1,0,'L');
            $pdf->Cell(2,1,$dados['estado'],1,0,'L');
            $pdf->Cell(5,1,$dados['rua'],1,0,'L');
            $pdf->Cell(5,1,$dados['bairro'],1,0,'L');
        }
        ob_start ();
        $pdf->Output();
    

    Note that the code in this file is exactly the code I put in the past!

    Conclusion

    In this way, we have the full flow of the application. I have not checked that your queries are correct. Without having the bank's DDL, it's tricky to do this, and apparently this is not the problem.

    The AJAX solution posted in another answer is also an option. However, I recommend that you first do this, which is simpler because it only requires knowledge in PHP. The AJAX solution needs Javascript knowledge, and it does not exclude PHP knowledge, so it's better to consolidate in the simplest way, and then move on to the more complex implementation (and in this case, not even that complex and much better!) .

    I hope I have helped.

        
    20.05.2015 / 17:31