Nested while loop needed? - sql

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;

Related

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);

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

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);

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");

How to search within Sphinx results using partial words?

I have a form and when I enter a value, I want to get the results on submit;
If I search for fish and I have a fishing value I want to get all the data that contain the fish value;
In my code, I get the results that I need only if I enter the full fishing word;
I am searching for the full expression and also for each word from the expression;
$sphinx_search = Yii::app()->search;
$sphinx_search->setSelect('*');
$sphinx_search_final = '';
$sphinx_search_query = trim($this->q);
if (strlen($sphinx_search_query) > 0) {
$sphinx_search_query = str_replace('-', ' ', $sphinx_search_query);
$keys = explode(' ', $sphinx_search_query);
foreach ($keys as $k => $key) {
if ($key) {
$keys[$k] = $key;
} else {
unset($keys[$k]);
}
}
if (count($keys) > 0) {
$sphinx_search_final = ' #(position_name,employer_name,employer_first_name,employer_last_name,employer_position) ' . $sphinx_search_query .
' #(position_name,employer_name,employer_first_name,employer_last_name,employer_position) ' . implode('|', $keys);
}
}
Either enable min_prefix_len or min_infix_len
to get explicit substring matching capablities.
Do also check enable_star as that changes how exactly it behaves.
Alternativly maybe stemming is what you looking for? Enabled via morphology

my query take a long time to run ,why?

I have a query that returns c.a 1800 records( not many) but It takes long time to ran (10 seconds) but I can not understand why?
this is my query that takes a long time:
SELECT tb_KonzeptFunktionen.Konzept AS KonzeptID, tb_KonzeptFunktionen.Funktion,
tb_KonzeptFunktionen.Version,
qryFunktionen_Übersicht.ID,
qryFunktionen_Übersicht.Fehlerpfad_Kommentar AS Kommentar,
qryFunktionen_Übersicht.Fehlerpfadname,
qryFunktionen_Übersicht.Fehlerpfad_CDT,
qryFunktionen_Übersicht.Fehlerpfad_Kommentar,
qryFunktionen_Übersicht.symptombasiert,
qryFunktionen_Übersicht.Beschreibung_vorhanden,
qryFunktionen_Übersicht.Max_Pfad,
qryFunktionen_Übersicht.Max_Info,
qryFunktionen_Übersicht.Max_Status,
qryFunktionen_Übersicht.Max_Strategie,
qryFunktionen_Übersicht.Max_Prüfplan,
qryFunktionen_Übersicht.Min_Pfad,
qryFunktionen_Übersicht.Min_Info,
qryFunktionen_Übersicht.Min_Status,
qryFunktionen_Übersicht.Min_Strategie,
qryFunktionen_Übersicht.Min_Prüfplan,
qryFunktionen_Übersicht.Sig_Pfad,
qryFunktionen_Übersicht.Sig_Info,
qryFunktionen_Übersicht.Sig_Status,
qryFunktionen_Übersicht.Sig_Strategie,
qryFunktionen_Übersicht.Sig_Prüfplan,
qryFunktionen_Übersicht.Plaus_Pfad,
qryFunktionen_Übersicht.Plaus_Info,
qryFunktionen_Übersicht.Plaus_Status,
qryFunktionen_Übersicht.Plaus_Strategie,
qryFunktionen_Übersicht.Plaus_Prüfplan,
qryFunktionen_Übersicht.Beschreibung_allgemein,
qryFunktionen_Übersicht.Funktionsname
FROM tb_KonzeptFunktionen RIGHT JOIN qryFunktionen_Übersicht
ON tb_KonzeptFunktionen.Funktion = qryFunktionen_Übersicht.Funktionsname
WHERE (((tb_KonzeptFunktionen.Konzept)=[Formulare]![frm_Fahrzeug]![ID]))
and this is another related query to above query:
SELECT tbFunktionen_Übersicht.*,
tbFunktionen.Funktionsname,
tbFunktionen.Funktionsbeschreibung,
tbFunktionen.diagnoserelevant,
tbFunktionen.ID AS FunktionsID
FROM tbFunktionen_Übersicht INNER JOIN tbFunktionen
ON tbFunktionen_Übersicht.Funktion = tbFunktionen.ID
ORDER BY tbFunktionen.Funktionsname, tbFunktionen_Übersicht.Fehlerpfadname;
I added an index to the fields that appear in ORDER oder JOINS but no effect
Would you please help me if possible?
Please refer to this post,hier you can find the answer
for me PDO brought improvement in my sql query's: http://www.php.net/manual/en/intro.pdo.php
//SQL-HANDLING*******************************************************//
//*********************************************************************//
//General Example-----------------
//$sqlHandling = new sqlHandling;
//$sqlHandling->connectSQL($sqlArray['host'], $sqlArray['user'],$sqlArray['pass'], $sqlArray['dbName']);
//$tableNameArray = $sqlHandling->showTablesSQL($sqlHandling->dbh);
class sqlHandling{
//PDO - Object/ callItOutside with: $sqlClass->dbh
public $dbh = null;
//connectDataBase-------------------//
//-----------------
//example: $dbh = connectSQL('localhost','admin','1admin','test');
//start transaction
//$dbh->beginTransaction();
//-----------------
//end transaction
//$dbh->commit();
//$dbh = null;
//-----------------
//1:SQL-Location
function connectSQL($host, $user, $pass, $dbname){
//endPrevTransaction
if($this->dbh != null){
$this->dbh->commit();
$this->dbh = null;
}
//establish connection
try{
$this->dbh = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass);
$this->dbh->exec('SET CHARACTER SET utf8');
return $this->dbh;
}catch(PDOException $e){
print 'Error!: ' . $e->getMessage() . '<br/>';
die();
return FALSE;
}
}
//createColumnSQL-------------------//
//example: createColumnSQL($dbh, 'backupexample', array('SomeName1 VARCHAR(30)', 'SomeName2 VARCHAR(30)', 'SomeName3 VARCHAR(30)', 'SomeName4 VARCHAR(30)'));
//1:PDO connection/2:table/3:array holding columns
function createColumnSQL($dbh, $table, $columns){
//cicle through each array
foreach($columns as $tempColumn){
//set query
$query = 'ALTER TABLE `'.$table.'` ADD COLUMN '.$tempColumn;
//send the query
$stmt = $dbh->prepare($query);
//send the data
$stmt->execute();
}
}
}
This is only a working example for my PDO sqlHandling Class. I know retrieveAllValues won't help you further but you may adopt this code for your use.
MFG