How to remove rows below a certain row number phpexcel - php

I would like to remove the bottom records. When I reach row 6 anything below that I would like to remove in phpexcel.
for($tot=2; $tot<$highestRowEMDist; $tot++){
$chkven = $objPHPExcel->getActiveSheet()->getCell('C'.$tot)->getValue();
if ($chkven!="Test" ) {
$objPHPExcel->getActiveSheet()->removeRow(7,0);
}
}
All I want is if I reach row 6 anything below that needs to be deleted

I have found the solution
for($row=2; $row < $highestRowEMDist; $row++){
$value = $objPHPExcel->getActiveSheet()->getCell('C'.$row)->getValue();
if ($value != "Quest") {
$objPHPExcel->getActiveSheet()->removeRow($row, $row);
}
}

Related

PHPExcel deleting rows from large worksheet

I am trying to delete rows based on conditions such as Cell B = 59/61. However, PHPExcel loads the rows very slowly. For a worksheet with 18000 rows/3MB, it takes around 4 hours 30 minutes to load. How can I improve the speed of the data worksheet to load and delete?
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//Defining File Type
$fileType = "Excel2007";
//Retrieving File
$tmpfname = "bigfile.xlsx";
//Loading file into PHPExcel
$objPHPExcel = PHPExcel_IOFactory::load($tmpfname);
$worksheet = $objPHPExcel->getSheet(0); //Worksheet of file defined as first
$lastRow = $worksheet->getHighestRow();
//Determine which rows to be remove
$DeletedRows = [];
$DeletedRowCount = 0;
for ($row = 2; $row <= $lastRow; $row++) {
//Checker
$CellA = $worksheet->getCell('A' . $row)->getValue();
$CellB = $worksheet->getCell('B' . $row)->getValue();
$CellE = $worksheet->getCell('E' . $row)->getValue();
//To check condition
if ($CellB != 8 && $CellB != 9 && $CellB != 18 && $CellB != 19) {
$DeletedRows[] = $row;
continue;
}
//To check if condition
else if ($CellE == 59 || $CellE == 61){
$DeletedRows[] = $row;
continue;
}
}
//Removing the rows
//Deleting this way as when one row deleted, one row less.
foreach ($DeletedRows as $key => $value) {
$row = $value - $DeletedRowCount;
$objPHPExcel->getActiveSheet()->removeRow($row, 1);
$DeletedRowCount++;
}
//Write file into original file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($tmpfname);
Well one way to improve performance is to work upwards from the bottom of the spreadsheet rather than downwards.
When you delete a row, PHPExcel has to check the cells in every row below it and adjust references in those cells if necessary. This is more efficient, with fewer checks/updates necessary when you work from the bottom of the worksheet upwards.
It's also a lot more efficient if you can delete more than one row at a time. If you need to delete rows 15, 16 and 17; then
$objPHPExcel->getActiveSheet()->removeRow(15, 3);
is 3x faster than
$objPHPExcel->getActiveSheet()->removeRow(15, 1);
$objPHPExcel->getActiveSheet()->removeRow(16, 1);
$objPHPExcel->getActiveSheet()->removeRow(17, 1);
So spending a bit of time evaluating your $DeletedRows array looking for ranges could be beneficial
Your code would also be a lot faster if, instead of building an array of row numbers to delete in the first loop and deleting them in the second, you deleted them in the first loop... 1 loop will inevitably be faster than 2 loops
Lastly, there is one major flaw with your approach. If your first loop tells you that you need to delete rows 1, 5; you delete row 1 and then 5.... except that row 5 is actually row 4 since you deleted row 1, and you've actually just deleted the row that was row 6 when you did your original loop to assess which rows to delete.... working from bottom to top would prevent this problem as well as being more efficient

Total count inside an if of while loop

