How to show Level on each members on Recursive mysql php? - php

How to output the red color level on each member's name like the following screenshot :
Here is the demo webpage URL : http://client.bfm.expert/test_UnilevelBonus.php
Here is the code :
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//Use the following function to get the data of downlines
function getChildren($parent) {
$servername = "11";
$username = "11";
$password = "11";
$dbname = "11";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT user_id, first_name, sponsor_id FROM tbl_user_master WHERE sponsor_id = $parent";
$result = $conn->query($query);
$children = array();
$i = 0;
$result = $conn->query($query) or die($conn->error);
while($row = $result->fetch_assoc()) {
$children[$i] = array();
$children[$i]['name'] = $row['first_name'];
$children[$i]['children'] = getChildren($row['user_id']);
$i++;
}
return $children;
$conn->close();
}
//enter sponsor_id here, change 151 to 145 has a lot of downlines to test
$finalResult = getChildren(145);
//display all downlines of the sponsor
function printList($array = null) {
if (count($array)) {
echo "<ul>";
foreach ($array as $item) {
echo "<li>";
echo $item['name'];
echo " - level : ";
if (count($item['children'])) {
printList($item['children']);
}
echo "</li>";
}
echo "</ul>";
}
}
printList($finalResult);
?>

You need to pass level into 'printList' function and increment it after.
function printList($array = null, $level = 1) {
if (count($array)) {
echo "<ul>";
foreach ($array as $item) {
echo "<li>";
echo $item['name'];
echo " - level : " . $level;
if (count($item['children'])) {
printList($item['children'], $level+1);
}
echo "</li>";
}
echo "</ul>";
}
}
printList($finalResult, 1);

Related

How to display mysql results in separate unordered lists by alphabet

I have a webpage that queries a list of links to subpages. The subpages are divided by alphabet, and formatted as dynamic nested unordered lists with each letter of the alphabet having its own heading. I currently have it working, but with 26 separate queries. I'd like to do the same thing, but with only 1 query instead of 26. Here's a snippet of what I have now, showing just letters A and B. I'm fairly new to PHP, so any ideas would be greatly appreciated. I've looked extensively, but haven't found any answers that allow for the nested unordered list formatting, or anything remotely similar.
<ul class="navMenu">
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM addsor WHERE title LIKE 'A%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<li class='navMenuItem'>A";
echo "<ul class='navSubMenu'>";
while($row = $result->fetch_assoc()) {
$title = $row["title"];
$linkname = $row["linkname"];
echo "<li class='navSubMenuItem'><a href='addsor_result.php?sortitle=$linkname' onclick='popsor(this.href); return false;'>$title</a></li>";
}
echo "</ul>";
echo "</li>";
}
$sql = "SELECT * FROM addsor WHERE title LIKE 'B%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<li class='navMenuItem'>B";
echo "<ul class='navSubMenu'>";
while($row = $result->fetch_assoc()) {
$title = $row["title"];
$linkname = $row["linkname"];
echo "<li class='navSubMenuItem'><a href='addsor_result.php?sortitle=$linkname' onclick='popsor(this.href); return false;'>$title</a></li>";
};
echo "</ul>";
echo "</li>";
};
?>
</ul>
Per Jerson, I edited the code to use an if statement:
<ul class="navMenu">
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM addsor";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$find = substr($row["title"], 0, 1);
$title = $row["title"];
$linkname = $row["linkname"];
if ($find === "A") {
echo "<li class='navMenuItem menu-header'>A";
echo "<ul class='navSubMenu'>";
echo "<li class='navSubMenuItem'><a href='addsor_result.php?sortitle=$linkname' onclick='popsor(this.href); return false;'>$title</a></li>\r\n";
echo "</ul>";
echo "</li>";
}
elseif ($find === "B") {
echo "<li class='navMenuItem menu-header'>B";
echo "<ul class='navSubMenu'>";
echo "<li class='navSubMenuItem'><a href='addsor_result.php?sortitle=$linkname' onclick='popsor(this.href); return false;'>$title</a></li>";
echo "</ul>";
echo "</li>";
}
};
};
</ul>
But now, instead of being grouped like:
A
    Aaa
    Aab
    Aac
B
    Baa
    Bab
    Bac
etc.
they're appearing like:
A
    Aaa
A
    Aab
A
    Aac
B
    Baa
B
    Bab
