rgb192
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)
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)
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/reco rd is testme. Once located, you can perform the actions you want.
using PHP you will scan a row/record at a time looking for a reference i.e. Specific/field/column/reco
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.
ASKER
$data = $xlsx->rows();
for ($x=1;$x<100;$x++){
$query = "INSERT INTO table (..., col2, col3, ...) VALUES (...,'" . $data[{$x}][1] . "','" . $data[{$x}][2] . "'...)"
}
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
$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.
Using a for loop means that you know exactly how many rows have data.
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
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
Can I trim the rows I am not usingWhat criteria would you use to select these rows?
ASKER
>>What criteria would you use to select these rows?
I am doing a database update
there are less than 100 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.
$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.
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
I do not want to include the first (header) row
or blank (empty) rows
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
this was the best and most complete answer recommending and implementing foreach
thanks
thanks
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);