0

I have a php script which outputs my search into excel. It doesn't do the formatting right and it includes my menu from my Website and my searchbar.

How can I bring it to not output the menu or the searchbar? Or should I just export the data into csv? I'm not sure what's easier..

Heres my current working code :

normalsearch.php :

<html>
<header>

<?php include 'menu2.php'; ?>
<br>
<form action="normalsearch.php" method="GET">
    LF. Nr / Code : 
    <input type="text" name="query" />
    <input type="submit" value="Suchen" />
</form>

<form action="export.php" method="GET">
    <input type="text" name="query" />
    <input type="submit" value="Excel">
</form>

</header>

<br>
<br>

<body>
<table border="1">
<tr>
<th>Number</th>
<th>Product ID</th>
<th>Description</th>
<th>Stock</th>
</tr>

<?php
$query = $_GET['query']; 

//connection to mysql
mysql_connect("host", "user", "password"); //server , username  , password
mysql_select_db("database");

//query get data
$sql = mysql_query("SELECT * FROM database WHERE productID LIKE '%".$query."%' LIMIT 100");
$no = 1;
while($data = mysql_fetch_assoc($sql)){
echo '
<tr>
    <td>'.$no.'</td>
    <td>'.$data['productID'].'</td>
    <td>'.$data['description'].'</td>
    <td>'.$data['stock'].'</td>
</tr>
';
$no++;
}
?>
</body>
</html>

export.php :

<?php
// The function header by sending raw excel
header("Content-type: application/vnd.ms-excel");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=Artikelsuche.xls");
// Add data table
include 'normalsearch.php';
?>
Veve
  • 6,643
  • 5
  • 39
  • 58
Exelion
  • 27
  • 4
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 11 '17 at 10:08
  • Well try outputting a string formatted as comma seperated instead of a whole web page – RiggsFolly Jan 11 '17 at 10:11
  • As this would basically require a total rewrite I am voting to close this as Too Broad – RiggsFolly Jan 11 '17 at 10:15

1 Answers1

0

Here I can give you a sample PHP code for creating CSV file. I strongly recommend you - Do not use mysql. Use PDO or mysqli. And I'm not going to write a full code. Because New Developers/Learners are just copying from stack overflow or some other blogs. They are not trying themselves. And Last year, Developers/programmers/engineers got another caption.

Developers/programmers/engineers - Copy-Paste Job from StackOverflow.

This is ridiculous. Sorry to say about these things.

$filename = 'Your filename'; //You can include your filename with the date or other data.

//Suppose you want to show some product data

$csvdata="ProductId,ProductName,Category,Subcategory,Price\n";

header("Content-Disposition: attachment;filename=".$filename.".csv");

print $csvdata;


//Now you can retrieve data from the database. I hope, you will have a common DB connection function.

$Databaseconnection = DBConnection();

//Your conditions

//Your Query

  if(!$result=$Databaseconnection->query($query))
    {
        error_log("SQL error ");
        return;
    }

    if($result->num_rows==0)
    {
        error_log("Your Message");
        return;
    }



    while($row=$result->fetch_assoc())
    {

        $csvdata=implode(",",array_values($row));
        $csvdata.="\n";
        print $csvdata;
        flush();
        ob_flush();
    }

So please try this way.

Renjith V R
  • 2,981
  • 2
  • 22
  • 32