0

Can I print a set of data after running a query for two or more tables?

This is from two tables of my query:

1st query

$query = mysql_query("SELECT id, BuyerName,BuyerEmail,BuyerAddress,TransactionID,
        ItemAmount,DateTime FROM `order` WHERE YEAR(DateTime)= 
       '$Year'AND MONTH(DateTime) = '$Month' LIMIT $start, $per_page")
         or die(mysql_error());

2nd Query

$querydetail = mysql_query("SELECT * FROM orderlist WHERE TransactionID  = '$f4'");

And is it possible to print it the way that it's set in php by using table?

This is how I display my data in table format. Is there a way to import this output directly into Microsoft Word or something that is in A4 paper format?

 <?php  
    while($row = mysql_fetch_array($query))
    {  
              
    $f1 = $row['BuyerName'];
    $f2 = $row['BuyerEmail'];
    $f3 = $row['BuyerAddress'];
    $f4 = $row['TransactionID'];
    $f5 = $row['ItemAmount'];
    $f6 = $row['DateTime'];
 
 
  ?>
  <table class="table">
     <tr>
      <th>Nama Pelanggan</th>
      <th>Email Pelanggan</th>
      <th>Alamat Pelanggan</th>
      <th>ID Transaksi</th>
      <th>Harga</th>
      <th>Tarikh</th>
   
   <tr>
    <td><?php echo $f1 ?></td>
    <td><?php echo $f2 ?></td>
    <td><?php echo $f3 ?></td>
    <td><?php echo $f4 ?></td>
    <td><?php echo $f5 ?></td>
    <td><?php echo $f6 ?></td>

   </tr>
   <table class="table">
     <tr>
      <th>Nama Barang</th>
      <th>Kod Barang</th>
      <th>Kuantiti</th>
      
   
   
     </tr>
   <?php
 $querydetail = mysql_query("SELECT * FROM orderlist WHERE TransactionID  = '$f4'");
 while($rows = mysql_fetch_array($querydetail))
    {  
              
    $fd1 = $rows['ItemName'];
    $fd2 = $rows['ItemNumber'];
    $fd3 = $rows['ItemQTY'];
 ?>
<tr>
    <td><?php echo $fd1 ?></td>
    <td><?php echo $fd2 ?></td>
    <td><?php echo $fd3 ?></td>
</tr>
Pascal Le Merrer
  • 5,883
  • 20
  • 35
Mohd Fadli
  • 143
  • 10

1 Answers1

0

If you do not save the result of the first query e.g. in a variable, it will be lost as soon as the next one executes. Fetch the queries' results as soon as you execute them, store them in arrays and use them as soon as you have all the data available that you need for the next step, like printing them out. Is that what you asked?

B.t.w. don't use mysql_query anymore, it will be removed in future php releases and might make your code vulnerable to sql injection attacks. It's much safer to use PDOs or mysli for your database work.

EDIT For creating a word document from PHP have a look at this question.

Or you could just leave out the HTML tags, separate the columns with a suitable delimiting character (e.g. a semicolon, if you don't use semicolons in your data)

<?php $delimiter = ";"; ?>
...
<?php echo $f1 . $delimiter ?>

instead of

<td><?php echo $f1 ?></td>

and import into Excel or OpenOffice Calc. Here's a solution to output with a CSV mime type to download instead of browser output.

Or you could e.g. use FPDF to create PDF files.

Community
  • 1
  • 1
  • this is just for my fyp, too lazy to change it to mysqli. btw i already did save the result in a variable, can u kindly show me the source code to print it? – Mohd Fadli Sep 13 '15 at 08:15
  • Ah, now i get it. You mean actually print on paper, right? For direct output to word or in case of a table excel, the easiest way would be copy-paste from the browser, I guess. Or just print from the browser... – some-non-descript-user Sep 13 '15 at 09:50
  • hahaha, thats why im in a dilemma, i search the web and all i get its echo print_r and blablabla... need a soft copy. and this is for my final year projects, so its not really practical to print a report by using copy-paste. i need a built in function to do so – Mohd Fadli Sep 13 '15 at 11:12
  • is there any simpler way to get this? im on a total loss already read the link u gave me and its complicated. never did use 3rd party for php – Mohd Fadli Sep 13 '15 at 14:36
  • Creating doc(x) or pdf *is* relatively complicated. Sorry. Using plain text or csv is the easiest way, I guess. Use PHP to create that and and send a header like explained in the link ("download"). That way it will be relatively easy to open with an application faciliating printing. If you're familiar with LaTeX you can also use that to create PDFs serverside. – some-non-descript-user Sep 13 '15 at 15:58
  • 1
    @MohdFadli you can have a look at phpexcel library. You should also find a php to word library from the same users. It's well documented and it will be easy to turn your html into an office doc. Ps MySQL_* functions are deprecated and removed in the next release of php! Turn to MySQL please!!! – Lelio Faieta Sep 13 '15 at 20:34
  • ok, trying to understand PHPWord for now.. that's the simplest with some raw code for start – Mohd Fadli Sep 14 '15 at 02:09