stackumhi Posted September 28, 2009 Share Posted September 28, 2009 Using dompdf I made a script that will print an invoice for a contractor, this works fine. There are about 10 clients in my database and they each have their own contractors anywhere from 5 to 100. I use contractor_id and client_id as primary keys. How can I put all the contractors invoice information in an array, create a single file, and then send it to my invoice script? I have done some research on this and I think I need to use a foreach loop with print_r but I am not sure how to set it up. Thanks for any help. My code is below. // Get Payroll information $result = mysql_query("SELECT * FROM PAYROLL WHERE payroll_id = '1' LIMIT 1"); $row = mysql_fetch_array($result) or die(mysql_error()); $today = date("Y/m/d"); $contractor_id = $row['contractor_id']; $payroll_start_date = $row['payroll_start_date']; $payroll_end_date = $row['payroll_end_date']; $hours_worked = $row['hours_worked']; $hourly_rate = $row['hourly_rate']; $deductions = $row['deductions']; $advance = $row['advance']; $total_hourly = $hours_worked * $hourly_rate; $sub_total = ($hours_worked * $hourly_rate) + $advance - $deductions; $kb_fee = $sub_total * .05; $total_net_pay = round($sub_total -($sub_total * .05), 2); // Get Contractor Name $result2 = mysql_query("SELECT first_name, last_name, client_assigned_number FROM CONTRACTORS WHERE contractor_id = '$contractor_id' LIMIT 1"); $row2 = mysql_fetch_array($result2) or die(mysql_error()); $first_name = $row2['first_name']; $last_name = $row2['last_name']; $client_assigned_number = $row2['client_assigned_number']; $html = <<<EOD <html> <head> <style type="text/css"> <!-- table.change_order_items { font-size: 10pt; width: 100%; border-collapse: collapse; margin-top: 2em; margin-bottom: 2em; } table.change_order_items>tbody { border: 1px solid black; } table.change_order_items>tbody>tr>th { border-bottom: 1px solid black; } table.change_order_items>tbody>tr>td { border-right: 1px solid black; padding: 0.5em; } td.change_order_total_col { padding-right: 4pt; text-align: right; } td.change_order_unit_col { padding-left: 2pt; text-align: left; } .odd_row td { background-color: transparent; border-bottom: 0.9px solid } .even_row td { background-color: #f6f6f6; border-bottom: 0.9px solid #ddd; } --> </style> </head> <!--Header--> <table style="width: 100%;" class="header"> <tr> <td><h1 style="text-align: right">INVOICE</h1></td> </tr> </table> <!--Name & Date Section--> <table style="width: 100%;"> <tr> <td style="text-align: left"><?php echo '$first_name';?> <?php echo '$last_name';?></td><td style="text-align: right">Date: <?php echo '$today';?></td> </tr> <tr> <td style="text-align: left"><?php echo '$client_assigned_number';?></td><td style="text-align: right">Invoice Reference: <?php echo '$row[payroll_start_date]';?> - <?php echo '$row[payroll_end_date]';?></td> </tr> </table> <!--Invoice To Section--> <table style="width: 100%;"> <tr><td style="text-align: left"><br /></td></tr> <tr><td style="text-align: left"><strong>Invoice To</strong> <br />Knightsbridge Associates Limited<br />P.O. Box 653<br />Guernsey<br />GY1 3PT<br /></td> <td style="text-align: right">For Professional Services to:<br />London Parking Control Ltd.<br />BM 2896 <br />London <br />WC1N 3XX<br /></td></tr> </table> <!--Item Description Section--> <table class="change_order_items"> <tr><td colspan="6"><h2>For Professional Services</h2></td></tr> <tbody> <tr> <th>Item</th> <th>Description</th> <th>Quantity</th> <th colspan="2">Rate</th> <th>Amount</th> </tr> <tr class="even_row"> <td style="text-align: center">1</td> <td>Hours Worked</td> <td style="text-align: center"><?php echo '$row[hours_worked]';?></td> <td style="text-align: right; border-right-style: none;">£<?php echo '$row[hourly_rate]';?></td> <td class="change_order_unit_col" style="border-left-style: none;"></td> <td class="change_order_total_col">£<?php echo '$total_hourly';?></td> </tr> <tr class="odd_row"> <td style="text-align: center"></td> <td>Pay Advance</td> <td style="text-align: center"></td> <td style="text-align: right; border-right-style: none;"></td> <td class="change_order_unit_col" style="border-left-style: none;"></td> <td class="change_order_total_col">£<?php echo '$advance';?></td> </tr> <tr class="even_row"> <td style="text-align: center"></td> <td>Deductions</td> <td style="text-align: center"></td> <td style="text-align: right; border-right-style: none;"></td> <td class="change_order_unit_col" style="border-left-style: none;"></td> <td class="change_order_total_col">£<?php echo '$deductions';?></td> </tr> <tr class="odd_row"> <td style="text-align: center"></td> <td style="text-align: right;">Sub Total</td> <td style="text-align: center"></td> <td style="text-align: right; border-right-style: none;"></td> <td class="change_order_unit_col" style="border-left-style: none;"></td> <td class="change_order_total_col">£<?php echo '$sub_total';?></td> </tr> <tr class="odd_row"> <td style="text-align: center"></td> <td style="text-align: right;">Less 5% Commission</td> <td style="text-align: center"></td> <td style="text-align: right; border-right-style: none;"></td> <td class="change_order_unit_col" style="border-left-style: none;"></td> <td class="change_order_total_col">£<?php echo '$kb_fee';?></td> </tr> </tbody> <tr> <td colspan="3" style="text-align: right;"></td> <td colspan="2" style="text-align: right;"><strong>GRAND TOTAL:</strong></td> <td class="change_order_total_col"><strong>£<?php echo '$total_net_pay';?></strong></td></tr> </table> <p><strong>THANK YOU FOR YOUR BUSINESS</strong><br /> Please advise us immediately if any of your details change or are wrong at either the above Knightsbridge<br /> Associates address, by email at [email protected] or by telephone on 01481 740631<br /></p> <p><strong>PLEASE NOTE:</strong> Knightsbridge Associates are an international invoicing service. The above invoice is for services as a self employed contractor. This invoice should be retained for your refernce and for your accounts as a self employed contractor. Knightsbridge Associates take a 5% commision as a service fee. Please read the terms and conditions for using Knightsbridge Associates Ltd. as your invoicing company.</p></td> </tr> </body> </html> EOD; $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); $dompdf->stream("Invoice.pdf"); ?> Link to comment https://forums.phpfreaks.com/topic/175798-foreach-help/ Share on other sites More sharing options...
jjacquay712 Posted September 28, 2009 Share Posted September 28, 2009 <?php $store_array = array(); //Make array to store Database results $query = mysql_query("SELECT * FROM whatever"); while ( $result = mysql_fetch_array($query) ) { $store_array[] = $result; // Loop through results putting them in the array } $to_file = var_export($store_array, true); // Use var export to make a parse able string of the array $file = "testFile.txt"; $fh = fopen($file, 'w'); fwrite($fh, $to_file); // Write string to file fclose($fh); ?> Here is how you would get the data into the other PHP script: <?php $arr = file_get_contents("testFile.txt"); eval("$thearray = " . $arr . ";"); // You can now use the array here. ?> Link to comment https://forums.phpfreaks.com/topic/175798-foreach-help/#findComment-926379 Share on other sites More sharing options...
stackumhi Posted September 28, 2009 Author Share Posted September 28, 2009 Thanks jjacquay, I am going to try your suggestions now. Link to comment https://forums.phpfreaks.com/topic/175798-foreach-help/#findComment-926394 Share on other sites More sharing options...
stackumhi Posted September 28, 2009 Author Share Posted September 28, 2009 I got the array working properly with results in array like: array ( 0 => array ( 0 => '2', 'contractor_id' => '2', 1 => '2009-09-10', 'payroll_start_date' => '2009-09-10', 2 => '2009-10-10', 'payroll_end_date' => '2009-10-10', 3 => '25', 'hours_worked' => '25', 4 => '10.00', 'hourly_rate' => '10.00', 5 => '0.00', 'deductions' => '0.00', 6 => '0.00', 'advance' => '0.00', ), Now how do I assign each array value to a variable that I can use in my invoice? Or would I not assign the variable and just use the array value directly in the invoice? Example: The manual version: $payroll_start_date = $row['payroll_start_date']; $payroll_end_date = $row['payroll_end_date']; The new version with array: $payroll_start_date = ??????? $payroll_end_date = ??????? Thanks. Link to comment https://forums.phpfreaks.com/topic/175798-foreach-help/#findComment-926415 Share on other sites More sharing options...
redarrow Posted September 28, 2009 Share Posted September 28, 2009 could do this crazy but good stuff. <?php session_start(); $work=array("Work name"=>"redarrow","Work date"=>"11/10/2009","Work hours"=>"10","Earnings per hour"=>"10.00"); foreach($work as $key=>$val){ $_SESSION[$key]=$val; } echo $_SESSION['Work name']; ?> Funky maths <?php session_start(); $work=array("Work name"=>"redarrow","Work date"=>"11/10/2009","Work hours"=>"10","Earnings per hour"=>"10.00"); foreach($work as $key=>$val){ $_SESSION[$key]=$val; } $_SESSION['Total earnings']=$_SESSION['Work hours'] * $_SESSION['Earnings per hour']; echo £.number_format($_SESSION['Total earnings'],2); ?> Link to comment https://forums.phpfreaks.com/topic/175798-foreach-help/#findComment-926433 Share on other sites More sharing options...
jjacquay712 Posted September 28, 2009 Share Posted September 28, 2009 array ( 0 => array ( 0 => '2', 'contractor_id' => '2', 1 => '2009-09-10', 'payroll_start_date' => '2009-09-10', 2 => '2009-10-10', 'payroll_end_date' => '2009-10-10', 3 => '25', 'hours_worked' => '25', 4 => '10.00', 'hourly_rate' => '10.00', 5 => '0.00', 'deductions' => '0.00', 6 => '0.00', 'advance' => '0.00', ), If you used my second example to evaluate the file you could do it like this: <?php $arr = file_get_contents("testFile.txt"); eval("$thearray = " . $arr . ";"); // You can now use the array here. $payroll_start_date = $thearray[0]['payroll_start_date']; ?> Link to comment https://forums.phpfreaks.com/topic/175798-foreach-help/#findComment-926497 Share on other sites More sharing options...
stackumhi Posted September 28, 2009 Author Share Posted September 28, 2009 Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/175798-foreach-help/#findComment-926594 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.