0

I'm unable to generate the array that contains keys [0][1][2][3][4][5]

       <?php
            $cnt =0;
            while ($page = mysql_fetch_assoc($jpgpages))
            {
                $cnt++;                    
                echo '<a href="'.JPG_PAGE_IMAGE_FOLDER.'/'.$page['filename'].'" target="_blank">';
                echo '<div class="jpg-img-wrapper">';                                           
                echo '<img border="0" class="jpg-img-thumb" src="'.JPG_PAGE_IMAGE_FOLDER.'/thumb/thumb_'.$page['filename'].'" />';
                echo '<div>'.$page['title'].'</div>';                    
                echo '</div>';                       
                echo '</a>';                 
                //if ($cnt % 6 == 0)
                    //echo '<div clear:both;></div>';   


                        $image_path = JPG_PAGE_IMAGE_FOLDER.'/'.$page['filename'];                  

                    $pdf_page = array($image_path);
                    print_r($pdf_page); 
                    $pdf = new Imagick($pdf_page);
                    $pdf->setImageFormat('pdf');
                    //$pdf->writeImages('combined.pdf', true);

            }

it returns the following array :

print_r($pdf_page);

 `Array ( [0] => pages/jpp_12052020211355.jpg ) Page 2Array ( [0] => pages/jpp_12052020211420.jpg ) Page 3Array ( [0] => pages/jpp_12052020211423.jpg ) Page 4Array ( [0] => pages/jpp_12052020211445.jpg )` 
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
Waqas Saeed
  • 31
  • 1
  • 1
  • 10
  • That is because you are returning / printing an array on each pass of the while loop. So you have Array ([key]=>[Element]) and then on the next pass, Array ([key]=>[Element]) so the key value does not increment. If you want this to be an array then change $pdf_page to $pdf_page[]. If you want an array of arrays, then use $pdf_page[] = array(foo); – alpharomeo May 13 '20 at 10:11
  • it returns the following result if I move out print_r out of while loop and make the $pdf_page to $pdf_page[] Array ( [0] => Array ( [0] => pages/jpp_12052020211355.jpg ) [1] => Array ( [0] => pages/jpp_12052020211420.jpg ) [2] => Array ( [0] => pages/jpp_12052020211423.jpg ) [3] => Array ( [0] => pages/jpp_12052020211445.jpg ) [4] => Array ( [0] => pages/jpp_12052020211500.jpg ) [5] => Array ( [0] => pages/jpp_12052020211507.jpg ) [6] => Array ( [0] => pages/jpp_12052020211526.jpg ) [7] => Array ( [0] => pages/jpp_12052020211544.jpg ) ) – Waqas Saeed May 13 '20 at 10:29
  • Yes. that is true, I require multiple page in a pdf. the result; it generated the unordered keys in arrays Array ( [0] => Array ( [0] => pages/jpp_12052020211355.jpg ) [1] => Array ( [0] => pages/jpp_12052020211420.jpg ) [2] => Array ( [0] => pages/jpp_12052020211423.jpg ) [3] => Array ( [0] => pages/jpp_12052020211445.jpg ) [4] => Array ( [0] => pages/jpp_12052020211500.jpg ) [5] => Array ( [0] => pages/jpp_12052020211507.jpg ) [6] => Array ( [0] => pages/jpp_12052020211526.jpg ) [7] => Array ( [0] => pages/jpp_12052020211544.jpg ) ) – Waqas Saeed May 13 '20 at 10:33
  • That is correct - if you don't want an array of arrays, then simply remove the array(foo) in the $php_page line. Eg: if you want result to be Array ( [0] => pages/jpp_12052020211355.jpg, [1] => pages/jpp_12052020211420.jpg, [2] => pages/jpp_12052020211423.jpg, [3] => pages/jpp_12052020211445.jpg) ....... so on and so forth then you will use $php_page[] = $image_path; – alpharomeo May 13 '20 at 10:35
  • thanks a lot @alpharomeo it works and able to get all 8 files in a pdf format. thanks a lot – Waqas Saeed May 13 '20 at 10:43
  • Does this answer your question? [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dharman May 13 '20 at 11:40

0 Answers0