want to insert multidimensional array value into php database with unique value - php

I have one multidimensional-array and i want to insert into my database, there is one issue if one entry in my database and same entry is on my multidimensional-array that value should not insert again in my database.
$activity[] = array('user_id'=> $_SESSION['user_id'], 'choosen_date' => $ch_date, 'distance' => $distance, 'pace' => $pace );
Array
(
[0] => Array
(
[user_id] => 1200
[choosen_date] => 2018-12-11
[distance] => 1.72
[pace] => 12.4812
)
[1] => Array
(
[user_id] => 1200
[choosen_date] => 2018-12-09
[distance] => 3.17
[pace] => 3.8736
)
[2] => Array
(
[user_id] => 1200
[choosen_date] => 2018-11-14
[distance] => 2.26
[pace] => 6.3504
)
[3] => Array
(
[user_id] => 1200
[choosen_date] => 2018-11-07
[distance] => 0.53
[pace] => 3.6576
)
)
And following is my database entry
----------------------------------------------------
| S.No | user Id | choose date | distance | Pace |
----------------------------------------------------
| 1 | 1200 | 2018-12-09 | 3.17 | 3.8736 |
----------------------------------------------------
| 2 | 1200 | 2018-12-11 | 2.17 | 5.67 |
----------------------------------------------------
so here, in database S.no 1 and array index 1 both are the same entry so I want to insert rest value into my database. So how can I insert?

I am assuming that you are using MySQL database, you can use INSERT IGNORE INTO
or INSERT ON DUPLICATE KEY UPDATE

If you want to do it through PHP then you can try this.
$i=0;
foreach($activity as $key=>$val)
{
$sn_check=mysqli_query($con,"SELECT id FROM table WHERE S.No='$i'");
if(mysqli_num_rows($sn_check)==0)
{
$user_id=$val[$i]['user_id']; //all other values can be catch like this.
$query_insert=mysqli_query($con,"INSERT QUERY");
}
$i++;
}

Related

Why is my recursive function not working when used within a loop?

I have a database that stores how users were referred into the system like so:
| user_id | referrer_id |
| 3 | 2 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 4 |
| 8 | 5 |
| 9 | 5 |
| 10 | 6 |
| 11 | 10 |
| 12 | 10 |
and I would like to display a specific user's network, i.e., the user, the other users which s/he referred directly, and also other users referred by the direct referrals, in an array.
So far, I've been able to create a recursive function
// The actual get_downlines() function
function get_downlines($upline_id) {
// Init the UsersMatrix model
$um_model = model('UsersMatrix');
$um_model->select('user_id')->where('upline_id', $upline_id);
$downlines_check = $um_model->findAll(5);
$downlines = [];
if ( ! empty($downlines_check) ) {
foreach($downlines_check as $check) {
$downlines[] = $check->user_id;
}
}
return $downlines;
}
// User to check
$user = 3;
// The initial network array
$network_array['user_network'][0] = [
'id' => $user,
'text' => "user-$user"
];
// Get the user's network
function get_network($referrer, $network_array){
// This function basically runs a SELECT user_id FROM users WHERE referrer_id = $referrer_id
// and returns the user_ids in an array: get_downlines()
$downlines = get_downlines($referrer);
if (count($downlines) > 0){
// Loop through the downlines
foreach($downlines as $downline){
// Update the array
array_push($network_array['user_network'], [
'id' => $downline,
'text' => "user-$downline",
]);
// Check for the downlines of this user and inner levels
get_network($downline, $network_array);
}
}
// Return the network array
return $network_array;
}
With this, I expect that when I run the get_network(3, $network_array);, I should get an array like so:
Array
(
[user_network] => Array
(
[0] => Array
(
[id] => 3
[text] => user-3
),
[1] => Array
(
[id] => 4
[text] => user-4
),
[2] => Array
(
[id] => 5
[text] => user-5
),
[3] => Array
(
[id] => 6
[text] => user-6
),
[4] => Array
(
[id] => 7
[text] => user-7
),
[5] => Array
(
[id] => 8
[text] => user-8
),
[6] => Array
(
[id] => 9
[text] => user-9
),
[7] => Array
(
[id] => 10
[text] => user-10
)
)
)
instead, the page dies with a maximum execution time error. But if I use a user ID without an indirect referrer, it works well. E.g user id 10.
Where am I going wrong?