etc.
I think using if statement instead of repeating query
$sql = "SELECT * FROM addsor";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$find = substr( $row["title"], 0, 1 );
if($find === "A") {
// add your code here
}
};
};
my test example
$arr = ["Aaa","Aab","Aac","Baa","Bab","Bac"];
foreach($arr as $val) {
$find = substr($val, 0, 1);
if($find == "A") {
echo $val . "\r\n";
} elseif($find == "B") {
echo $val . "\r\n";
}
}
i got following result
 run-project -l php
Aaa
Aab
Aac
Baa
Bab
Bac

I tried a different approach, and got the results I was looking for. Thanks to all those that offered assistance. Here's the new code:
<ul class="navMenu">
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM addsor";
$result = $conn->query($sql);
$firstletter = "";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$title = $row["title"];
$linkname = $row["linkname"];
if (substr($row["title"], 0, 1) !== $firstletter) {
if (!empty($firstletter)) {echo "</li></ul>";}
$firstletter = substr($row["title"], 0, 1);
echo "<li class='navMenuItem'>".$firstletter."<ul class='navSubMenu'>";
}
echo "<li class='navSubMenuItem'><a href='addsor_result.php?sortitle=$linkname' onclick='popsor(this.href); return false;'>$title</a></li>\r\n";
};
};
$conn->close();
?>
</ul>

PHP only display last id number in last row

I'm trying to display 6 items in one page with 3 items in each row. And when the user clicks on the image link it will redirect to another page and display the id. However when the user clicks on the image it does redirect to another page but it shows the id of the last product in the last row of the page, and this is not correct. I'm not sure where I made the mistake. I hope you can look at my code and give me a hint where the mistake lies.
<?PHP
session_start();
function connect()
{
$servername = xxxxxxxx;
$username = xxxxxxxx;
$password = xxxxxxxx;
$dbname = xxxxxxxx;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
echo 'connection is invalid';
} else {
mysqli_select_db($conn, "mytest");
}
return $conn;
}
function getData()
{
$conn = connect();
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
$startrow = 0;
} else {
$startrow = (int) $_GET['startrow'];
}
$sql = "SELECT * FROM tbl_products LIMIT $startrow, 6";
$getdata = mysqli_query($conn, $sql) or die(mysqli_error());
$cell_img = mysqli_num_rows($getdata);
$i = 0;
$per_row = 3;
echo "<table id='productTumb'><tr id='proRow'>";
$data = '';
while ($row = mysqli_fetch_assoc($getdata)) {
//echo "<a href='ProDet.html'></a>";
echo "<td><a href='test.php'><img style='vertical-align: bottom;' width='218px' height='332px' src='" . $row['products_image'] . "'/ ></a></td>";
$data .= "<td style='background-color:#FF0004'>" . $row['product_name'] . "</td>";
$product_id = $row['products_id'];
$_SESSION['id'] = $product_id;
if (++$i % $per_row == 0 && $i > 0 && $i <= $cell_img) {
echo "</tr><tr>$data</tr><tr>";
$data = '';
}
}
for ($x = 0; $x < $per_row - $i % $per_row; $x++) {
echo "<td></td>";
}
echo "</tr>";
echo "</table>";
echo 'Next >>>';
$prev = $startrow - 5;
if ($prev >= 0)
echo ' <<<< Previous';
}
?>
This line of code $_SESSION['id'] = $product_id; will set last product id to SESSION.(overwrites previous ids)
Try to add product id to the anchor href tag
while ($row = mysqli_fetch_assoc($getdata)) {
echo "<td><a href='test.php?id=".$row['products_id']."'><img style='vertical-align: bottom;' width='218px' height='332px' src='" . $row['products_image'] . "'/ ></a></td>";
......
}
In test.php file get id by below code
if(isset($_GET)){
$pid = $_GET['id'];
echo $pid;
}
hope this will helps you.

MySQL Data to JSON Array with inserting a string

This is my Code for now but it only works until the for loop in the mySQLResultsetToJSON().
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "123456";
$table = "ticketing";
$link = new mysqli($servername, $username, $password, $table);
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
echo "Connected successfully";
$result = $link->query("SELECT * from Ticket");
print_r($result);
$final_result = mySQLResultsetToJSON($result);
print_r($final_result);
$link->close();
function mySQLResultsetToJSON($resultSet)
{
for($i = 0; sizeof($resultSet); $i++)
{
$rows = array();
while($r = mysqli_fetch_assoc($resultSet[$i])) {
$rows[] = $r;
}
$jsonResult[$i] = json_encode(array('Results' => $rows));
}
print_r($jsonResult);
return $jsonResult;
}
?>
Thank you!
Thomas
echo "mysql data<br />";
$result = $link->query("SELECT * from users");
print_r($result->fetch_object());
echo "<br />";
echo "in json<br />";
$res = ['Results' => $result->fetch_object() ];
echo json_encode($res);
$link->close();
User like
$result = $link->query("SELECT * from Ticket");
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print "<pre>";
print_r(json_encode(array('Results' =>$rows)));
$link->close();

