php pdo PDO::FETCH_LAZY Keeping track of db cursor/pointer - php

What am I doing wrong? How do I track the db cursor if the primary key is not consecutive?
MySQL driver.
$sql = "SELECT * FROM employees";
$stmt = $db->query($sql);
$result = $stmt->fetch(PDO::FETCH_LAZY);
var_dump($result);
output:
object(PDORow)[3]
public 'queryString' => string 'SELECT * FROM employees' (length=23)
public 'employeeNumber' => string '1002' (length=4)
public 'lastName' => string 'Murphy' (length=6)
public 'firstName' => string 'Diane' (length=5)
public 'extension' => string 'x5800' (length=5)
public 'email' => string 'dmurphy#classicmodelcars.com' (length=28)
public 'officeCode' => string '1' (length=1)
public 'reportsTo' => null
public 'jobTitle' => string 'President' (length=9)
$result = $stmt->fetch(PDO::FETCH_ORI_FIRST);
var_dump($result);
output:
array (size=8)
'employeeNumber' => string '1056' (length=4)
'lastName' => string 'Patterson' (length=9)
'firstName' => string 'Mary' (length=4)
'extension' => string 'x4611' (length=5)
'email' => string 'mpatterso#classicmodelcars.com' (length=30)
'officeCode' => string '1' (length=1)
'reportsTo' => string '1002' (length=4)
'jobTitle' => string 'VP Sales' (length=8)
$result = $stmt->fetch(PDO::FETCH_ORI_NEXT);
var_dump($result);
output:
array (size=16)
'employeeNumber' => string '1076' (length=4)
0 => string '1076' (length=4)
'lastName' => string 'Firrelli' (length=8)
1 => string 'Firrelli' (length=8)
'firstName' => string 'Jeff' (length=4)
2 => string 'Jeff' (length=4)
'extension' => string 'x9273' (length=5)
3 => string 'x9273' (length=5)
'email' => string 'jfirrelli#classicmodelcars.com' (length=30)
4 => string 'jfirrelli#classicmodelcars.com' (length=30)
'officeCode' => string '1' (length=1)
5 => string '1' (length=1)
'reportsTo' => string '1002' (length=4)
6 => string '1002' (length=4)
'jobTitle' => string 'VP Marketing' (length=12)
7 => string 'VP Marketing' (length=12)
$result = $stmt->fetch(PDO::FETCH_ORI_LAST);
var_dump($result);
output:
array (size=8)
0 => string '1088' (length=4)
1 => string 'Patterson' (length=9)
2 => string 'William' (length=7)
3 => string 'x4871' (length=5)
4 => string 'wpatterson#classicmodelcars.com' (length=31)
5 => string '6' (length=1)
6 => string '1056' (length=4)
7 => string 'Sales Manager (APAC)' (length=20)

Related

How to get specific data from multidimensional array using php

How to get 'syn' and 'sim' values as a string from the given arrays, as array could vary i only want to extract 'syn' and 'sim' as an example following arrays are given ,i know it might be a simple question but i am new to multidimensional array that's why can't seem to solve it.
array (size=1)
'adjective' =>
array (size=2)
'syn' =>
array (size=7)
0 => string 'cagey' (length=5)
1 => string 'cagy' (length=4)
2 => string 'canny' (length=5)
3 => string 'apt' (length=3)
4 => string 'cunning' (length=7)
5 => string 'ingenious' (length=9)
6 => string 'adroit' (length=6)
'sim' =>
array (size=4)
0 => string 'adroit' (length=6)
1 => string 'artful' (length=6)
2 => string 'intelligent' (length=11)
3 => string 'smar
array (size=2)
'noun' =>
array (size=3)
'syn' =>
array (size=23)
0 => string 'passion' (length=7)
1 => string 'beloved' (length=7)
2 => string 'dear' (length=4)
3 => string 'dearest' (length=7)
4 => string 'honey' (length=5)
5 => string 'sexual love' (length=11)
6 => string 'erotic love' (length=11)
7 => string 'lovemaking' (length=10)
8 => string 'making love' (length=11)
9 => string 'love life' (length=9)
10 => string 'concupiscence' (length=13)
11 => string 'emotion' (length=7)
12 => string 'eros' (length=4)
13 => string 'loved one' (length=9)
14 => string 'lover' (length=5)
15 => string 'object' (length=6)
16 => string 'physical attraction' (length=19)
17 => string 'score' (length=5)
18 => string 'sex' (length=3)
19 => string 'sex activity' (length=12)
20 => string 'sexual activity' (length=15)
21 => string 'sexual desire' (length=13)
22 => string 'sexual practice' (length=15)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
'usr' =>
array (size=1)
0 => string 'amour' (length=5)
'verb' =>
array (size=2)
'syn' =>
array (size=29)
0 => string 'love' (length=4)
1 => string 'enjoy' (length=5)
2 => string 'roll in the hay' (length=15)
3 => string 'make out' (length=8)
4 => string 'make love' (length=9)
5 => string 'sleep with' (length=10)
6 => string 'get laid' (length=8)
7 => string 'have sex' (length=8)
8 => string 'know' (length=4)
9 => string 'do it' (length=5)
10 => string 'be intimate' (length=11)
11 => string 'have intercourse' (length=16)
12 => string 'have it away' (length=12)
13 => string 'have it off' (length=11)
14 => string 'screw' (length=5)
15 => string 'jazz' (length=4)
16 => string 'eff' (length=3)
17 => string 'hump' (length=4)
18 => string 'lie with' (length=8)
19 => string 'bed' (length=3)
20 => string 'have a go at it' (length=15)
21 => string 'bang' (length=4)
22 => string 'get it on' (length=9)
23 => string 'bonk' (length=4)
24 => string 'copulate' (length=8)
25 => string 'couple' (length=6)
26 => string 'like' (length=4)
27 => string 'mate' (length=4)
28 => string 'pair' (length=4)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
Any help would be appreciated thanks!
Create a function:
function getArrayAsString($array, $key) {
if (isset($array[$key])) {
return is_array($array[$key]) ? implode(', ', $array[$key]) : $array[$key];
}
return '';
}
Usage example:
$synData = getArrayAsString($array['adjective'], 'sim');
$simData = getArrayAsString($array['noun'], 'syn');