Determining which objects are or are not linked to the main root object

I am trying to navigate through a bunch of objects with links to other objects. I want to start with the lowest id number (the root object) and navigate through each of the objects based on connected links. Some of the object links will loop back to previous objects, so I want to make sure I look at each one only once otherwise I will get stuck in an infinite loop. I also want to be able to tell which objects cannot be accessed by navigating through the links beginning at the first link.
The tables in my database look like this:
Object Table:
+----+---------+
| id | title |
+----+---------+
| 1 | Apple |
| 3 | Carrot |
| 4 | Dill |
| 5 | Egg |
| 6 | Fred |
| 7 | Goat |
| 8 | Harry |
| 9 | Igloo |
| 10 | Jason |
| 11 | Klaus |
| 12 | Banana |
| 15 | Oyster1 |
| 16 | Oyster2 |
+----+---------+
Object_Links Table:
+----+---------+--------------+
| id | obj_id | obj_link_id |
+----+---------+--------------+
| 1 | 1 | 12 |
| 2 | 1 | 5 |
| 3 | 3 | 1 |
| 4 | 3 | 12 |
| 5 | 3 | 3 |
| 6 | 4 | 1 |
| 7 | 4 | 5 |
| 8 | 5 | 6 |
| 9 | 6 | 7 |
| 10 | 7 | 7 |
| 11 | 7 | 8 |
| 12 | 9 | 12 |
| 13 | 9 | 5 |
| 14 | 10 | 1 |
| 15 | 10 | 5 |
| 16 | 10 | 8 |
| 17 | 11 | 1 |
| 18 | 11 | 5 |
| 19 | 11 | 10 |
| 20 | 12 | 3 |
| 21 | 15 | 16 |
| 22 | 16 | 15 |
+----+---------+--------------+
So, from the table you can see that object 1 has links to both objects 12 and 5.
My SQL query looks like this:
select object.id, title, obj_link_id
from object
left join object_links ON object.id = object_links.object_id
order by object.id
which gives the table:
+----+---------+--------------+
| id | title | obj_link_id |
+----+---------+--------------+
| 1 | Apple | 12 |
| 1 | Apple | 5 |
| 3 | Carrot | 1 |
| 3 | Carrot | 12 |
| 3 | Carrot | 3 |
| 4 | Dill | 1 |
| 4 | Dill | 5 |
| 5 | Egg | 6 |
| 6 | Fred | 7 |
| 7 | Goat | 7 |
| 7 | Goat | 8 |
| 8 | Harry | NULL |
| 9 | Igloo | 12 |
| 9 | Igloo | 5 |
| 10 | Jason | 1 |
| 10 | Jason | 5 |
| 10 | Jason | 8 |
| 11 | Klaus | 1 |
| 11 | Klaus | 5 |
| 11 | Klaus | 10 |
| 12 | Banana | 3 |
| 15 | Oyster1 | 16 |
| 16 | Oyster2 | 15 |
+----+---------+--------------+
In PHP I am using:
$objects = $stmt->fetchAll(PDO::FETCH_CLASS);
I wasn't sure whether there was a better way to fetch these for my purposes so am open to suggestions.
A print_r($objects) yields:
Array
(
[0] => stdClass Object
(
[id] => 1
[title] => Apple
[obj_link_id] => 12
)
[1] => stdClass Object
(
[id] => 1
[title] => Apple
[obj_link_id] => 5
)
[2] => stdClass Object
(
[id] => 3
[title] => Carrot
[obj_link_id] => 1
)
[3] => stdClass Object
(
[id] => 3
[title] => Carrot
[obj_link_id] => 12
)
[4] => stdClass Object
(
[id] => 3
[title] => Carrot
[obj_link_id] => 3
)
[5] => stdClass Object
(
[id] => 4
[title] => Dill
[obj_link_id] => 1
)
[6] => stdClass Object
(
[id] => 4
[title] => Dill
[obj_link_id] => 5
)
[7] => stdClass Object
(
[id] => 5
[title] => Egg
[obj_link_id] => 6
)
[8] => stdClass Object
(
[id] => 6
[title] => Fred
[obj_link_id] => 7
)
[9] => stdClass Object
(
[id] => 7
[title] => Goat
[obj_link_id] => 7
)
[10] => stdClass Object
(
[id] => 7
[title] => Goat
[obj_link_id] => 8
)
[11] => stdClass Object
(
[id] => 8
[title] => Harry
[obj_link_id] =>
)
[12] => stdClass Object
(
[id] => 9
[title] => Igloo
[obj_link_id] => 12
)
[13] => stdClass Object
(
[id] => 9
[title] => Igloo
[obj_link_id] => 5
)
[14] => stdClass Object
(
[id] => 10
[title] => Jason
[obj_link_id] => 1
)
[15] => stdClass Object
(
[id] => 10
[title] => Jason
[obj_link_id] => 5
)
[16] => stdClass Object
(
[id] => 10
[title] => Jason
[obj_link_id] => 8
)
[17] => stdClass Object
(
[id] => 11
[title] => Klaus
[obj_link_id] => 1
)
[18] => stdClass Object
(
[id] => 11
[title] => Klaus
[obj_link_id] => 5
)
[19] => stdClass Object
(
[id] => 11
[title] => Klaus
[obj_link_id] => 10
)
[20] => stdClass Object
(
[id] => 12
[title] => Banana
[obj_link_id] => 3
)
[21] => stdClass Object
(
[id] => 15
[title] => Oyster1
[obj_link_id] => 16
)
[22] => stdClass Object
(
[id] => 16
[title] => Oyster2
[obj_link_id] => 15
)
)
Please note, that the number in the brackets is just the array index, not the object id number, so don't let the index throw you off.
I am trying to find a way to determine which are the linked and which are the unlinked objects. Based on the above scenario the objects should be separated as follows:
**Linked:**
Apple
Banana
Carrot
Egg
Fred
Goat
Harry
**Not Linked:**
Dill
Igloo
Jason
Klaus
Oyster1
Oyster2
My main question:
How can I create a loop in PHP to loop through a structure like this especially when each object can have multiple links? Ultimately I would like to produce two collections of objects, one containing the linked objects and one containing the unlinked objects. A sample collection might look like this:
stdClass Object
(
[LinkedElements] => stdClass Object
(
[1] => stdClass Object
(
[id] => 1
[name] => Apple
[link] => Array
(
[0] => 14
[1] => 5
)
)
[14] => stdClass Object
(
[id] => 14
[name] => Banana
[link] => Array
(
[0] => 3
)
)
[3] => stdClass Object
(
[id] => 3
[name] => Carrot
[link] => Array
(
[0] => 1
[1] => 14
[2] => 3
)
)
[5] => stdClass Object
(
[id] => 5
[name] => Egg
[link] => Array
(
[0] => 6
)
)
[6] => stdClass Object
(
[id] => 6
[name] => Fred
[link] => Array
(
[0] => 7
)
)
[7] => stdClass Object
(
[id] => 7
[name] => Goat
[link] => Array
(
[0] => 7
[1] => 8
)
)
[8] => stdClass Object
(
[id] => 8
[name] => Harry
)
)
[UnLinkedElements] => stdClass Object
(
[4] => stdClass Object
(
[id] => 4
[name] => Dill
[link] => Array
(
[0] => 1
[1] => 5
)
)
[9] => stdClass Object
(
[id] => 9
[name] => Igloo
[link] => Array
(
[0] => 14
[1] => 5
)
)
[10] => stdClass Object
(
[id] => 10
[name] => Jason
[link] => Array
(
[0] => 1
[1] => 5
[2] => 8
)
)
[11] => stdClass Object
(
[id] => 11
[name] => Klaus
[link] => Array
(
[0] => 1
[1] => 5
[2] => 10
)
)
[15] => stdClass Object
(
[id] => 15
[name] => Oyster1
[link] => Array
(
[0] => 16
)
)
[16] => stdClass Object
(
[id] => 16
[name] => Oyster2
[link] => Array
(
[0] => 15
)
)
)
)
Please note:
Navigation is done from object to link, not the other way around.
It is okay to have an object point to itself (as object 7 does).
The above sample structure (underneath my main question) is a suggestion only and I am open to other suggestions.
Disclaimer: This question is based on another question I previously asked.
In my original question I manually created the test objects, but I was not able to pull them out of my database in this fashion.
It's easier (IMHO) to deal with the data as two separate arrays. A set of objects and their links. Also, as the first part I convert the object to have the ID as the key, this allows me to use it directly rather than having to search for the ID each time.
Also to make the solution a lot simpler, I've created a flag in the object array as I visit it, so that when I try and reference it again, I can check if it's already been visited.
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
$objects =[[1,'apple'],
[3, 'Carrot'],
[4, 'Dill'],
[5, 'Egg '],
[6, 'Fred'],
[7, 'Goat'],
[8, 'Harry'],
[9, 'Igloo'],
[10, 'Jason'],
[11, 'Klaus'],
[12, 'Banana'],
[15, 'Oyster1'],
[16, 'Oyster2' ]];
$links =[[1,12],
[1,5],
[3,1],
[3,12],
[3,3],
[4,1],
[4,5],
[5,6],
[6,7],
[7,7],
[7,8],
[8,null],
[9,12],
[9,5],
[10,1],
[10,5],
[10,8],
[11,1],
[11,5],
[11,10],
[12,3],
[15,16],
[16,15 ]];
function buildTree ( $id, &$objects, $links ) {
foreach ( $links as $testNode ) {
if ( $testNode[0] == $id &&
$testNode[1] != $id &&
$testNode[1] != null &&
!isset($objects[$testNode[1]]['visited']) ) {
$objects[$testNode[1]]['visited'] = true;
buildTree ( $testNode[1], $objects, $links);
}
}
}
// Convert array to have the ID as key
$objects = array_combine(array_column($objects, 0), $objects);
// Fetch ID of first item
reset($objects);
$startID = key($objects);
// Mark as visited
$objects[$startID]['visited'] = true;
// Process
buildTree ( $startID, $objects, $links);
$linked = [];
$notLinked = [];
foreach ( $objects as $object) {
if ( isset($object['visited']) ) {
$linked[] = $object[1];
}
else {
$notLinked[] = $object[1];
}
}
echo "Linked...".PHP_EOL;
print_r($linked);
echo "Not linked...".PHP_EOL;
print_r($notLinked);
As you can see, the core is the recursive buildTree function. This uses &$objects as this means that all calls to the function will use the same array. As I want to build up all the uses of the items, this is important.
The condition in buildTree, checks to see if it's the node we want, it's not referring to the same node (waste of time looking any more), not null (not sure why you link to null, but again not worth looking any further) and that the node hasn't already been visited. If these conditions are OK, it marks the next node as visited and goes down into the next level.
The output is...
Linked...
Array
(
[0] => apple
[1] => Carrot
[2] => Egg
[3] => Fred
[4] => Goat
[5] => Harry
[6] => Banana
)
Not linked...
Array
(
[0] => Dill
[1] => Igloo
[2] => Jason
[3] => Klaus
[4] => Oyster1
[5] => Oyster2
)
This is a graph traversal problem. Starting from a node (root) you want to traverse the graph keeping track of each node visited along the way. Once the traversal is over, visited ones are connected. Breadth-first search can be done in this way:
//To form a graph fetch all objects from the database (sorted by id) and
//index them in a hash map
$objects = $stmt->fetchAll(PDO::FETCH_OBJ);
$nodes = [];
foreach ($objects as $object) {
$nodes[$object->id] = new Node($object);
}
//fetch all connections from the database and link the objects
$links = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($links as $link) {
$nodes[$link->obj_id]->addLink($nodes[$link->obj_link_id]);
}
//let's assume root is the first node (sorted by id),
//traverse the graph starting from root
$root = reset($nodes);
$root->traverse();
//now if a node can be reached by the root it is marked as visited
$linked = [];
$notLinked = [];
foreach ($nodes as $node) {
if ($node->isVisited()) {
$linked[] = $node;
} else {
$notLinked[] = $node;
}
}
And the node class:
class Node
{
/**
* List of neighbor nodes.
*
* #var Node[]
*/
private $links = [];
/**
* id, title info
*
* #var array
*/
private $data = [];
/**
* To track visited nodes.
*
* #var bool
*/
private $visited = false;
/**
* Node constructor.
* #param array $data
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Add a link to this node.
*
* #param Node $node
* #return void
*/
public function addLink(Node $node)
{
$this->links[] = $node;
}
/**
* Traverse the graph in a Breadth-First-Search fashion marking
* every visited node.
* #return void
*/
public function traverse()
{
//initialize queue
$q = new SplQueue();
//add root to queue and mark as visited
$q->enqueue($this);
$this->visited = true;
while (!$q->isEmpty()) {
/** #var Node $cur */
$cur = $q->dequeue();
foreach ($cur->links as $link) {
//if link not visited already add it to queue and mark visited
if (!$link->visited) {
$link->visited = true;
$q->enqueue($link);
}
}
}
}
/**
* Checks if node has been visited.
*
* #return bool
*/
public function isVisited()
{
return $this->visited;
}
}
Let's say the "root" is obj_id 1.
Here's a somewhat brute force algorithm, but it takes advantage of SQL's liking of 'set' operations.
Insert into table1 the root (1)
Loop
Create table2 with all nodes linked to any node in table1
Exit if number of rows in table2 = num rows in table1
table1 := table2
Closer to SQL:
# Initialize:
CREATE TABLE table1 (
obj_id ...
PRIMARY KEY(obj_id)
)
SELECT 1; # assuming root is 1
start of loop:
CREATE TABLE table2 (
obj_id ...
PRIMARY KEY(obj_id)
)
SELECT DISTINCT obj_link_id
FROM table1
JOIN object_links USING(obj_id);
SELECT #fini := ( SELECT COUNT(*) FROM table1 ) =
( SELECT COUNT(*) FROM table2 ) # will give true/false
DROP TABLE table1;
RENAME TABLE table2 TO table1;
loop if #fini=0
The output is all the 'connected' ids. If you want the unconnected ones:
SELECT obj_id
FROM object_links
LEFT JOIN table1 USING(obj_id)
WHERE table1.obj_id IS NULL; # that is, missing from table1
Here is a pretty short way to get all linked IDs:
$pdo = new PDO('mysql:host=localhost;dbname=test_obj_link', 'testread', 'testread');
$links = $pdo
->query('select obj_id, obj_link_id from object_links')
->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN);
function getLinks($objId, $links, $indirectNodes = []) {
$directNodes = isset($links[$objId]) ? $links[$objId] : [];
foreach($directNodes as $linkedNode) {
if (!in_array($linkedNode, $indirectNodes)) {
$indirectNodes[] = $linkedNode;
$indirectNodes = array_unique(array_merge(
$indirectNodes,
getLinks($linkedNode, $links, $indirectNodes)
));
}
}
return $indirectNodes;
}
$linkedObjectIds = getLinks(1, $links);
fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN) will return a structured array with links per object indexed by objectIDs which looks like:
$links = [
1 => ['5', '12'],
3 => ['1', '3', '12'],
4 => ['1', '5'],
5 => ['6'],
6 => ['7'],
7 => ['7', '8'],
9 => ['5', '12'],
10 => ['1', '5', '8'],
11 => ['1', '5', '10'],
12 => ['3'],
15 => ['16'],
16 => ['15'],
];
The getLinksfunction will "walk" the $links array recursively and merge all child arrays that are found on the way. Since PHP doesn't seem to have an array_union function - array_unique(array_merge(..)) is used instead.
The result:
$linkedObjectIds = array (
0 => '5',
1 => '6',
2 => '7',
3 => '8',
4 => '12',
10 => '3',
11 => '1',
)
Note that the indexes have no meaning here.
To get the corresponding objects you can do the following:
$objects = $pdo
->query('select id, title from object')
->fetchAll(PDO::FETCH_KEY_PAIR);
$linkedObjects = array_intersect_key($objects, array_flip($linkedObjectIds));
$notLinkedObjects = array_diff_key($objects, $linkedObjects);
The variables will contain:
$objects = array (
1 => 'Apple',
3 => 'Carrot',
4 => 'Dill',
5 => 'Egg',
6 => 'Fred',
7 => 'Goat',
8 => 'Harry',
9 => 'Igloo',
10 => 'Jason',
11 => 'Klaus',
12 => 'Banana',
15 => 'Oyster1',
16 => 'Oyster2',
);
$linkedObjects = array (
1 => 'Apple',
3 => 'Carrot',
5 => 'Egg',
6 => 'Fred',
7 => 'Goat',
8 => 'Harry',
12 => 'Banana',
);
$notLinkedObjects = array (
4 => 'Dill',
9 => 'Igloo',
10 => 'Jason',
11 => 'Klaus',
15 => 'Oyster1',
16 => 'Oyster2',
);
Demo: http://rextester.com/ZQQGE35352
Note that in_array() and array_unique() are probably slow, since they have to search in values, which are not sorted. This might lead to performance issues on some datasets. Assuming that PHP can search the keys faster, we could use array_key_exists() instead of in_array() and the array operator + (union by keys) instead of array_unique(array_merge()). And the code will even be a bit shorter:
function getLinks($objId, $links, $indirectNodes = []) {
$directNodes = isset($links[$objId]) ? $links[$objId] : [];
foreach($directNodes as $linkedNode) {
if (!array_key_exists($linkedNode, $indirectNodes)) {
$indirectNodes[$linkedNode] = 0;
$indirectNodes = $indirectNodes + getLinks($linkedNode, $links, $indirectNodes);
}
}
return $indirectNodes;
}
However - since the function now returns the needed result as keys, we would need to use array_keys() to extract them:
$linkedObjectIds = array_keys(getLinks(1, $links));
Demo: http://rextester.com/GHO7179

