SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorting values by date

    I am displaying a list of data from my array with:
    PHP Code:
    include("datafile.php");
    foreach ( 
    $programs as $prog ) {
        if ( 
    $prog['genre'] == 'action-thriller' ) {
        echo 
        
    '<dl><dt>'
            
    $prog['prd_id'],
            
    $prog['project'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
            
    'genre: '$prog['genre'],'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
            
    'pilot-date: '$prog['date_pilot'],
        
    '</dt><dd>'
            
    $prog['description'], 
        
    '</dd></dl>'
        }

    ?> 
    in the datafile, the syntax for date is this:
    'date_pilot'=>
    '09/02/20',

    I want to have each sub array(program with its data) to be displayed according to the value of 'date_pilot' with the newst first.
    Since I am using YY/MM/DD format, until 91 years(when the first 2 digits go back to '00' in year 2100), the later the date, the higher the numerical value will be, so there should be no problem there(like there would be if I had used DD/MM/YY). I still have the problem that each pair of digits is seperated by slashs '/' though.

    So the function needs to go, before displaying arrays/data, sort arrays by the value of 'date_pilot', ignore the slashs, and put sort arrays/programs from highest value of date_pilot at the top -so newest is first.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function cmp_date($a$b) {
        return 
    strtotime($b['date_pilot']) - strtotime($a['date_pilot']) ;
    }

    usort($programs'cmp_date'); 

  3. #3
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks. i am having trouble implementing it though. i have
    PHP Code:
    <?php

    include("addressbook.php");
    function 
    cmp_date($a$b) {
        return 
    strtotime($b['date_pilot']) - strtotime($a['date_pilot']) ;
    }
    usort($programs'cmp_date'); 
    foreach ( 
    $programs as $prog ) {
        if ( 
    $prog['genre'] == 'action-thriller' ) {
        echo 
        
    '<dl><dt>'
            
    $prog['prd_id'],
            
    $prog['project'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
            
    'genre: '$prog['genre'],'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
            
    'pilot-date: '$prog['date_pilot'],
        
    '</dt><dd>'
            
    $prog['description'], 
        
    '</dd></dl>'
        }

    ?>
    it seems to have no effect. i tried adding your function between the "if" and "echo" and other random things but they just give syntax errors... so i think i have it in the right place.... i research the usort function a bit and it all looks correct but it is not working for some reason.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You had it right, it's just strtotime() isn't going to work right with that format. Try this
    PHP Code:
    function cmp_date($a$b) {
        return 
    strcmp($b['date_pilot'], $a['date_pilot']);

    This will only work if YMD are 2 digits each(zero padded).

  5. #5
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks works perfectly.
    I am wondering about my actual array structure too.

    I have:
    PHP Code:
    array(
    'title'=>
        
    'heroes'
    'date_pilot'=>
        
    '00/00/00',
    'project'=>
        
    '<a href="/tv/shows/"></a>',
    products => array (
        
    'season1_bluray'=>
               
    '<a href="product link><img src="src /></a>,
           '
    next_product' =>
                   '
    next product link
    You can see I have a sub-array for products and the name as variable and link+image as value.
    Would it be better to create a new sub-array for each product?

    products => array (
    product1 =>array ('name' => 'season1', 'link' => 'link', 'format' => 'bluray')

    For instance I may come to a point where I need to display all bluray products, would it make more sense if I have a new subarray with a value of blueray in format..... or would just work as well if I have it like before where I have 'season1_bluray' as a variable in an array. I am wondering if it is worth creating a new sub array just for a couple more values/variables. Or does it not really matter either way?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seperating it wouldn't be a bad idea at all. Currently you would need to do string parsing in order to extract the blueray entries. This is more error prone, and slower. A seperate sub array would be the way to go.

    A flexible multidimensional array structure shares many of the same principals with good database design(although only some) if you're familiar with that.

  7. #7
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have decided it is indeed worth creating those sub arrays. I have just discovered that arrays can be generated on the fly as well. I have different numbers of products for each array so the script that generates the array on the fly would need to be smart enough to detect the numbers of rows and probbaly lots of string parsing to determin how many array values for each parent array need to be created.

    Since the array syntax is pretty simple, the code reduced would not be much, but I wonder if it is still worth creating a script that generates arrays on the fly? Is it more common for people to have actual arrays in their data files, or is it more common to have plain text files and then a script which creates arrays on the fly by scanning the text files?

    I ask because I have just started creating my text/written php database(I am not using any frameworks or apps, just a text editor). I don't want to spend ages creating something in a format/structure and realize much later I have to reformat everything again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •