0

Hey guys.
I have a function that is returning options that the user has selected.
I need to get these options in an array for matching.

Here is my rough code.

$test = array();

$newVals = (printList($file_names));
$newArray = array_fill_keys($test, $newVals);




if (in_array("this.pdf", $test, TRUE))
  {
  echo "Match found<br>";
  }
else
  {
  echo "Match not found<br>";
  } 

The funtion that spits out user selections is this

function printList($file_names){
    //print the list
    echo "<p class=cart-list-header ></p>";
    echo "<ul class=cart-list>";
    $pdf_check = glob('pdf/*.pdf');
    $tpdf = array();
    foreach($pdf_check as $pdf){
        $tpdf[] = substr($pdf, 4);      
    }


    foreach($file_names as $file){
        if (in_array($file, $tpdf)){
             echo "'$file',";
        }else{
            echo "<li>$file <span class='red_txt'>(no pdf aviliable)</span></li>";
        }
    }       
    echo "</ul>";
}   

It generates a great list but I cannot seem to get it into the array correctly.
Any ideas?

3
Contributors
19
Replies
72
Views
2 Years
Discussion Span
Last Post by double_cola
0

The printList() should return a value if you want to use it in assignment (line 3 in the first snippet).

function printList($file_names){
    //print the list
    echo "<p class=cart-list-header ></p>";
    echo "<ul class=cart-list>";
    $pdf_check = glob('pdf/*.pdf');
    $tpdf = array();
    foreach($pdf_check as $pdf){
        $tpdf[] = substr($pdf, 4);      
    }


    foreach($file_names as $file){
        if (in_array($file, $tpdf)){
             echo "'$file',";
        }else{
            echo "<li>$file <span class='red_txt'>(no pdf aviliable)</span></li>";
        }
    }       
    echo "</ul>";

    // I guess this should be returned
    return $tpdf;
}   

Also I have a feeling that you are mixing two functionalities in one function (reading a directory and printing a list from the function parameter). You could split this into two functions.

Edited by broj1

0

Thanks. It is returning values but for some reason they are visible when they should be hidden on page and it does not work for matching.

0

So the glob function does not find any files? To confirm this put the following code immediatelly after line 5:

if($pdf_check == false) {
    die('Error: no matches found');
}

This will return error and stop the script if the glob is a problem. Then you have to check the path you provide to glob function. Post the result here.

Edited by broj1

0

No errors.
It doesnt seem to be posting into the array correctly.

If I manually type the results in and upload it works fine.
And for some reason whenever I try to pull printList($file_names) into the array the text is visible.

0

Ultimately what I need is a way for users to select pages that post to a cart (array) and when they go to view their cart they can see what thy have selected and what else is available.

0

So you think the $tpdf array is empty? Put this code on line 10 in the function above:

die(print_r($tpdf, 1));

This will display the contents of the array after it has been filled with the result of the glob function and stop the script. Please post the result here.

Edited by broj1

0

Here is the page with the function

<?php

/**
 * Doc Block
 *  this need to be on the landing page the rest just need a session start and an add page include
 * session_start(); // start up your PHP session!
 * 
 * $_SESSION['page_url'] = array();
 *  include('add_page.php');
 * 
 *  
 * add page functions
 * 
 * addpage();
 *  adds page to cart
 *
 *  cartCount() prints the cart count
 *
 *  findFileNames() finds pdf name based on url file name
 *  printList prints the list of pdfs to be printed in report
 *
 *  createPDF() creates the merged pdf needs to have include ('includes/PDFMerger.php');
 *  before it is called
 *
 *   
 * 
 */

function addpage(){
    $current_page_url = $_SERVER["REQUEST_URI"];    

    if(!in_array($current_page_url, $_SESSION['page_url'])){
        $_SESSION['page_url'][] = $current_page_url;
        echo "page has been added to the download list<br>";     
    }
}


function printCount(){
    $count = count($_SESSION['page_url']);
    echo $count;
}


//this trims to just the file name with out .php and create a list of all pages
//makes an array to find pdf files
function findFileNames(){
    $file_names = array();
    foreach($_SESSION['page_url'] as $page_url){

        //finds the position of the last slash to cut the rest of the url we dont need
        $trim_from = strrpos($page_url, '/');
        //trims off the .php
        $trim_from2 = strrpos($page_url, '.');

        $trimmed = substr($page_url, $trim_from + 1);
        $only_name = substr($trimmed, 0, -4);
        $file_names[] = $only_name . ".pdf";            
    }
    return $file_names;
}   

function printList($file_names){
    //print the list

    $pdf_check = glob('pdf/*.pdf');
    $tpdf = array();
    foreach($pdf_check as $pdf){
        $tpdf[] = substr($pdf, 4);      
    }


    foreach($file_names as $file){
        if (in_array($file, $tpdf)){
             echo "'$file',";
        }else{
            echo "<li>$file <span class='red_txt'>(no pdf aviliable)</span></li>";
        }
    }       
    echo "</ul>";

    return $tpdf;
}       