I'm trying to figure out how to do the totalCount inside an if of a while loop
but so far are not successfull in doing so
How can i reach my goal with this code
$rowCount = 1;
while ($clientrows = $statement->fetch()) {
// should return 1000 rows
if ($clientrows['company'] != $current_client) {
echo $rowCount++;
$rowCount++;
// return clientX (one time)
}
if ($clientrows['time'] != NULL ) {
// returns X rows of clientX(from the previews count
// could be any number but lets say here it should be 100 rows
echo $rowCount++;
$rowCount++;
// i need the total counts inside this IF($clientrows['time'] != NULL )
}
echo $rowCount++; // doesn't show anything
// create here the IF total count of previews IF function
// if($totalCounts) {
// echo ' Do something here ';
// }
}
echo $rowCount++; //shows the total rows of while loop while ($clientrows = $statement->fetch())
if i put the count++ right after the IF function it doesn't show anything if i do it at the end then it counts the total of everything
EDIT
Number of counts returns all the rows of my while loop of while ($clientrows = $statement->fetch() (that is about 1000)
what i need to achieve is to get the total rows of this IF statment
if ($clientrows['time'] != NULL ) { }
Which could be 100 or 101 or 200 of the 1000 rows
Help?
"++" is incrementation. It will add + 1 into your variable. It's equal as $a = $a + 1;
Intead of
echo $rowCount++;
$rowCount++;
write
echo $rowCount;
$rowCount++;
Edit.
I just read your comment in script
//shows the total rows of while loop while ($clientrows = $statement->fetch())
Can not you just simply use $statment->num_rows()?
When you do echo $rowCount++; you increment the $rowCount even if you only echo it.

How to do something different for the last item in an array (from mysql_fetch_array)? [duplicate]

This question already has answers here:
Echo a comma on all but the last value? (result from mysql_fetch_array)
(11 answers)
Why doesn't it prevent further looping?
(2 answers)
Closed 8 years ago.
I am trying to make sure that commas go after all of the items EXCEPT for the last one - I am trying to figure out how to calculate out what the last item is for variable lists. I decided to try to use count($variablename) but it doesn't seem to work.
Is there something wrong with my syntax?
$servicearray = mysql_query("select serv_id from org_serv_xref where org_id='".$org_id."'");
$servi = 0;
while ($servicearrayrow = mysql_fetch_array($servicearray)) {
$servdescarray = mysql_query("select serv_desc from service where serv_id='".$servicearrayrow['serv_id']."'");
while ($servdescarrayrow = mysql_fetch_array($servdescarray)) {
if ($servi < 5 OR $servi < count($servdescarrayrow)) {
ECHO $servdescarrayrow['serv_desc'].",";
$servi++;
}
else if ($servi == 5 OR $servi == count($servdescarrayrow)) {
echo $servdescarrayrow['serv_desc'];
$servi++;
}
else {
$servi = 0;
break 2;
}
}
}
Because you're starting $servi at 0. It will always be less than the count of the array. If you start it at 1 it should work the way you expect it to.
change your query to
$servicearray = mysql_query("select GROUP_CONCAT(serv_id) from org_serv_xref where org_id='".$org_id."'");
now sql builds the nice comma delimited list for you :)
As near as I can figure, your trying to do something like this (although I could be totally wrong):
// Initialise vars
$lastid = '';
$items = array();
// Do the query
$query = "SELECT serv_desc, serv_id
FROM service
WHERE serv_id IN (
SELECT serv_id
FROM org_serv_xref
WHERE org_id='".$org_id."'
)
ORDER BY serv_id";
$servdescarray = mysql_query($query);
while ($servdescarrayrow = mysql_fetch_assoc($servdescarray)) {
// We've reached the next ID, echo the result of the last one.
if ($lastid != $servdescarrayrow['serv_id']) {
$lastid = $servdescarrayrow['serv_id'];
if (count($items)) {
echo implode(',',$items);
$items = array();
}
}
if (count($items) == 5) { // We don't want more than 5 items
continue;
} else { // Add an item to the results
$items[$servdescarrayrow['serv_desc']];
}
}
// Make sure we get the last one
if (count($items)) {
echo implode(',',$items);
}

MySQL consequent numbers

I use this function in my while loop to track the numbers of results.
$lvarcontributor = "SELECT * FROM dD_Contributors WHERE idAlbum = '".$idAlbum."' ORDER BY idContributor ASC";
$lvarresult=mysql_query($lvarcontributor);
$var = 0;
while ($lvarrow = mysql_fetch_array($lvarresult))
{
$var++;
//STUFF HERE...
}
How do I know the last result?
I need to know when $var++ is arrived at the last result. such as
If($var == "LAST RESULT") {
//do this
}
THANKS!
Use mysql_num_rows function - http://php.net/manual/en/function.mysql-num-rows.php
This function may be helpfull:
mysql_num_rows
if (($var + 1) == musql_num_rows($result))
{
//this is the last row
}
if( $var == mysql_num_rows($lvarresult) - 1 ) {
// this is the last row, do your thing
}
Maybe you want to check the mysql_num_rows function.
If you want to access only the last result, you should review your SQL query to fetch only this one (try messing with ORDER BY and LIMIT).
If you want to fetch every rows, but have a special treatment for the last result, then you can dut as follow:
$nextRow = mysql_fetch_array($lvarresult);
while ($nextRow)
{
$currentRow = $nextRow;
if($nextRow = mysql_fetch_array($lvarresult)) {
//Not the last row, data in $currentRow
}
else {
//Last row, data in $currentRow
}
}

PHP mysql_fetch_array

$n=mysql_num_rows($rs);
$i=0;
while($n>0)
{
while(($row=mysql_fetch_array($rs))&&$i<=5)
{
echo $row['room_name'];
$i=$i+1;
//echo $n."<br>";
}
echo "<br>";
//echo "n1=".$n;
$n=$n-5;
//
$i=0;
}
Output:101102103104105106
108109110
The row for roomname 107 is missing....
anybody please tell me what is the problem while reentering the loop again...
When $i becomes 6 you fetch a row but do nothing. Because fetching happens before the $i<=5 check, the fetched row gets skipped.
Change the order of conditions in the while loop.
while(($row=mysql_fetch_array($rs))&&$i<=5)
To
while($i<=5 && ($row=mysql_fetch_array($rs)))
Just to follow up on my comment, this whole chunk of code could have been written much more clearly as follows. (assuming you meant to put in a <br> after every 5 records, right now you're doing it after 6 but I think that's probably a mistake)
$rownum = 1;
while ($row = mysql_fetch_array($rs))
{
echo $row['room_name'];
if ($rownum % 5 == 0)
echo '<br>';
$rownum++;
}
here if are checking $i<=5 condition so array stats from 0 , so your database values stats from 101,102,..106, so it will 6 elements .
$i<=5 condition remove this condition in while keep the condition if($i%5==0) echo ""; it will works

Categories