How to save special array data into Three MySQL table in PHP

I have JSON which need to insert into MySQL table.
What I already did is, convert it to array using:
$json_data = json_decode($geo_json, true);
and get an output of array but I do not know how to inset into second and third MySQL table.
My MySQL Table:
Table 1:
geo_id | user_id | geo_title | geo_description | geo_date |geo_status |geo_action |action_reason | geo_json | remote_ip | map_type |geo_snapshot
I can able to insert into above table easily but problem is in table two and Three listed below.
Table 2:
id | layer_id | map_id | layer_name | user_id | draw_type | latlng | radious
Table 3:
data_id | geo_key | geo_value | map_id | layer_id
Array I am getting:
Array
(
[type] => FeatureCollection
[features] => Array
(
[0] => Array
(
[type] => Feature
[properties] => Array
(
[action] => a
[poi_name] => a
[fac_type] => 17
[ph_number] => a
[hno] => a
[postcode] => a
[mail] => a
[str_nm] => a
[photo] => developer-page.png
[comment] => a
[url] => a
[sub_loc] => a
[employee] => a
)
[geometry] => Array
(
[type] => Point
[coordinates] => Array
(
[0] => 88.434448242188
[1] => 22.510971144638
)
)
)
[1] => Array
(
[type] => Feature
[properties] => Array
(
[action] => b
[poi_name] => b
[fac_type] => 18
[ph_number] => b
[hno] => b
[postcode] => b
[mail] => b
[str_nm] => b
[photo] => 1475131600_developer-page.png
[comment] => b
[url] => b
[sub_loc] => b
[employee] => b
)
[geometry] => Array
(
[type] => Point
[coordinates] => Array
(
[0] => 88.321151733398
[1] => 22.50906814933
)
)
)
)
)
Now problem is to insert above data into two separate tables:
Table 2: This is only require to insert draw_type | latlng from above php array.
Example: draw_ type: point and latlng : coordinates
Table 3:
This is require to insert geo_key | geo_value | map_id | layer_id from above PHP array.
Example:
geo_key : properties [action,poi_name,fac_type,ph_number,hno,postcode,mail,str_nm, photo, comment, url, sub_loc, employee]
geo_value : [properties values ]
map_id :[this will be table 1 insert id]
layer_id : [this can be blank]
Please guide me and show me how to start.
$json_data["features"][$array_index]["geometry"]["type"]
For table2:
foreach($json_data["features"] as $info){
$type = $info["geometry"]["type"];
$latlng_0 = $info["geometry"]["coordinates"][0];
$latlng_1 = $info["geometry"]["coordinates"][1];
// DO INSRET with $type, $latling_0, $latling_1
$sql = "INSERT INTO Table2 (draw_type, latlng0, latlng1) VALUES ('".$type."','".$latlng_0."','".$latlng_1."')";
....
}
For table3:
Is 'map_id' a auto increment key in table1?
You'll need to know map_id first by select * where (conditions)
after successful insert data to table1
And if 'layer_id' can accept blank (null) data, it'll be fine if you don't specific value in INSERT command. Just make sure your table have correct settings.

