如何从 PHP 演示文稿中的幻灯片中删除重复文本?

How to remove duplicate text from slides in PHP Presentation?

我做了一个简单的演示,并从 mysql 获取了文本和图像。例如,我有一个文本和 5 个图像。每张幻灯片必须有 2 张图像。但问题是每张幻灯片的 while 循环中的文本重复。

示例:

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
----------                                        ----------------

问题是这样的: 我想从第二张和第三张幻灯片中删除重复的文本。 这是我的代码

$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;

      }
```

如果您只想打印一次文本,则创建一个变量来存储信息,无论该信息是否已打印。此外,通过条件语句(如果有...)包装打印信息的代码,然后检查是否满足条件(在您的情况下,如果未打印信息),如果满足条件,则打印信息,否则没有。

查看添加的注释并修改代码如下

<?
$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;

      }