I have two tables Resources and Categories. I have made a join query and got the result. But I dont know how to work around it. Below is outputted result array.
array (size=4)
0 =>
array (size=5)
'resourceID' => string '24' (length=2)
'resourceName' => string 'Tafe Resources' (length=14)
'categoryID' => string '3' (length=1)
'categoryName' => string 'Accounting' (length=10)
'subcategoryID' => string '0' (length=1)
1 =>
array (size=5)
'resourceID' => string '24' (length=2)
'resourceName' => string 'Tafe Resources' (length=14)
'categoryID' => string '4' (length=1)
'categoryName' => string 'Adult Tertiary' (length=14)
'subcategoryID' => string '0' (length=1)
2 =>
array (size=5)
'resourceID' => string '26' (length=2)
'resourceName' => string 'College Resources' (length=17)
'categoryID' => string '7' (length=1)
'categoryName' => string 'Automotive' (length=10)
'subcategoryID' => string '0' (length=1)
3 =>
array (size=5)
'resourceID' => string '26' (length=2)
'resourceName' => string 'College Resources' (length=17)
'categoryID' => string '8' (length=1)
'categoryName' => string 'Busniess & Management' (length=21)
'subcategoryID' => string '0' (length=1)
this was my query
$this->db->select()->from('resources');
$this->db->join('categories','resources.resourceID = categories.resourceID');
if it was from single table i will use for-each loop and get data.
I want the duplicated columns to be merged.
I want to display a table where resourceID and resourceName is unique.
How do you want the table to be unique? You could use GROUP BY but you would lose the category information for any duplicates of resourceID and resourceName.
Edit: You can use a JOIN with a GROUP_CONCAT for a subquery but that would limit you to the max length set for the concat operation.
I think the best solution would just to loop through the data and set the data to ensure the rows for resourceID and resourceName are unique.
Related
Need to change some object values of a doctrine collection fetched from database and save again to the database. For this, there is a old written code and In that they have used a loop to save objects one by one. The reason of saving objects one by one is because there some other processes are done in the middle. In the end of the first loop there is a function which is trying to fetch another doctrine collection from the database. Now that entire collection is existed of db values. Because of that 1st collection is replaced by existing database values. So updating process is not working. Why is this happening ?
Example Code :
class mainAction extends sfAction {
function execute($request) {
$a = new ExampleA();
$collection = $a->updateDoctrineCollectionWithNewValues(6);
$b = new ExampleB();
$b->saveDoctrineCollectionToDB($collection);
}
}
class ExampleA {
function updateDoctrineCollectionWithNewValues($empNumber){
$empSalaryComponents = Doctrine_Core::getTable('EmployeeSalaryComponent');
$empSalComCollection = $empSalaryComponents->findBy('employee_number', $empNumber);
foreach ($empSalComCollection as $empSalComponent) {
$empSalComponent->setValue(9999999999999);
}
return $empSalComCollection;
}
}
class ExampleB {
function saveDoctrineCollectionToDB($doctrineCollection){
var_dump($doctrineCollection->toArray()); // 1st var_dump
foreach ($doctrineCollection as $object) {
$this->addToLogger($object);
var_dump($doctrineCollection->toArray()); //2nd var_dump
die;
}
}
function addToLogger($object) {
$empSalaryComponents = Doctrine_Core::getTable('EmployeeSalaryComponent')->findBy('employee_number', $object->getEmployeeNumber());
/**
* Do something
**/
}
}
Results
1st var_dump :
array (size=4)
0 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '1' (length=1)
'value' => int 9999999999999
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
1 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '2' (length=1)
'value' => int 9999999999999
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
2 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '3' (length=1)
'value' => int 9999999999999
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
3 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '4' (length=1)
'value' => int 9999999999999
'effectiveDate' => string '2019-01-19' (length=10)
'is_active' => string '1' (length=1)
2nd var_dump:
0 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '1' (length=1)
'value' => string '6787' (length=4)
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
1 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '2' (length=1)
'value' => string '71111' (length=5)
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
2 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '3' (length=1)
'value' => string '34%' (length=3)
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
3 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '4' (length=1)
'value' => string '6787' (length=4)
'effectiveDate' => string '2019-01-19' (length=10)
'is_active' => string '1' (length=1)
2nd var_dump is filled with db values since calling db in the middle of the loop.
Env : Doctrine 1.2, Php7.2, Symfony 1.4
I have two subqueries on which I am doing a join, first one returns following data:
array (size=110)
0 =>
array (size=3)
'scans' => string '7' (length=1)
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
second one:
array (size=21)
0 =>
array (size=2)
'redeems' => string '1' (length=1)
'date' => string '2017-06-13' (length=10)
1 =>
array (size=3)
'redeems' => string '1' (length=1)
'date' => string '2017-06-15' (length=10)
If I do a left join on those two like so:
LEFT JOIN query2 ON query2.date=query1.date;
I get:
array (size=110)
0 =>
array (size=4)
'scans' => string '7' (length=1)
'redeems' => '1'
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
Row with the date 2017-06-15 gets lost as the second query doesnt have the match with the first.
How can I get both rows returned? Like so:
array (size=110)
0 =>
array (size=4)
'scans' => string '7' (length=1)
'redeems' => '1'
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
1 =>
array (size=4)
'scans' => null
'redeems' => '1'
'bonus_points' => null
'date' => string '2017-06-15' (length=10)
I have a database structure like this:
ID name sort parent
1 item1 1 0
2 subitem1 2 1
3 subsubitem1 1 2
4 subitem2 1 1
I write the database into an array
array (size=4)
0 =>
array (size=4)
'id' => string '1' (length=1)
'name' => string 'item1' (length=5)
'parent' => string '0' (length=1)
'sort' => string '1' (length=1)
1 =>
array (size=4)
'id' => string '2' (length=1)
'name' => string 'subitem1' (length=8)
'parent' => string '1' (length=1)
'sort' => string '2' (length=1)
2 =>
array (size=4)
'id' => string '3' (length=1)
'name' => string 'subsubitem1' (length=11)
'parent' => string '2' (length=1)
'sort' => string '1' (length=1)
3 =>
array (size=4)
'id' => string '4' (length=1)
'name' => string 'subitem2' (length=8)
'parent' => string '1' (length=1)
'sort' => string '1' (length=1)
and restructure that array to set up child-parent relations with this function:
function generateNavArray($arr, $parent = 0)
{
$items = Array();
foreach($arr as $item)
{
if($item['parent'] == $parent)
{
$item['child'] = isset($item['child']) ? $item['child'] : GenerateNavArray($arr, $item['id']);
$items[] = $item;
}
}
return $items;
}
and the generated array looks like this
array (size=1)
0 =>
array (size=5)
'id' => string '1' (length=1)
'name' => string 'item1' (length=5)
'parent' => string '0' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=2)
0 =>
array (size=5)
'id' => string '2' (length=1)
'name' => string 'subitem' (length=4)
'parent' => string '1' (length=1)
'sort' => string '2' (length=1)
'child' =>
array (size=1)
0 =>
array (size=5)
'id' => string '3' (length=1)
'name' => string 'subsubitem1' (length=11)
'parent' => string '2' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=0)
empty
1 =>
array (size=5)
'id' => string '3' (length=1)
'name' => string 'subitem2' (length=8)
'parent' => string '1' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=0)
empty
now i need to sort every dimension of the array by the sort value, (my "real array" has more subarrays then this one).
i played around with multisort but i can't seem to find the solution
any ideas?
Sort the array when it is 1 dimension before you build the multi-dimensional array. Even better if you are using a query, sort it there. Sort by parent then sort. When you build your multidimensional array, each child will be appended to the parent. If they are already in the correct order, they will end up in the same order.
I am creating a online shopping website in php. In the add to cart process a user adds the product details and quantity in the session using the multidimensional array.
If the user want to remove a particular product from the cart list they can remove it using the (X)mark.
I have the following array stucture:
array (size=8)
'id' =>
array (size=4)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
3 => string '2' (length=1)
'pwidth' =>
array (size=4)
0 => string '1' (length=1)
1 => string '5' (length=1)
2 => string '9' (length=1)
3 => string '5' (length=1)
'pheight' =>
array (size=4)
0 => string '2' (length=1)
1 => string '6' (length=1)
2 => string '10' (length=2)
3 => string '6' (length=1)
'pdepth' =>
array (size=4)
0 => string '3' (length=1)
1 => string '7' (length=1)
2 => string '11' (length=2)
3 => string '7' (length=1)
'handel' =>
array (size=4)
0 => string 'no' (length=2)
1 => string 'no' (length=2)
2 => string 'yes' (length=3)
3 => string 'no' (length=2)
'quantity' =>
array (size=4)
0 => string '4' (length=1)
1 => string '8' (length=1)
2 => string '12' (length=2)
3 => string '8' (length=1)
'add' =>
array (size=4)
0 => string 'Add to Cart' (length=11)
1 => string 'Add to Cart' (length=11)
2 => string 'Add to Cart' (length=11)
3 => string 'Add to Cart' (length=11)
'count' => int 4
If the user removes a product from the array, the rest of the list should automatically align from the top. please help me to know the concept. Thank you
When you click your (X) mark to delete/remove product from cart, at that time you can get that product's array key. Now you know that this key is unique as (i hope you are using the same key to store that products data in all arrays) in all other arrays i.e. id,pwidth, etc.., You can unset($key) that key from all the arrays and than can do array_values($array) to reindex the arrays.
I want to run what I thought was going to be a simple query.
I want to return a database table just the values) in the same 2D style it is written in into $data[i][k]
I am using PHP's PDO, and the closest I have been able to get is:
$result=$database->query("SELECT * FROM `garage_statistics`",$bind = null,$fetch = 'FETCH_COLUMN');
$this->statement = $this->pdo->prepare($query);
$result = $this->statement->fetchAll(PDO::FETCH_COLUMN,1);
Which returns
0 => string 'Garage2' (length=1)
1 => string 'Garage3' (length=7)
2 => string 'Garage4' (length=7)
3 => string 'Garage6' (length=7)
4 => string 'Garage7' (length=7)
However I can't get it to loop for all the columns. I tried fetchall and got back:
array (size=10)
0 =>
array (size=14)
'name' => string 't' (length=1)
0 => string 't' (length=1)
'tablename' => string 't' (length=1)
1 => string 't' (length=1)
'numfloors' => string '3' (length=1)
2 => string '3' (length=1)
'status' => string '4' (length=1)
3 => string '4' (length=1)
'numspots' => string '0' (length=1)
4 => string '0' (length=1)
'spotsinuse' => string '0' (length=1)
5 => string '0' (length=1)
'time' => string '2012-12-07 13:47:13' (length=19)
6 => string '2012-12-07 13:47:13' (length=19)
1 =>
array (size=14)
'name' => string 'Garage 3' (length=8)
0 => string 'Garage 3' (length=8)
'tablename' => string 'Garage3' (length=7)
1 => string 'Garage3' (length=7)
'numfloors' => string '2' (length=1)
2 => string '2' (length=1)
'status' => string '3' (length=1)
3 => string '3' (length=1)
'numspots' => string '0' (length=1)
4 => string '0' (length=1)
'spotsinuse' => string '0' (length=1)
5 => string '0' (length=1)
'time' => string '2012-12-07 13:49:46' (length=19)
6 => string '2012-12-07 13:49:46' (length=19)
While this does contain all the data, it has so much stuff that I don't want.
Is there a simple way to get a basic 2D array of a table using PDO?
$result = $this->statement->fetchAll(PDO::FETCH_NUM);
Returns correct type