Jump to content

Jamiesw

Members
  • Content Count

    12
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Jamiesw

  • Rank
    Member
  1. thanks yes, however the bit i have been struggling with is the children can have children so it is N deep. and of course then the children can be parents aswell. Array ( [1] => Array ( [question] => looking for accommodation? [pageid] => 1A [relations] => Array ( [0] => Array ( [question] => How do you pay? [pageid] => 1A1 [relations] => Array ( [0]=> Array ( [question] => Can you stay with friends? [pageid] => 2A2 [relations] => Array ( ) [1] => 4 ) ) [1] => 3 [2] => 4 ) ) [2] => Array ( [question] => How do you pay? [pageid] => 1A1 [relations] => Array ( [0] => 3 [1] => 4 ) ) [3] => Array ( [question] => Can you stay with friends? [pageid] => 2A2 [relations] => Array ( [0] => 5 [1] => 6 ) ) )
  2. Thanks for relying. We have 2 tables Table 1 - tbl_questions (pageid has nothing to do with the parent \child, it references something else) id | Question | pageid 1 | looking for accommodation? | 1A 2 | How do you pay? | 1A1 3 | Can you stay with friends? | 2A2 Table 2 - tbl_question_relations parentid | childid 1|2 1|3 1|4 2|3 2|4 3|5 3|6 4|3 4|1 so the output would be in hierarchical json
  3. I have modified the code and i think it is giving me the right answers. My question is adding the child array to the parent. $question = new Question($db); //query $results = $question->read(); $numRows = $results->rowCount(); //check for data if ($numRows >0) { $question_arr['questions'] = array(); $child_arr = array(); $question_arr['questions'] = $results->fetchAll(PDO::FETCH_ASSOC); foreach ($question_arr['questions'] as $row){ //Need to add $child_arr to the current $question_arr['question'] $child_arr = buildTree($row['id']); } echo json_encode($question_arr['questions']); } else { echo json_encode(array('message'=>'nothing here!.')); } function buildTree($id) { global $question; $question->id = $id; $child =array(); $children = array(); $results = $question->read_children(); $numRows = $results->rowCount(); if ($numRows>0) { while ($row = $results->fetch(PDO::FETCH_ASSOC)) { $children[] =$row; $child = buildTree($row['childid']); if ($child) { $children[] = $child; } } } return $children; }
  4. Hi Barand, here is the result. <pre>Array ( [parentid] => 1 [question] => Looking for somewhere to stay [pageid] => 1 [typeid] => 1 [numclicks] => 503 [date_created] => ) </pre>
  5. Hi All, Thanks, i have changed the code but get new error. Line 42 is : $question->parentid = $row['parentid']; <b>Warning</b>: Illegal string offset 'parentid' in <b>C:\htdocs\api\questions\read.php</b> on line <b>42</b><br /> function buildTree($child) { global $question; $branch = array(); foreach ($child as $row) { $question->parentid = $row['parentid']; $childresults = $question->read_children(); while ($childrow =$childresults->fetch(PDO::FETCH_ASSOC)) { $children = buildTree($childrow); if ($children) { $row['children'] = $children; } $branch[] = $childrow; } } //} return $branch; } -----------------------In My Questions.php-------------------- class Question{ public $childid; public $parentid; public $question; public function read_children() { $query = 'SELECT parent_question, parentid, parent_pageid, child_question, childid, child_pageid from ct_vquestion_parent_child_lookup where parentid=?'; // Prepare statement $stmt = $this->conn->prepare($query); // Bind ID $stmt->bindParam(1, $this->parentid); // Execute query $stmt->execute(); return $stmt; } }
  6. Hi I am trying to create a hierarchy array from our mysql database. What i have so far is: //instantiate DB & connect $database = new Database(); $db = $database->connect(); //instantiate question object $question = new Question($db); //query $results = $question->read(); //check for data if ($results) { $question_arr['questions'] = array(); $child_arr = array(); while ($row = $results->fetch(PDO::FETCH_ASSOC)) { array_push($question_arr['questions'], $row); $question_arr['questions'][] = buildTree($row); } print_r($question_arr['questions']); } else { echo json_encode(array('message'=>'nothing here!.')); } function buildTree($child) { $branch = array(); foreach ($child as $row) { $childresults = $question->read_children($row['parentid']); while ($childrow =$childresults->fetch(PDO::FETCH_ASSOC)) { $children = buildTree($childrow); if ($children) { $row['children'] = $children; } $branch[] = $childrow; } } //} return $branch; } in my Questions.php file: <?php class Question{ private $conn; public $id; public $childid; public $parentid; public $question; public $pageid; public $numclicks; public $typeid; public $date_created; // Constructor with DB public function __construct($db) { $this->conn = $db; } public function read() { //create query(sql statement) $query = 'SELECT id as parentid, question, pageid, typeid,numclicks,date_created from ct_questions'; // Prepare statement $stmt =$this->conn->prepare($query); // Execute query $stmt->execute(); return $stmt; } public function read_children($parentid) { //This is from a View I created in Mysql $query = 'SELECT parent_question, parentid, parent_pageid, child_question, childid, child_pageid from ct_vquestion_parent_child_lookup where parentid='.$parentid.''; // Prepare statement $stmt = $this->conn->prepare($query); // Execute query $stmt->execute(); return $stmt; } } the current errors i am getting are: <br /> <b>Notice</b>: Undefined variable: question in <b>C:\htdocs\api\questions\read.php</b> on line <b>41</b><br /> <br /> <b>Fatal error</b>: Uncaught Error: Call to a member function read_children() on null in C:\htdocs\api\questions\read.php:41 Stack trace: #0 C:\htdocs\api\questions\read.php(28): buildTree(Array) #1 {main} thrown in <b>C:\htdocs\api\questions\read.php</b> on line <b>41</b><br />
  7. yeah i can use either htmlentities($pagecontent) or htmlspecialchars($pagecontent) and it displays all my data now as json. Now need to work out how to convert it back to html in javascript. thanks for everyone's help.
  8. Hi there, Thanks for everyones help, the code works properly including reducing it down and not use the extract etc. The problems looks like the json_encode doesn't like the content within the Database, some of the pagecontent records contain html syntax for forms, i changed my sql to a different table and it worked as expected. any ideas on getting the json_encode to handle html syntax. (see below of sample data in the pagecontent field). [pagecontent] => <p>We just need to ask you a few questions so we can get you the right answer</p><h2>What is your age?</h2> <form> <select id="verifyage" name="verifyage" class="dropdown-fields" required> <option value="0">--Select your age--</option> <option value="3A1">Under 16</option> <option value="3A2">16 Or 17</option> <option value="3A3">18 Or Over</option> </select> <input type="submit" name="btnsubmit" id="btnsubmit" class="button" onClick="gotonextpage($('#verifyage option:selected').val());"> <form> <p> </p>
  9. Thanks, where can i find your examples?
  10. Hi, As I mentioned I am new to using PDO and was using an example from brad traversy building a backend api, the code i have used is more or less what he provided in his example including the fetch. The idea being i can pull the outputted json and use on the frontend using javascript.
  11. Thanks for replying so quickly, I am not echoing the json_encode within the loop it is echoing within the if{}, once the while{} is completed. If i print_r($page_arr['pages']); instead of trying to echo json_encode then i can see all the entries within the array. I just seems to be the json_encode isnt working, i am using PHP 7.3.6 if that makes a difference? i tried it at the end and it still made no difference..
  12. Hi there, Hoping some one can help. I am new to using PDO and i am trying to get data from my db to json I can get the data down to my $result so i know that part is working, my issue is with encoding it to json, if i pull a single record then it works if i pull all records then it doesn't, an i am not sure why $result = $page->read($_GET['pageid']); //check for data if ($result) { //page array $page_arr['pages'] = array(); while ($row =$result->fetch(PDO::FETCH_ASSOC)) { extract($row); $page_item = array( 'id'=> $id, 'title' => $title, 'pageid' => $pageid, 'pagecontent' =>html_entity_decode($pagecontent), 'mdkeywords' => $mdkeywords, 'description' =>$description, 'date_added' =>$date_added, 'hasAudio' => $hasAudio, 'excludeSearch' =>$excludeSearch ); //push to data array_push($page_arr['pages'], $page_item); } //print_r($page_arr['pages']); //If i enable this then i can see all the data //turn to json echo json_encode($page_arr['pages'], JSON_FORCE_OBJECT); //This is where my page is blank. } } else { echo json_encode(array('message'=>'nothing here!.')); }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.