Jump to content

Code not working


Revolutsio

Recommended Posts

I have some PHP code that should give me the dates in year order but it is giving me the date order.

So it  shows a list of 10 items in the following order see attached png

it should show the ten items i sorted by year then month then day,

<?php
  $sql = "SELECT GAME, YR, PLATFORM, PUBLISHER, FINISHED FROM games WHERE COMPLETED='✔' ORDER BY FINISHED DESC LIMIT 10;";
  $result = mysqli_query($conn, $sql) or die("Bad Query: $sql");
  $num_rows = mysqli_num_rows($result);?>

  <?php while ($row = mysqli_fetch_assoc($result)):
   ?>


  <tr>
    <td><?php echo $row['GAME']; ?></td>
    <td><?php echo $row['PLATFORM']; ?></td>
    <td><?php echo date('d F Y', strtotime($row['FINISHED'])); ?></td>
  <tr>

  <?php endwhile; ?>

This use to work on until this week

 

order.png

Link to comment
Share on other sites

2 minutes ago, cyberRobot said:

Sorry, I misread your post. Are the values in the FINISHED column stored as a MySQL DATE type? If not, switching the column type will make this much easier.

no there are stored as varchar, the database is from a google spreadsheet

Link to comment
Share on other sites

1 minute ago, Barand said:

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?

As i said in a previous post, I have put this database together on a google spreadsheet and downloaded it to a csv doc and then imported it to Mysql with PHPMyAdmin.

The date format on the sheet is in the format dd-mm-yyyy.

As above the tick is from the spreadsheet and it shows if i have completed a game.

 

Link to comment
Share on other sites

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

 

Edited by Barand
Link to comment
Share on other sites

11 hours ago, Barand said:

As for that '✔', store in your table as 1/0 for completed/not completed.

You convert to a '✔' on output, not on input.

Thank you i will try this.

 

Now with the date query, when i show the page with items that have not been completed i get the date '0000-00-00' how do i change this , i searched on here and found the same problem but it did not work with my code.

How do i make this date blank if  'FINISHED' = '0000-00-00'

 

if (FINISHED='0000-00-00', '', FINISHED) as FINISHED

i have tried this but get '01 January 1970' and if I put a space in the second quotes I get today's date , a little bit confused

Edited by Revolutsio
MORE INFORMATION
Link to comment
Share on other sites

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']

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.