-
Posts
24,565 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Why am I not getting the number of expected result through the loop?
Barand replied to Abel1416's topic in PHP Coding Help
Your code worked fine for me. Are you sure that, when you ran it, your logged user has more than 1 course? -
Use code tags ( <> button in toolbar ) when posting code.
-
Unfortunately, this isn't a reliable guide. I usually browse the Activity page and select a topic. If I feel that the topic is not in my area of expertise, I will click the "Back" button and return to the Activity list. However, this still shows me as viewing the topic, which may be for a while if there are no other new topics. It isn't until I click "Home" that it again shows me as viewing the index.
-
Do not use SELECT * Specify what you want and your (PDO) code becomes $query = "SELECT id , name as title , dep_date as start , ret_date as end FROM tours ORDER BY id"; $result = $con->query($query); echo json_encode($result->fetchAll());
- 8 replies
-
- fullcalendar
- javascrip
-
(and 2 more)
Tagged with:
-
Is there a better way to do php ajax live search from the database?
Barand replied to imgrooot's topic in PHP Coding Help
It looks like you are not returning valid JSON in your ajax response. If you are retrieving multiple rows then you need to send back an encoded array of rows (the whole result set) and then process it as such. You are encoding each row thus returning a list of several json encode array strings -
Is there a better way to do php ajax live search from the database?
Barand replied to imgrooot's topic in PHP Coding Help
Very good!. In which case var_dump() would give string(5) "foot%" But if, say, an extra character (maybe a space) had crept in there, it would show something like string(6) "foot %" Your mission, should you choose to accept it, it to determine if $param_term really contains what you think it should contain. It's part of a process called "debugging". -
Problem transferring data between pages using PHP session
Barand replied to dunno's topic in PHP Coding Help
What if no new value has been posted but your session value already contains 50? Perhaps... $get_ppp = $_POST['ppp'] ?? $_SESSION['ppp'] ?? 15; $_SESSION['ppp'] = $get_ppp; -
Is there a better way to do php ajax live search from the database?
Barand replied to imgrooot's topic in PHP Coding Help
Does this improve things? $get_data->bindParam(':param', $param_term, PDO::PARAM_STR); EDIT: nm - that's the default anyway. Does var_dump($param_term) give the expected resul? -
This is the code for the first <?php require '../db_inc.php'; $db = myConnect('josen'); $res = $db->query("SELECT concat(m.first_name, ' ', m.last_name) as name , m.email , m.phone , c.value as phone2 , t.region_id , GROUP_CONCAT(DISTINCT t.postcode ORDER BY region_id, postcode SEPARATOR ', ' ) as postcodes FROM hfwji_swpm_members_tbl m JOIN hfwji_members_towns mt USING (member_id) JOIN hfwji_towns t ON t.id = mt.town_id LEFT JOIN hfwji_swpm_form_builder_custom c ON m.member_id = c.user_id AND c.field_id = 33 GROUP BY region_id, m.member_id ORDER BY region_id, m.last_name; "); // // PROCESS THE QUERY RESULTS // $prev_id = ''; $prev_name = ''; $output = ''; $prev = ''; foreach ($res as $r) { if ($r['region_id'] != $prev) { if ($prev != '') { $output .= "</div></div>\n"; } $prev = $r['region_id']; $output .= <<<HEAD <div class='w3-container w3-padding w3-margin w3-border'> <div class="w3-panel w3-blue-gray"> <h3>Region {$r['region_id']}</h3> </div> <div class="w3-row"> HEAD; } $output .= <<<MEMBER <div class="w3-col m4"> <div class="w3-card-4 w3-light-gray w3-margin w3-padding member"> <i class="fas fa-user"></i><b>{$r['name']}</b><br> <i class="fas fa-envelope"></i>{$r['email']}<br> <i class="fas fa-phone"></i>{$r['phone']} {$r['phone2']} <div class="w3-panel w3-padding w3-blue pcodes"> {$r['postcodes']} </div> </div> </div>\n MEMBER; } $output .= "</div>\n"; ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 19.5 (Build 19515, 64bit)"> <title>Example</title> <meta name="author" content="Barand"> <meta name="creation-date" content="07/12/2021"> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.1/css/all.css"> <style type='text/css'> .member { line-height: 20px; } i { color: #2DABE1; margin-right: 16px; } .pcodes { font-size: 12pt; font-weight: 600; } </style> </head> <body> <?=$output?> </body> </html>
-
That wasn't the issue. The postcodes in my data all have have only one region. The point is that Peter has several postcodes but not all in the same region.
-
Yes. How you do it depends on how you want to handle those members who cover postcodes in more than one region (if there are any) Like this or show Peter like this
-
period is the PHP string concatenation operator, $a = 'oran'; $b = '223' echo $a.$b; // oran223 .= is a shorcut... $name = $a; $name = $name . $b; // oran223 can also be written as $name = $a $name .= $b; Just as with numeric operators $x = 50; $x += 20; $x += 10; echo $x; // 80 As for the <<<MEMBER, read up on heredoc syntax
-
You got the last line correct to close the final div group so why did you think echo '">' would do it earlier on?
-
Thanks. I was just scratching my head wondering who that came from.
-
There is an alternative approach given the type of output you require this time. This time the query will return a single row for each member which contains a list of the postcodes covered by each. The processing then becomes a lot more simple. <?php require '../db_inc.php'; $db = myConnect('josen'); $res = $db->query("SELECT concat(m.first_name, ' ', m.last_name) as name , m.email , m.phone , c.value as phone2 , GROUP_CONCAT(DISTINCT t.region_id, ' ', t.postcode ORDER BY region_id, postcode SEPARATOR ', ' ) as postcodes FROM hfwji_swpm_members_tbl m JOIN hfwji_members_towns mt USING (member_id) JOIN hfwji_towns t ON t.id = mt.town_id LEFT JOIN hfwji_swpm_form_builder_custom c ON m.member_id = c.user_id AND c.field_id = 33 GROUP BY m.member_id ORDER BY m.last_name; "); // // PROCESS THE QUERY RESULTS // $prev_id = ''; $prev_name = ''; $output = ''; foreach ($res as $r) { $output .= <<<MEMBER <div class="w3-col m4"> <div class="w3-card-4 w3-light-gray w3-margin w3-padding member"> <i class="fas fa-user"></i><b>{$r['name']}</b><br> <i class="fas fa-envelope"></i>{$r['email']}<br> <i class="fas fa-phone"></i>{$r['phone']} {$r['phone2']} <div class="w3-panel w3-padding w3-blue pcodes"> {$r['postcodes']} </div> </div> </div> MEMBER; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 19.5 (Build 19515, 64bit)"> <title>Example</title> <meta name="author" content="Barand"> <meta name="creation-date" content="07/12/2021"> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.1/css/all.css"> <style type='text/css'> .member { line-height: 20px; } i { color: #2DABE1; margin-right: 16px; } .pcodes { font-size: 12pt; font-weight: 600; } </style> </head> <body> <div class="w3-row"> <?=$output?> </div> </body> </html>
-
The processing remains basically the same with a couple of small additions prev_town = "" foreach results if town != prev_town if prev_town != "" // if not first group close previous div end if open new div group // open new group echo town heading prev_town = town end if echo member data end foreach close previous div // close final div group
-
I am wondering if @mcfc4heatons is is really referring to NULL values occuring as the result of a LEFT JOIN, in which case putting the conditions in a WHERE clause does not work - they need to be in the JOIN .. ON conditions
-
Problem transferring data between pages using PHP session
Barand replied to dunno's topic in PHP Coding Help
Don't assume that the only way data can be sent to your page is from a nice user correctly using your form. Assume the opposite - that the data hasn't come from a benvolent source. -
Error occurs while updating data in MySQL using CSV file
Barand replied to sashavalentina's topic in PHP Coding Help
$product_stock, $product_status and $product_id are all nemeric so do not put them in side quotes within your query string. Any extra whitespace will prevent a match. It is more efficient to prepare a query once and bind the parameters then execute it within the loop. You are doing it the slowest way possible. However, the most efficient way would be to put the data in array then use a single multi-record insert. $fp = fopen('myfile.csv', 'r'); $rec = fgetcsv($fp); // header rec while ($rec = fgetcsv($fp)) { $data[] = vsprintf("(%d, %d)", $rec); // store in array } // now insert/update the array data into the table $conn->query("INSERT INTO products (id, product_stock) VALUES " . join(',', $data) . " ON DUPLICATE KEY UPDATE product_stock = VALUES(product_stock) , product_status = 1 ");- 1 reply
-
- 1
-
Problem transferring data between pages using PHP session
Barand replied to dunno's topic in PHP Coding Help
1 ) Are you calling session_start() on page1 and page2? 2) I see no mention of $_POST['ppp'] when setting the session value. -
Hmm! Method A or method B? When you are caught on the horns of a dilemma, look for a third way Consider a sticky registration form. When first loaded the input values are blank, but if the validation fails you want to show the user's values. In this situation I like the "null coalesce" operator, ie "??". Example <?php $fname = $_POST['fname'] ?? ''; $lname = $_POST['lname'] ?? ''; $email = $_POST['email'] ?? ''; $mobile = $_POST['mobile'] ?? ''; $error_message = ''; if ($_SERVER['REQUEST_METHOD']=='POST') { $errors = []; // array for any error messages // validate input here if (!$errors) { // update database here header("location: thispage.php"); // reload page exit; } else { $error_message = 'yada yada'; } } ?> <html> <body> <form method='POST'> First name : <input type='text' name='fname' value='<?=$fname?>'> <br> Last name : <input type='text' name='lname' value='<?=$lname?>'> <br> Email : <input type='text' name='email' value='<?=$email?>'> <br> Mobile : <input type='text' name='mobile' value='<?=$mobile?>'> <br> <br> <button type='submit'>Submit</button> </form> <?=$error_message?> </body> </html>
-
I don't understand your question.
-
If you haven't got it displaying errors, and startup errors, check the error log.
-
Selecting from duplicate values but wanting the max
Barand replied to mongoose00318's topic in MySQL Help
If you make that a (INNER) JOIN instead of a LEFT JOIN, you can remove... The join will do the filtering for you. -
Having had a second look at my query and data, the only table that requires a LEFT JOIN is the "form_builder_custom" one, giving SELECT t.town , t.postcode , concat(m.first_name, ' ', m.last_name) as name , m.email , m.phone , c.value as phone2 FROM hfwji_towns t JOIN hfwji_members_towns mt ON t.id = mt.town_id JOIN hfwji_swpm_members_tbl m USING (member_id) LEFT JOIN hfwji_swpm_form_builder_custom c ON m.member_id = c.user_id AND c.field_id = 33 ORDER BY town, last_name what error message did you get?