Asked  7 Months ago    Answers:  5   Viewed   39 times

I'm trying to send an array to another page.

What I tryed before was:

page1

<input type='hidden' name='input_name' value='<?php print_r($array_name); ?>' />

And page2

<?php 
$passed_array = $_POST['input_name'];
?>

Now how do I make $passed_array act like an array?

Or do you know of any other way of solving this problem?

Thanks, Mike.

Edit: The reason I want to do it this way is because I need to avoid sessions and cookies.

 Answers

68

You could put it in the session:

session_start();
$_SESSION['array_name'] = $array_name;

Or if you want to send it via a form you can serialize it:

<input type='hidden' name='input_name' value="<?php echo htmlentities(serialize($array_name)); ?>" />

$passed_array = unserialize($_POST['input_name']);

The session has the advantage that the client doesn't see it (therefore can't tamper with it) and it's faster if the array is large. The disadvantage is it could get confused if the user has multiple tabs open.

Edit: a lot of answers are suggesting using name="input_name[]". This won't work in the general case - it would need to be modified to support associative arrays, and modified a lot to support multidimensional arrays (icky!). Much better to stick to serialize.

Wednesday, March 31, 2021
 
Dev
answered 7 Months ago
Dev
92

If you could index the array by gear or some unique value, it would be a lot easier.

$indexed = array();

// create an array using 'gear' as the index
foreach($arrayValue as $value) {
    $indexed[$value['gear']] = $value;
}

// loop over each object
foreach($objectArray as $obj) {
    $value = $indexed[$obj->gear]; // find the corresponding array
    foreach($value as $name => $val) {
        $obj->$name = $val; // assign each array index/value pair to the object
    }
}

If possible to get your code to return the array with the index by default, you can remove the first foreach loop.

Hope that helps.

Wednesday, March 31, 2021
 
wavyGravy
answered 7 Months ago
39

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;
    }

    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);
    }
}

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Friday, May 28, 2021
 
Wilk
answered 5 Months ago
53

To send an array of params via URL from Javascript to PHP/Codeigniter, you need to follow 2 steps:

  1. Convert your params array to JSON String and send that as a single query param.
    para = JSON.stringify(para);
    window.open('somePDFView?params='+para,'_blank');

  2. Decode the query string param (i.e JSON string) at your controller
    $params = json_decode($_GET['params']);

Now you can use all the params at your controller by accessing through $params.

Your code block becomes:

function viewReport(mode,someid){
    if(mode==0){
        var para= new Array();
        para['para1'] = 'para1'||0;
        para['para2']= 'para2' || 0;
        console.log(para);
        para = JSON.stringify(para);
        window.open('somePDFView?params='+para,'_blank'); 
    }else{
        var para=[];
        var paraelements={
          para1:'anotherpara1'||0,
          para2:'anotherpara2'||0
        };
        para[0]=paraelements;
        para = JSON.stringify(para);
        window.open('somePDFView?params='+para,'_blank'); 
    }
}

Try this out and let me know if you still faces this issue.

Saturday, May 29, 2021
 
HexaGridBrain
answered 5 Months ago
13

There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

Try reading some of the threads on the PHPExcel discussion board discussing the issue, such as this one; or previous answers here on SO such as this one or this one

Tuesday, July 6, 2021
 
Slinky
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :