Jump to content

Jamiesw

Members
  • Posts

    14
  • Joined

  • Last visited

Jamiesw's Achievements

Member

Member (2/5)

0

Reputation

  1. Perfect thanks, code works a treat now.
  2. Hi there, I am wanting to insert post data into my DB, this is guest information if i do a $safePost = filter_input_array(INPUT_POST); print_r($safePost); i get the following output. Array ( [action] => addGuestInfo [guestno] => 2 [bookingid] => 151 [customerid] => 22 [Guestname-0] => Jamie [Guestname-1] => Joe [GuestAge-0] => 4-3 [GuestInfo-0] => celiac [GuestAge-1] => 18 [GuestInfo-1] => wheat ) normally the guestname-0, guestage-0, guestinfo-0 would be in correct order but we have to ensure the just incase it isns't (see output above) we cater for that aswell, we can have upto 6 guest details coming in at once. so I need to loop through the data and insert into my DB. I have tried various for / foreach loops but cant seem to get it to group the data together I have tried things like below, but it inserts each record multiple times. $safePost = filter_input_array(INPUT_POST); foreach($safePost as $key => $value){ $exp_key = explode('-', $key); // for ($i =0; $i <= $guestcount; $i++) { switch ($exp_key[0]) { case 'Guestname': $booking->guestno = $exp_key[1]; $booking->guestname = $val; break; case 'GuestAge': $booking->guestno = $exp_key[1]; $booking->guestage = $val; break; case 'GuestInfo': $booking->guestno = $exp_key[1]; $booking->guestinfo = $val; break; default: break; } //CALLS FUNCTION TO INSERT TO DB result = lastInsertId(); $result= $booking->insertGuestDetails(); //} }
  3. 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 ) ) )
  4. 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
  5. 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; }
  6. 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>
  7. 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; } }
  8. 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 />
  9. 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.
  10. 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>
  11. Thanks, where can i find your examples?
  12. 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.
  13. 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..
  14. 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.