push all the records from DB into one hash in PHP

I am trying to push all the records from DB into one hash (associative array) on the basis of uid (as key) below.
Sample dump of Records from DB:
array (size=4)
'id' => string '18' (length=2)
'job_title' => string 'Rubyyyy' (length=4)
'job_skills' => string 'Rubyyyy' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '19' (length=2)
'job_title' => string 'Ruby on Rails' (length=13)
'job_skills' => string 'Ruby on Rails' (length=13)
'uid' => string '2' (length=1)
array (size=4)
'id' => string '20' (length=2)
'job_title' => string 'Ruby' (length=4)
'job_skills' => string 'Ruby' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '21' (length=2)
'job_title' => string 'Rubyon' (length=13)
'job_skills' => string 'Rubyon' (length=13)
'uid' => string '2' (length=1)
Expected Output:
array (size=1)
1 =>
array (size=4)
'id' => string '20' (length=2)
'job_title' => string 'Ruby' (length=4)
'job_skills' => string 'Ruby' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '18' (length=2)
'job_title' => string 'Rubyyyy' (length=4)
'job_skills' => string 'Rubyyyy' (length=4)
'uid' => string '1' (length=1)
2 =>
array (size=4)
'id' => string '21' (length=2)
'job_title' => string 'Rubyon' (length=13)
'job_skills' => string 'Rubyon' (length=13)
'uid' => string '2' (length=1)
array (size=4)
'id' => string '19' (length=2)
'job_title' => string 'Ruby on Rails' (length=13)
'job_skills' => string 'Ruby on Rails' (length=13)
'uid' => string '2' (length=1)

Show match array data between database and array

I have a problem.
This data on my database table of column name "XYZ"
string 'aaa' (length=3)
string 'bbb' (length=3)
string 'ccc' (length=3)
and this is api array (fetch data). Now I want which "property_name" match in my database column "XYZ" show only this array. How can I do that?
array (size=12)
'property_code' => string 'YXDUB006' (length=8)
'property_name' => string 'bbb' (length=19)
'address' =>
array (size=4)
'line1' => string 'Jessop Street' (length=13)
'city' => string 'County Laois' (length=12)
'postal_code' => string 'R32 RV20' (length=8)
'country' => string 'IE' (length=2)
'contacts' =>
array (size=2)
0 =>
array (size=2)
'type' => string 'PHONE' (length=5)
'detail' => string '353-578-678588' (length=14)
1 =>
array (size=2)
'type' => string 'FAX' (length=3)
'detail' => string '353-57-8678577' (length=14)
array (size=13)
'property_code' => string 'YXDUB006' (length=8)
'property_name' => string 'aaa' (length=19)
'address' =>
array (size=4)
'line1' => string 'Jessop Street' (length=13)
'city' => string 'County Laois' (length=12)
'postal_code' => string 'R32 RV20' (length=8)
'country' => string 'IE' (length=2)
'contacts' =>
array (size=2)
0 =>
array (size=2)
'type' => string 'PHONE' (length=5)
'detail' => string '353-578-678588' (length=14)
1 =>
array (size=2)
'type' => string 'FAX' (length=3)
'detail' => string '353-57-8678577' (length=14)
array (size=14)
'property_code' => string 'YXDUB006' (length=8)
'property_name' => string 'ggg' (length=19)
'address' =>
array (size=4)
'line1' => string 'Jessop Street' (length=13)
'city' => string 'County Laois' (length=12)
'postal_code' => string 'R32 RV20' (length=8)
'country' => string 'IE' (length=2)
'contacts' =>
array (size=2)
0 =>
array (size=2)
'type' => string 'PHONE' (length=5)
'detail' => string '353-578-678588' (length=14)
1 =>
array (size=2)
'type' => string 'FAX' (length=3)
'detail' => string '353-57-8678577' (length=14)
You need to do it like below:-
$final_array = array();
$sql="select hotel FROM hotels";
$result=$mysqli->query($sql);
while ($myrow = $result->fetch_array(MYSQLI_ASSOC)){
foreach($api_array as &$value){
if($myrow['hotel'] == $value['property_name']){
$final_array[] = $value;
}
}
}
echo "<pre/>";print_r($final_array);

