Asked  7 Months ago    Answers:  5   Viewed   36 times

this may be a simple question but am struggling to understand how to solve it. I have a form which allows the user to select either "custom" or "all" staff" to assign to a job.

If custom is selected the user selects staff by clicking each checkbox, I then insert these into a jobs table. This produces the array below (3, 1, 10 are the staff IDs)

    [0] => 3
    [1] => 1
    [2] => 10

If "all staff" is selected, I first query a select statement to get all the staff ID's from the staff table, and then insert these into the job table the same as above. However this produces the array :

    [0] => Array
            [staffID] => 1
            [0] => 1

    [1] => Array
            [staffID] => 23
            [0] => 23

    [2] => Array
            [staffID] => 26
            [0] => 26

    [3] => Array
            [staffID] => 29
            [0] => 29

How can I convert the array above to the first array shown?

I'm using the code below to query the database to get all the staff ID's and then inserting them.

    $select = $db->prepare("SELECT staffID FROM staff");
       $staff = $select->fetchAll();

    for($i = 0; $i<count($staff); $i++)
    $staffStmt = $db->prepare("INSERT INTO staffJobs (jobID, userID) VALUES (:jobID, :staffID)");
    $staffStmt->bindParam(':jobID', $jobID, PDO::PARAM_INT);
    $staffStmt->bindParam(':staffID', $staff[$i], PDO::PARAM_INT);



The first array inserts fine, however the last array inserts zeros for the staffID.

Any suggestions?

Thanks =).



Take a look at example 2 in the manual. In your first query you can use:

$staff = $select->fetchAll(PDO::FETCH_COLUMN, 0);

And your second array will have the same form as the first array.

Wednesday, March 31, 2021
answered 7 Months ago

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();
Friday, May 28, 2021
answered 5 Months ago

showdev's comment is correct that the PDO DSN does not allow host:port syntax.

If your CMS is defining DB_HOST outside of your control, you can't use that constant directly. But you can pull information out of it.

$host_port = preg_replace('/:(d+)/', ';port=${1}', DB_HOST);
$db = new PDO("mysql:host={$host_port};dbname=".DB_NAME.";charset=utf8", 
Friday, May 28, 2021
answered 5 Months ago

You could use some string manipulation.

You can count the number of ? you'd need by using str_repeat("?", count(explode(",", $refIdsPartial))). This will create your placeholders.

$totalCount = 
"SELECT referral, COUNT(username) AS cnt FROM accounts
WHERE referral IN (". str_repeat("?,", count(explode(",", $refIdsPartial))-1) . "?) GROUP BY referral";

Now that the placeholders are in place, you can explode the , from the string and execute

$ps_totalCounts->execute( explode(",", $refIdsPartial) );
Saturday, May 29, 2021
answered 5 Months ago

You can try

$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$l = iterator_to_array($it, false);

var_dump($l); // one Dimensional 
Saturday, May 29, 2021
answered 5 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 :