1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with MD array with template...

Discussion in 'PHP' started by whofarted, Sep 14, 2020.

  1. #1
    I need some help with this problem with my array. I'm trying to display "complaints" listed by date.
    I'm using this template -> ( https://github.com/isRuslan/php-template )
    I don't know how to loop inside a loop (or if it's possible with this template).

    I'm still new to (and learning) PHP / MySQL How would you go about this?

    Here's the relevant parts of the code. Let me know if you need more.

    // Pull all the post to display
    $sql = "SELECT *
            FROM complaint
            ORDER BY c_time DESC
            LIMIT 20";
    
    $result = $db->sql_query($sql);
    
    
    $complaints = array();
    
    while($thisdata = $result->fetch_assoc())
    {
        $complaints[] = array(
            'ID'            => $thisdata['id'],
            'DATE'             => date("g:i a - M j, Y", $thisdata['c_time']),
            'COMPLAINT'     => nl2br($thisdata['complaint']),
            'COMPLAINT_EDIT'=> $thisdata['complaint']);
    }
    
    $template->assign( 'CURRENT_DATE', date( "M j, Y - g:i a", time()));
    
    $template->assign( 'COMPLAINTS', $complaints);
    
    $template->parse('templates/default/complaint.html');
    PHP:
    the html:
    
    {loop COMPLAINTS}
     {ID}
     {DATE}
     {COMPLAINT}
    {end loop}
    HTML:
    This gives me every complaint in a seperate "table" or section of the page, what I WANT is to get the complaints listed by day IE:
    -> Day
    --> list of complaints for that day
    -> Day
    --> list of complaints for that day
    -> Day
    --> list of complaints for that day

    etc. I don't even understand how to use this part of the template code, does this have anything to to with it?
    $template->assign( 'items', array( array( 'name' => 'First' ), array( 'name' => 'Second' ) ) );
    PHP:
    I've never been able to use this part of the code. Anyway, any help is appreciated.
     
    Solved! View solution.
    whofarted, Sep 14, 2020 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #2
    welcome, love your username :0

    consider setting up your array like this

    
    while($thisdata = $result->fetch_assoc())
    {
    $complaints[$thisdata['c_time']][] = array(
    'ID' => $thisdata['id'],
    'DATE' => date("g:i a - M j, Y", $thisdata['c_time']),
    'COMPLAINT' => nl2br($thisdata['complaint']),
    'COMPLAINT_EDIT'=> $thisdata['complaint']);
    }
    
    Code (php):
    now if you do a simple foreach($complaints as $k => $v) you know when $k changes and to show the new date

    I'm questioning your use of the template system, they can be great but they can also slow you down because you have to learn another thing. Generating HTML isn't that hard.

    
    <?php
    // lets pretend your database returned this info, where 1000, 2000, 3000 are really timestamps
    $complaints = [1000 => [
        ['ID' => 1, 'DATE' => '2020-05-01', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ'],
        ['ID' => 2, 'DATE' => '2020-05-01', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ'],
        ['ID' => 3, 'DATE' => '2020-05-01', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ']
    ],
                   2000 => [
                       ['ID' => 1, 'DATE' => '2020-06-02', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ'],
                       ['ID' => 2, 'DATE' => '2020-06-02', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ'],
                       ['ID' => 3, 'DATE' => '2020-06-02', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ']
                   ],
                   3000 => [
                       ['ID' => 1, 'DATE' => '2020-07-03', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ'],
                       ['ID' => 2, 'DATE' => '2020-07-03', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ'],
                       ['ID' => 3, 'DATE' => '2020-07-03', 'COMPLAINT' => 'XYZ', 'COMPLAINT_EDIT' => 'XYZ']
                   ]
                  ];  
    
    
    ?><table>
        <?php
    
    foreach($complaints as $k => $v){
        $first = true;
        foreach($v as $row){
            echo "<tr><td>".($first?$row['DATE']:'&nbsp;')."</td><td>{$row['ID']}</td><td>{$row['COMPLAINT']}</td></tr>";
            $first = false;
        }
    }
        ?>
    </table>
    
    Code (php):
     
    sarahk, Sep 14, 2020 IP
  3. whofarted

    whofarted Greenhorn

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Thanks for the help. That's pretty much exactly what I want. My biggest problem now is getting it to work with the template class i'm using.

    sample of the array i've got now:
    Array
    (
        [Sep 14, 2020] => Array
            (
                [0] => Array
                    (
                        [ID] => 63
                        [DATE] => 4:02 pm - Sep 14, 2020
                        [COMPLAINT] => test 7
                        [COMPLAINT_EDIT] => test 7
                    )
    
                [1] => Array
                    (
                        [ID] => 62
                        [DATE] => 11:10 am - Sep 14, 2020
                        [COMPLAINT] => test 6 here we go!
                        [COMPLAINT_EDIT] => test 6 here we go!
                    )
    
                [2] => Array
                    (
                        [ID] => 61
                        [DATE] => 10:58 am - Sep 14, 2020
                        [COMPLAINT] => test 5, yup, yet again man!
                        [COMPLAINT_EDIT] => test 5, yup, yet again man!
                    )
    
            )
    
        [Sep 13, 2020] => Array
            (
                [0] => Array
                    (
                        [ID] => 60
                        [DATE] => 10:30 pm - Sep 13, 2020
                        [COMPLAINT] => test 4
                        [COMPLAINT_EDIT] => test 4
                    )
    
                [1] => Array
                    (
                        [ID] => 59
                        [DATE] => 8:26 pm - Sep 13, 2020
                        [COMPLAINT] => test 3
                        [COMPLAINT_EDIT] => test 3
                    )
    
                [2] => Array
                    (
                        [ID] => 58
                        [DATE] => 8:25 pm - Sep 13, 2020
                        [COMPLAINT] => test 2
                        [COMPLAINT_EDIT] => test 2
                    )
    
                [3] => Array
                    (
                        [ID] => 57
                        [DATE] => 8:24 pm - Sep 13, 2020
                        [COMPLAINT] => test 1
                        [COMPLAINT_EDIT] => test 1
                    )
    PHP:
     
    whofarted, Sep 14, 2020 IP
  4. #4
    upload_2020-9-15_16-7-30.png

    Based on their example it looks like you have to have a single dimension array and just omit the date from the non-first items and, honestly, by the time you've done all that you might as well just run the code.

    Is there a reason you're committed to this particular template? SMARTY might be a better fit.
     
    sarahk, Sep 14, 2020 IP
  5. whofarted

    whofarted Greenhorn

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Honestly it was the easiest to start using. This is mostly for fun & learning. I'm using this only for me, not a live site. ;) Smarty seemed to be overkill & from what I remember there was an "install" process. I just wanted to grab a template class get it running. Smarty just looked like bloatware for what I use it for.

    I started learning php by playing around and adapting phpbb2 (a long time ago) I got use to their template system. Now, with version phpbb3, it looks way more involved & changed. I was hoping this would fit my needs.

    I guess i'll have to look into Smarty again. Thanks for you help though.
     
    whofarted, Sep 14, 2020 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #6
    or just learn to write the html unassisted. That would be really valuable learning.
     
    sarahk, Sep 14, 2020 IP
  7. whofarted

    whofarted Greenhorn

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    yeah, I've done that before. I like the templating idea because it makes the code cleaner, easier to write, update, etc. Mixing the php & html is too messy. ;)
     
    whofarted, Sep 15, 2020 IP
  8. whofarted

    whofarted Greenhorn

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #8
    Any chance you could show me how to use this in Smarty templates now? :D
     
    whofarted, Sep 15, 2020 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #9
    I haven't used it in years, it was an early player in the move towards MVC style structuring of code. There should be loads of tutorials available.
     
    sarahk, Sep 15, 2020 IP
  10. whofarted

    whofarted Greenhorn

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #10
    I've gone through dozens so far & been through the documentation a few dozen times. It makes no sense to me.

    I can't even get it to print out an my array here.

    I now remember why I never started using it last time :(
     
    whofarted, Sep 15, 2020 IP