-
Posts
10 -
Joined
-
Last visited
Profile Information
-
Gender
Male
-
Location
Peru
-
Age
44
walermo's Achievements

Newbie (1/5)
0
Reputation
-
I am using a script to send files by email. Currently my script calculates the sum of all file sizes and if the sum > 20MB it sends as many emails as there are attachments. If the size < 20MB then all documents are attached to a single email. Ideally, I would like to keep each email sent to a maximum of 10MB regardless of the number of attachments. Any suggestions on how I can accomplish this? Below the relevant part of my script: $attachments_size = 0; foreach ($files as $attachment) { // calculate size of all attachments $url = "/path/to/attachments/{$attachment['attachment_filename']}"; $url = stat(iconv('UTF-8', 'ISO-8859-1', $url)); $attachments_size += $url[7]; } $attachments = array(); if ($attachments_size > 20971520) { // send 1 attachment per email $email_count = 1; foreach ($files as $attachment) { $subject ="[" . $attachment_id . "]-[" . $email_count . "/" . $file_count . "] " . $attachment['attachment_filename']; // file_count = number of attachments array_push($attachments, $attachment['attachment_filename']); $send_it = send_to_scans($subject, $body, $attachments); // sends using PHPMailer $email_count++; } } else { // send all attachments in 1 email $subject = "[" . $attachment_id . "] " . ($file_count > 1 ? "Multiple files attached" : $attachment['attachment_filename']); foreach ($files as $attachment) { array_push($attachments, $attachment['attachment_filename']); } $send_it = send_to_scans($subject, $body, $attachments); // sends using PHPMailer }
-
Gah! I just noticed that I left that typo in the code again Sorry kicken
-
Hi kicken! Indeed, that syntax error was left over when I copied over the function code, which I trimmed for convenience. Also duly noted your suggestion about not using * in my select list. I actually don't use it and again, i put it here because I trimmed the code. In retrospect, that was not a good idea when coming for coding help. Requinix: below is the rest of my code. Thanks again for your help and suggestions! // functions being called for database access public function get_job_report_dates($start, $end) { $query = $this->db->prepare("SELECT DISTINCT job_date FROM jobs WHERE job_date BETWEEN ? AND ? AND entry = 1 ORDER BY job_date ASC"); $query->bindValue(1, $start); $query->bindValue(2, $end); try { $query->execute(); return $query->fetchAll(); } catch (PDOException $e) { die($e->getMessage()); } } public function get_job_report_interp($contractor, $date) { $query = $this->db->prepare("SELECT job_num, job_date, contractor_id, job_facility, job_procedure, job_start, job_end, TIMEDIFF(job_end, job_start) AS job_time, users.first_name AS contractor_name, users.last_name AS contractor_last, users.rate AS contractor_rate, FROM jobs LEFT JOIN users ON jobs.contractor_id = users.id) WHERE job_date = ? AND contractor_id = ? AND entry = 1 ORDER BY job_date ASC, job_start ASC"); $query->bindValue(1, $date); $query->bindValue(2, $contractor); try { $query->execute(); } catch (PDOException $e) { die($e->getMessage()); } return $query->fetchAll(); } ##################################### <?php /* billing_report_by_contractor */ include 'assets/inc/functions.inc'; include 'assets/inc/header.inc'; ?> <body> <?php include 'assets/inc/menu.admin.inc'; if (isset($_POST['next']) || isset($_POST['generate'])) extract($_POST); ?> <section class='container'> <div class='well hero-unit'> <div class='well'> <h3>Job Reports by contractor</h3> <h4><?php echo (isset($_POST['generate']) ? "Showing jobs for the following period" : "Please select a date range"); ?>:</h4> <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='formid' class='form-inline'> <input type='text' name='startdate' id='startdate' <?php echo ((isset($_POST['next']) || isset($_POST['generate'])) ? "placeholder='" . datef($startdate, "m/d/Y") . "' value='" . datef($startdate, "m/d/Y") . "' readonly" : "placeholder='Start Date' required"); ?>> <input type='text' name='enddate' id='enddate' <?php echo ((isset($_POST['next']) || isset($_POST['generate'])) ? "placeholder='" . datef($enddate, "m/d/Y") . "' value='" . datef($enddate, "m/d/Y") . "' readonly" : "placeholder='End Date' required"); ?>> <?php if (!isset($_POST['next']) && !isset($_POST['generate'])) { ?> <input type='hidden' name='by' value='contractor'> <button type='submit' class='btn btn-vlb' name='next'>Next</button> <?php } //!isset($_POST['next']) && !isset($_POST['generate']) ?> <?php if (isset($_POST['startdate'])) { extract($_POST); $startdate = datef($startdate, "Y-m-d"); $enddate = datef($enddate, "Y-m-d"); $report_select = $users->get_report_contractor($startdate, $enddate); ?> <h4><?php echo (isset($_POST['generate']) ? "Select another contractor" : "...and now select a contractor"); ?></h4> <select name='contractorid'> <?php foreach ($report_select as $report_option) { echo "<option value='" . $report_option['contractor_id'] . "'>" . $report_option['contractor_name'] . " " . $report_option['contractor_last'] . "</option>"; } //$report_select as $report_option ?> </select> <input type='hidden' name='startdate' value='<?php echo $startdate; ?>'> <input type='hidden' name='enddate' value='<?php echo $enddate; ?>'> <button type='submit' class='btn btn-vlb' name='generate'>Generate Report!</button> </form> <?php } //isset($_POST['startdate']) if (isset($_POST['generate'])) { $records = 1; // for numbering rows $contractor_total_time = 0; $contractor_total_up_time = 0; $contractor_total_fee = 0; $contractor_total_up_fee = 0; $rowdata = ''; // get distinct job dates with contractor job time entries in the selected time range $report_dates = $users->get_job_dates($startdate, $enddate); foreach ($report_dates as $report_date_group) { $date_total_time = 0; $date_total_up_time = 0; $contractor_date_fee = 0; $contractor_date_up_fee = 0; $job_total = 0; // get jobs serviced by the selected contractor for each date found $report_group = $users->get_jobs($contractorid, $report_date_group['job_date']); if ($report_group) { foreach ($report_group as $report_group_row) { $contractor_name = $report_group_row['contractor_name'] . " " . $report_group_row['contractor_last'] . " (" . $report_group_row['job_lang'] . ")"; $job_up_time = ''; $job_date = datef($report_group_row['job_date'], "m/d/Y"); $job_start = datef($report_group_row['job_start'], "g:i a"); $job_end = datef($report_group_row['job_end'], "g:i a"); $job_time = decimals($report_group_row['job_time']); $job_up_time = ($job_time < 2) ? 2 : $job_time; // two-hour minimum $job_rate = $report_group_row['contractor_rate']; $job_total = $job_time * $job_rate; $job_up_total = $job_up_time * $job_rate; // add up totals $contractor_total_fee += $job_total; $contractor_date_fee += $job_total; $contractor_total_up_fee += $job_up_total; $contractor_date_up_fee += $job_up_total; $contractor_total_time += $job_time; $date_total_time += $job_time; $date_total_up_time += $job_up_time; $contractor_total_up_time += $job_up_time; $rowdata .= "<tr> <td>" . $records . "</td> <td>" . $report_group_row['job_num'] . "</td> <td>" . substr($report_group_row['job_facility'], 0, 6) . "</td> <td>" . $report_group_row['job_dept'] . "</td> <td>" . substr($report_group_row['job_procedure'], 0, 10) . "</td> <td>" . $job_date . "</td> <td style='text-align:right;'>" . $job_start . "</td> <td style='text-align:right;'>" . $job_end . "</td> <td style='text-align:right;'>" . number_format($job_time, 2, '.', '') . "</td> <td style='text-align:right;'>" . cash($job_total) . "</td> <td style='text-align:right;'>" . ($job_time < 2 ? "<span style='color:blue'><em>" . number_format($job_up_time, 2, '.', '') . "</em></span>" : number_format($job_up_time, 2, '.', '')) . "</td> <td style='text-align:right;'>" . ($job_time < 2 ? "<span style='color:blue'><em>" . cash($job_up_total) . "</em></span>" : cash($job_up_total)) . "</td> </tr>"; $records++; } //$report_group as $report_group_row $rowdata .= "<tr> <td colspan=9 style='text-align:right;color:green;'><em>Date total</em></td> <td style='text-align:right;color:green;'><em>" . number_format($date_total_time, 2, '.', '') . "</em></td> <td style='text-align:right;color:green;'><em>" . cash($contractor_date_fee) . "</em></td> <td style='text-align:right;color:green;'><em>" . number_format($date_total_up_time, 2, '.', '') . "</em></td> <td style='text-align:right;color:green;'><em>" . cash($contractor_date_up_fee) . "</em></td> </tr>"; } //$report_group } //$report_dates as $report_date_group ?> <h4>Job report for <?php echo $contractor_name; ?></h4> <table class="table-bordered table-condensed" style="font-size: 12px; line-height:12px;"> <thead class="cf"> <tr> <th>#</th> <th>Job#</th> <th>Facility</th> <th>Dept</th> <th>Procedure</th> <th>Job Date</th> <th>Start</th> <th>End</th> <th>Time</th> <th>Total</th> <th>Up-time</th> <th>Total Up</th> </tr> </thead> <tbody> <?php echo $rowdata; ?> </tbody> <tfoot> <tr> <td colspan=10 style='text-align:right;'><strong>Total for <?php echo $contractor_name; ?></strong></td> <td style='text-align:right;'><strong><?php echo number_format($contractor_total_time, 2, '.', ''); ?></strong></td> <td style='text-align:right;'><strong><?php echo cash($contractor_total_fee); ?></strong></td> <td style='text-align:right;'><strong><?php echo number_format($contractor_total_up_time, 2, '.', ''); ?></strong></td> <td style='text-align:right;'><strong><?php echo cash($contractor_total_up_fee); ?></strong></td> </tr> </tfoot> </table> <?php } //isset($_POST['generate']) ?> </div> <?php include('assets/inc/footer.inc'); ?> </div> </section> </body> </html>
-
They are the PDO queries, which I summarily referenced in the code comments above: public function get_job_dates($start, $end) { $query = $this->db->prepare("SELECT DISTINCT job_date FROM jobs WHERE job_date BETWEEN ? AND ? ORDER BY job_date ASC"); $query->bindValue(1, $start); $query->bindValue(2, $end); try { $query->execute(); return $query->fetchAll(); } catch (PDOException $e) { die($e->getMessage()); } } public function get_jobs($contractor, $date) { $query = $this->db->prepare("SELECT *, TIMEDIFF(job_end, job_start) AS job_time, users.first_name AS int_name, users.last_name AS int_last, users.rate AS int_rate, FROM jobs LEFT JOIN users ON jobs.contractor_id = users.id WHERE job_date = ? AND contractor_id = ? AND entry = 1 ORDER BY job_date ASC, job_start ASC"); $query->bindValue(1, $date); $query->bindValue(2, $interp); try { $query->execute(); } catch (PDOException $e) { die($e->getMessage()); } return $query->fetchAll(); }
-
I have the following code to create a report of contractor times: I basically displays jobs serviced by the contractors during a specific time range. <?php // fetch all job_dates within the specified date range ($startdate - $enddate) // select distinct job_date from jobs where job_start between ? and ? order by job_date $rep_dates = $users->get_job_dates($startdate, $enddate); foreach ($rep_dates as $rep_date_group) { // fetch all contractor jobs for each job_date in the date range // select * from jobs where contractor_id = ? and job_date = ? order by job_start $rep_group = $users->get_jobs($contractor_id, $rep_date_group['job_date']); if ($rep_group) { foreach ($rep_group as $rep_group_row) { # process results here } } } The code works and the results are properly processed. All database queries are made using prepared statements with PDO. Is there are more efficient way to do this? Thanks for your help!
-
Need help in starting to structure code for a specific problem
walermo replied to walermo's topic in PHP Coding Help
Thank you very much, Ignace! I will explore it and see how I can make it work with what I have now. I will certainly let you know how it goes. -
Need help in starting to structure code for a specific problem
walermo replied to walermo's topic in PHP Coding Help
Thanks Jessica! I'm stuck on turning my logic into code. That's it. Not looking for someone to actually code it for me. Just looking for help in figuring out how to structure my logic into code. Thanks again! -
I have put together a simple billing system for my contractors. They are assigned specific jobs at a specific site. Each job is unique with respect to my clients, but my contractors get paid an hourly rate with a minimum of 2 hours per site visit. The problem I am facing is trying to come up with a way to calculate their per site visit total times, when each visit may include several jobs, in order to calculate their total hours. Here is a sample of what I am trying to accomplish: On a specific site visit, if a contractor works more than one job and the idle time between jobs <= 30 minutes, all jobs make up a single block of job time. If the idle time between jobs > 30 minutes, then the job(s) on either side of that idle time become independent blocks of job time. # Job# Start End diff thisjobtime thisblocktime + difference + thisjobtime = thisblocktime 1 130516-03 10:00 am 11:00 am 0 1.00 0 + 0 + 1.00 = 1.00 2 130516-12 11:00 am 12:30 pm 0 1.50 1.00 + 0 + 1.50 = 2.50 3 130516-08 12:30 pm 2:00 pm 0 1.50 2.50 + 0 + 1.50 = 4.00 4 130516-10 2:00 pm 3:30 pm 0 1.50 4.00 + 0 + 1.50 = 5.50 5 130516-11 3:30 pm 5:30 pm 0 2.00 5.50 + 0 + 2.00 = 7.50 6 130517-01 7:30 am 8:00 am 0 0.50 0 + 0 + 0.50 = 0.50 7 130517-02 8:00 am 10:00 am 0 2.00 0.50 + 0 + 2.00 = 2.50 8 130517-09 12:30 pm 1:30 pm > 30 1.00 0 + 0 + 1.00 = 1.00 9 130517-11 1:30 pm 3:30 pm 0 2.00 1.00 + 0 + 2.00 = 3.00 10 130520-02 7:30 am 8:30 am 0 1.00 0 + 0 + 1.00 = 1.00 11 130520-04 9:00 am 10:00 am 30 1.00 1.00 + 0.50 + 1.00 = 2.50 12 130520-07 10:00 am 11:00 am 0 1.00 2.50 + 0 + 1.00 = 3.50 13 130520-11 11:15 am 1:15 pm 15 2.00 3.50 + 0.25 + 2.00 = 5.75 14 130520-13 2:00 pm 3:15 pm > 30 2.00 0 + 0 + 1.25 = 2.00 So my contractors record their time entries on my system. By default, when I query their job entry data, I do the following on a per row basis: $rql = "SELECT jobs.*, contractors.rate AS contractor_rate, TIMEDIFF(jobs.job_end,jobs.job_start) AS job_time, FROM jobs LEFT JOIN contractors ON jobs.contractor_id = contractors.id"; $report = $mysqli->query($rql); while($row = $report->fetch_assoc()) { $int_rate = $row['contractor_rate']; $job_time = decimalHours($row['job_time']); $job_bill_time = decimalHours(2); if($job_time > 2){ $job_bill_time = $job_time; } $job_pay = $job_bill_time * $int_rate; What this does is simply show that if the contractor worked less than two hours for a given job, he gest paid the 2-hour minimum. But this does not help me calculate time worked during specific blocks of time. My attempts at writing code to calculate this when displaying the table got me nowhere, if only because it would be inefficient to perform this calculation on every query. Next, I thought it would be perhaps simpler and certainly more efficient if the calculation is made when the contractors register their time. But that presented another problem: Every time the contractor records their time for a given job, I would have to look for previous job entries, calculate the difference between the previous job entry end time and the new job entry start time, and then record the time difference in a specific field (i.e.: job_time) if the new job entry is part of an existing time block or, because the difference between the previous job time is > 30 minutes, as a new time block (with the corresponding 2-hour minimum). This would work nicely if contractors recorded their time entries in chronological order (and would certainly be feasible with my current coding skills), but this is not always the case. So for every job entry, I would have to look for (and potentially modify) previous job entries to make sure that consecutive job times are assigned to a single time block (also stumped as to how exactly I can accomplish this). This is my logic: Get all of a contractor's jobs for a given day. Compare the start times of a current job with the end time of a previous job (chronological, not job #) and get the difference. If the difference is <= 30 minutes, then the job block time = the sum of each of the job times and the difference. Record each the job times in a given time block as real time instead of the 2-hour minimum. If the difference > 30 minutes, the current job is in a time block different than the previous job's time block. Rinse and repeat. So this is where I am stuck. Any help pointing me in the right direction would be very much appreciated! Thanks!
-
Newbie help - data not displaying when variables are passed
walermo replied to walermo's topic in PHP Coding Help
Thank you I will try that! Now, syntax aside, the problem is that even if I pass ?pg=2, $pg stays at 1 all the time. So when I click the 'Page 2' link, only Page 1 data will show. I tried replacing <? to <?php, as you suggested, but the problem persists. I will read up on register globals to see if I can make some sense out of it. Cheers! -
Hello guys! I have tried to search these forums for a similar problem, to no avail. I am quite new to php and I have encountered a problem that has me stumped. I am coding a simple site with several pages that can be displayed in one of two languages. The problem is that any variable I pass to display specific content is apparently ignored. I have structured my files thus: index.php - main display structure, includes other files _css/ - my css folder _inc/ - my includes folder (head.php, header.php, main.php, footer.php, variables.php) This is a sample of my code: index.php <? if ($id=="") {$id=1;} if ($pg=="") {$pg=1;} if ($l=="") {$l= en;} include("_inc/variables.php"); include("_inc/head.php"); ?> <body> <div id="wrapper"> <!-- Header and Navigation --> <? include("_inc/header.php"); ?> <div style="background: url(<? echo $img_path; ?>bg_main.jpg) repeat-y;"> <!-- Main --> <? include("_inc/main.php"); ?> </div> <!-- Footer --> <? include("_inc/footer.php"); ?> </div> </body> </html> variables.php <? if($l=='sp') { /* $l = language */ /* Buttons */ $submit_btn ='Enviar'; $cancel_btn ='Cancelar'; $reset_btn ='Borrar'; } if($l=='en') { /* Buttons */ $submit_btn ='Submit'; $cancel_btn ='Cancel'; $reset_btn ='Reset'; } ?> header.php <div id="header"> <a href="index.php?pg=2&l=<? echo $l; ?>" title="Page 2">Page 2</a> <a href="index.php?pg=3&l=<? echo $l; ?>" title="Page 3">Page 3</a> <a href="index.php?pg=4&l=<? echo $l; ?>" title="Page 4">Page 4</a> </div> main.php <? if ($pg=='1'){ ?> <h1>This is page<? echo $pg; ?>.</h1> <? } ?> <? if ($pg=='2'){ ?> <h1>This is page<? echo $pg; ?>.</h1> <? } ?> <? if ($pg=='3'){ ?> <h1>This is page<? echo $pg; ?>.</h1> <? } ?> With the above code, if I click on a link that send me to page 2 and in English, the link on the address bar reads ".../index.php?pg=2&l=en." However, main.php does not display the data for page 2, it sticks to $pg=1. What am I doing wrong? Thanks in advance for your help!