How to parse multi-line CSV into multidimensional associative array?

Content of $csv_content is:
1415797658,456ABC,789,123,"bla"
1415797656,654XYZ,897,567,"foo"
1415797654,639HJW,465,146,"bar"
str_getcsv(file_get_contents($csv_content)) results in:
array
0 => string '1415797658' (length=10)
1 => string '456ABC' (length=6)
2 => string '789' (length=3)
3 => string '123' (length=3)
4 => string 'bla' (length=3)
5 => string '1415797656' (length=10)
6 => string '654XYZ' (length=6)
7 => string '897' (length=3)
8 => string '567' (length=3)
9 => string 'foo' (length=3)
10 => string '1415797654' (length=10)
11 => string '639HJW' (length=6)
12 => string '465' (length=3)
13 => string '146' (length=3)
14 => string 'bar' (length=3)
Desired result:
array
0 =>
array
'timestamp' => string '1415797658' (length=10)
'id' => string '456ABC' (length=6)
'id2' => string '789' (length=3)
'id3' => string '123' (length=3)
'text' => string 'bla' (length=3)
1 =>
array
'timestamp' => string '1415797656' (length=10)
'id' => string '654XYZ' (length=6)
'id2' => string '897' (length=3)
'id3' => string '567' (length=3)
'text' => string 'foo' (length=3)
2 =>
array
'timestamp' => string '1415797654' (length=10)
'id' => string '639HJW' (length=6)
'id2' => string '465' (length=3)
'id2' => string '146' (length=3)
'text' => string 'bar' (length=3)
What would be the neatest way to do this?
$array=array();
$handle=fopen($csv_content,'r');
while ($row=fgetcsv($handle)){
$array[]=array(
'timestamp' => $row[0],
'id' => $row[1],
'id2' => $row[2],
'id3' => $row[3],
'text' => $row[4]
);
}

php display multilevel treenode menu

How do I display this structure into a multi-level menu?
Here's the structure:
array
0 =>
array
'product_category_code' => string 'bracelets' (length=9)
'product_category_desc' => string 'Bracelets' (length=9)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:04:08' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-14 22:09:05' (length=19)
1 =>
array
'product_category_code' => string 'floral-dress' (length=12)
'product_category_desc' => string 'Floral Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:49' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:06:58' (length=19)
2 =>
array
'product_category_code' => string 'flowery-bracelets' (length=17)
'product_category_desc' => string 'Flowery Bracelets' (length=17)
'parent_node' => string 'bracelets' (length=9)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:16' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:08:44' (length=19)
3 =>
array
'product_category_code' => string 'small-flowery-bracelets' (length=23)
'product_category_desc' => string 'Small Flowery Bracelets' (length=23)
'parent_node' => string 'flowery-bracelets' (length=17)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:08:35' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:09:44' (length=19)
4 =>
array
'product_category_code' => string 'summer-dress' (length=12)
'product_category_desc' => string 'Summer Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:29' (length=19)
'modified_by' => string '0' (length=1)
'modified_date' => null
And output should be like this:
Bracelets
Flowery Bracelets
Small Flowery Bracelets
Floral Dress
Summer Dress
Here is what I did but it still display the child nodes a
function getChildren($rows, $p = 0) {
$r = array();
foreach($rows as $row) {
if ($row['parent_node']==$p) {
var_dump($p);
$r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']);
}
}
return $r;
Thanks!
It's because you still have the categories in the array when you already assigned them. What you can do is to do the function where you pass the argument as a reference, and the in the foreach loop to have the ability to clear the array from that already assigned category. Simple implementation below.
function getChildren(&$rows, $p = 0) {
$r = array();
foreach($rows as $row_id => $row) {
if ($row['parent_node']==$p) {
$r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']);
unset($rows[$row_id]);
}
}
return $r;
}

Categories