PHP - Moved: Displaying Record In A Tree Structure (network Marketing)
This topic has been moved to Application Design.
http://www.phpfreaks.com/forums/index.php?topic=347192.0 Similar TutorialsHey guys, I have a bunch of categories and products in a tree format. For several reasons I need to restructure them from the tree format into a flattened format. I have 2 arrays, one full of categories, the other products. Each category and product has an ID, and a reference to it's parent's ID. The top level categories reference to 0. What I need is to have the categories output like thus: Top Level Categories: Category 1 Category 2 Category 1 Sub category 1 Sub category 2 Category 2 Sub category 3 Sub category 4 Sub category 1 Product 1 Product 2 Sub category 2 Product 3 Product 4 Sub category 3 Product 5 Product 6 Sub category 4 Product 7 Product 8 There could be an unlimited number of sub categories within categories before we get to products, so this needs to be done through a function, however I cannot for the life of me think how to do this. Initially I thought about using 2 arrays, a buffer of categories outputted, and a queue of categories to be outputted, but quickly realised that when I go more than 2 layers deep I can't keep track of the queue's properly. Hi, I am working on a project, where i want to add sub-sections within a section , and add once again adding sub-subsection under that section. How to make a table structure for that, any idea please Thanks, Hi, I use this code (found in post http://www.phpfreaks.com/forums/index.php?topic=205633.0) to create a tree structure of categories and subgategories. Code: [Select] $prevcat = ''; $prevsubcat = ''; $sql = "SELECT * FROM $tbl_name ORDER BY categ, subcateg"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); while($row = mysql_fetch_assoc($result)) { $cat = $row['categ']; $subcat = $row['subcateg']; $item = $row['itemname']; $description = $row['itemdesc']; if($cat != $prevcat){ echo $cat.'<br />'; echo 'sc '. $subcat.'<br />';//if the category has changed, we also want to show the new subcat }elseif($subcat != $prevsubcat){ echo $subcat.'<br />'; } echo 'it '.$item.'<br />'; echo 'desc '.$description.'<br />'; $prevcat = $cat; $prevsubcat = $subcat; } The above code works fine but I am trying to figure out how to count results of the deepest subcategory, so I will be able to change cell color or have results presented in two colums (left and right). Example: - main category 1 -- subcategory under category 1 ------ result 1 ------ result 2 ------ result 3 - main category 2 -- subcategory under category 2 ------ result 1 ------ result 2 - main category 3 -- subcategory under category 3 ------ result 1 ------ result 2 ------ result 3 ------ result 4 etc I want to count results under each subcategory, so on the above example I should have: 3 results for category 1 2 results for category 2 4 results for category 3 Any suggestions? Thank you. Hi there, I think this is a big question but I'd appretiate any help you can provide!! I have a list of items and subitems in a table that looks like this: id parent_id title 1 0 House Chores 2 1 Take Out Trash 3 1 Clean Room 4 0 Grocery List 5 4 Eggs 6 4 Produce 7 6 Lettuce 8 6 Tomato 9 4 Milk I want to display it like this: (+) House Chores: > Take Out Trash > Clean Room (+) Grocery List: > Eggs (+) Produce > Letutce > Tomato > Milk So basically each entry in the table has an unique id and also a parent id if it's nested inside another item. I "sort of" got it figured out in one way, but it doesnt really allow for nested subgroups. I'd like to know how would y'all PHP freaks to this Also taking suggestions for the javascript code to expand/collapse the tree !! Thank you! This is my first try to display binary tree so please go easy on me. I am trying to display binary tree using array. The values are stored in mysql. Structure of mysql- Code: [Select] CREATE TABLE IF NOT EXISTS `tree` ( `parent` int(7) NOT NULL, `rchild` int(7) NOT NULL, `lchild` int(7) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; Display should look like Code: [Select] 1 ______|______ | | 2 3 ______|______ | | 4 5 My Code till now- Code: [Select] <?php include 'connection.php'; $rsearch = "SELECT rchild FROM tree WHERE parent='".$parent."'"; $lsearch = "SELECT lchild FROM tree WHERE parent='".$parent."'"; $rexec = mysql_query($rsearch); $lexec = mysql_query($lsearch); while($rrow = mysql_fetch_assoc($rexec)) // Putting right branch in an array first { $rtree[$i][0] = $rrow['parent'] ; $rtree[$i][1] = $rrow['lchild'] ; $rtree[$i][2] = $rrow['rchild'] ; $i++; } while($lrow = mysql_fetch_assoc($lexec)) // Putting left branch in an array { $ltree[$j][0] = $lrow['parent'] ; $ltree[$j][1] = $lrow['lchild'] ; $ltree[$j][2] = $lrow['rchild'] ; $j++; } function displaytree($parent,$array) { // Im stuck here. Can someone please help me with it } ?> I am stuck at displaying the tree function. Can someone please point me in the right direction ? This topic has been moved to Other. http://www.phpfreaks.com/forums/index.php?topic=327407.0 This topic has been moved to Other Web Server Software. http://www.phpfreaks.com/forums/index.php?topic=318962.0 This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=331047.0 I don't know if I'm wording this right. I have a textarea that is entered into a database. When this is edited, I want to store and display a list of all the changes that have been made to it. Like a conversation in a forum. The reason for this is that I want past changes to be locked from editing. Would a new database table storing only this text fields data need to be created? Maybe have column for the work request number and a second column for the text entered? But then wouldn't I need an third identifier column so the changes can be displayed in the right order? Looking for sage like wisdom Thanks Brad_Strickland This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=345560.0 I have the following code: Code: [Select] $result = mysql_query("SELECT * FROM ESSAY_QUESTIONS WHERE SUBJECT = 'ENGLISH'") or die(mysql_error()); while($essay_data=mysql_fetch_array($result)){ $question = $essay_data['QUESTION']; $id = $essay_data['ID']; echo "<a href=\"englishessays.php?id=$id>"; echo "$question </a>"; echo "<br><br>"; } It seems to be almost working but it just displays one link and the address is all the rest of the code including </a>"; echo "<br><br>"; and other questions. I presume there's an error with how I've written the echo statements so can anyone see it? Or can anyone suggest a better way to do this? On the next page, the php will read the ID from the address and display the information form that record. I making an web app for teachers to assess their students online. The problems I am currently having are on select only one record is displayed and after update no records are displayed in the form even though it was successfully updated.. The program has two files, index.php and process.php. The problems started when got the class name into the query to search and display the class. $result = $mysqli->query("SELECT * FROM data WHERE classroom = '".$classroom."' ORDER BY '.name';") or die($mysqli->error); The query works but doesn't order by name I would like. I would be grateful if anyone could cast an eye over it and give me some tips. Thanks guys. The beers are on me if anyone can sort it, but you'll have to come to Phnom Penh, Cambodia as that is where I live. Anyway here's the code. process.php <html> <?php session_start(); $mysqli = new mysqli("localhost","ray","password","reports") or die(mysqli_error($mysqli)); $id = 0; $update = false; $name = ''; $classroom = ''; if (isset($_GET['edit'])){ $id = $_GET['edit']; $update = true; $result = $mysqli->query("SELECT * FROM data WHERE id=$id") or die($mysqli->error()); if(isset($result->num_rows) && $result->num_rows > 0) { $row = $result->fetch_array(); $name = $row['name']; $classroom = $row['classroom']; $pacomment = $row['pacomment']; } } if (isset($_POST['update'])){ $id = $_POST['id']; $pacomment = $_POST['pacomment']; $mysqli->query("UPDATE data SET pacomment= '$pacomment' WHERE id=$id") or die($mysqli->error); $_SESSION['message'] = "Record has been updated!"; $_SESSION['msg_type'] = "warning"; header('location: index.php'); } index,php <?php require_once 'process.php'; ?> <!--************************************** Setup Messages **************************************** --> <?php if (isset($_SESSION['message'])): ?> <div class="alert alert-<?=$_SESSION['msg_type']?>"> <?php echo $_SESSION['message']; unset($_SESSION['message']); ?> </div> <?php endif ?> <!--**************************************End Setup Messages *********************************************** --> <?php ?> <!DOCTYPE html> <html> <head> <title>Home of English Reports</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style>= body { margin: 0; font-family: Arial, Helvetica, sans-serif; } .topnav { overflow: hidden; background-color: #008080; } .topnav a { float: left; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } .topnav a:hover { background-color: #ddd; color: black; } .topnav a.active { background-color: #4CAF50; color: white; } </style> </head> <body style="background-color:linen;"> <!--************************************** End Setup Page Styles ************************************* --> <div class="container"> <form action="index.php" method = get> <label for="classroom">Class name:</label><br> <input type="text" id="classroom" name="classroom" value="107i am"><br> <input type="submit" value="Submit"> </form> </div> <?php $resultcomment = $mysqli->query("SELECT EnglishComment FROM comments"); ?> <!-- ************************************** Begin Connect DB ************************************************ --> <div class="container" align-content-center> <?php $classroom = (isset($_GET['classroom']) ? $_GET['classroom'] : null); $result = $mysqli->query("SELECT * FROM data WHERE classroom = '".$classroom."' ORDER BY '.name';") or die($mysqli->error); ?> <!-- ************************************** End Connect DB **************************************************** --> <div class="row justify-content-center"> <form action="process.php" method="POST"> <input type="hidden" name="id" value="<?php echo $id; ?>"> <div class="form-group"> <h1><label><?php echo $name?></label></h1> </div> <form action="process.php" method="POST"> <input type="hidden" name="id" value="<?php echo $id; ?>"> <div class="form-group"> <h3><label>PA Teacher's Comment</label></h3> <select name = "pacomment"> <?php while($rows = $resultcomment-> fetch_assoc()) { $EnglishComment = $rows['EnglishComment']; echo "<option value='$EnglishComment'>$name.$EnglishComment</option>"; } ?></h2> </select><br> <p> <div class="form-group"> <?php if ($update == true): ?> <button type="submit" class="btn btn-info" name="update">Update</button> <?php else: ?> <!-- <button type="submit" class="btn btn-primary" name="save">Save</button> --> <?php endif; ?> </div> </form> <!-- ************************************** Begin Setup Table Headers ****************************************** --> <div class="row justify-content-center"> <table class="table" width = "20%" border = "5" cellpadding = "1";> <thead> <tr> <th><center>Action</center></th> <th><center>ID</center></th> <th>Name and Comment</th> </tr> </thead> <!-- ************************************** End Setup Classlist Table Headers ****************************************** --> <!-- ****** Loop thru Every Record From $result Query Variable and get variables and echo each variable into the table rows ********** --> <?php while ($row = $result->fetch_assoc()): ?> <tr> <td> <center><a href="index.php?edit=<?php echo $row['id']; ?>" class="btn btn-info">Assess</a></center> </td> <!-- ************************************** Put data into Classlist table rows ****************************************** --> <td><center><?php echo $row['studentid']; ?></center></td> <td><?php echo $row['name']." ".$row['pacomment'] ?></td> </tr> <?php endwhile; ?> <!-- ****************** End While() Loop **************************** --> </table> <!-- *************** End of Classlist Table ****************************************** --> </div> </div> </div> </body> </html>
Edited July 25, 2020 by raymon What is the best way to display the next record on a web page, from a list of records, without executing the query over and over again each time you display the next record? For example, imagine my user has a list of messages displayed on a web page, and the user filters that list to display all messages with the title "Test", and let's say that returns 12 records. He then clicks on record 1 to read that message. Once he is done reading it, he then wants to read msg 2, and then msg 3 etc. He could hit the Back button in his browser and click on the next record from the list, but it would be more useful to just click on a link that says "Next" which displays the next message in the filtered list of messages (without having to go back to the list). A lot of web sites do this sort of thing, but what is the best way to do it? Do you have to run the original query again each time a new message is loaded, so you know which message is next, or can you somehow store the query to save the server from having to run the same query over and over again? If a user is viewing a list of pictures for example, they could be flicking through the pictures quite quickly, and I would not like to have the server running the same query over and over again every few seconds when it doesn't need to. Advice? (Thanks.) All I am trying to do is add a record on a page without the page refreshing. For that ajax is used. Here is the code.
It does not add the record to mysql table. Can anyone tell me what I am doing wrong?
record.php
<!DOCTYPE HTML> <html lang="en"> <head> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> <script type="text/javascript" > $(function() { $(".submit_button").click(function() { var textcontent = $("#content").val(); var name = $("#name").val(); var dataString = 'content='+ textcontent + '&name='+name; if(textcontent=='') { alert("Enter some text.."); $("#content").focus(); } else { $("#flash").show(); $("#flash").fadeIn(400).html('<span class="load">Loading..</span>'); $.ajax({ type: "POST", url: "action.php", data: dataString, cache: true, success: function(html){ $("#show").after(html); document.getElementById('content').value=''; $("#flash").hide(); $("#content").focus(); } }); } return false; }); }); </script> </head> <body> <?php $record_id = $_GET['id']; // getting ID of current page record ?> <form action="" method="post" enctype="multipart/form-data"> <div class="field"> <label for="title">Name *</label> <input type="text" name="name" id="name" value="" maxlength="20" placeholder="Your name"> </div> <div class="field"> <label for="content">content *</label> <textarea id="content" name="content" maxlength="500" placeholder="Details..."></textarea> </div> <input type="submit" name="submit" value="submit" class="submit_button"> </form> <div id="flash"></div> <div id="show"></div> </body> </html>action.php if(isset($_POST['submit'])) { if(empty($_POST['name']) || empty($_POST['content'])) { $error = 'Please fill in the required fields!'; } else { try { $name = trim($_POST['name']); $content = trim($_POST['content']); $stmt = $db->prepare("INSERT INTO records(record_id, name, content) VALUES(:recordid, :name, :content"); $stmt->execute(array( 'recordid' => $record_id, 'name' => $name, 'content' => $content )); if(!$stmt){ $error = 'Please fill in the required fields.'; } else { $success = 'Your post has been submitted.'; } } catch(Exception $e) { die($e->getMessage()); } } } When having different levels of directories, using relative paths will not work anymore, for example: controller - authentication File 1: include('../../model/header.php') model File 2: header.php view File 3. style.css The header.php file includes the css file with a relative path, but the problem is it includes it as follows: ../view/style.css When now the header.php file gets included into File 1 in the folder "authentication", then the css file will not be accessible anymore, for it to be accessible you would have to go two directories up. In this sense my question is, what would be the proper path structure for a folder structure with multiple levels? Should I rather use absolute paths, I am not so prone of absolute path. What if the folders changes a bit, or the domain changes, or the location changes? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=353620.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=348130.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=354650.0 This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=309496.0 |