Jump to content

Barand

Moderators
  • Content Count

    19,390
  • Joined

  • Last visited

  • Days Won

    382

Everything posted by Barand

  1. 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.
  2. 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"; }
  3. By passing the key of the item you want to delete from the SESSION and not removing the entire table.
  4. 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
  5. Same here. When I started work in IT there was a theory around that, one day, every company would have a computer!
  6. Aah, memories! I cut my SQL teeth on dBase and FoxPro around 30+ years ago.
  7. 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.
  8. If you read the previous posts and code in this topic before jumping in, all those questions will be answered.
  9. Why don't you acquire a UK postcode database with latitude and longitude information and base the prices/deliverability on distance (ranges)?
  10. In this excerpt from your code, all lines except the execute() are assigning values - you know how to assign in PHP.
  11. Right idea, but you can't just paste javascript code into php and expect it to work. The variable names and syntax are different.
  12. Just echo another <td>..</td> with the delete button html inside it.
  13. Was the comment on that line not a sufficient clue?
  14. In your loop above, when you output the key and the value, also output a delete button (just as I did in my example).
  15. Which is which? Can you provide your table structures and, perhaps, some sample data. (SQL dump maybe)
  16. I have modofied my original example to include "Delete" buttons in the playlist. At some point you will need to commit the playlist stored in the session to permanant storage. Selection page... <?php // // FOR DEBUG PURPOSES ONLY - LIST CONTENTS OF SESSION PLAYLIST // session_start(); if (isset($_SESSION['playlist'])) { echo '<pre>', print_r($_SESSION['playlist'], 1), '</pre>'; echo "</hr><br>\n"; } ?> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $().ready( function() { $(".btn").click( function() { var vid = $(this).data("id"); var vname = $(this).data("name"); $.post( "my_cart.php", { "voice_id" : vid, "voice_name" : vname, "action" : "Add" }, function(resp) { outputPlaylist(resp) }, "JSON" ) }) function outputPlaylist(resp) { var list = "<tr><td><b>ID</b></td><td><b>Title</b></td></tr>\n"; $.each(resp, function(k, v) { list = list + "<tr><td>" + k + "</td><td>" + v + "</td>" list = list + "<td><button class='delbtn' data-id='"+ k +"'>Delete</button></td></tr>\n" // add "Delete" button to each playlist item }) $("#playlist").html(list) // define action for new delbtn's $(".delbtn").click( function() { var vid = $(this).data("id"); $.post( "my_cart.php", { "voice_id" : vid, "action" : "Delete"}, function(resp) { outputPlaylist(resp) }, "JSON" ) }) } }) </script> </head> <body> Song 1 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="1" data-name="song-1.mp3">Add to PlayList </button> <br> Song 2 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="2" data-name="song-2.mp3">Add to PlayList </button> <br> Song 3 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="3" data-name="song-3.mp3">Add to PlayList </button> <br> Song 4 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="4" data-name="song-4.mp3">Add to PlayList </button> <br> Song 5 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="5" data-name="song-5.mp3">Add to PlayList </button> <br> Song 6 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="6" data-name="song-6.mp3">Add to PlayList </button> <br> <br> <h2>Playlist</h2> <table style="width:600px" id="playlist"> </table> </body> </html> my_cart.php (handles ajax requests)... <?php session_start(); if ($_SERVER['REQUEST_METHOD']=='POST') { $voice_id = $_POST['voice_id'] ?? 0; $action = $_POST['action'] ?? ''; if ($action == 'Delete') { // we are deleting the item from the session array if ($voice_id) { unset($_SESSION['playlist'][$voice_id]); exit(json_encode($_SESSION['playlist'])) ; } } elseif ($action == 'Add') { // we are adding the item $voice_name = $_POST['voice_name'] ?? ''; if ($voice_id && $voice_name) { $_SESSION['playlist'][$voice_id] = $voice_name; exit(json_encode($_SESSION['playlist'])) ; } } else { exit("ERROR") ; } } exit("ERROR") ; ?>
  17. Therein lies your problem. The parent id is $child['parentid'] since $child is the array, not $row. (When you get that error message on the first iteration, $row contains "1")
  18. You need to be aware of what your variables contain. It would seem that $row is a string and not an array. What does this output... echo '<pre>', print_r($child, 1), '</pre>';
  19. You are referencing the variable $question inside your function $question must be defined inside the function or passed to the function as an argument,
  20. You can't pass arguments by reference in the function call. The "by reference" should be defined in the function definition EG // define function function my_func(&$chr) { // do something to $chr ++$chr; } // call function $x = "A"; echo $x; // A my_func($x); echo $x; // B (I am surprised that that 5.3 didn't have an adverse reaction to your code. I thought that restriction had been around longer than that)
  21. Barand

    Dark Theme

    Well done, that looks much better. The "patchwork" effect has gone now and there is a subtle but distinct grouping of the elements. Icons next to board titles are also clearer.
  22. Barand

    Dark Theme

    The rectangle around the latest post on each board
  23. Barand

    Dark Theme

    "Home" page looks a mess with element backgrounds not quite matching parent background. Blobs and stars next to topics are indistinct
  24. Perhaps something like $links['company_website'] = get_the_company_website(); $links['company_twitter'] = get_the_company_twitter(); $links['company_facebook'] = get_post_meta( get_the_ID(), '_facebook_link', true ); foreach (array_filter($links) as $cls = $link) { // output link }
×
×
  • 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.