Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Yes (except fot the typo) function getClientName($pdo, $display) { .... }
  2. PS Also, require is not a function The $pdo variable is created inside the code within your page scope.
  3. You need to pass $pdo as a paramter to your function $myselect = getClientNames($pdo, 'select');
  4. The most common one, I think, is FPDF Another one, which is an enhanced version of that one, is TCPDF but I find the documentation a nightmare (the source file itself seems the only place). There are lots of example but it is difficult to know what the arguments for the methods are without a decent reference manual. For example // Image example with resizing $pdf->Image('images/image_demo.jpg', 15, 140, 75, 113, 'JPG', 'http://www.tcpdf.org', '', true, 150, '', false, false, 1, false, false, false); You then have to dig into the source file to find /** * Puts an image in the page. * The upper-left corner must be given. * The dimensions can be specified in different ways:<ul> * <li>explicit width and height (expressed in user unit)</li> * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li> * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul> * Supported formats are JPEG and PNG images whitout GD library and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM; * The format can be specified explicitly or inferred from the file extension.<br /> * It is possible to put a link on the image.<br /> * Remark: if an image is used several times, only one copy will be embedded in the file.<br /> * @param $file (string) Name of the file containing the image or a '@' character followed by the image data string. To link an image without embedding it on the document, set an asterisk character before the URL (i.e.: '*http://www.example.com/image.jpg'). * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL). * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL). * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated. * @param $type (string) Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension. * @param $link (mixed) URL or identifier returned by AddLink(). * @param $align (string) Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul> * @param $resize (mixed) If true resize (reduce) the image to fit $w and $h (requires GD or ImageMagick library); if false do not resize; if 2 force resize in all cases (upscaling and downscaling). * @param $dpi (int) dot-per-inch resolution used on resize * @param $palign (string) Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul> * @param $ismask (boolean) true if this image is a mask, false otherwise * @param $imgmask (mixed) image object returned by this function or false * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0))) * @param $fitbox (mixed) If not false scale image dimensions proportionally to fit within the ($w, $h) box. $fitbox can be true or a 2 characters string indicating the image alignment inside the box. The first character indicate the horizontal alignment (L = left, C = center, R = right) the second character indicate the vertical algnment (T = top, M = middle, B = bottom). * @param $hidden (boolean) If true do not display the image. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned). * @param $altimgs (array) Array of alternate images IDs. Each alternative image must be an array with two values: an integer representing the image ID (the value returned by the Image method) and a boolean value to indicate if the image is the default for printing. * @return image information * @public * @since 1.1 */ public function Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false, $alt=false, $altimgs=array()) { At least FPDF has a manual. Luckily that same manual applies largely to TCPDF - although not the extras that TCPDF has. The most useful addition is the tcpdf::WriteHTML($html_string) method which renders html markup as PDF. I haven't used that facility much so I'm not sure how well it uses CSS. Other improvements are better rendering of non-latin text (eg Arabic) and the ability to embed SVG graphics.
  5. @AndyJones If you compare the above 2 lines you will see that my PDO connection is $pdo whereas yours is $modx. Just change my $pdo to $modx to use your connection.
  6. RTFM https://php.net/array_sum https://php.net/array_column https://php.net/printf
  7. @Revolutsio - there were 12 posts (wasted time) in this thread before you got around to posting the code you were having a problem with - do better in future!
  8. Strange! $pdo->exec("create temporary table games (game varchar(50))"); $pdo->exec("insert into games (game) values ('university challenge'), ('manchester university zombie apocalypse'), ('manchester scenic tours'), ('call of duty - manchester university edition') "); $search = "manchester_university"; // [EDIT] $search = "manchester university"; GIVES SAME RESULTS as // "manchester_university $res = $pdo->prepare("select game from games where game like ? "); $res->execute([ "%{$search}%" ]); foreach ($res as $row) { echo $row['game'] . '<br>'; } outputs manchester university zombie apocalypse call of duty - manchester university edition I would expect that result if you were using something like <a href="search.php?search=manchester university">Manchester University</a> but not from a form field
  9. Try with $search = "manchester_university";
  10. Can we see the code for your search form?
  11. I get $z = "5123hg5" so there has to be something else going on. Show us your db update code your table structure definition
  12. You were told what was wrong 12 days ago - read your reply.
  13. or... $res = $pdo->query("SELECT `option`, total FROM vote"); $data = $res->fetchAll(); $votes_cast = array_sum( array_column($data, 'total') ); foreach ($data as $r) { printf ("%s has %d votes (%0.1f %%)<br>", $r['option'], $r['total'], $r['total']*100/$votes_cast); }
  14. You don't need dayofweek in attendance table for the same reason you don't need month and year columns. They are derived from the entry date so you are just duplcating data and creating unnecessary columns. mysql> SELECT YEAR('2022-06-09') as year -> , MONTH('2022-06-09') as month -> , DAYOFWEEK('2022-06-09') as dow; +------+-------+------+ | year | month | dow | +------+-------+------+ | 2022 | 6 | 5 | +------+-------+------+ Care to share?
  15. Do you really mean 2 and 3, or do you mean 2 or 3 ?
  16. What have you tried so far?
  17. I don't see how they can be. They may not be right but they must be different. You stated that WS occured on Saturday mornings, so why would there be more than 1 per week? (each serid only appeared on one date in the test data you provided) You came here with a problem and I helped you. Now a shedload of never before mentioned requirements and conditions suddenly appear. How many more will creep out of the woodwork? I don't like "mission creep" . As well as only accepting live services (aforementioned where condition) you could also specify the day of week for each service +-------+----------------+--------------+-----------+ | serid | service_name | service_code | dayofweek | 1=Sun,..,7=Sat +-------+----------------+--------------+-----------+ | 1 | Prayer meeting | PM | 4 | | 2 | Worship | WS | 7 | | 3 | Thanks giving | PBB | 7 | | 4 | LS | LS | NULL | unspecified | 5 | Thanks giving | D1 | NULL | | 6 | Thanks giving | D2 | NULL | | 7 | Thanks giving | D3 | NULL | +-------+----------------+--------------+-----------+ and only process dates/services that match those specified days. That will limit them to one per week. The two queries then become -- -- headers query -- SELECT DISTINCT weekofyear(entrydate) - weekofyear(?) + 1 as wk_no , date_format(entrydate, '%e/%c') as edate , concat(date_format(entrydate, '%d'), ' ', service_code) as dayser , service_code FROM attendance a JOIN service s ON a.serid = s.serid AND (dayofweek(a.entrydate) = s.dayofweek OR s.dayofweek IS NULL) WHERE month(entrydate) = ? AND stype = 1 ORDER BY entrydate, a.serid; -- -- member attendance query -- SELECT weekofyear(entrydate) - weekofyear(?) + 1 as wk_no , concat(date_format(entrydate, '%d'), ' ', service_code) as dayser , group_no , mem_name , memid FROM attendance a JOIN member m USING (memid) JOIN service s ON a.serid = s.serid AND (dayofweek(a.entrydate) = s.dayofweek OR s.dayofweek IS NULL) WHERE month(entrydate) = ? AND stype = 1 ORDER BY group_no, mem_name, dayser
  18. Can't you just add AND stype = 1 to the WHERE clauses in the two queries?
  19. Diid you try switching off then on again 😀
  20. As your CMS is using mysqli there isn't much you can do at that end (unless you want to rewrite it using PDO instead). You need to ensure that whatever version of MySql you use is implemented with the native driver (mysqlndnd) [PS] https://dev.mysql.com/downloads/connector/php-mysqlnd/
  21. mysqli::get_result() is not available on all implementations Another reason to ditch mysqli for PDO.
  22. I've been trawling my archive and found a sample pagination script from a few years ago. Outputs... Code (There is code at the end of the script to create the test data if required) <?php /* PDO CONNECTION *********************************************/ $host = 'localhost'; $username = '????'; $password = '????'; $database = 'jointute'; $dsn = "mysql:dbname=$database; host=$host; charset=utf8"; $db = new pdo($dsn, $username, $password, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); /***************************************************************/ const PERPAGE = 2; $res = $db->query("SELECT COUNT(*) FROM pupil "); $total = $res->fetchColumn(); $page = $_GET['page'] ?? 1; $stmt = $db->prepare("SELECT fname , lname , DATE_FORMAT(dob, '%b %D') as birthday FROM pupil ORDER BY MONTH(dob), DAY(dob) LIMIT ?,? "); $stmt->execute( [ ($page-1) * PERPAGE, PERPAGE ]); $output = ''; foreach ($stmt as $rec) { $output .= "<div class='pupil'> <div class='label'>Name:</div> {$rec['fname']} {$rec['lname']}<br> <div class='label'>Birthday:</div> {$rec['birthday']} </div>\n"; } /************************************************************************************** * 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;" : ''; 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>" : ''; return $out; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>Pagination sample</title> <meta name="author" content="Barand"> <meta name="creation-date" content="07/02/2018"> <style type="text/css"> body { font-family: verdana; font-size: 10pt; } #title { font-size: 16pt; background-color: #369; color: #FFF; text-align: center; padding: 10px; margin-bottom: 40px; } .pagipage { display: inline; width: 25px; height: 15px; padding: 3px 5px; text-align: center; font-size: 9pt; border: 1px solid #3C9DBA ; color: #3C9DBA; background-color: #FFF; cursor: pointer; margin-left: -1px; } .pagipage:hover { background-color: #3C9DBA; border-color: #F0F; color: white; } .pagicurrent { display: inline; width: 25px; height: 15px; text-align: center; font-size: 9pt; font-weight: 600; border: 1px solid #3C9DBA; background-color: #3C9DBA; color: white; padding: 3px 5px; } .paginate_panel { text-align: center; margin: 20px 0; width: 100%; color: #3C9DBA; } .pupil { width: 300px; padding: 10px; margin-top: 5px; margin-left: auto; margin-right: auto; border: 1px solid gray; } .label { width: 100px; font-weight: 600; display: inline-block; } </style> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { $(".pagipage").click( function() { $("#page").val( $(this).data("pn") ); $("#form1").submit(); }) }) </script> </head> <body> <div id='title'> Pagination Sample </div> <form id='form1'> <input type="hidden" name="page" id="page" value="0"> </form> <?=$output?> <div class="paginate_panel"> <?=page_selector($total, $page)?> </div> <!-- DATA --- CREATE TABLE `pupil` ( `pupilID` int(10) unsigned NOT NULL AUTO_INCREMENT, `fname` varchar(45) NOT NULL, `lname` varchar(45) NOT NULL, `houseID` int(10) unsigned NOT NULL DEFAULT '1', `classid` char(1) NOT NULL DEFAULT 'A', `dob` date DEFAULT NULL, PRIMARY KEY (`pupilID`), KEY `house` (`houseID`), KEY `idx_pupil_classid` (`classid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `pupil` VALUES (1,'Adam','Simms',1,'A','2001-06-22'), (2,'Allan','Blair',2,'B','2001-03-04'), (3,'Anna','Hamilton',4,'B','2002-01-16'), (4,'Anne','Bailey',3,'D','2001-08-02'), (5,'Anthony','Bell',2,'E','2001-10-01'), (6,'Caroline','Freeman',2,'F','2000-12-13'), (7,'David','Powell',1,'A','2001-05-03'), (8,'Emma','Watson',4,'C','2001-11-20'), (9,'George','Wilson',1,'C','2001-06-30'), (10,'Henry','Irving',4,'D','2001-08-12'), (11,'Jane','Morrison',1,'E','2001-08-24'), (12,'John','Patterson',3,'F','2001-09-06'), (13,'John','Tully',3,'A','2001-09-03'), (14,'John','Watson',2,'B','2001-09-30'), (15,'Jack','Williams',2,'D','2001-09-08'), (16,'Margaret','Norton',4,'D','2001-04-23'), (17,'Mary','Blake',4,'E','2001-10-04'), (18,'Mary','Sheldon',3,'F','2001-06-14'), (19,'Mary','Whitehouse',2,'A','2001-09-06'), (20,'Michael','Grove',3,'B','2001-08-11'), (21,'Peter','Adamson',1,'C','2001-09-18'), (22,'Peter','Appleby',3,'D','2001-04-26'), (23,'Wayne','Jones',1,'E','2001-05-06'), (24,'William','Smith',4,'F','2001-12-08'); --> </body> </html>
  23. It seems to be either a problem with your error reporting or you have a problem between your chair and keyboard. Not only is it inventing errors that aren't errors, it's saying they are in lines of code that don't exist.
  24. Here's my attempt. This the data I used. Some of my column names may differ from yours, so adjust the queries accordingly. attendance member service +-----------+-----------+ +----------+-------------+ +--------------+-------------+ | Field | Type | | Field | Type | | Field | Type | +-----------+-----------+ +----------+-------------+ +--------------+-------------+ | atid | int(11) | | memid | int(11) | | serid | int(11) | | memid | int(11) | | mem_name | varchar(45) | | service_name | varchar(45) | | serid | int(11) | | group_no | int(11) | | service_code | varchar(3) | | entrydate | timestamp | +----------+-------------+ +--------------+-------------+ | createdat | timestamp | | updatedat | timestamp | | viewfrom | int(11) | | astatus | int(11) | | stype | int(11) | +-----------+-----------+ Output Code <?php include 'db_inc.php'; ### CREATE YOUR OWN $pdo = pdoConnect('test'); ### PDO CONNCTION HERE $month = $_GET['month'] ?? date('n')-1; $year = $_GET['year'] ?? date('Y'); $day1 = "{$year}-{$month}-01"; $dt = new DateTime($day1); $monthname = $dt->format('F'); ################################################################################ # # # BUILD REPORT HEADINGS FOR SELECTED MONTH # # # ################################################################################ $res = $pdo->prepare("SELECT DISTINCT weekofyear(entrydate) - weekofyear(?) + 1 as wk_no , date_format(entrydate, '%e/%c') as edate , concat(date_format(entrydate, '%d'), ' ', service_code) as dayser , service_code FROM attendance JOIN service s USING (serid) WHERE month(entrydate) = ? ORDER BY entrydate, serid "); $res->execute([ $day1, $month ]); $rows = $res->fetchAll(); $empty = array_fill_keys(array_column($rows, 'dayser'), [ 'wk'=>0, 'att'=>0 ]); $hdata = []; foreach ($rows as $r) { if (!isset($hdata[$r['wk_no']])) { $hdata[$r['wk_no']] = [ 'dates' => [], 'servs' => [] ]; } $hdata[$r['wk_no']]['dates'][] = $r['edate']; $hdata[$r['wk_no']]['servs'][] = $r['service_code']; $empty[$r['dayser']]['wk'] = $r['wk_no']; } $thead = "<tr><th rowspan='3' class='namecol'>Name</th>"; foreach ($hdata as $w => $wdata) { $cs = count($wdata['dates']); $cls = $w % 2 ? 'oddwk' : ''; $thead .= "<th class='$cls' colspan='$cs'>WEEK $w</th>"; } $thead .= "</tr>\n<tr>"; foreach ($hdata as $w => $wdata) { foreach ($wdata['dates'] as $d) { $cls = $w % 2 ? 'oddwk' : ''; $thead .= "<th class='$cls'>$d</th>"; } } $thead .= "</tr>\n<tr>"; foreach ($hdata as $w => $wdata) { foreach ($wdata['servs'] as $d) { $cls = $w % 2 ? 'oddgrn' : 'evengrn'; $thead .= "<th class='$cls'>$d</th>"; } } $thead .= "</tr>\n"; ################################################################################ # # # BUILD DATA ROWS FOR SELECTED MONTH # # # ################################################################################ $res = $pdo->prepare("SELECT weekofyear(entrydate) - weekofyear(?) + 1 as wk_no , concat(date_format(entrydate, '%d'), ' ', service_code) as dayser , group_no , mem_name , memid FROM attendance a JOIN member m USING (memid) JOIN service s USING (serid) WHERE month(entrydate) = ? ORDER BY group_no, mem_name, dayser "); $res->execute([ $day1, $month ]); $mdata = []; foreach ($res as $r) { if (!isset($mdata[$r['group_no']][$r['mem_name']])) { $mdata[$r['group_no']][$r['mem_name']] = $empty; } $mdata[$r['group_no']][$r['mem_name']][$r['dayser']]['att'] = 1; } $tdata = ''; foreach ($mdata as $grp => $mems) { $cs = count($empty); $tdata .= "<tr><td class='w3-brown'>Group $grp</td> <td colspan='$cs'>&nbsp;</td> </tr> "; foreach ($mems as $mname => $attend) { $tdata .= "<tr><td>$mname</td>"; $prevwk = 0; foreach ($attend as $a) { $bar = $a['wk'] != $prevwk ? 'w3-leftbar' : ''; $prevwk = $a['wk']; $cls = $bar; if ($a['att']) { $clr = $a['wk'] % 2 ? 'oddgrn' : 'evengrn'; $cls .= " check $clr"; $tdata .= "<td class='$cls'>&check;</td>"; } else $tdata .= "<td class='$cls'>&nbsp;</td>"; } $tdata .= "</tr>\n"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Attendances</title> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <style type='text/css'> table { border-collapse: collapse; width: 100%; } caption { text-transform: uppercase; font-size: 14pt; } th { padding: 4px 0; width: 50px; } td { padding: 4px; } th.oddwk { background-color: #eee; } th.namecol { width: 150px; } .oddgrn { background-color: #80ff80; } .evengrn { background-color: #b3ffb3; } .check { font-weight: 600; text-align: center; } </style> </head> <body> <header class='w3-dark-gray w3-padding'> <h1>Attendance</h1> </header> <form class='w3-bar w3-light-gray w3-padding'> <label class='w3-bar-item'>Year </label> <input type='number' class='w3-bar-item' name='year' value='<?=$year?>'> <label class='w3-bar-item'>Month</label> <input type='number' class='w3-bar-item' name='month' value='<?=$month?>'> <button class='w3-button w3-bar-item w3-indigo'>Search</button> </form> <div class='w3-content w3-margin-top'> <table border='1' class='w3-small'> <caption class='w3-light-green w3-padding w3-center'><?=$monthname?></caption> <?=$thead?> <?=$tdata?> </table> </div> </body> </html>
  25. If I understand corrrectly, no update is required - just query the data SELECT CASE WHEN now() BETWEEN entrytime AND closetime THEN 'Enabled' ELSE 'Disabled' END as status FROM ....
×
×
  • 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.