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>

4
  • Are you sure you are not getting any errors/unexpected results? because I see some of them. Commented Sep 13, 2015 at 8:31
  • works like a charm :D just need to add the print function Commented Sep 13, 2015 at 8:32
  • So <td><?php echo $f1 ?></td> doesn't need a ending ; after $f1. Commented Sep 13, 2015 at 8:34
  • well, just tried it again after reading your comment and still works great. Commented Sep 13, 2015 at 8:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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?
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...
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
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
@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!!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.