How can I make a submenu

I have the next database:
I have the below function:
<?php
$connection = mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("database1", $connection) or die(mysql_error());
function loop_array($array = array(), $parent_id = 0)
{
if (!empty($array[$parent_id])) {
echo '<ul>';
foreach ($array[$parent_id] as $items) {
echo '<li>';
echo ''.$items['title'].'';
loop_array($array, $items['id']);
echo '</li>';
}
}
}
function displays_menus_revised()
{
$sql = "SELECT * FROM pages";
$query = mysql_query($sql) or die(mysql_error());
$array = array();
if (mysql_num_rows($query)) {
while ($rows = mysql_fetch_array($query)) {
$array[$rows['parent_id']][] = $rows;
}
loop_array($array);
}
}
?>
My problem is the funtion show the records like in the left picture. I want the function to show the records like in the right picture. Can you tell me where is the problem? What i must change that the function show records like in the right picture? Thanks.
So nice code, with such a small slip. Only the closing 'ul' is missing.
function loop_array($array = array(), $parent_id = 0)
{
if (!empty($array[$parent_id])) {
echo '<ul>';
foreach ($array[$parent_id] as $items) {
echo '<li>';
echo ''.$items['title'].'';
loop_array($array, $items['id']);
echo '</li>';
}
echo '</ul>';
}
}

Display List of Moodle 2.4 Courses for a user Outside of Moodle

I am trying to create a page where it displays a bullet list of Course Title and link to any courses that user is teacher of for Moodle 2.4.
I found this code here and it works for displaying Moodle course categories but not the courses for a user. I am not a programmer so any help would be very appreciated.
<?php
$con = mysql_connect("localhost","moodle","moodle");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("moodle", $con);
$result = mysql_query("SELECT name AS COURSE_NAME,parent FROM mdl_course_categories");
if (isset($result)!=1) {
$message = 'Invalid query: ' . mysql_error() . "\n";
}
echo "<p> The courses taught are: </p>";
///Display Course Categories
$query_catetories = mysql_query('SELECT cc.id, cc.parent, cc.name FROM mdl_course_categories cc ');
$categories = mysql_fetch_all($query_catetories);
$tmp_categories = array();
foreach ($categories AS $row) {
$row['id'] = (int) $row['id'];
$row['parent'] = (int) $row['parent'];
if (!$tmp_categories[$row['parent']])
$tmp_categories[$row['parent']] = array();
$tmp_categories[$row['parent']][] = $row;
}
$course_catetories = buildNode($tmp_categories);
echo '<ul>';
foreach ($course_catetories as $course_catetory) {
print_category_child($course_catetory);
}
echo '</ul>';
function print_category_child($category) {
echo '<li>' . $category['name'];
if (array_key_exists('children', $category)) {
echo '<ul>';
foreach ($category['children'] as $child) {
print_category_child($child);
}
echo '</ul>';
}
echo '</li>';
}
function buildNode($inputArray, $parent = 0) {
$return = array();
foreach ($inputArray[$parent] AS $key => $row) {
if (#$inputArray[$row['id']]) {
$row['children'] = buildNode($inputArray, $row['id']);
}
$return[] = $row;
}
return $return;
}
function mysql_fetch_all($result) {
$all = array();
while ($all[] = mysql_fetch_assoc($result)) {
}
return array_filter($all);
}
///END Course Display
?>'`
Look at your Mysql query :
$result = mysql_query("SELECT name AS COURSE_NAME,parent FROM mdl_course_categories");
Your results are a list of categories' names from the table "mdl_course_categories".
If you want to display a list of courses names, do it using the right table "mdl_course". It may be something like :
mysql_select_db("moodle", $con);
$result = mysql_query("SELECT id, fullname FROM mdl_course WHERE category = "your_course_category_here");
while($row = mysql_fetch_array($result)){
echo $row[0];
echo "<br>";
echo $row[1];
}

Categories