-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Student subject Positioning based on score using php and mysql database
Barand replied to Ponel's topic in PHP Coding Help
PS. This is my grade table DROP TABLE IF EXISTS `grade`; CREATE TABLE `grade` ( `id` int(11) NOT NULL AUTO_INCREMENT, `grade` char(2) NOT NULL DEFAULT '', `lomark` int(11) DEFAULT NULL, `himark` int(11) DEFAULT NULL, `comment` varchar(15) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Dumping data for table `grade` -- INSERT INTO `grade` VALUES (1,'A*',91,100,'Distinction'),(2,'A',80,90,'Excellent'),(3,'B1',75,79,'V Good'), (4,'B2',70,74,'V Good'),(5,'C1',68,69,'Good'),(6,'C2',66,67,'Good'), (7,'C3',64,65,'Good'),(8,'C4',61,63,'Good'),(9,'D1',59,60,'Pass'), (10,'D2',57,58,'Pass'),(11,'D3',55,56,'Pass'),(12,'D4',53,54,'Pass'), (13,'D5',51,52,'Pass'),(14,'D6',49,50,'Pass'),(15,'D7',40,48,'Pass'), (16,'E ',21,39,'Poor'),(17,'F ',0,20,'Fail'); -
Student subject Positioning based on score using php and mysql database
Barand replied to Ponel's topic in PHP Coding Help
It just occured to me that our grade tables are not the same. The missing subjects are 4, 9 and 12 so I extracted those records... mysql> SELECT * FROM subject_position -> WHERE subjectid in (4,9,12) -> and semesterid='1' -> and yearid='1' -> and armsLevelId='1' -> and armsId='1' -> ORDER BY regno, subjectid; +----+-------+-----------+-------------+--------+------------+--------+-------+ | id | regNo | subjectid | armsLevelId | armsId | semesterid | yearid | total | +----+-------+-----------+-------------+--------+------------+--------+-------+ | 23 | 4663 | 12 | 1 | 1 | 1 | 1 | 69 | | 7 | 4663 | 4 | 1 | 1 | 1 | 1 | 99 | | 17 | 4663 | 9 | 1 | 1 | 1 | 1 | 96 | | 24 | 6073 | 12 | 1 | 1 | 1 | 1 | 94 | | 8 | 6073 | 4 | 1 | 1 | 1 | 1 | 95 | | 18 | 6073 | 9 | 1 | 1 | 1 | 1 | 96 | +----+-------+-----------+-------------+--------+------------+--------+-------+ Could it be that those total values are missing from the grade ranges in your grade table and so no matches are found? Try LEFT JOIN grade and see if all records now appear. [edit] Your results - Getting 100% and still failing must have been a little disheartening! -
Student subject Positioning based on score using php and mysql database
Barand replied to Ponel's topic in PHP Coding Help
The only change that I made at my end was to define the columns as int, not the varchar(50) that you used) I have just changed mine to varchar to match yours but the only difference was the subject sequence (1, 10, 11, 12, 2,... as expected) and I still got all 12 subjects. So still head-scratching. -
Student subject Positioning based on score using php and mysql database
Barand replied to Ponel's topic in PHP Coding Help
mysql> select version(); +------------+ | version() | +------------+ | 5.7.21-log | +------------+ -
This seems to work <?php include 'db_inc.php'; // use your own $db = pdoConnect('testdb'); // connection process session_start(); $_SESSION['user'] = 1; // just for testing, You use your log in value if (isset($_GET['ajax'])) { $events = []; $t = round($_GET['date'],0); $date = (new DateTime("@$t"))->format('Y-m-d'); $res = $db->prepare("SELECT day(date) as day , status FROM pto_tracker WHERE account = ? AND EXTRACT(YEAR_MONTH FROM date) = EXTRACT(YEAR_MONTH FROM ?) "); $res->execute( [ $_SESSION['user'], $date ]); foreach ($res as $r) { $events[$r['day']] = $r['status']; } exit(json_encode($events)); } ?> <!DOCTYPE HTML> <html> <head> <title>Test</title> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script type='text/javascript'> $(document).ready(function() { var currentDate = new Date(); $('#left').click(function(e) { $('table').text(''); if (currentDate.getMonth() === 0) { currentDate = new Date(currentDate.getFullYear() - 1, 11); generateCalendar(currentDate); } else { currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1) generateCalendar(currentDate); } e.preventDefault(); }); $('#right').click(function(e) { $('table').html('<tr></tr>'); if (currentDate.getMonth() === 11) { currentDate = new Date(currentDate.getFullYear() + 1, 0); generateCalendar(currentDate); } else { currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1) generateCalendar(currentDate); } e.preventDefault(); }); generateCalendar(currentDate); }); function generateCalendar(d) { Date.prototype.monthDays = function() { var d1 = new Date(d.getFullYear(), d.getMonth() + 1, 0); return d1.getDate(); }; var details = { // totalDays: monthDays(d.getMonth(), d.getFullYear()), totalDays: d.monthDays(), weekDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], }; var start = new Date(d.getFullYear(), d.getMonth()).getDay(); var cal = []; var day = 1; for (var i = 0; i <= 6; i++) { cal.push(['<tr>']); for (var j = 0; j < 7; j++) { if (i === 0) { cal[i].push('<td>' + details.weekDays[j] + '</td>'); } else if (day > details.totalDays) { cal[i].push('<td> </td>'); } else { if (i === 1 && j < start) { cal[i].push('<td> </td>'); } else { cal[i].push('<td class="day" data-day="' + day + '">' + day++ + '</td>'); } } } cal[i].push('</tr>'); } cal = cal.reduce(function(a, b) { return a.concat(b); }, []).join(''); $('#caltable').append(cal); $('#month').text(details.months[d.getMonth()]); $('#year').text(d.getFullYear()); $('td.day').mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); }); getEventsArray(d); } function getEventsArray(d) { $.get( "", {"ajax":1, "date":d.getTime()/1000}, function(resp) { console.log(resp) //var r = JSON.parse(resp) $.each(resp, function (k,v) { var day = $(".day[data-day="+k+"]") if (v == 'Approved') { $(day).css("background-color", "#4CAF50") } else if (v == 'Pending') { $(day).css("background-color", "#f0cb11") } else { $(day).css("background-color", "#c62d1f") } $(day).attr("title", v) }) }, "JSON" ) } function monthDays(month, year) { var result = []; var days = new Date(year, month, 0).getDate(); for (var i = 1; i <= days; i++) { result.push(i); } return result; } </script> <style> .container { width: 80%; margin: 15px auto; } </style> </head> <body> <div><h1>Calendar</h1></div> <div> <h3><span id='month'></span> <span id='year'></span></h3> </div> <div> <table id="caltable"></table> </div> </body> </html> Not very pretty but gives ... from this data ... +----+---------+------------+----------+ | id | account | date | status | +----+---------+------------+----------+ | 1 | 1 | 2020-11-05 | Approved | | 2 | 2 | 2020-11-10 | Denied | | 3 | 1 | 2020-11-12 | Pending | | 4 | 1 | 2020-11-16 | Denied | | 5 | 2 | 2020-12-15 | Approved | | 6 | 1 | 2020-12-08 | Approved | * | 7 | 2 | 2020-12-12 | Pending | | 8 | 1 | 2020-12-15 | Denied | * | 9 | 1 | 2020-12-17 | Pending | * +----+---------+------------+----------+
-
Data not matching but code does not acknowledge this
Barand replied to samanj's topic in PHP Coding Help
Check the number of affected rows to see if any rows were updated. Not finding a record is not a query failure. -
Student subject Positioning based on score using php and mysql database
Barand replied to Ponel's topic in PHP Coding Help
No such issues my end. My results for 4663 were posted earlier and for 6073 I get +--------+------------+-------+-----------+------+-------+-------+-------------+ | yearid | semesterid | regno | subjectid | rank | total | grade | comment | +--------+------------+-------+-----------+------+-------+-------+-------------+ | 1 | 1 | 6073 | 1 | 1 | 73 | B2 | V Good | | 1 | 1 | 6073 | 2 | 1 | 61 | C4 | Good | | 1 | 1 | 6073 | 3 | 2 | 61 | C4 | Good | | 1 | 1 | 6073 | 4 | 2 | 95 | A* | Distinction | | 1 | 1 | 6073 | 5 | 1 | 100 | A* | Distinction | | 1 | 1 | 6073 | 6 | 2 | 67 | C2 | Good | | 1 | 1 | 6073 | 7 | 1 | 80 | A | Excellent | | 1 | 1 | 6073 | 8 | 2 | 77 | B1 | V Good | | 1 | 1 | 6073 | 9 | 1 | 96 | A* | Distinction | | 1 | 1 | 6073 | 10 | 2 | 77 | B1 | V Good | | 1 | 1 | 6073 | 11 | 1 | 88 | A | Excellent | | 1 | 1 | 6073 | 12 | 1 | 94 | A* | Distinction | +--------+------------+-------+-----------+------+-------+-------+-------------+ As you can see, 12 subjects for both using your data posted at the start. Pulling both together, ordered by subject/rank gives +--------+------------+-------+-----------+------+-------+-------+-------------+ | yearid | semesterid | regno | subjectid | rank | total | grade | comment | +--------+------------+-------+-----------+------+-------+-------+-------------+ | 1 | 1 | 6073 | 1 | 1 | 73 | B2 | V Good | | 1 | 1 | 4663 | 1 | 2 | 72 | B2 | V Good | | 1 | 1 | 6073 | 2 | 1 | 61 | C4 | Good | | 1 | 1 | 4663 | 2 | 2 | 47 | D7 | Pass | | 1 | 1 | 4663 | 3 | 1 | 82 | A | Excellent | | 1 | 1 | 6073 | 3 | 2 | 61 | C4 | Good | | 1 | 1 | 4663 | 4 | 1 | 99 | A* | Distinction | | 1 | 1 | 6073 | 4 | 2 | 95 | A* | Distinction | | 1 | 1 | 6073 | 5 | 1 | 100 | A* | Distinction | | 1 | 1 | 4663 | 5 | 2 | 70 | B2 | V Good | | 1 | 1 | 4663 | 6 | 1 | 69 | C1 | Good | | 1 | 1 | 6073 | 6 | 2 | 67 | C2 | Good | | 1 | 1 | 6073 | 7 | 1 | 80 | A | Excellent | | 1 | 1 | 4663 | 7 | 2 | 77 | B1 | V Good | | 1 | 1 | 6073 | 8 | 2 | 77 | B1 | V Good | | 1 | 1 | 4663 | 8 | 2 | 58 | D2 | Pass | | 1 | 1 | 4663 | 9 | 1 | 96 | A* | Distinction | | 1 | 1 | 6073 | 9 | 1 | 96 | A* | Distinction | | 1 | 1 | 4663 | 10 | 1 | 78 | B1 | V Good | | 1 | 1 | 6073 | 10 | 2 | 77 | B1 | V Good | | 1 | 1 | 6073 | 11 | 1 | 88 | A | Excellent | | 1 | 1 | 4663 | 11 | 2 | 48 | D7 | Pass | | 1 | 1 | 6073 | 12 | 1 | 94 | A* | Distinction | | 1 | 1 | 4663 | 12 | 2 | 69 | C1 | Good | +--------+------------+-------+-----------+------+-------+-------+-------------+ Looks like you broke it or changed your data. Not much I can do about either. -
Student subject Positioning based on score using php and mysql database
Barand replied to Ponel's topic in PHP Coding Help
Like this? You need to include those "unknown columns" in the intermediate subqueries SELECT yearid , semesterid , regno , subjectid , rank , total , grade , comment FROM ( SELECT yearid , semesterid , subjectid , @seq := CASE WHEN yss <> @prev THEN 1 ELSE @seq + 1 END as seq , @rank := CASE WHEN total = @prevtot THEN @rank ELSE @seq END as rank , @prevtot := total as total , @prev := yss as yss , regno , armsLevelId -- added , armsid -- added FROM ( SELECT yearid , semesterid , subjectid , concat(yearid, semesterid, subjectid) as yss , regno , total , armsLevelId -- added , armsid -- added FROM subject_position ORDER BY yearid, semesterid, subjectid, total DESC ) sorted JOIN (SELECT @prev := '', @seq := 0, @rank := 0, @prevtot := 0) as init ) ranked JOIN grade ON total BETWEEN grade.lomark AND grade.himark WHERE regNo='4663' -- A and armsLevelId='1' -- D and armsId='1' -- D and semesterid='1' -- E and yearid='1' -- D ORDER BY subjectid -- . Giving +--------+------------+-------+-----------+------+-------+-------+-------------+ | yearid | semesterid | regno | subjectid | rank | total | grade | comment | +--------+------------+-------+-----------+------+-------+-------+-------------+ | 1 | 1 | 4663 | 1 | 2 | 72 | B2 | V Good | | 1 | 1 | 4663 | 2 | 2 | 47 | D7 | Pass | | 1 | 1 | 4663 | 3 | 1 | 82 | A | Excellent | | 1 | 1 | 4663 | 4 | 1 | 99 | A* | Distinction | | 1 | 1 | 4663 | 5 | 2 | 70 | B2 | V Good | | 1 | 1 | 4663 | 6 | 1 | 69 | C1 | Good | | 1 | 1 | 4663 | 7 | 2 | 77 | B1 | V Good | | 1 | 1 | 4663 | 8 | 2 | 58 | D2 | Pass | | 1 | 1 | 4663 | 9 | 1 | 96 | A* | Distinction | | 1 | 1 | 4663 | 10 | 1 | 78 | B1 | V Good | | 1 | 1 | 4663 | 11 | 2 | 48 | D7 | Pass | | 1 | 1 | 4663 | 12 | 2 | 69 | C1 | Good | +--------+------------+-------+-----------+------+-------+-------+-------------+ -
Student subject Positioning based on score using php and mysql database
Barand replied to Ponel's topic in PHP Coding Help
Try this SELECT yearid , semesterid , subjectid , regno , rank , total , grade , comment FROM ( SELECT yearid , semesterid , subjectid , @seq := CASE WHEN yss <> @prev THEN 1 ELSE @seq + 1 END as seq , @rank := CASE WHEN total = @prevtot THEN @rank ELSE @seq END as rank , @prevtot := total as total , @prev := yss as yss , regno FROM ( SELECT yearid , semesterid , subjectid , concat(yearid, semesterid, subjectid) as yss , regno , total FROM subject_position ORDER BY yearid, semesterid, subjectid, total DESC -- LIMIT 18446744073709551615 -- MariaDB requires this line uncommented * ) sorted JOIN (SELECT @prev := '', @seq := 0, @rank := 0, @prevtot := 0) as init ) ranked JOIN grade ON total BETWEEN grade.lomark AND grade.himark; (I invented my own grades) mysql> select * from grade; +----+-------+--------+--------+-------------+ | id | grade | lomark | himark | comment | +----+-------+--------+--------+-------------+ | 1 | A* | 91 | 100 | Distinction | | 2 | A | 80 | 90 | Excellent | | 3 | B1 | 75 | 79 | V Good | | 4 | B2 | 70 | 74 | V Good | | 5 | C1 | 68 | 69 | Good | | 6 | C2 | 66 | 67 | Good | | 7 | C3 | 64 | 65 | Good | | 8 | C4 | 61 | 63 | Good | | 9 | D1 | 59 | 60 | Pass | | 10 | D2 | 57 | 58 | Pass | | 11 | D3 | 55 | 56 | Pass | | 12 | D4 | 53 | 54 | Pass | | 13 | D5 | 51 | 52 | Pass | | 14 | D6 | 49 | 50 | Pass | | 15 | D7 | 40 | 48 | Pass | | 16 | E | 21 | 39 | Poor | | 17 | F | 0 | 20 | Fail | +----+-------+--------+--------+-------------+ Query results... +--------+------------+-----------+-------+------+-------+-------+-------------+ | yearid | semesterid | subjectid | regno | rank | total | grade | comment | +--------+------------+-----------+-------+------+-------+-------+-------------+ | 1 | 1 | 1 | 6073 | 1 | 73 | B2 | V Good | | 1 | 1 | 1 | 4663 | 2 | 72 | B2 | V Good | | 1 | 1 | 2 | 6073 | 1 | 61 | C4 | Good | | 1 | 1 | 2 | 4663 | 2 | 47 | D7 | Pass | | 1 | 1 | 3 | 4663 | 1 | 82 | A | Excellent | | 1 | 1 | 3 | 6073 | 2 | 61 | C4 | Good | | 1 | 1 | 4 | 4663 | 1 | 99 | A* | Distinction | | 1 | 1 | 4 | 6073 | 2 | 95 | A* | Distinction | | 1 | 1 | 5 | 6073 | 1 | 100 | A* | Distinction | | 1 | 1 | 5 | 4663 | 2 | 70 | B2 | V Good | | 1 | 1 | 6 | 4663 | 1 | 69 | C1 | Good | | 1 | 1 | 6 | 6073 | 2 | 67 | C2 | Good | | 1 | 1 | 7 | 6073 | 1 | 80 | A | Excellent | | 1 | 1 | 7 | 4663 | 2 | 77 | B1 | V Good | | 1 | 1 | 8 | 6073 | 2 | 77 | B1 | V Good | | 1 | 1 | 8 | 4663 | 2 | 58 | D2 | Pass | | 1 | 1 | 9 | 4663 | 1 | 96 | A* | Distinction | | 1 | 1 | 9 | 6073 | 1 | 96 | A* | Distinction | | 1 | 1 | 10 | 4663 | 1 | 78 | B1 | V Good | | 1 | 1 | 10 | 6073 | 2 | 77 | B1 | V Good | | 1 | 1 | 11 | 6073 | 1 | 88 | A | Excellent | | 1 | 1 | 11 | 4663 | 2 | 48 | D7 | Pass | | 1 | 1 | 12 | 6073 | 1 | 94 | A* | Distinction | | 1 | 1 | 12 | 4663 | 2 | 69 | C1 | Good | | 1 | 2 | 1 | 6073 | 1 | 70 | B2 | V Good | | 1 | 2 | 1 | 4663 | 2 | 28 | E | Poor | | 1 | 2 | 2 | 4663 | 1 | 68 | C1 | Good | | 1 | 2 | 2 | 6073 | 2 | 59 | D1 | Pass | | 1 | 2 | 3 | 6073 | 1 | 70 | B2 | V Good | | 1 | 2 | 3 | 4663 | 2 | 68 | C1 | Good | | 1 | 2 | 4 | 4663 | 1 | 81 | A | Excellent | | 1 | 2 | 4 | 6073 | 2 | 72 | B2 | V Good | | 1 | 2 | 5 | 4663 | 1 | 84 | A | Excellent | | 1 | 2 | 5 | 6073 | 2 | 72 | B2 | V Good | | 1 | 2 | 6 | 6073 | 2 | 72 | B2 | V Good | | 1 | 2 | 6 | 4663 | 2 | 58 | D2 | Pass | | 1 | 2 | 7 | 4663 | 1 | 71 | B2 | V Good | | 1 | 2 | 7 | 6073 | 2 | 70 | B2 | V Good | | 1 | 2 | 8 | 6073 | 1 | 55 | D3 | Pass | | 1 | 2 | 8 | 4663 | 2 | 48 | D7 | Pass | | 1 | 2 | 9 | 4663 | 1 | 66 | C2 | Good | | 1 | 2 | 9 | 6073 | 2 | 51 | D5 | Pass | | 1 | 2 | 10 | 6073 | 1 | 58 | D2 | Pass | | 1 | 2 | 10 | 4663 | 2 | 37 | E | Poor | | 1 | 2 | 11 | 6073 | 1 | 59 | D1 | Pass | | 1 | 2 | 11 | 4663 | 2 | 57 | D2 | Pass | | 1 | 2 | 12 | 6073 | 1 | 69 | C1 | Good | | 1 | 2 | 12 | 4663 | 2 | 67 | C2 | Good | | 1 | 3 | 1 | 4663 | 1 | 94 | A* | Distinction | | 1 | 3 | 1 | 6073 | 2 | 82 | A | Excellent | | 1 | 3 | 2 | 6073 | 1 | 76 | B1 | V Good | | 1 | 3 | 2 | 4663 | 2 | 69 | C1 | Good | | 1 | 3 | 3 | 6073 | 1 | 81 | A | Excellent | | 1 | 3 | 3 | 4663 | 2 | 63 | C4 | Good | | 1 | 3 | 4 | 4663 | 1 | 81 | A | Excellent | | 1 | 3 | 4 | 6073 | 2 | 77 | B1 | V Good | | 1 | 3 | 5 | 6073 | 1 | 83 | A | Excellent | | 1 | 3 | 5 | 4663 | 2 | 72 | B2 | V Good | | 1 | 3 | 6 | 6073 | 1 | 83 | A | Excellent | | 1 | 3 | 6 | 4663 | 2 | 78 | B1 | V Good | | 1 | 3 | 7 | 4663 | 1 | 77 | B1 | V Good | | 1 | 3 | 7 | 6073 | 2 | 75 | B1 | V Good | | 1 | 3 | 8 | 6073 | 1 | 82 | A | Excellent | | 1 | 3 | 8 | 4663 | 2 | 74 | B2 | V Good | | 1 | 3 | 9 | 6073 | 1 | 95 | A* | Distinction | | 1 | 3 | 9 | 4663 | 2 | 56 | D3 | Pass | | 1 | 3 | 10 | 4663 | 1 | 87 | A | Excellent | | 1 | 3 | 10 | 6073 | 2 | 79 | B1 | V Good | | 1 | 3 | 11 | 6073 | 1 | 71 | B2 | V Good | | 1 | 3 | 11 | 4663 | 2 | 70 | B2 | V Good | | 1 | 3 | 12 | 6073 | 1 | 90 | A | Excellent | | 1 | 3 | 12 | 4663 | 2 | 82 | A | Excellent | +--------+------------+-----------+-------+------+-------+-------+-------------+ [edit] * The idiots at Maria maintain that as the subquery effectively creates a table, and tables are inherently unordered, then the ORDER BY clause should be ignored. The rest of the world think this is a resultset and there can be ordered. The LIMIT 264-1 forces it to use an ordered temporary table. -
Sounds like you need some code other than that above. If it doesn't produce what you need, don't use it. Find one which will return an array or rewrite that one.
-
-
It probably works even when you do redirect. You don't see it because it immediately goes to another page.
-
Effect of string starting with an ampersand
Barand replied to NotionCommotion's topic in PHP Coding Help
I hope you weren't planning on shooting the messenger. -
I am a little confused by your ajax query. Your code produces a calendar for the current month and your ajax code sends the date. All OK so far, but then your query searches by account only. I would have expected you to get the events just for that current month as those are the only ones you need. Having got an array of this month's events (as in my example above) I would for each element in array find <td> with data-day == element key change its background color add a tooltip title attribute with element value end for each That will highlight days with an event and display the event on hovering over the day. EG But then, you haven't really explained how you envisage it working.
-
Effect of string starting with an ampersand
Barand replied to NotionCommotion's topic in PHP Coding Help
1 ) Why are you searching for "amprsand" when there are no ampersands (&) in your code. 2) the numeric value of a string is value of the numeric chars up to the first non-numeric character echo intval('12@3'); // 12 echo intval('@123'); // 0 -
Display another file on button click in div on same page
Barand replied to pfoster77's topic in PHP Coding Help
-
I would create the event array with the days as the keys and the event descriptions as the values [ "18":"Carol service", "25":"Christmas Day", "31":"New Years Eve" ] and give each <td> a data-day attribute <tr><td data-day='01'>1</td> ... <td data-day='31'>31</td></tr> Now it's easy to match them.
-
The sort code worked for me Array ( [0] => Array ( [name] => DSCN0025.JPG [time] => 1399035071 ) [1] => Array ( [name] => DSCN0055.JPG [time] => 1453121551 ) [2] => Array ( [name] => DSCN0052.JPG [time] => 1575540845 ) [3] => Array ( [name] => DSCN0043.JPG [time] => 1330013161 ) [4] => Array ( [name] => DSCN0062.JPG [time] => 1527770426 ) ) Original array of files DSCN0025.JPG 2014-05-02 DSCN0055.JPG 2016-01-18 DSCN0052.JPG 2019-12-05 DSCN0043.JPG 2012-02-23 DSCN0062.JPG 2018-05-31 Sort the array DSCN0052.JPG 2019-12-05 DSCN0062.JPG 2018-05-31 DSCN0055.JPG 2016-01-18 DSCN0025.JPG 2014-05-02 DSCN0043.JPG 2012-02-23 Code: <?php $arr = glob('Pictures/Nikon Transfer 2/001/*.JPG'); shuffle($arr); $files = []; foreach ($arr as $k => $f) { $files[$k]['name'] = basename($f); $files[$k]['time'] = filemtime($f) ; } echo '<pre>', print_r($files, 1); echo "Original array of files\n\n"; foreach ($files as $f) { printf("%-15s %15s\n", $f['name'], date('Y-m-d', $f['time'])); } echo "\n\nSort the array\n\n"; uasort($files, function($a, $b) { return $b['time'] <=> $a['time']; }); foreach ($files as $f) { printf("%-15s %15s\n", $f['name'], date('Y-m-d', $f['time'])); } echo '</pre>'; ?>
-
Get a pencil and read through the code line by line as if it were being executed. For each iteration, note the values of i and d and the values that would be output. You should get this (try it yourself before peeping)
-
There is a syntax error in my code - there's a missing ' character uasort($files, function($a, $b) { return $b['time'] <=> $a['time']; } ); ^ Add
-
"continue" says "Stop processing this iteration and continue with the next. Just a single "continue (1)" would take you to the start of the inner loop and start with the next value of $d. Because you have "continue 2" it goes to the start of the outer loop and continues with the next value of $i.
-
I thought I would check out the impact of the larger key. I creted two tables, one with a smallint PK and the other using bigint. CREATE TABLE `keytest_si` ( `id` smallint(5) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `keytest_bi` ( `id` bigint(20) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Then I looked for my largest table that would fit in a smallint. I then wrote the ids from this table to each of the two new tables. mysql> select count(*) from production_status; +----------+ | count(*) | +----------+ | 60000 | +----------+ 1 row in set (0.01 sec) mysql> insert into keytest_si (id) select id from production_status; Query OK, 60000 rows affected (1.67 sec) Records: 60000 Duplicates: 0 Warnings: 0 mysql> insert into keytest_bi (id) select id from production_status; Query OK, 60000 rows affected (1.86 sec) Records: 60000 Duplicates: 0 Warnings: 0 That's a difference of 0.2 seconds over 60,000 records. The added cost per record insert is therefore 0.000003 seconds. Do you really think that's worth the worry and the extra processing it's causing you? (Which probably takes far longer than the time saved) And Merry Christmas to you too.
-
Have you looked in this thread - about an hour ago?
-
Then why the **** do you want to set the auto_increment back to 1?