-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
It would help to see what your db data looks like EG, is it +------------+-----------+-----------+----------+ | start_time | no_shows | cancelled | attended | +------------+-----------+-----------+----------+ | 2019-12-20 | 1 | 0 | 0 | | 2019-12-21 | 0 | 0 | 1 | | 2019-12-22 | 0 | 1 | 0 | If so, then SELECT weekofyear(start_time) as weekno , SUM(no_shows = 1) as no_shows , SUM(cancelled = 1) as cancelled , SUM(attended = 1) as attended FROM mytable WHERE start_time BETWEEN ? AND ? GROUP BY weekno;
-
So you can do it the easy way. What about the solutions not using array_reverse?
-
Very good! But the question explicitly wanted to do it without array_reverse
-
becomes... $sql_2_2 = "UPDATE analytics SET visitor_visitor_sessions = ?, visitor_visitor_pageviews = ?, visitor_visitor_pages = ? WHERE visitor_visitor_id = ? "; $stmt = $this->db->prepare($sql_2_2); $stmt->execute( [ $analytics['visitor_visitor_sessions'], $analytics['visitor_visitor_pageviews'], $analytics['visitor_visitor_pages'], $visitor_visitor_id ]);
-
The first impression I get when I see a table definition like that is "Spreadsheet!". Relation database tables are not spreadsheets and require data normalization. Do not store derived data (Year, Month, Day, Weekday, Hour are all derived from the datetime) Do not use "SELECT *". Specify the required columns. That doesn't work, only the $sql_1_1 query will be executed (thankfully as it would open up a whole new vista of SQL injection possibilities). You can update more than one column in an update query, you don't need a separate one for each. Use prepared queries, do not insert data values directly into query strings.
-
HINT: try using array_pop() and build a new array or, method 2, sort the array keys in reverse order
-
Yep, more than one way to skin a cat.
-
ctype_digit() function fact ($x) { if (!ctype_digit("$x") ) { // CAVEAT: cast $x to string type return 'Please provide an integer'; } if($x <= 1) { return 1; } else { return $x * fact($x - 1); } }
-
The code I used gives that format mysql> SELECT CASE WHEN FINISHED = 0 -> THEN '' -> ELSE DATE_FORMAT(FINISHED, '%d %M %Y') -> END as FINISHED -> FROM games -> ORDER BY FINISHED DESC -> LIMIT 5; +------------------+ | FINISHED | +------------------+ | 31 October 2016 | | 31 December 2016 | | 29 October 2018 | | 29 June 2019 | | 29 January 2017 | +------------------+ For future consistency of content you should EITHER ALTER TABLE `games` CHANGE COLUMN `FINISHED` `FINISHED` DATE NOT NULL DEFAULT '0000-00-00' ; to prevent new records having the date set to NULL OR Change those "0000-00-00" dates back to NULL
-
I hadn't realised that you had already converted your finished column to the correct DATE type and format. Well done. SELECT CASE WHEN FINISHED = 0 THEN '' ELSE DATE_FORMAT(FINISHED, '%d %M %Y') END as FINISHED FROM games
-
By default a dump (export) file is .sql. But a .csv would be fine if you prefer to provide it that way
-
What does you FINISHED column contain if there is no finished date yet? (I am assuming a blank string) Can you provide some sample data? (Dump of your 'games' table)
-
What does $row['FINISHED'] contain when you get that result? EDIT And why didn't you just use strtotime() again? $date = date('d F Y', strtotime($date))
-
You have a similar problem in your original code when the finished date is blank echo date('d F Y', strtotime('')); //--> 01 January 1970 With STR_TO_DATE(), if the date string is blank you get 0000-00-00 mysql> select finished -> , str_to_date(finished, '%d-%m-%Y') -> from games; +------------+-----------------------------------+ | finished | str_to_date(finished, '%d-%m-%Y') | +------------+-----------------------------------+ | | 0000-00-00 | | 20-12-2019 | 2019-12-20 | +------------+-----------------------------------+ I recommend formatting the date in the query instead of in php EG SELECT FINISHED , CASE WHEN FINISHED = '' THEN '' ELSE DATE_FORMAT(STR_TO_DATE(FINISHED, '%d-%m-%Y'), '%d %M %Y') END as DATE_FINISHED FROM games; +------------+------------------+ | FINISHED | DATE_FINISHED | +------------+------------------+ | | | | 20-12-2019 | 20 December 2019 | +------------+------------------+ Then in the php code , just output $row['DATE_FINISHED']
-
if () and else are useful in such situations
-
As for that '✔', store in your table as 1/0 for completed/not completed. You convert to a '✔' on output, not on input.
-
You can use the sql function str_to_date() to convert your dates to the correct format. In the mean time you can also use that in your query SELECT GAME, YR, PLATFORM, PUBLISHER, STR_TO_DATE(FINISHED, '%d-%m-%Y') as FINISHED FROM games WHERE COMPLETED='✔' ORDER BY STR_TO_DATE(FINISHED, '%d-%m-%Y') DESC LIMIT 10
-
What format are you storing them in? You can get away with varchar so long as they are in" yyyy-mm-dd" format. Why the **** are you storing values as '✔' in your database?
-
I don't know if you are using PDO or MySqli, but whichever it is, use its error reporting functionality to find out why.
-
EG +---------------+ | school | +---------------+ | school_id |---+ | name | | +--------------+ +---------------+ | | team | +---------------+ | +--------------+ | player | | | team_id |--+ +---------------+ | | team_name | | +---| player_id | +---<| school_id | | | | name | | sport_id | | | | height | +--------------+ | | | weight | | | | email | | | | dob | | | +---------------+ | +-----------------+ | | | team_player | | | +-----------------+ | | | id | | +---<| team_id | | | position | | | player_id |>--+ | played_from | | played_until | +-----------------+
-
Forget the json then. This should give you the first 5 records in the format your C# is currently expecting $res = $conn->query("SELECT username , level , points , killrate FROM atable ORDER BY points DESC LIMIT 5; "); $data = []; foreach ($res as $row) { $data[] = join('|', $row); } echo join('/', $data);
-
$res = $conn->query("SELECT username , level , points , killrate FROM atable LIMIT 5; "); $data = $res->fetchAll(); echo json_encode($data); That doesn't look like much of a hassle.