How to remove duplicate text from slides in PHP Presentation?

163 views Asked by At

I made a simple presentation and get text and images from mysql. For example I have one text and 5 images. There must be 2 images per slide. But the problem is the text duplicates in while loop on every slide.

Example:

This is how it works now:                         I want this:
-----------                                       ----------------
Slide1                                            Slide1
text 1                                            text 1
2 images                                          2 images
-----------                                       ----------------
-----------                                       ----------------
Slide2                                            Slide2
text 1                                        
2 images                                          2 images
-----------                                       ----------------
-----------                                       ----------------
Slide3                                            Slide3
text2        
1 image                                           1 image
----------                                        ----------------

This is the problem: enter image description here I want to remove duplicate text from second and third slide. This is my code

$brojacslikapostrani = 0;
$query1 = "SELECT izvestaji.operacija, izvestaji.ucinak, izvestaji.id, operacije.nazivEng FROM izvestaji INNER JOIN operacije ON izvestaji.operacijaId=operacije.id WHERE izvestaji.datum='$datum' AND izvestaji.projekatId='$projekatId'";
$result1 = mysqli_query($con, $query1);
while ($row = mysqli_fetch_array($result1)) {           

            $id = $row['id'];

            $query2 = "SELECT img_name FROM slike WHERE izvestajId='$id' AND datum='$datum'";
            $result2 = mysqli_query($con, $query2);

            while ($row2 = mysqli_fetch_array($result2)) {

                if ($brojacslikapostrani % 2 == 0) {
                    $currentSlide = createTemplatedSlide($objPHPPresentation);

                    //Pozadina
                    $oBkgImage = new Image();
                    $oBkgImage->setPath('./resources/background.png');
                    $currentSlide->setBackground($oBkgImage);

                    $shape = $currentSlide->createRichTextShape();
                    $shape->setHeight(100)
                          ->setWidth(930)
                          ->setOffsetX(10)
                          ->setOffsetY(20);
                    $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);

                    $textRun = $shape->createTextRun(strtoupper($row['nazivEng']));
                    $textRun->getFont()->setBold(true)
                          ->setSize(48)
                          ->setName('Times New Roman')
                          ->setColor($colorBlack);


                    // ----------- THIS CODE MAKES THE PROBLEM ------------------------
                        echo date('H:i:s') . ' Create a shape (rich text)' . EOL;
                        $shape = $currentSlide->createRichTextShape()
                              ->setHeight(600)
                              ->setWidth(930)
                              ->setOffsetX(10)
                              ->setOffsetY(130);
                        $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)
                              ->setMarginLeft(25)
                              ->setIndent(-25);
                        $shape->getActiveParagraph()->getFont()->setSize(20)
                              ->setName('Times New Roman')
                              ->setColor($colorBlack);
                        $shape->getActiveParagraph()->getBulletStyle()->setBulletType(Bullet::TYPE_BULLET);


                        $shape->createTextRun($row['operacija'])->getFont()->setBold(true);
                        $shape->createTextRun(": ")->getFont()->setBold(true);
                        $shape->createTextRun($row['ucinak']);
                    //------------------------------------------------------------------------

                    $shape1 = $currentSlide->createDrawingShape();
                    $shape1->setName('Part page');
                    $shape1->setDescription('Page');
                    $shape1->setPath('../../../../files/izvestaji/' . $project . '/' . $datum . '/' . $row['nazivEng'] . '/' . $row2['img_name']);
                    $shape1->setResizeProportional(false);
                    $shape1->setOffsetX(75);
                    $shape1->setOffsetY(300);
                    $shape1->setHeight(400);
                    $shape1->setWidth(400);
                } else {
                    $shape1 = $currentSlide->createDrawingShape();
                    $shape1->setName('Part page');
                    $shape1->setDescription('Page');
                    $shape1->setPath('../../../../files/izvestaji/' . $project . '/' . $datum . '/' . $row['nazivEng'] . '/' . $row2['img_name']);
                    $shape1->setResizeProportional(false);
                    $shape1->setOffsetX(495);
                    $shape1->setOffsetY(300);
                    $shape1->setHeight(400);
                    $shape1->setWidth(400);
                }

                $brojacslikapostrani++;
            }
            $brojacslikapostrani = 0;

      }
