Jump to content

Barand

Moderators
  • Posts

    24,356
  • Joined

  • Last visited

  • Days Won

    798

Posts posted by Barand

  1. 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

     

    • Like 2
  2. 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")  ; 
    ?>

     

  3. You are referencing the variable $question inside your function

    Quote

    function buildTree($child) {

        $branch = array();

        foreach ($child as $row) {

             $childresults = $question->read_children($row['parentid']); 

    $question must be defined inside the function or passed to the function as an argument,

  4. 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)

  5. 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.

×
×
  • 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.