Select where HSTORE value is $value Laravel

Using the query builder to try and select rows where a value in the HSTORE key $type is present.
My Where Statement:
->where("meta_data_fields->'$type'", 'like', '%'.$query.'%')
Not sure what I'm doing wrong. I get this error:
Undefined column: 7 ERROR: column "meta_data_fields->'Entity::hstore'" does not exist
Any ideas?
to get all records in table that have HSTORE
Model::where("meta_data_fields", 'like', '%'.$query.'%')->get();
This should do normally do the trick. In your case, data in meta_data_fields is a json string. if the search is performed on all rows in the table, will select all. So here is What I did,
I created a queryScope in the model
1 - fetch all data
2 - extract meta_data_field and decode it
3 - loop through meta_data_field
4 - select only that match the criteria and build the array
here is a table
+----+-------------+------------------------------------+-----------+
| id | name | desc | vendor_id |
+----+-------------+------------------------------------+-----------+
| 1 | apples | {"type":"fruit","origin":"mexico"} | 1 |
| 2 | oranges | {"type":"fruit","origin":"peru"} | 1 |
| 3 | Peaches | {"type":"fruit","origin":"mexico"} | 2 |
| 4 | Cherries | {"type":"fruit","origin":"us"} | 1 |
| 5 | banans | {"type":"fruit","origin":"brazil"} | NULL |
| 6 | Water Melon | {"type":"fruit","origin":"mexico"} | 1 |
+----+-------------+------------------------------------+-----------+
public function scopeItems($name ="mexico"){
$items = array();
$data = Self::get();
$meta_fields = json_decode($data -> pluck('desc') ,true);
foreach ($meta_fields as $key => $value) {
if (isset($value['origin']) && $value['origin'] == $name ){
$items[$key] = $data[$key] -> toArray();
}
}
return $items;
}
// output
Array
(
[0] => Array
(
[id] => 1
[name] => apples
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 1
)
[2] => Array
(
[id] => 3
[name] => Peaches
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 2
)
[5] => Array
(
[id] => 6
[name] => Water Melon
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 1
)
)
Hope this works for you this time.
So here's what worked in my situation with Laravel.
$result = \DB::select(\DB::raw("SELECT hstore_fields, id from table where lower(hstore_fields->'$type') like lower('%$query%')"));

