I have a post regarding this which was answered though I now have a different problem related to the same topic. You may want to reference here for the original question. php display multilevel treenode menu
Now that the array is displaying by tree-node, it seems that it is not fully recursive. I notice that when the first element is a child node, it is not displaying as a child.
I tried doing a krsort then ksort.
function getChildren(&$rows, $p = 0) {
$r = array();
krsort($rows);
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]);
}
}
ksort($rows);
return $r;
}
Here's the structure of the array:
array
0 =>
array
'product_category_code' => string 'akamia' (length=6)
'product_category_desc' => string 'Akamia' (length=6)
'parent_node' => string 'summer-dress' (length=12)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-04-01 10:03:42' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-04-01 10:03:47' (length=19)
1 =>
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)
2 =>
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-04-01 10:03:30' (length=19)
3 =>
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)
4 =>
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)
5 =>
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
The output (without the krsort / ksort) is showing this, which is wrong:
Akamai
Bracelets
Flowery Bracelets
Small Flowery Bracelets
Floral Dress
Summer Dress
This should be the output:
Bracelets
Flowery Bracelets
Small Flowery Bracelets
Floral Dress
Summer Dress
Akamai
Never mind, I got the answer. The $p on function getChildren(&$rows, $p = 0) should be $p = '' instead of a zero (0) since the parent node on the array is empty. Silly me. XD
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'm getting this error while submitting my form. Have read similar questions but couldn't find my problem.
I just disabled '_clean_input_keys' function temporarily and printed the $_POST to see what's wrong. but could not see anything weird.
Where should I search for this disallowed character?
This is my $_POST:
array (size=67)
'Naam' => string '1' (length=1)
'NaamKhanevadegi' => string '1' (length=1)
'NaamPedar' => string '1' (length=1)
'Shenasname' => string '1' (length=1)
'CodeMelli' => string '1111111222' (length=10)
'TarikhTavalodDay' => string '10' (length=2)
'TarikhTavalodMonth' => string '10' (length=2)
'TarikhTavalodYear' => string '1361' (length=4)
'Jensiat' => string '1' (length=1)
'MahalTavalod' => string '1' (length=1)
'Khedmat' => string '1' (length=1)
'SerialShenasname' => string '222222' (length=6)
'Taahol' => string '1' (length=1)
'SarparastHastam' => string '1' (length=1)
'TedadeTakafol' => string '2' (length=1)
'Ghad' => string '2' (length=1)
'Jesmani' => string '1' (length=1)
'Vazn' => string '2' (length=1)
'SabegheBimari' => string '1' (length=1)
'SabegheBime' => string '1' (length=1)
'Bimegar' => string '1' (length=1)
'BimeShode' => string '1' (length=1)
'ShomareBime' => string '2' (length=1)
'BimeDay' => string '28' (length=2)
'BimeMonth' => string '8' (length=1)
'BimeYear' => string '8' (length=1)
'ShoruMostamariDay' => string '8' (length=1)
'ShoruMostamariMonth' => string '8' (length=1)
'ShoruMostamariYear' => string '1360' (length=4)
'BimeBikari' => string '1' (length=1)
'PayanMostamariDay' => string '8' (length=1)
'PayanMostamariMonth' => string '8' (length=1)
'PayanMostamariYear' => string '1360' (length=4)
'Isargari' => string '1' (length=1)
'NameHemayati' => string '1' (length=1)
'PostalCode' => string '1212121212' (length=10)
'Tel' => string '' (length=0)
'Mobile' => string '09090909090' (length=11)
'Email' => string '' (length=0)
'Address' => string '' (length=0)
'MarjaNaam' => string '' (length=0)
'MarjaTel' => string '0909090909' (length=10)
'MarjaMobile' => string '09090909090' (length=11)
'Savad' => string '5' (length=1)
'Reshte' =>
array (size=5)
0 => string '0' (length=1)
1 => string '0' (length=1)
2 => string '0' (length=1)
3 => string '0' (length=1)
4 => string '0' (length=1)
'Moadel' =>
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
'NoeDaneshgah' =>
array (size=4)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '1' (length=1)
'Mokaleme' =>
array (size=1)
''.$i.'' => string '1' (length=1)
'Neveshtan' =>
array (size=1)
''.$i.'' => string '1' (length=1)
'ZabanTasalot' =>
array (size=1)
''.$i.'' => string '0' (length=1)
'NoeMaharat' =>
array (size=1)
0 => string '' (length=0)
'DarajeMaharat' =>
array (size=1)
0 => string '' (length=0)
'Gavahiname' =>
array (size=3)
0 => string '1' (length=1)
1 => string '10' (length=2)
2 => string '11' (length=2)
'NaameMoasese' =>
array (size=1)
0 => string '' (length=0)
'SabegheJobTitle' =>
array (size=1)
0 => string '0' (length=1)
'SabegheKar' =>
array (size=1)
0 => string '' (length=0)
'DarkhastiJobTitle' =>
array (size=1)
0 => string '0' (length=1)
'NoeEstekhdam' => string '1' (length=1)
'ShahreDarkhasti1' => string '' (length=0)
'ShahreDarkhasti2' => string '' (length=0)
'ShahreDarkhasti3' => string '' (length=0)
'KhodEshteghali' => string '2' (length=1)
'Kharej' => string '2' (length=1)
'Keshvar1' => string '' (length=0)
'Keshvar2' => string '' (length=0)
'Comment' => string '' (length=0)
'Tamas' => string '1' (length=1)
I found it!
That little $ in $i made the problem.
I forgot some php tag there.
In most of the cases when you have a existing software and you are trying to deploy in a new enviroment this kind of error should be caused by the PHP property
short_open_tag
Check if you have enabled in your new enviroment. In other words PHP couldn't read the tags in your code.
This question already has answers here:
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
Closed 7 years ago.
I have an array which looks like (var_dump):
array (size=3)
0 =>
array (size=8)
'id' => string '1' (length=1)
'user_id' => string '64' (length=2)
'level' => string '1' (length=1)
'score' => string '9999' (length=4)
't1' => string '1' (length=1)
't2' => string '0' (length=1)
't3' => string '0' (length=1)
'attempts' => string '1' (length=1)
1 =>
array (size=8)
'id' => string '2' (length=1)
'user_id' => string '64' (length=2)
'level' => string '2' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '4' (length=1)
2 =>
array (size=8)
'id' => string '3' (length=1)
'user_id' => string '64' (length=2)
'level' => string '3' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '7' (length=1)
How can I change the key 0, 1, 2 etc.. to be the value of level inside that array?
For example:
1 =>
array (size=8)
'id' => string '1' (length=1)
'user_id' => string '64' (length=2)
'level' => string '1' (length=1)
'score' => string '9999' (length=4)
't1' => string '1' (length=1)
't2' => string '0' (length=1)
't3' => string '0' (length=1)
'attempts' => string '1' (length=1)
2 =>
array (size=8)
'id' => string '2' (length=1)
'user_id' => string '64' (length=2)
'level' => string '2' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '4' (length=1)
3 =>
array (size=8)
'id' => string '3' (length=1)
'user_id' => string '64' (length=2)
'level' => string '3' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '7' (length=1)
I have already tried renaming the key inside a forloop, that did not replace only the key, but instead, it replaced the whole array and left it blank.
Thanks
$arr = [ ['id'=>1], ['id'=>2], ['id'=>3]];
$new = [];
foreach($arr as $item)
$new[$item['id']] = $item;
print_r($new);
result
Array (
[1] => Array ( [id] => 1)
[2] => Array ( [id] => 2)
[3] => Array ( [id] => 3)
}
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;
}
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