Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 12/01/2019 in all areas

  1. Christmas has come early! <?php const IMGDIR = 'images/'; const THUMBDIR = 'thumbs/'; const THUMBSIZE = 150; // max thumbnail dimension const NUM = 100; // number of images to be processed on each run $images = glob(IMGDIR.'{*.png,*.jpg}', GLOB_BRACE); $thumbs = glob(THUMBDIR.'{*.png,*.jpg}', GLOB_BRACE); // reduce to basenames only $images = array_map('basename', $images); $thumbs = array_map('basename', $thumbs); // copy the next NUM images to $todo list where thumbnails do not yet exist $todo = array_slice(array_diff($images, $thumbs), 0, NUM); $output = ''; foreach ($todo as $fn) { $sz = getimagesize(IMGDIR.$fn); if ($sz[0] == 0) continue; // not an image $ok = 0; $out = null; switch ($sz['mime']) { // check the mime types case 'image/jpeg': $im = imagecreatefromjpeg(IMGDIR.$fn); $ok = $im; $out = 'imagejpeg'; break; case 'image/png': $im = imagecreatefrompng(IMGDIR.$fn); $ok = $im; $out = 'imagepng'; break; default: $ok = 0; } if (!$ok) continue; // not png or jpg // calculate thumbnail dimensions if ($sz[0] >= $sz[1]) { // landscape $w = THUMBSIZE; $h = THUMBSIZE * $sz[1]/$sz[0]; } else { // portrait $h = THUMBSIZE; $w = THUMBSIZE * $sz[0]/$sz[1]; } // copy and resize the image $tim = imagecreatetruecolor(THUMBSIZE, THUMBSIZE); $bg = imagecolorallocatealpha($tim,0xFF,0xFF,0xFF,127); imagefill($tim, 0, 0, $bg); imagecolortransparent($tim, $bg); // centre the image in the 150 pixel square $dx = (THUMBSIZE - $w) / 2; $dy = (THUMBSIZE - $h) / 2; imagecopyresized($tim, $im, $dx, $dy, 0, 0, $w, $h, $sz[0], $sz[1]); imagesavealpha($tim, true); $out($tim, THUMBDIR.$fn); imagedestroy($im); imagedestroy($tim); $output .= "<img src='".THUMBDIR."$fn' alt='$fn'>\n"; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Create Thumbnails</title> <meta name="author" content="Barry Andrew"> <meta name="creation-date" content="10/09/2019"> <style type="text/css"> body { font-family: verdana, sans-serif; font-size: 11pt; } header { background-color: black; color: white; padding: 15px 10px;} img { margin: 5px; } </style> </head> <body> <header> <h1>New Thumbnail Images</h1> </header> <?=$output?> </body> </html>
    2 points
  2. This example uses glob() to get all .png and .jpg in a folder. By default, the folder is assumed to be named "images" and is a subdirectory of the folder containing the script. Images are displayed as thumbnails, 5 in each row with 25 per page. <?php session_start(); const IMGDIR = 'images/'; const PERPAGE = 25; $page = $_GET['page'] ?? 1; $imgdir = $_GET['dir'] ?? IMGDIR; if (!isset($_SESSION['imgdir']) || $_SESSION['imgdir'] != $imgdir) { unset($_SESSION['images']); $_SESSION['imgdir'] = $imgdir; $page = 1; } if (!isset($_SESSION['images'])) { $_SESSION['images'] = glob($imgdir.'{*.png,*.jpg}', GLOB_BRACE); // get .jpg and .png images } $total = count($_SESSION['images']); /** ************************************************************************************** * display paginated images from SESSION['images] * * @param int $page * @param int $perpage */ function displayImages($page, $perpage) { $start = ($page - 1) * $perpage; $ilist = array_slice($_SESSION['images'], $start, $perpage); foreach ($ilist as $i) { $n = trim(basename($i)); list($iw, $ih,, $sz) = getimagesize($i); if ($iw >= $ih) { // landscape $w = 150; $h = 150 * $ih/$iw; } else { // portrait $h = 150; $w = 150 * $iw/$ih; } $alt = substr($n, 0, 15); echo " <div class='image'> <img src='$i' height='$h' width = '$w' alt='$alt'> </div> "; } echo "<div style='clear:both'></div>"; } /** ************************************************************************************ * function to output page selection buttons * * @param int $total total records * @param int $page current page number * @return string selection buttons html */ function page_selector($total, $page) { if ($total==0) { return ''; } $kPages = ceil($total/PERPAGE); $filler = '&nbsp;&middot;&nbsp;&middot;&nbsp;&middot;&nbsp;'; $lim1 = max(1, $page-2); $lim2 = min($kPages, $page+3); $p = $page==1 ? 1 : $page - 1; $n = $page== $kPages ? $kPages : $page + 1;; $out = "$kPages page" . ($kPages==1 ? '' : 's') . " &emsp;"; if ($kPages==1) { return $out; } $out .= ($page > 1) ? "<div class='pagipage' data-pn='$p'>Prev</div>&ensp;" : "<div class='pagipage x' data-pn='$p' disabled>Prev</div>&ensp;"; if ($page > 4) { $out .= "<div class='pagipage' data-pn='1'>1</div> $filler"; } elseif ($page==4) { $out .= "<div class='pagipage' data-pn='1'>1</div>"; } for ($i=$lim1; $i<=$lim2; $i++) { if ($page==$i) $out .= "<div class='pagicurrent'>$i</div>"; else $out .= "<div class='pagipage' data-pn='$i'>$i</div>"; } if ($page < $kPages-3) { $out .= "$filler <div class='pagipage' data-pn='$kPages'>$kPages</div>"; } $out .= $page < $kPages ? "&ensp;<div class='pagipage' data-pn='$n'>Next</div>" : "&ensp;<div class='pagipage x' data-pn='$n' disabled>Next</div>"; return $out; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="author" content="B A Andrew"> <meta name="creation-date" content="11/29/2019"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <title>Example</title> <script type="text/javascript"> $().ready( function() { $(".pagipage").click( function() { $("#page").val( $(this).data("pn") ) $("#form1").submit() }) }) </script> <style type="text/css"> body { font-family: verdana, sans-serif; font-size: 11pt; } label { display: inline-block; width: 150px; font-weight: 600; } #image_wrapper { margin: 30px; } .image { width: 18%; min-height: 200px; margin: 10px; float: left; text-align: center; padding: auto;} /* pagination styles */ .pagipage { display: inline; width: 25px; height: 15px; padding: 3px 5px; text-align: center; font-size: 9pt; border: 1px solid #BB9A21 ; color: #BB9A21; background-color: #FFF; cursor: pointer; margin-left: -1px; } .pagipage.x { background-color: #CCC;} .pagipage:hover { background-color: #BB9A21; border-color: #F0F; color: white; } .pagicurrent { display: inline; width: 25px; height: 15px; text-align: center; font-size: 9pt; font-weight: 600; border: 1px solid #BB9A21; background-color: #BB9A21; color: white; padding: 3px 5px; } .paginate_panel { text-align: center; margin: 20px 0; width: 100%; color: #BB9A21; } </style> </head> <body> <header> <h1>Example Image List</h1> </header> <form id="form1"> <fieldset> <label>Image Folder</label> <input type="text" name="dir" value="<?=$imgdir?>" size="80"> <input type="hidden" name="page" id="page" value="<?=$page?>"> <br> <label>&nbsp;</label> <input type="submit" name="btnSubmit" value="Submit"> </fieldset> </form> <div class='paginate_panel'> <?=page_selector($total, $page, PERPAGE)?> </div> <div id="image_wrapper"> <?=displayImages($page, PERPAGE)?> </div> <div class='paginate_panel'> <?=page_selector($total, $page, PERPAGE)?> </div> </body> </html>
    2 points
  3. You could've had a database set up in less time than you spent moaning about what a PITA it is. It takes about half a dozen statements to create an image table from your image directory. JFDI. const IMGDIR = 'images/'; $db->exec("CREATE TABLE IF NOT EXISTS `image_lib` ( `img_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, `path` varchar(255) DEFAULT NULL, `width` int(11) DEFAULT NULL, `height` int(11) DEFAULT NULL, `mime_type` varchar(20) DEFAULT NULL, `description` varchar(255) DEFAULT NULL, `img_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`img_id`), FULLTEXT KEY `idx_image_lib_description` (`description`) ) "); $ins = $db->prepare("INSERT INTO image_lib (name, path, width, height, mime_type) VALUES (?,?,?,?,?)") ; $imgs = glob(IMGDIR.'{*.png,*.jpg}', GLOB_BRACE); foreach ($imgs as $i) { $s = getimagesize($i); $ins->execute( [ basename($i), IMGDIR, $s[0], $s[1], $s['mime'] ]); } Job done!
    1 point
  4. Or you could take the view that the product is a timeslot. If they want part of one and part of the next then they buy both. Check which slots contain or over lap the required times as this example does (see line 40) ... <?php $business_hours = [ 1 => [ 'am_from' => '10:00', 'am_to' => '12:00', 'pm_from' => '13:00', 'pm_to' => '19:00' ], 2 => [ 'am_from' => '08:00', 'am_to' => '12:00', 'pm_from' => '13:00', 'pm_to' => '17:00' ], 3 => [ 'am_from' => '12:00', 'am_to' => '12:00', 'pm_from' => '13:00', 'pm_to' => '19:00' ], 4 => [ 'am_from' => '08:00', 'am_to' => '12:00', 'pm_from' => '13:00', 'pm_to' => '17:00' ], 5 => [ 'am_from' => '08:00', 'am_to' => '12:00', 'pm_from' => '13:00', 'pm_to' => '13:00' ], ]; //------------------------------------------------------------ // from above array, generate daily 30 minute slots // for the next 15 days // $slots = []; $dt1 = new DateTime(); $intwd = DateInterval::createFromDateString('next weekday'); $dp_days = new DatePeriod($dt1, $intwd, 14); foreach ($dp_days as $d) { $dno = $d->format('w'); $times = $business_hours[$dno]; $slots[$d->format('Y-m-d')] = generateSlots($times); } //------------------------------------------------------------ // place these bookings in their slots // $bookings = [ ['title' => 'Test 1', 'start' => '2019-11-26 10:15', 'end' => '2019-11-26 11:30'], ['title' => 'Test 2', 'start' => '2019-11-29 11:00', 'end' => '2019-11-29 12:00'], ['title' => 'Test 3', 'start' => '2019-12-02 15:30', 'end' => '2019-12-02 16:30'] ]; foreach ($bookings as $b) { $sd = new DateTime($b['start']); $ed = new DateTime($b['end']); $date = $sd->format('Y-m-d'); $st = $sd->format('H:i'); $et = $ed->format('H:i'); foreach ($slots[$date] as &$s) { if ($s['t'] != '') continue; // not available if ($et > $s['s'] && $st < $s['e']) { // if booking is in or overlaps slot ( LINE 40 ) $s['t'] = $b['title']; // place title into slot } } } //------------------------------------------------------------ // output the slots to the page // $tdata = ''; foreach ($slots as $d => $ddata) { if ((new DateTime($d))->format('w')==1) { $tdata .= "<tr><td colspan='12'> </td></tr>\n"; } $tdata .= "<tr><td>" . date('D d M', strtotime($d)) . '</td>'; foreach ($ddata as $slt) { switch ($slt['t']) { case 'closed': $cls = 'class="closed"'; $txt = ''; break; case '': $cls = ''; $txt = ''; break; default: $cls = 'class="booked"'; $txt = $slt['t']; } $tdata .= "<td $cls>$txt</td>"; } $tdata .= "</tr>\n"; } /** * generate daily timeslots * * @param array $times - array of business hours for the day */ function generateSlots($times) { $stime = ''; $dt1 = new DateTime('08:00:00'); $dt2 = new DateTime('19:00:00'); $di = new DateInterval('PT1H'); $dp = new DatePeriod($dt1, $di, $dt2); $slots = []; foreach ($dp as $t) { $k = $t->format('G'); $h1 = $t->format('H:i'); $h2 = $t->modify('+1 hour')->format('H:i'); $text = ($h1 >= $times['am_from']) && ($h1 < $times['am_to']) || ($h1 >= $times['pm_from']) && ($h1 < $times['pm_to']) ? '' : 'closed' ; $slots[$k] = [ 's'=>$h1, 'e'=>$h2, 't'=>$text]; } return $slots; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example Bookings</title> <style type="text/css"> body, table { font-family: calibri, sans-serif; font-size: 11pt; } table { border-collapse: collapse; width: 90%; margin: 0 auto; } th { padding: 4px 2px; background-color: #006EFC; color: #FFF; border-color: #FFF;} td { background-color: #FFF; padding: 2px; } td.closed { background-color: #888; color: #FFF; } td.booked { background-color: #FFC0C0; } </style> </head> <body> <header> <h1>Example Bookings</h1> </header> <table border='1'> <tr><th rowspan='2'>Date</th><th colspan='4'>AM</th><th></th><th colspan='6'>PM</th></tr> <tr><th>8 – 9</th><th>9 – 10</th><th>10 – 11</th><th>11 – 12</th> <th>12 – 1</th><th>1 – 2</th><th>2 – 3</th><th>3 – 4</th><th>4 – 5</th><th>5 – 6</th><th>6 – 7</th></tr> <?=$tdata?> </table> </body> </html>
    1 point
  5. Yes but you don't want to run both at the same time. If you really wanted to, you would need to change the Apache port on one of them as they both use port 80
    1 point
  6. Use ids, not names, to link records. TABLE: user +----+----------+------------+ | id | username | leader_id | +----+----------+------------+ | 1 | fred | 2 | | 2 | mo | NULL | | 3 | brian | 2 | | 4 | john | 2 | | 5 | peter | 1 | | 6 | curly | 1 | | 7 | joan | 1 | | 8 | Dennis | 6 | +----+----------+------------+ Recursion is your friend here. $res = $db->query("SELECT id , username , leader_id FROM usertest order by leader_id "); $users = []; // store users in array for each leader foreach ($res as $r) { $users[$r['leader_id']][] = [ 'id' => $r['id'], 'username' => $r['username'] ]; } echo '<pre>'; listStaff(null, $users, 0); // list staff for leader "null" echo '</pre>'; /** * recursive function to list staff * * @param int $id * @param array $users * @param int $level */ function listStaff($id, &$users, $level=0) { $indent = str_repeat("\t", $level); foreach ($users[$id] as $u) { // for each of their staff echo "$indent{$u['username']}<br>"; // outout the name if (isset($users[$u['id']])) { // if they have staff listStaff($u['id'], $users, $level+1); // list their staff } } } Giving mo fred peter curly Dennis joan brian john
    1 point
  7. I believe I may have it. $originalstart = date("Y-m-d"); $originalstart = date("Y-m-d",strtotime('-24 months', strtotime($originalstart))); for( $i= 0 ; $i <= 25 ; $i++ ) { $start = date('Y-m-01',strtotime('+'."$i".' month', strtotime($originalstart))); $firstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month', strtotime($start))))); $lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month', strtotime($start))))); echo "$firstday - $lastday<br />"; } 2017-10-01 - 2017-10-28 2017-10-29 - 2017-11-25 2017-11-26 - 2017-12-30 2017-12-31 - 2018-01-27 2018-01-28 - 2018-02-24 2018-02-25 - 2018-03-31 2018-04-01 - 2018-04-28 2018-04-29 - 2018-05-26 2018-05-27 - 2018-06-30 2018-07-01 - 2018-07-28 2018-07-29 - 2018-09-01 2018-09-02 - 2018-09-29 2018-09-30 - 2018-10-27 2018-10-28 - 2018-12-01 2018-12-02 - 2018-12-29 2018-12-30 - 2019-01-26 2019-01-27 - 2019-02-23 2019-02-24 - 2019-03-30 2019-03-31 - 2019-04-27 2019-04-28 - 2019-06-01 2019-06-02 - 2019-06-29 2019-06-30 - 2019-07-27 2019-07-28 - 2019-08-31 2019-09-01 - 2019-09-28 2019-09-29 - 2019-10-26 2019-10-27 - 2019-11-30
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.