i would like to get the levels of tree in yii2. every parent user in a tree can have an infinite number of users
->parent_user
-->sub_user [1 level]
--->sub_user [2 level]
---->etc [n level]
how i can get the level number?
My Database structure id | name | parent_id
My Tree output Code
public function getArray(){
$user = User::find()->all();
$array = [];
foreach($user as $u){
$array[$u->user_id][] = $v;
}
return $array;
}
public static function outTree($user_id){
$array = self::getArray();
if(isset($array[$user_id])){
echo '<ul>';
foreach($array[$user_id] as $v){
echo '<li>'.$v->username.'</li>';
self::outTree($v->id);
}
echo '</ul>
Any suggestions appreciated. Thanks!
<?php
function show_keys($ar, $i=1){
echo "<table width='100%' border='1' bordercolor='#6699CC' cellspacing='0' cellpadding='5'><tr valign='top'>";
foreach ($ar as $k => $v ) {
echo "<td align='center' bgcolor='#EEEEEE'>
<table border='2' cellpadding='3'><tr><td bgcolor='#FFFFFF'><font face='verdana' size='1'>
" . $i . "
</font></td></tr></table>";
if (is_array($ar[$k])) { $i++;
show_keys ($ar[$k],$i);
}
echo "</td>";
}
echo "</tr></table>";
}
// Multidimensional array ->
$arvore = array();
$arvore['2'] = array();
$arvore['2']['22'] = array('2.1.1', '2.1.2', '2.1.3');
$arvore['2']['22'] = array('2.2.1', '2.2.2', '2.2.3');
$arvore['2']['22'] = array('2.3.1', '2.3.2', '2.3.3');
$arvore['2']['22']['222'] = array('3.1.1', '3.1.2', '3.1.3');
$arvore['2']['22']['222'] = array('3.2.1', '3.2.2', '3.2.3');
$arvore['2']['22']['222'] = array('3.3.1', '3.3.2'=>array('3.3.2.1', '3.3.2.2'), '3.3.3');
// <-
show_keys($arvore);
?>
Related
My question is referring to my previous question in this link How to echo specific multiple same records only once using foreach loops in PHP? .
This is my current code
Code :
$sqlTXT = "SELECT * FROM TABLE";
$arr_old = DB::getInstance()->FetchArray($sqlTXT);
if(count($arr_old) > 0)
{
$arr = array();
foreach($arr_old as $key => $item)
{
if(!array_key_exists($item['ACCOUNT_NUMBER'], $arr))
{
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['ACCOUNT'] = $item['ACCOUNT'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY'] = $item['CATEGORY'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['VALUE'] = $item['VALUE'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['FUND'] = $item['FUND'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['AMOUNT'] = $item['AMOUNT'];
}
else
{
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY'] .= ",".$item['CATEGORY'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['VALUE'] .= ",".$item['VALUE'];
}
}
ksort($arr, SORT_NUMERIC);
echo "<table>";
echo "<tr>";
echo "<td>ACCOUNT</td>";
echo "<td>CATEGORY</td>";
echo "<td>VALUE</td>";
echo "<td>FUND</td>";
echo "<td>AMOUNT</td>";
echo "</tr>";
foreach($arr as $key => $item)
{
// Display Category
$xpl = explode(",",$item[$key]['CATEGORY']);
$n_category = "";
foreach($xpl as $b => $a){
$n_category .= ($b!=0) ? "<br>".$a : $a ;
}
// Display Value
$trl = explode(",",$item[$key]['VALUE']);
$n_value = "";
foreach($trl as $c => $d){
$n_value .= ($c!=0) ? "<br>".$d : $d ;
// $new = number_format($n_value, 2, '.', ',');
}
echo "<tr>";
echo "<td>".$item[$key]['ACCOUNT']."</td>";
echo "<td>".$n_category."</td>";
echo "<td>".$new."</td>";
echo "<td>".$item[$key]['FUND']."</td>";
echo "<td>".$item[$key]['AMOUNT']."</td>";
echo "</tr>";
}
echo "</table>";
}
And the output as shown as below.
Output :
ACCOUNT CATEGORY VALUE FUND AMOUNT
0001 Category1 10000 BIN 300,000.00
Category2 0
Category3 500
Category4 15000
0002 Category1 8500 BIN 70,000.00
Category2 7000
Category3 100
Category4 0
But my current issue is I can't convert the VALUE column into 2 decimals format. When I added code
$new = number_format($n_value, 2, '.', ',');
The output only shows the first value of array.
I hope that someone can help me to solve this issue.
Thanks in advance.
You need to convert each value in the column independently. Try changing your foreach loop to this:
foreach($trl as $c => $d){
if (is_numeric($d)) $d = number_format($d, 2, '.', ',');
$n_value .= ($c!=0) ? "<br>".$d : $d ;
}
I'm trying to list sport league offerings for intramurals. My current php foreach spits out all the leagues and offerings. Great.
Now I want to only show 1 instance of the sport (course), like "Basketball" and then list the multiple league offerings (offering) in columns.
I can't do another loop based on
if $nt['course'] == "Basketball" because these will change
Not sure if my mssql query needs to change or if I can use another foreach loop?
query
$sql2 = SELECT category
,course
,offering
,Semester
,spots
,registerLink from dbtable WHERE semester like "%Fall 2016%" and category like "%Leagues%" ORDER BY course
$rs= mssql_query ($sql2, $con)
or die("Error!");
$result = array();
if (mssql_num_rows($rs)) {
while ($row = mssql_fetch_assoc($rs)) {
$result[] = $row;
}
}
return $result;
exit();
my foreach loop in view:
<?php
foreach ($data as $nt) {
echo "<div class='col-md-2'>";
echo "<h2><a href='#'>".$nt['course']."</a></h2>";
echo "<h3>".$nt['offering']."</h3>";
echo "<h4>".$nt['Semester']."</h4>";
echo "<p>".$nt['spots']."</p>";
echo "<button>".$nt['registerLink']."</button>";
echo "</div>";
}
So just to clarify:
category = "Leagues" which is in my query
course = Basketball and Flag Football, etc
It's very simple:-
instead of
$result[] = $row;
do
$result[ $row['course']][] = $row;
Simple solution:
$result = array();
if (mssql_num_rows($rs)) {
while ($row = mssql_fetch_assoc($rs)) {
if(!in_array($row["course"], $result))
{
array_push($result, $row["course"]);
}
array_push($result[$row["course"]], $row);
}
}
return $result;
And loop in view:
foreach($data as $key => $value)
{
echo "<h1>".$key."</h1>";
foreach($value as $nt)
{
echo "<div class='col-md-2'>";
echo "<h3>".$nt['offering']."</h3>";
echo "<h4>".$nt['Semester']."</h4>";
echo "<p>".$nt['spots']."</p>";
echo "<button>".$nt['registerLink']."</button>";
echo "</div>";
}
}
I'm using foreach loops to access records in a nested array:
while ($row = mysql_fetch_assoc($result)){
$test_groups[$row['group_name']][] = $row['lab_test'];
}
foreach($test_groups as $group_name => $tests){
echo "<tr><td><span class='test_group_name'>" . $group_name . "</span></td></tr>";
foreach($tests as $test){
echo "<tr><td>" . $test . "</td></tr>";
}
echo "<tr><td> </td></tr>";
}
echo '</table>';
This works OK. Now I need to add another level to the nested array, like:
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];
Would this work and how should I adjust the for each loops above to cater for this?
Assign to array should be with [] at the end
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];
Foreach loop will be:
foreach ($departments as $dep_name => $groups) {
echo $dep_name;
foreach ($groups as $group_name => $tests) {
echo $group_name;
foreach ($tests as $test) {
echo $test;
}
}
}
It's just with echoes, make there HTML as you need.
I tried to create a table that calculating the data which stored in mutli-dimensional arrays' value and show the result by table format.
I don't know how to count the values which stored in the array's array(third level array's value.
Can I using the function to count values in the mutli-dimensional arrays' value? Hope anyone can teach me, thanks.
<?php
$itemList = array
("book"=>array("ID"=>"14567",
"name"=>array("History"=>array("American"=>12,"Europe"=>2), "SF"=>array("Space"=>32), "Chinese"=>array("kungFu"=>10))),
"stationary"=>array("ID"=>"24547", "name"=>array("HB"=>array("ABC"=>123, "MC"=>161,"GCD"=>26)))
);
$item = "<table border=1>";
$item .= "<td>item count(s)</td>";
foreach($itemList as $key => $value){
$item .= "<tr>";
$item .= "<td align=center>" .
/*count the categories of the items(e.g. American,Europe,Space,Kungfu show
the result: "4")*/
. "</td>";
$item .= "</tr>";
}
$item .= "</table>";
echo $item;
?>
foreach($itemList['book']['name'] as $key => $value)
{
foreach ($itemList['book']['name'][$key] as $key1 => $value1) {
$category[] = $key1;
$value2[] = $value1;
}
}
echo count($category);
here you got the total numebr of count of your category
I am trying to view values from two separate tables using a foreach loop. The user chooses a "Start semester" and an "End semester" from a drop down list, and those two semesters are stored into an array called $semesterarray
<table style='width: 75%; text-align: left;' cellpadding = '4'>
<tr bgcolor=#000090>
<th><FONT COLOR = #FFFFFF><b><?php echo $startsem ?></b></th>
<th><FONT COLOR = #FFFFFF><b><?php echo $endsem ?></b></th>
</tr>
// If $semesterarray contains 10 and 11, I want to be able to view the
// courses taken in the 10 semester and the 11 semester under two separate columns.
<?php
function getSemesterDetails($semester)
{
$output = "";
$semA = $semester."reg";
$query = "SELECT course1,course2,course3 FROM $semA";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
// row displays: [course1]=> [course2]=> [course3]=>
// Add semester code to array Keys to indicate proper semester
// [course1.11]=> [course2.11]=> [course3.11]=>
foreach ($row as $key => $value)
{
$row[$key.".".$semester] = $value;
unset($row[$key]);
}
$startcourse1 = $row['course1.'.$semester];
$startcourse2 = $row['course2.'.$semester];
$startcourse3 = $row['course3.'.$semester];
$startcoursesarray = array($startcourse1, $startcourse2, $startcourse3);
$startcourses = implode("<br>", $startcoursesarray);
$endcourse1 = $row['course1.'.$semester];
$endcourse2 = $row['course2.'.$semester];
$endcourse3 = $row['course3.'.$semester];
$endcoursesarray = array($endcourse1, $endcourse2, $endcourse3);
$endcourses = implode("<br>", $endcoursesarray);
echo "<tr bgcolor=#ABB5F6>
<td>$startcourses</td>
<td>$endcourses</td>
</tr>";
}
}
foreach ($midsemarrayA as $key => $semester)
{
echo getSemesterDetails($semA);
}
?>
Any ideas?
This should be enough for you to make a start:
function getSemesterDetails ($semester) {
$output = "";
$query = "SELECT course1,course2,course3 FROM $semester";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
// row displays: [course1]=> [course2]=> [course3]=>
// Add semester code to array Keys to indicate proper semester
// [course1.11]=> [course2.11]=> [course3.11]=>
foreach ($row as $key => $value) {
$row[$key.".".$semester] = $value;
unset($row[$key]);
}
$startcourse1 = $row['course1.'.$semester];
$startcourse2 = $row['course2.'.$semester];
$startcourse3 = $row['course3.'.$semester];
$startcoursesarray = array($startcourse1, $startcourse2, $startcourse3);
$startcourses = implode("<br>", $startcoursesarray);
$endcourse1 = $row['course1.'.$semester];
$endcourse2 = $row['course2.'.$semester];
$endcourse3 = $row['course3.'.$semester];
$endcoursesarray = array($endcourse1, $endcourse2, $endcourse3);
$endcourses = implode("<br>", $endcoursesarray);
$output .= "<tr bgcolor=#ABB5F6>
<td>$startcourses</td>
<td>$endcourses</td>
</tr>";
}
return $output;
}
foreach ($semesterarray as $key => $semester) {
getSemesterDetails($semester);
}
You'll want to get it to return an array of values rather than a string, but shifting it into a subroutine should do the trick.