how to count column(with data) number in laravel excel maatwebsite - laravel-4.2

if(Input::hasFile('excel_file')){
$path = Input::file('excel_file')->getRealPath();
$data = Excel::load($path, function($reader) {
$reader->setDateFormat('Y-m-d');
})->get();
$objPHPExcel = new PHPExcel();
$highestColumn = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
echo $highestColumnIndex;
exit();
}
I can import excel file and count the rows are there by using $data->count(). But i can't figure out column numbers. How can i get how many columns are in the rows?i tried with phpexcel too but failed.need the solution badly.

$path = Input::file('excel_file')->getRealPath();
$data = Excel::load($path, function($reader) {
$reader->setDateFormat('Y-m-d');
$objExcel = $reader->getExcel();
$sheet = $objExcel->getSheet(0);
$highestColumn = $sheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
Session::put('val',$highestColumnIndex);
})->get();

I know this is late, but can help for others:
Get columns like:
function getColumnNames($sheename, $path){
return Excel::selectSheets($sheename)->load($path)->keys()->toArray();
}
Then count:
count(getColumnNames('my_sheet', $my_file);

Related

PhpSpreadsheet import data from excel to database symfony 3.4

i would like to know how can i use PhpSpreadsheet symfony 3 ,
should i use this bundle : roromix/SpreadsheetBundle,
can i have a little exemple how to use it to read rows from exemple_file.xlst
thank you
I would advice to use the PHPSpreadsheet package directly.
$spreadsheet = PhpSpreadsheet\IOFactory::load('exemple_file.xlst' );
$worksheet = $spreadsheet->getActiveSheet(); // get active worksheet
$rows = []; //empty array of rows
foreach ($worksheet->getRowIterator() AS $row) {
$cells = $row->getCellIterator();
$cells->setIterateOnlyExistingCells(FALSE); // iterates through all cells, including empty ones
$cellData = [];//
foreach ($cells as $cell) {
$cellData[] = $cell->getValue();
}
$rows[] = $cells;
}
This will create 2 - dimensional array "rows" containing all the worksheet data which you can then use to import to DB. Or you can import it directly row by row in the for loop.

Adding two variables containing integer values

I have fetched integer data in two variables from different tables. I need to apply the add operation so that I can get the sum of the values in those variables. I am a beginner though so please try to help me out here. I tried the code in Model and the result I am getting is : Object of class CI_DB_mysqli_result could not be converted to int
$fromrequest = $this->db->query("SELECT Requests.sunit FROM Requests WHERE Requests.sid = 2");
$fromstock = $this->db->query("SELECT Stock.sunit FROM
Stock WHERE Stock.ssid = 5");
$sum = $fromrequest + $fromstock
first your queries are not definite. Look at "SELECT Requests.sunit FROM Requests", the query is going to list all Sunits in the table Requests. But you want to count or sum the sunit and assign them to the valuable $fromrequest the query like
$fromrequest = $this->db->query("SELECT count(Requests.sunit) as Units FROM Requests");
or
$fromrequest = $this->db->query("SELECT sum(Requests.sunit) as Units FROM Requests");
would results into an integer which would make your life easy. Try it and we see what we get. Good luck
check the values and better use active record..
$get_fromrequest = $this->db->select('sunit')->where('sid',2)->get("Requests");
$fromrequest = $get_fromrequest->row();
if(isset($fromrequest)){
$freq = $fromrequest->sunit;
} else {
$freq = 0;
}
$get_fromstock = $this->db->select('sunit')->where('ssid',2)->get("Stock");
$fromstock = $get_fromstock->row();
if(isset($fromstock)){
$fstock = $fromstock->sunit;
} else {
$fstock = 0;
}
chang it
$sum = $fromrequest + $fromstock
to
$sum = abs($fromrequest) + abs($fromstock);

Multiple between statements in sql builder

I am creating a fairly large query using laravel 4 sql builder. it roughly looks like, get data from table
where(yada yada)and where age between(yada, yada)or between(yada, yada) and where(yad yada).
But what I want is this.
where(yada yada)and where (age between(yada, yada)or between(yada, yada)) and where(yad yada).
some code:
$query = Subject::grabBasic();// start writing query, which results to grab.
$agelength = sizeof($fromAgeValue);
$count = 0;
$ageflag = 0;
foreach( array_combine($fromAgeValue, $toAgeValue) as $from=>$to ){
$count+=1;
if($ageflag==0){
if(($from!=null)&&($to!=null)){
$query = Subject::addfromAgeToAge($query, $from, $to);
$ageflag=1;
}
}else if(($from!=null)&&($to!=null)){
$query = Subject::orfromAgeToAge($query, $from, $to);
}
}
... keep adding to $query
Subject code:
static function addfromAgeToAge($query, $fromAge, $toAge){
return $query->whereBetween('Age', array($fromAge, $toAge));
}
static function orfromAgeToAge($query, $fromAge, $toAge){
return $query->orWhereBetween('Age', array($fromAge, $toAge));
}
So, is there a way to write the addToAge builder function to take in the arrays of fromage and toage values and create a statement, which when added to the rest of the query, will give the desired results. e-g. the sql query will have parentheses in the correct place.
I would also like to do this without raw queries, I suspect there is a way to do this I just haven't been able to find it.
Thanks,
I buckled and used raw queries :(
Here's what it looks like.
$agecounter = 0;// tells me how many from age to age pairs were filed out coorrectly.
foreach( array_combine($fromAgeValue, $toAgeValue) as $from=>$to ){
if(($from!=null)&&($to!=null)){
$agecounter+=1;
}
}
$agelength = sizeof($fromAgeValue);
$ageflag = 0;
foreach( array_combine($fromAgeValue, $toAgeValue) as $from=>$to ){
if($ageflag==0){
if(($from!=null)&&($to!=null)){
$query = Subject::addfromAgeToAge($query, $from, $to, $agecounter); // ages put in for loop when multiple are added.
$ageflag=1; // add or's
$agecounter -=1;
}
}else if(($from!=null)&&($to!=null)){
$query = Subject::orfromAgeToAge($query, $from, $to, $agecounter);
$agecounter -=1;
}
}
and in my Model:
static function addfromAgeToAge($query, $from, $to, $count){
if($count>1){
return $query->whereRaw('( Age between ? and ?', array($from, $to));
}
return $query-> whereBetween('Age', array($from, $to));
}
static function orfromAgeToAge($query, $from, $to, $count){
if($count==1){
return $query->orWhereRaw(' Age between ? and ?)', array($from, $to));
}
return $query->orWhereRaw(' Age between ? and ?', array($from, $to));//('Age', array($fromAge, $toAge));
}
Hope this helps someone else put ()'s in a query builder.

Nested while loop needed?

I've got this code working but it's only pulling out the first neighbourhood group that matches one of the values from the 1st query. The code is in a Node ID PHP handler in a view in Drupal.
The first query puts all of the postal codes from the government's jurisdiction into an array. The second query reads through all of the neighbourhood groups to find all those that match those in that array.
It looks like I need a second loop or something since the while that's there now isn't getting every neighbourhood groups that match any of the values in the array. Can anyone see what I'm missing?
Here's the code:
$government_nid = custom_get_groupid();
$codes_list = array();
$neighbourhood_list = array();
$results = db_query("SELECT field_jurisdiction_postal_codes_value FROM {content_field_jurisdiction_postal_codes} WHERE
nid = %d", $government_nid);
while($return_list = db_fetch_array($results)){
$codes_list[] = $return_list[field_jurisdiction_postal_codes_value];
}
$results1 = db_query("SELECT nid FROM {content_type_neighbourhood_group} WHERE
field_postal_code_3_value IN ('%s')", $codes_list);
while($return_list1 = db_fetch_array($results1)){
$neighbourhood_list[] = $return_list1[nid];
}
$neighbourhood_string = implode(', ', $neighbourhood_list);
return $neighbourhood_string;
Here's the answer:
foreach ($codes_list AS $value) {
$results1 = db_query("SELECT nid FROM {content_type_neighbourhood_group} WHERE
field_postal_code_3_value = '%s'", $value);
while($return_list1 = db_fetch_array($results1)){
$neighbourhood_list[] = $return_list1[nid];
}
}
$neighbourhood_string = implode(', ', $neighbourhood_list);
return $neighbourhood_string;

SELECT issue moving from PG_query to PDO

I have a select statement see below. Using PDO how would I recreate this same Select statement, as I want to grab two values from it and combine them into the $geomstring. I can figure out the combine, but not the first 3 lines.
$sql1 = "SELECT easting_value, northing_value FROM gridreference_tbl WHERE gridref_id='$_POST[gridref_id]'";
$result1 = pg_query($sql1);
$row1 = pg_fetch_array($result1);
$geomstring = $row1['easting_value']. $_POST['grid_eastings']." ".$row1['northing_value'].$_POST['grid_northings'];
*php website for prepared statements says *
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
I have something similar working for populating a dropdown that partly uses this
$stmt = $conn->prepare("SELECT easting_value, northing_value FROM gridreference_tbl WHERE gridref_id=$gridref_id");
$stmt->setFetchMode(PDO::FETCH_OBJ);
Found it on php.net, I was googling the wrong stuff:
$stmt4 = $conn->prepare("SELECT easting_value, northing_value from gridreference_tbl WHERE gridref_id = 4");
$stmt4->execute();
print("PDO::FETCH_ASSOC: ");
print("Return next row as an array indexed by column name\n");
$result = $stmt4->fetch(PDO::FETCH_ASSOC);
print_r($result);
print("\n");