function emptyCart(){
    //empty cart
    if(isset($_POST['reset'])){
        unset($_SESSION['page_url']);
        $_SESSION['page_url'] = array();
        $_SESSION['count'] = 0;
    }
}


function createPDF($file_names){
    //start pdf write command
    if(isset($_POST['action'])){            
        if(!empty($file_names)){
            array_unshift ($file_names, 'cover.pdf');

        //avalibable pdfs
        $pdfs_avalible = array();       

        //check for avaliable pdfs
        foreach($file_names as $file_name){
            // check to see if the pdf is avliable
            $pdf_check = glob('pdf/*.pdf');
            $tpdf = array();
            foreach($pdf_check as $pdf){
                $tpdf[] = substr($pdf, 4);      
            }
            //if pdf is avaliable add to pdf list
            if(in_array($file_name, $tpdf)){
                $pdfs_avalible[] = $file_name;

            }
        }

        if(!empty($pdfs_avalible)){
            $pdf = new PDFMerger;   
            foreach($pdfs_avalible as $p){
                $pdf->addPDF('pdf/' . $p, 'all');       
            }   

            $pdf->merge('download', 'CintasCSR.pdf');
        }






        }else{
            echo "<div class=alert>no files in cue</div>";
        }

    }   
}

//adds check out buttons to cart page

//page redirect funtion location is page to go to

function page_redirect($location)
 {
   echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$location.'">';
   exit; 
 }

This is a seperate page where I am trying to run the array.

  <?php

$test = array();
$newVals = (printList($file_names));
$newArray = array_fill_keys($test, $newVals);


if (in_array("index.pdf", $test, TRUE))
  {
  echo "<br>Match found<br>";
  }
else
  {
  echo "Match not found<br>";
  } 
if (in_array("Glenn",$test, TRUE))
  {
  echo "Match found<br>";
  }
else
  {
  echo "Match not found<br>";
  }

if (in_array(23,$test, TRUE))
  {
  echo "Match found<br>";
  }
else
  {
  echo "Match not found<br>";
  }
?>
0

So you think the $tpdf array is empty? Put this code on line 10 in the function above:

die(print_r($tpdf, 1));

This will display the contents of the array after it has been filled with the result of the glob function and stop the script. Please post the result here.

0

I think the $test = array(); is empty.

I tried the die(print_r($tpdf, 1)); and it broke the page.

0

So I have found that printList($file_names) is posting but it is not going into the array $test = array();
This is really becoming a brain buster for me.... lol

0

I have simplified the function

function addpage(){
    $current_page_url = $_SERVER["REQUEST_URI"];    

    if(!in_array($current_page_url, $_SESSION['page_url'])){
        $_SESSION['page_url'][] = $current_page_url;
        echo "page has been added to the download list<br>";     
    }
}


function printCount(){
    $count = count($_SESSION['page_url']);
    echo $count;
}


//this trims to just the file name with out .php and create a list of all pages
//makes an array to find pdf files
function findFileNames(){
    $file_names = array();
    foreach($_SESSION['page_url'] as $page_url){

        //finds the position of the last slash to cut the rest of the url we dont need
        $trim_from = strrpos($page_url, '/');
        //trims off the .php
        $trim_from2 = strrpos($page_url, '.');

        $trimmed = substr($page_url, $trim_from + 1);
        $only_name = substr($trimmed, 0, -4);
        $file_names[] = $only_name . ".pdf";            
    }
    return $file_names;
}   

function printList($file_names){


    foreach($file_names as $file){

             echo "$file,";

    }       

    // I guess this should be returned

}   

still no good tho.

0

OK, I am a bit confused now :-). Are you saying the printList() function in above code does not echo anything?

0

Double_cola, Broj1 is really trying to help you. Do what he says and ANSWER HIS QUESTIONS. Restating the problem and reposting large sections of code is not going to get you to a solution any faster.

3

My fault guys, I did not mean to frustrate anyone, please forgive. I really do appreciate the help. Im still a newbie and a little overwhelmed lol. I am still learning how to accurately discuss these problems. Daniweb has been such a vast resource.

Broj1, it seems that printList($file_names) does echo correctly, it just doesnt go into the array.

I will try my best to answer these question better.

Edited by double_cola

0

My fault guys, I did not mean to frustrate anyone

No worries, mate. It's easy to get lost after that many posts :-)

Broj1, it seems that printList($file_names) does echo correctly, it just doesnt go into the array.

OK, but the thing is that you are echoing one thing ($file_names, which is a function parameter), and storing into array another thing (the result array of the glob function). This is why your simplified test above won't yield any useful result.

You have to test the function the way it will show whether the glob function returns valid results which you then store into the array. You can do that by using the die and print_r tricks I showed above. This debugging principle is simple: stop the execution of script using the die function right after the point where you want to inspect some variable and print out the variable using print_r. There are more advanced debugging techniques but at the moment you do not need them.

Edited by broj1

0

Ok Broj1, I will give it a shot and post back what I find.
Thanks again.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.