Jump to content

Barand

Moderators
  • Posts

    24,343
  • Joined

  • Last visited

  • Days Won

    795

Everything posted by Barand

  1. But $test = $interval->format('%m'); was your example. Are you claiming that isn't OO?
  2. Not sure what you are trying to say with that comment. How would you use date intervals without "the OO method of coding"?
  3. Note that instead of using the format method to get the months value ... $test = $interval->format('%m'); you can get it directly from the "m" property of the interval object... $test = $interval->m; Similarly, you can use the ->days property instead of ->format('%a')
  4. As the error message says" ... expects 1 parameter". You haven't provided any in ... or die( mysqli_error() ); RTFM
  5. Immedialtely after posting my first reply I realised I hadn't posted my results. I therefore edited the reply and added them. But they aren't there! Anyway, they were 2019-07-15 12:30:00 | 2019-07-17 14:15:30 | 0 0 2 1 45 30 2019-08-15 00:00:00 | 2019-08-17 01:45:30 | 0 0 2 1 45 30
  6. That could account for a one hour difference, but a whole day!?
  7. When I do this I get exactly the same differences??? function test2 ($start, $end, $newstart) { $dt1 = new DateTime($start); $dt2 = new DateTime($end); $dif = $dt1->diff($dt2); $dt3 = new DateTime($newstart); $dt4 = clone $dt3; $dt4->add($dif); echo $dt1->format('Y-m-d H:i:s') . ' | ' ; echo $dt2->format('Y-m-d H:i:s') . ' | ' ; echo $dif->format('%y %m %d %h %i %s') . '<br>' ; echo $dt3->format('Y-m-d H:i:s') . ' | ' ; echo $dt4->format('Y-m-d H:i:s') . ' | ' ; echo $dt3->diff($dt4)->format('%y %m %d %h %i %s') . '<br>' ; } test2('2019-07-15 12:30:00', '2019-07-17 14:15:30', '2019-08-15');
  8. Apply the mthod in my previous post
  9. num_rows() is for SELECT queries. affected_rows() is for INSERTS, UPDATES, DELETES
  10. Assuming the "parentid" in the relations table relates to the question.id $res = $db->query("SELECT q.id , q.question , q.pageid , childid FROM question q LEFT JOIN question_relations r ON q.id = r.parentid; "); $questions = []; foreach ($res as $r) { if (!isset($questions[$r['id']])) { $questions[$r['id']] = [ 'question' => $r['question'], 'pageid' => $r['pageid'], 'relations'=> [] ]; } $questions[$r['id']]['relations'][] = $r['childid']; } $jQuestions = json_encode($questions); The resulting questions array looks like... Array ( [1] => Array ( [question] => looking for accommodation? [pageid] => 1A [relations] => Array ( [0] => 2 [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 ) ) ) Is that what you are trying to do?
  11. You need to remove them from the SESSION, not just from the page display, then redisplay the list from the session data. Otherwise, when you eventually save the session data the deleted items will still be there.
  12. I have no idea what you are starting with or what you want to finish with, so here is general method of dealing with a hierarchy. category table +--------+-----------+--------+ | cat_id | catname | parent | +--------+-----------+--------+ | 1 | Cat 1 | 0 | | 2 | Cat 2 | 0 | | 3 | Cat 3 | 0 | | 4 | Cat 1 1 | 1 | | 5 | Cat 1 2 | 1 | | 6 | Cat 2 1 | 2 | | 7 | Cat 2 2 | 2 | | 8 | Cat 2 3 | 2 | | 9 | Cat 3 1 | 3 | | 10 | Cat 1 1 1 | 4 | | 11 | Cat 1 1 2 | 4 | | 12 | Cat 2 3 1 | 8 | +--------+-----------+--------+ Code output Store arrays of items belonging to each parent in an array Starting with parent 0, list its children but each time you list a child you check if it is itself a parent and list its children. The recursive function does this for you. Code $sql = "SELECT cat_id, catname, parent FROM category"; $res = $db->query($sql); // // store arrays of items for each parent in an array // while (list($id, $name, $parent) = $res->fetch_row()) { $data[$parent][] = array('id'=>$id, 'name'=>$name); } echo '<pre>', print_r($data, 1), '</pre>'; // call the recursive function displayHierarchy($data, 0); // function to print a category then its child categories function displayHierarchy(&$arr, $parent) { if (isset($arr[$parent])) echo "<ul>\n"; foreach($arr[$parent] as $rec) { echo "<li>{$rec['name']}"; if (isset($arr[$rec['id']])) displayHierarchy($arr, $rec['id']); echo "</li>\n"; } echo "</ul>\n"; }
  13. By passing the key of the item you want to delete from the SESSION and not removing the entire table.
  14. I have been playing around with a possible database solution to your problem Given that a postcode such as "EH12 3AB" breaks down into four parts viz +------+----------+--------+------+ | area | district | sector | unit | +------+----------+--------+------+ | EH | 12 | 3 | AB | +------+----------+--------+------+ ... I was toying with this table structure CREATE TABLE `postcode` ( `pc_id` int(11) NOT NULL AUTO_INCREMENT, `seller` int(11) DEFAULT NULL, `area` varchar(2) DEFAULT NULL, `district` varchar(2) DEFAULT NULL, `sector_min` char(1) DEFAULT NULL, `sector_max` char(1) DEFAULT NULL, `unit_min` char(2) DEFAULT NULL, `unit_max` char(2) DEFAULT NULL, `deliverable` tinyint(4) DEFAULT NULL, `price` decimal(8,2) DEFAULT NULL, PRIMARY KEY (`pc_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +-------+--------+------+----------+------------+------------+----------+----------+-------------+-------+ | pc_id | seller | area | district | sector_min | sector_max | unit_min | unit_max | deliverable | price | +-------+--------+------+----------+------------+------------+----------+----------+-------------+-------+ | 1 | 1 | EH | 1 | 1 | 4 | AA | ZZ | 1 | 1.50 | | 2 | 1 | EH | 1 | 5 | 5 | AA | BZ | 1 | 1.80 | | 3 | 1 | EH | 1 | 5 | 5 | CA | ZZ | 0 | 2.00 | | 4 | 1 | EH | 2 | 1 | 9 | AA | ZZ | 1 | 2.25 | | 5 | 1 | EH | 3 | 1 | 9 | AA | PZ | 1 | 2.50 | +-------+--------+------+----------+------------+------------+----------+----------+-------------+-------+ My code was $postcodes = [ 'EH1 2DB', 'eh15bg' , 'eh1 5ba', 'eh15dg', 'EH2 7HJ', 'EH3 2PT', 'EH3 8SX', 'EH146DE' ]; echo '<pre>'; foreach ($postcodes as $pc) { vprintf('%s%s %s%s : %s<br>', deliveryPrice($db, $pc)); } echo '</pre>'; function deliveryPrice($db, $pcode) { $pcode = strtoupper(str_replace(' ', '', $pcode)); $area = $district = ''; $sector = substr($pcode,-3, 1); $unit = substr($pcode, -2); $l = strlen($pcode); $first = str_split(substr($pcode, 0, $l-3)); foreach ($first as $c) { if (ctype_digit($c)) { $district .= $c; } else { $area .= $c; } } $res = $db->prepare("SELECT price FROM postcode WHERE area = ? AND district = ? AND ? between sector_min AND sector_max AND ? BETWEEN unit_min AND unit_max AND deliverable "); $res->execute( [ $area, $district, $sector, $unit ] ); $p = $res->fetchColumn(); $price = $p ? number_format($p, 2) : 'N/A'; return [$area, $district, $sector, $unit, $price ]; } RESULTS: EH1 2DB : 1.50 EH1 5BG : 1.80 EH1 5BA : 1.80 EH1 5DG : N/A EH2 7HJ : 2.25 EH3 2PT : 2.50 EH3 8SX : N/A EH14 6DE : N/A
  15. Same here. When I started work in IT there was a theory around that, one day, every company would have a computer!
  16. Aah, memories! I cut my SQL teeth on dBase and FoxPro around 30+ years ago.
  17. Have you checked what has been inserted into the invoice table? It looks like you get values from the first row then loop through the rest of the rows in the results, outputting the same values from the first row into every new record in the invoice table. All that processing could be accomplished with a single "INSERT ... SELECT ... " query. Keep trying and good luck.
  18. If you read the previous posts and code in this topic before jumping in, all those questions will be answered.
  19. Why don't you acquire a UK postcode database with latitude and longitude information and base the prices/deliverability on distance (ranges)?
  20. In this excerpt from your code, all lines except the execute() are assigning values - you know how to assign in PHP.
  21. Right idea, but you can't just paste javascript code into php and expect it to work. The variable names and syntax are different.
  22. Just echo another <td>..</td> with the delete button html inside it.
  23. Was the comment on that line not a sufficient clue?
  24. In your loop above, when you output the key and the value, also output a delete button (just as I did in my example).
  25. Which is which? Can you provide your table structures and, perhaps, some sample data. (SQL dump maybe)
×
×
  • 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.