#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    40
    Rep Power
    2

    Looking to improve code


    Does anybody here know how to list database entries by date?

    My code reads:

    $result = mysqli_query($con,"SELECT * FROM pics ORDER BY Date DESC");

    while($row = mysqli_fetch_array($result))
    {
    echo "<table border='1' width='1000'>
    <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Book</th>
    <th>Subject</th>
    <th>Price</th>
    <th>Description</th>
    <th>Picture</th>
    </tr>";
    echo "<tr>";
    echo '<td><a class="top" href="DetailPage.php?ID=' . $row["ID"];
    echo '">' . $row['ID'] . '</a></td>';
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['book'] . "</td>";
    echo "<td>" . $row['subject'] . "</td>";
    echo "<td>" . $row['Price'] . "</td>";
    echo "<td>" . $row['Description'] . "</td>";
    echo "<td>" . "<img src=\"" . $row['picname'] . "\" width=100 height=100 />" . "</td>";
    echo "</tr>";
    echo "</table>";
    }

    I want to list them as such:

    Friday
    Link

    Saturday
    Link

    Etc...

    This might be a bit advanced but any advice is much appreciated.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    so are you looking to improve your code (as the title suggests), or is there a problem with your code (as your text suggests)?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    Originally Posted by phpnewbie34
    Does anybody here know how to list database entries by date?
    Instead of listing them by date, are you trying to GROUP them by date? Each date on a different page?
    Thomas Tremain
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    Please wrap your code in [ PHP ] tags. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    Assuming there is date information in your database (you didn't post anything with date in it), are you asking how to find the day of the week from a date?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    40
    Rep Power
    2

    Smile Re:


    I want to do something like this:


    The code I listed works fine, but I can't figure out how to get it like it is on this website.

    Wed. Apr. 17
    Finance and Administrative Director - Greenpeace Canada (Toronto, ON)
    Managed Accounts Associate (Toronto)
    Tue. Apr. 16
    Senior Financial Analyst, Communications Revenue (Toronto, ON)
    SENIOR PROPERTY ACCOUNTANT
    Tax Manager
    Cost Controller
    Financial Analyst
    Financial Analyst
    Financial Analyst
    ACCOUNTANT
    Senior Manager, Tax
    Financial Advisor, Small Business
    Controller (Mining) - Canada
    CHARTERED ACCOUNTANT
    Junior Financial Analyst
    Financial Analyst, Automotive Accounting
    Residential Property Accountant
    Manager, Accounting Policies - 6 months contract
    Financial Controller - Real Estate/Manufacturing Wanted ASAP (Toronto, ON)

    Etc etc...

    Any pointers are much appreciated
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    Originally Posted by phpnewbie34
    I want to do something like this:


    The code I listed works fine, but I can't figure out how to get it like it is on this website.
    So you want to GROUP BY a specific date, or maybe order by date, and only show the date field prior to the listings. Either way, what format is your "date" field in?
    Thomas Tremain
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by phpnewbie34
    The code I listed works fine, but I can't figure out how to get it like it is on this website.
    Kind of a contradiction in terms, isn't it? The code works fine, but it doesn't do what it's supposed to do? Maybe we have a different understanding of "work".

    In other words: You want the entries to be grouped by date.

    If your "Date" columns actually contains a date (and not a timestamp), just loop through the entries, compare the date with the last date, and if it's different, you start a new group:

    Code:
    var last_date := null
    for pic in pics:
    	if pic.date != last_date:
    		print '<a new table or whatever>'
    	print pic
    	last_date := pic.date
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    40
    Rep Power
    2

    Re:


    Okay maybe a contradiction.

    My date format is a timestamp.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    40
    Rep Power
    2

    Re:


    I kind of see what you're saying but I don't understand that code.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    40
    Rep Power
    2

    Re:


    I messed around and finally figured it out

    Thanks for the advice

IMN logo majestic logo threadwatch logo seochat tools logo