-
Posts
24,356 -
Joined
-
Last visited
-
Days Won
798
Posts posted by Barand
-
-
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
- 2
-
20 minutes ago, charlie321 said:
Wish it was an option in my education when I was younger,
Same here. When I started work in IT there was a theory around that, one day, every company would have a computer!
-
28 minutes ago, ginerjm said:
dBase??? I remember when that first came out. And people are still using it?
Aah, memories! I cut my SQL teeth on dBase and FoxPro around 30+ years ago.
-
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.
-
27 minutes ago, ginerjm said:
How is this button going to work? What is it calling? How is the current row of data being passed to whatever script is going to do the delete?
If you read the previous posts and code in this topic before jumping in, all those questions will be answered.
-
Why don't you acquire a UK postcode database with latitude and longitude information and base the prices/deliverability on distance (ranges)?
-
On 7/2/2019 at 12:25 PM, aveeva said:
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '';
In this excerpt from your code, all lines except the execute() are assigning values - you know how to assign in PHP.
-
Right idea, but you can't just paste javascript code into php and expect it to work. The variable names and syntax are different.
-
Just echo another <td>..</td> with the delete button html inside it.
-
20 hours ago, Barand said:
list = list + "<td><button class='delbtn' data-id='"+ k +"'>Delete</button></td></tr>\n" // add "Delete" button to each playlist item
Was the comment on that line not a sufficient clue?
-
In your loop above, when you output the key and the value, also output a delete button (just as I did in my example).
-
7 minutes ago, charlie321 said:
One of the tables is the control table which will only have one row per buser. The other table I guess you would call the data table of the two and may have more than one row depending.
Which is which?
Can you provide your table structures and, perhaps, some sample data. (SQL dump maybe)
-
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") ; ?>
-
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")
-
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>';
-
You are referencing the variable $question inside your function
Quotefunction 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,
-
1 hour ago, Riko said:
Could you give an example of what the code would be in 7.1
I already have.
-
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)
-
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.
-
8 minutes ago, requinix said:
Where?
The rectangle around the latest post on each board
-
"Home" page looks a mess with element backgrounds not quite matching parent background.
Blobs and stars next to topics are indistinct
-
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 }
-
or
test = $(".sw_ui").val(); alert(test); isnum = /^\d+$/.test(test); alert(isnum);
- 1
-
What value is that JS supposed to be testing? I see nothing with a class = "test"
PHP - Ajax Passing value to another php file
in PHP Coding Help
Posted
By passing the key of the item you want to delete from the SESSION and not removing the entire table.