php (PDO) simple way to turn rows in lookup table into simple array

given a very simple table structure thus:
mysql> describe songpart;
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | MUL | NULL | auto_increment |
| partName | text | NO | | NULL | |
+----------+---------+------+-----+---------+----------------+
which results in an array like this in php (when queried)
Array ( [0] => Array ( [id] => 1 [0] => 1 [partName] => Lead Guitar [1] => Lead Guitar )
[1] => Array ( [id] => 2 [0] => 2 [partName] => Bass Guitar [1] => Bass Guitar )
[2] => Array ( [id] => 3 [0] => 3 [partName] => Drums [1] => Drums )
[3] => Array ( [id] => 4 [0] => 4 [partName] => Keyboard [1] => Keyboard ) )
Am I missing some simple trick to turn this into a simple array with id as the key like so:
Array ( [1] => Lead Guitar
[2] => Bass Guitar
[3] => Drums
[4] => Keyboard )
or is it possible to get PDO to deliver an array like this?
TiA
You can simply use the PDO::FETCH_KEY_PAIR is you have only 2 columns in your result.
You can also use the PDO::FETCH_GROUP and PDO::FETCH_ASSOC together if you have more. Example :
$result = $db->query('select * from channels')->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
This will yield an array indexed with the first column containing at each index an array of the result for this key. You can fix this by using array_map('reset', $result) to get your goal.
Example :
$result = array_map('reset', $db->query('select * from channels')->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC));
Try this:
$records = $pdo->query('SELECT id, partName FROM myTable');
$records->setFetchMode(PDO::FETCH_KEY_PAIR);
print_r($records->fetchAll());
For that you need to set only your desired fields in iteration.
$part_name = [];
$records = $pdo->query('SELECT * FROM your_table');
foreach ($records as $row) {
$part_name[$row['id']] = $row['partName'];
}

Categories