```
1

There are 1 answers

0
Jimmix On BEST ANSWER

If you want to print text just once then create variable that stores information whether the information was already printed or not. Additionally wrap the code that prints the information by conditional statement (if something ...) and then check if the condition is met (in your case if the information was not printed) and if the condition is met then information would be printed and otherwise not.

See comment ADDED and modify code as below

<?
$brojacslikapostrani = 0;
$query1 = "SELECT izvestaji.operacija, izvestaji.ucinak, izvestaji.id, operacije.nazivEng FROM izvestaji INNER JOIN operacije ON izvestaji.operacijaId=operacije.id WHERE izvestaji.datum='$datum' AND izvestaji.projekatId='$projekatId'";
$result1 = mysqli_query($con, $query1);
while ($row = mysqli_fetch_array($result1)) {           

            $id = $row['id'];

            $query2 = "SELECT img_name FROM slike WHERE izvestajId='$id' AND datum='$datum'";
            $result2 = mysqli_query($con, $query2);

            $textPrinted = false; #<= ADDED
            while ($row2 = mysqli_fetch_array($result2)) {

                if ($brojacslikapostrani % 2 == 0) {
                    $currentSlide = createTemplatedSlide($objPHPPresentation);

                    //Pozadina
                    $oBkgImage = new Image();
                    $oBkgImage->setPath('./resources/background.png');
                    $currentSlide->setBackground($oBkgImage);

                    $shape = $currentSlide->createRichTextShape();
                    $shape->setHeight(100)
                          ->setWidth(930)
                          ->setOffsetX(10)
                          ->setOffsetY(20);
                    $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);

                    $textRun = $shape->createTextRun(strtoupper($row['nazivEng']));
                    $textRun->getFont()->setBold(true)
                          ->setSize(48)
                          ->setName('Times New Roman')
                          ->setColor($colorBlack);


                    // ----------- THIS CODE MAKES THE PROBLEM ------------------------
                    #<= ADDED 2lines
                    if ($textPrinted === false) {
                        $textPrinted = true;

                        echo date('H:i:s') . ' Create a shape (rich text)' . EOL;
                        $shape = $currentSlide->createRichTextShape()
                              ->setHeight(600)
                              ->setWidth(930)
                              ->setOffsetX(10)
                              ->setOffsetY(130);
                        $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)
                              ->setMarginLeft(25)
                              ->setIndent(-25);
                        $shape->getActiveParagraph()->getFont()->setSize(20)
                              ->setName('Times New Roman')
                              ->setColor($colorBlack);
                        $shape->getActiveParagraph()->getBulletStyle()->setBulletType(Bullet::TYPE_BULLET);


                        $shape->createTextRun($row['operacija'])->getFont()->setBold(true);
                        $shape->createTextRun(": ")->getFont()->setBold(true);
                        $shape->createTextRun($row['ucinak']);

                    } #<= ADDED
                    //------------------------------------------------------------------------

                    $shape1 = $currentSlide->createDrawingShape();
                    $shape1->setName('Part page');
                    $shape1->setDescription('Page');
                    $shape1->setPath('../../../../files/izvestaji/' . $project . '/' . $datum . '/' . $row['nazivEng'] . '/' . $row2['img_name']);
                    $shape1->setResizeProportional(false);
                    $shape1->setOffsetX(75);
                    $shape1->setOffsetY(300);
                    $shape1->setHeight(400);
                    $shape1->setWidth(400);
                } else {
                    $shape1 = $currentSlide->createDrawingShape();
                    $shape1->setName('Part page');
                    $shape1->setDescription('Page');
                    $shape1->setPath('../../../../files/izvestaji/' . $project . '/' . $datum . '/' . $row['nazivEng'] . '/' . $row2['img_name']);
                    $shape1->setResizeProportional(false);
                    $shape1->setOffsetX(495);
                    $shape1->setOffsetY(300);
                    $shape1->setHeight(400);
                    $shape1->setWidth(400);
                }

                $brojacslikapostrani++;
            }
            $brojacslikapostrani = 0;

      }