CodeIgniter Forums
Only display the month once - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Only display the month once (/thread-69548.html)



Only display the month once - wolfgang1983 - 12-12-2017

Hi, In my list below as you can see it has printed the word "Nov" & "Jan" multiple times

I use codeigniter with phpword to get the database results


[Image: 3kV2uW1VzyU0.png]


I would like the word of the month to only show once on the first one if has more than one like in this image


[Image: 3kV4PYZ24C5K.png]


Here is my controller code

PHP Code:
public function export() {
    
$phpWord = new \PhpOffice\PhpWord\PhpWord();

    
$leftTabStyleName 'centerTab';
    
$phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center'4680))));

    
// New portrait section
    
$section $phpWord->addSection();

    
// Add listitem elements
    
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
    
$fontStyle->setBold(false);
    
$fontStyle->setName('Tahoma');
    
$fontStyle->setSize(16);
    
$section->addText("\tClub Program " date('Y') .' / ' date('Y'strtotime('+1 year')), $fontStyle$leftTabStyleName);

    
$section->addTextBreak();

    
$event_lists 'event_lists';

    
$phpWord->addParagraphStyle(
        
$event_lists,
         
   array(
         
   'tabs' => array(
         
       new \PhpOffice\PhpWord\Style\Tab('left'1000),
         
       new \PhpOffice\PhpWord\Style\Tab('center'1000),
         
       new \PhpOffice\PhpWord\Style\Tab('right'1000),
         
   )
        )
    );

    
$results $this->get_events_for_export();

    foreach (
$results as $result) {

        
$date strtotime($result['event_date']);

        
$section->addText(date('M'$date) ."\t"date('d'$date) ."\t"date('D'$date) ."\t"htmlentities($result['event_title']), null$event_lists);

    }

    
$filename 'club_program-' time() . '.docx';

    
header("Content-Description: File Transfer");
    
header('Content-Disposition: attachment; filename="' $filename '"');
    
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    
header('Content-Transfer-Encoding: binary');
    
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    
header('Expires: 0');

    
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord'Word2007');
    
$objWriter->save("php://output");

    exit();




RE: Only display the month once - ivantcholakov - 12-12-2017

Code:
$last_month = '*';

foreach ($results as $result) {

    $date = strtotime($result['event_date']);
    $month = date('M', $date);
    $day = date('d', $date);
    $day_of_week = date('D', $date);

    $section->addText(($month != $last_month ? $month : '')."\t". $day ."\t". $day_of_week ."\t". htmlentities($result['event_title']), null, $event_lists);

    $last_month = $month;
}



RE: Only display the month once - wolfgang1983 - 12-13-2017

(12-12-2017, 10:03 PM)ivantcholakov Wrote:
Code:
$last_month = '*';

foreach ($results as $result) {

   $date = strtotime($result['event_date']);
   $month = date('M', $date);
   $day = date('d', $date);
   $day_of_week = date('D', $date);

   $section->addText(($month != $last_month ? $month : '')."\t". $day ."\t". $day_of_week ."\t". htmlentities($result['event_title']), null, $event_lists);

   $last_month = $month;
}

Sorry did not work repeated list multiple times.

I have found solution'sn from this answer

https://stackoverflow.com/questions/47785003/limit-the-number-of-times-month-name-shows


RE: Only display the month once - ivantcholakov - 12-13-2017

This very basic piece of code can not cause the effect you described. You should have used it as a replacement of your cycle.