Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

assign a variable to rows 2 down

follow up to question

https://www.experts-exchange.com/questions/27874313/column2-and-column3.html


want to assign a variable to
rows 2 down
columns 2 and 3

I need to store in a database
(I will add database code)
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I am guessing that you're referring to this:

$xlsx->rows()
Array
(
    [0] => Array
        (
            [0] => Rank
            [1] => Country / Territory
            [2] => Population
            [3] => Date of estimate
            [4] =>  % of World population
        )

    [1] => Array
        (
            [0] => 1
            [1] =>  People's Republic of Chinan2
            [2] => 1,341,300,000
            [3] => December 21, 2010
            [4] => 0.195
        )

    [2] => Array
        (
            [0] => 2
            [1] =>  India
            [2] => 1,191,730,000
            [3] => December 21, 2010
            [4] => 0.173
        )

    [3] => Array
        (
            [0] => 3
            [1] =>  United States
            [2] => 310,951,000
            [3] => December 21, 2010
            [4] => 0.0451
        )

Open in new window

And you want to change 1,191,730,000 and December 21, 2010 to some other values.  This is not tested code but it might look something like this:

$rows = $xlsx->rows();
$rows[2][2] = 'Hello';
$rows[2][3] = 'World';
var_dump($rows);
Avatar of arnold
Coding you would not want to go into a fixed length type of requirement as it is limiting.
using PHP you will scan a row/record at a time looking for a reference i.e. Specific/field/column/record is testme. Once located, you can perform the actions you want.
want to assign a variable to
rows 2 down
columns 2 and 3

You want to assign a variable to those columns or extract the values from those columns

To store in a db
$data = $xlsx->rows();

$query = "INSERT INTO table (..., col2, col3, ...) VALUES (...,'" . $data[1][1] . "','" . $data[1][2] . "'...)"

Your arrays are 0 based so row 2 is index 1 and columns 2 and 3 are index 1 and 2 respectively.
Avatar of rgb192

ASKER

$data = $xlsx->rows();

for ($x=1;$x<100;$x++){
$query = "INSERT INTO table (..., col2, col3, ...) VALUES (...,'" . $data[{$x}][1] . "','" . $data[{$x}][2] . "'...)"
}

Open in new window



what if there are only 50 rows

is there a better foreach insert
you could do a batch insert so

$query = "INSERT INTO table (..., col2, col3, ...)  VALUES ";
for ($x=1;$x<100;$x++){
$query .= "(...,'" . $data[{$x}][1] . "','" . $data[{$x}][2] . "'...),"
}
$query = rtrim($quer, ',');
// execute query here

Not really much of a difference
You should check whether there is data in rows/columns before you insert into a database.
Using a for loop means that you know exactly how many rows have data.
Avatar of rgb192

ASKER

>>You should check whether there is data in rows/columns before you insert into a database.
Using a for loop means that you know exactly how many rows have data.

how


and julianH
can I trim the rows that I am not using
Something like this
$query = "INSERT INTO table (..., col2, col3, ...)  VALUES ";
foreach($xlsx->rows() as $row)
{
  $query .= "(...,'" . $row[1] . "','" . $row[2] . "'...),"
}

$query = rtrim($query, ',');
// execute query here

Open in new window

Can I trim the rows I am not using
What criteria would you use to select these rows?
Avatar of rgb192

ASKER

>>What criteria would you use to select these rows?

I am doing a database update

there are less than 100 rows
No what I meant was given

$xml->rows()

Are there any rows returned by the above that you do not want to process - if not then the code above will ensure you only process what is returned by the $xml->rows() call.
Avatar of rgb192

ASKER

>>Are there any rows returned by the above that you do not want to process

I do not want to include the first (header) row
or blank (empty) rows
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

this was the best and most complete answer recommending and implementing foreach

thanks