-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
In this code if($check) { header('location:success_package'); } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; } $error is set only if $check is false. Later on (where you get the error) you try to echo it when it hasn't been set. Also, you need to exit() from the script after that header() call to prevent the rest of the script from executing. Change the code to... $error = ''; if($check) { header('location:success_package'); exit; } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; }
-
query string problem is whooping my behind..help please!
Barand replied to Stan007's topic in PHP Coding Help
@mac_gyver gave the correct way to do it - http_build_query() EG $url = 'http://localhost/board/italian.php'; $data = [ 'page' => 2, 'country' => 'italian', 'comment' => 'this is a (simple) comment' ]; $qstr = http_build_query($data); $url .= '?' . $qstr; echo $url; //--> http://localhost/board/italian.php?page=2&country=italian&comment=this+is+a+%28simple%29+comment Note the encoding of the query string -
-
Create new line of dynamically populated content from button
Barand replied to Adamhumbug's topic in PHP Coding Help
Certainly can... CREATE TABLE `ssm_user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(45) DEFAULT NULL, `lastname` varchar(45) DEFAULT NULL, `role_id` int(11) DEFAULT NULL, PRIMARY KEY (`user_id`) ); INSERT INTO `ssm_user` VALUES (1,'Michel','Roux',2), (2,'Rick','Stein',2), (3,'Jamie','Oliver',2), (4,'Delia','Smith',2), (5,'Gordon','Ramsay',2), (6,'Nigella','Lawson',2), (7,'Marco','Pierre-White',2), (8,'Margaret','Thatcher',1), (9,'Mark','Zuckerberg',1), (10,'Bill','Gates',1), (11,'Boris','Johnson',1), (12,'Alan','Sugar',1); -
Need help on getting products to insert into database
Barand replied to purge08's topic in PHP Coding Help
I suspect that your "products" table is need of normalizing. I have marked with an "X" the bits that give me cause for concern. CREATE TABLE `products` ( `product_id` int(10) NOT NULL, `vendor_id` text NOT NULL, X `p_cat_id` int(10) NOT NULL, `cat_id` int(10) NOT NULL, `manufacturer_id` int(10) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `product_title` text NOT NULL, `product_seo_desc` text NOT NULL, `product_url` text NOT NULL, X `product_img1` text NOT NULL, X `product_img2` text NOT NULL, X `product_img3` text NOT NULL, `product_price` int(10) NOT NULL, `product_psp_price` int(100) NOT NULL, `product_desc` text NOT NULL, X `product_features` text NOT NULL, `product_video` text NOT NULL, X `product_keywords` text NOT NULL, `product_label` text NOT NULL, `product_type` text NOT NULL, `product_weight` decimal(10,1) NOT NULL, `product_views` int(10) NOT NULL, X `product_vendor_status` text NOT NULL, `status` varchar(255) NOT NULL ) -
Create new line of dynamically populated content from button
Barand replied to Adamhumbug's topic in PHP Coding Help
Here's my solution using jQuery/ajax. You can add multiple new rows to each section. Each time a row is added those staff already allocated are disabled in the new row's menu. <?php const HOST = 'localhost'; # const USERNAME = '????'; # const PASSWORD = '????'; # const DATABASE = '????'; # These lines would # function pdoConnect($dbname=DATABASE) # normally be in { # $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); # an included file $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); # $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); # $db->setAttribute(PDO::MYSQL_ATTR_LOCAL_INFILE, true); # return $db; # } # $db = pdoConnect('humbug'); // // HANDLE AJAX REQUESTS // if (isset($_GET['ajax'])) { exit(staffRow($db, $_GET['dt'], $_GET['role'], $_GET['used'])); } // // HANDLE POST DATA // if ($_SERVER['REQUEST_METHOD'] == 'POST') { // This where you handle the schedule updates. // for now we just output the posted data to check all is OK echo 'Posted data<pre>'; printf("%-15s%10s%10s%10s%10s\n\n", 'Date', 'Role', 'ID', 'Start', 'End'); foreach ($_POST['form'] as $date => $ddata) { foreach ($ddata as $role => $rdata) { foreach ($rdata['staff'] as $k => $id) { $start = $rdata['starttime'][$k]; $end = $rdata['endtime'][$k]; printf("%-15s%10s%10s%10s%10s\n", $date, $role, $id, $start, $end); } } } echo '</pre>'; } // // HANDLE GET DATA // $wkstart = $_GET['wkstart'] ?? date('Y-m-d'); $heads = ['Name', 'Start Time', 'End Time']; $sections = [ 1 => 'Management', 2 => 'Chefs' ]; $daterange = new DatePeriod(new DateTime($wkstart), new DateInterval('P1D'), 6); $tdata = ''; foreach ($daterange as $date) { $dthead = $date->format('l - jS F Y'); $dt = $date->format('Y-m-d'); $tdata .= "<tr><th colspan='3' class='w3-black w3-padding'>$dthead</th></tr>\n"; foreach ($sections as $role => $rolename) { $tdata .= "\n<tbody id='$dt$role'> <tr><th colspan='3' class='rolename'>$rolename <a href='#' class='w3-right addrow' data-dt='$dt' data-role='$role'>Add row</a> </th> </tr><tr>"; foreach ($heads as $h) { $tdata .= "<th>$h</th>"; } $tdata .= "</tr>\n"; $tdata .= staffRow($db, $dt, $role); $tdata .= "\n</tbody>\n"; } } function staffRow(PDO $db, $dt, $role, $used='') { return " <tr><td>" . staffOptions($db, $dt, $role, $used) . "</td> <td><input type='time' name='form[$dt][$role][starttime][]' value='' class='w3-input w3-border'></td> <td><input type='time' name='form[$dt][$role][endtime][]' value='' class='w3-input w3-border'></td> </tr>\n"; } function staffOptions(PDO $db, $dt, $role, $used='') { $scheduled = explode(',', $used); $opts = "<select class='w3-input' name='form[$dt][$role][staff][]'> <option value=''> -- select staff name --</option> "; $res = $db->query("SELECT user_id , firstname , lastname FROM ssm_user WHERE role_id = $role "); foreach ($res as $r) { $dis = in_array($r['user_id'], $scheduled) ? 'disabled' : ''; $opts .= "<option $dis value='{$r['user_id']}'>{$r['firstname']} {$r['lastname']}</option>\n"; } return $opts; } ?> <!DOCTYPE html> <html> <head> <title>Example Staff Schedule</title> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { $(".addrow").click( function() { var dt = $(this).data('dt') var role = $(this).data('role') var sectionid = dt+role; var sect = $("#"+sectionid) var used = [] $.each($(sect).find("select"), function(k,v) { if ($(v).val() != '') { used.push( $(v).val() ) } }) if (used.length > 0) { $.get( "", {"ajax":1, "dt":dt, "role":role, "used": used.join(",")}, function (resp) { $(sect).append(resp); }, "TEXT" ) } }) }) </script> <style type="text/css"> body { font-family: calibri, sans-serif; } table { border-collapse: collapse; font-size: 10pt; width: 90%; margin: 30px auto;} th,td { padding: 4px; width: 33%; background-color: #EEE; border: 1px solid #D0D0D0; } input[type='time'] { padding-right: 16px; text-align: center; } option:disabled { color: #F2D335; } .rolename { background-color: #D8D8D8; } .addrow { text-decoration: none; background-color: #54BC54; border: 1px solid #000; padding:2px 8px; } </style> </head> <body> <header class="w3-container w3-black w3-padding"> <h1>Example Staff Schedule</h1> <form method='GET'> <label>Week commencing </label> <input type='date' name='wkstart' value='<?=$wkstart?>'> <input type='submit' name='btnSub' value='Submit'> </form> </header> <form method='POST'> <table border='1'> <?=$tdata?> <tr><td colspan='3' class="w3-center"><input type='submit' class="w3-button w3-blue" name='btnSub2' value='Update'></td></tr> </table> </form> </body> </html> -
PHP mail function how to set sender mail id based on if condition?
Barand replied to aveeva's topic in PHP Coding Help
$addys = [ 2 => '[email protected]', 3 => '[email protected]', 4 => '[email protected]' ]; // CHECK SKU FIRST DIGIT $chk_sku=(int)substr($sku, 0, 1); // GET THE APPROPRIATE EMAIL ADDRESS $sendTo = $addys[$chk_sku] ?? '[email protected]'; // USE IT $emailTemplate->setToEmail($sendTo); return $emailTemplate->send(); -
Need help on getting products to insert into database
Barand replied to purge08's topic in PHP Coding Help
-
Need help on getting products to insert into database
Barand replied to purge08's topic in PHP Coding Help
Then you have several SELECT queries that are failing. That is not where I told you to put it. -
Need help on getting products to insert into database
Barand replied to purge08's topic in PHP Coding Help
Have you checked your error log to if there are any messages there? -
Create new line of dynamically populated content from button
Barand replied to Adamhumbug's topic in PHP Coding Help
You will need JS, definitely, and maybe ajax. One approach is to make each management and chefs section a separate <tbody>..</tbody> section. That way you have something you can append to. -
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br
-
PHP mail function how to set sender mail id based on if condition?
Barand replied to aveeva's topic in PHP Coding Help
+---------------------+ | | Yes | DOES IT WORK? +-----------------------+ | | | +----------+----------+ | | | | | | No | | | | | | | +---------------------+ +----------------+ | | | | | SOMETHING'S WRONG | | PROBABLY OK | | | | | +---------------------+ +----------------+ -
PHP mail function how to set sender mail id based on if condition?
Barand replied to aveeva's topic in PHP Coding Help
I suggested the $addys array to save you a lot of repetitive ifelse coding. Getting the email address to use becomes $chk_sku=substr($sku, 0, 1); $sendTo = $addys[$chk_sku] ?? '[email protected]'; -
PHP mail function how to set sender mail id based on if condition?
Barand replied to aveeva's topic in PHP Coding Help
$emailTemplate has not been defined inside that function and is, therefore, null. -
You certainly need to change those key definitions. Unique drink id means you cannot have the same drink id for multiple jobs, and similarly for unique job id. It is the combination that needs to be unique.
-
If it helps, this is how I would do it... <?php require 'db_inc.php'; $db = pdoConnect('humbug'); // // PROCESS POST DATA // if ($_SERVER['REQUEST_METHOD']=='POST') { $jobid = trim($_POST['jobid']); if (!empty($jobid)) { $placholders = []; $values = []; foreach ($_POST['qty'] as $did => $qty) { $placeholders[] = "(?,?,?)"; array_push($values, $jobid, $did, intval($qty)); } // single multiple-insert query $stmt = $db->prepare("INSERT IGNORE INTO ssm_drink_order (job_id, drink_id, drink_qty) VALUES " . join(',', $placeholders) . "ON DUPLICATE KEY UPDATE drink_qty = VALUES(drink_qty) "); $stmt->execute($values); // clear zero qty ordere $db->exec("DELETE FROM ssm_drink_order WHERE drink_qty = 0"); } // reload the form header("Location:?jobid="); exit; } // // PROCESS GET DATA // $jobid = $_GET['jobid'] ?? ''; $formdata = ''; $stmt = $db->prepare("SELECT d.drink_id , d.drink_name , o.drink_qty FROM ssm_drink d LEFT JOIN ssm_drink_order o ON d.drink_id = o.drink_id AND o.job_id = ? ORDER BY d.drink_id "); $stmt->execute([$jobid]); foreach ($stmt as $d) { $formdata .= "<label>{$d['drink_name']}</label> <input type='number' name='qty[{$d['drink_id']}]' value={$d['drink_qty']}> <br> "; } function jobOptions(PDO $db, $current) { $res = $db->query("SELECT job_id, job_name FROM ssm_job"); $opts = "<option value=''> - Choose Job - </option>\n"; foreach ($res as $r) { $sel = $r['job_id'] == $current ? 'selected' : ''; $opts .= "<option $sel value='{$r['job_id']}'>{$r['job_name']}</option>\n"; } return $opts; } ?> <!DOCTYPE html> <html> <head> <title>Example Drinks Order</title> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <style type="text/css"> label {color: #2DABE1; font-weight: 600; width: 250px; display: inline-block; } input[type='number'] { padding-right: 16px; text-align: right;} </style> </head> <body> <header class="w3-blue w3-container w3-padding"> <h1>Example Drinks Order</h1> </header> <form method="GET"> <fieldset class="w3-content w3-border w3-padding w3-margin"> <label>Job</label> <select class="w3-input w3-border" name="jobid" onchange="this.form.submit()"> <?=jobOptions($db, $jobid)?> </select> </fieldset> </form> <form method="POST"> <input type="hidden" name="jobid" value="<?=$jobid?>"> <fieldset class="w3-content w3-border w3-padding w3-margin"> <legend>Drinks Quantities</legend> <?=$formdata?> <br><br> <label> </label> <input type="submit" name="btnSub" value="Update Order"> </fieldset> </form> </body> </html> Note: Assumes ssm_drink_order is CREATE TABLE `ssm_drink_order` ( `job_id` int(11) NOT NULL, `drink_id` int(11) NOT NULL, `drink_qty` int(11) DEFAULT NULL, PRIMARY KEY (`job_id`,`drink_id`) ) or, at least, that (job_id, drink_id) is defined as UNIQUE
-
PHP mail function how to set sender mail id based on if condition?
Barand replied to aveeva's topic in PHP Coding Help
Seems like you need a couple of things 1 ) An array of email addresses (index = first digit of sku) $addys = [ 2 => '[email protected]', 3 => '[email protected]', 4 => '[email protected]' ]; 2 ) Alter the structure of your $itemData array to add the first digit of sku as a sub-index to separate each order into 3 groups $itemData[$orderNo][$skuFirst] = $message; -
So the "use" column has some significance to this problem!? That's news. Anyway, glad you found a solution to the problem you created for yourself.
-
Need help on getting products to insert into database
Barand replied to purge08's topic in PHP Coding Help
I assume you are referring to the insert query on line 1,246. I do not see you checking for any MySQL error messages anywhere following that query. These might just give you a clue as to why it isn't working as expected. I suggest you put this line of code (if you haven't already) just before you connect to the db server mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); -
A job well done.
-
Going back to your original query ... $query = "SELECT use,city,school from a_schools as s1 LEFT JOIN a_schools as s2 ON s1.bsect = s2.bsect WHERE '" . $query_school . "' = CONCAT(s1.city,' ',s1.school) || '" . $query_school . "' = s1.school ORDER BY concat(s2.city,s2.school) asc"; why aren't you basing your search on IDs instead of names, especially as you have so many schools with almost identical names?
-
Remember that the data sample you gave us contains "staff1", "staff2" etc and there is a translation to give staff initials e.g. staff1 => SP staff2 => MC that is hard-coded into your code (not the best method as you have to update the code whenever there are staff changes)
-
Of course, you could store the city and the full school name, then using the technique we discussed in this forum a couple of weeks ago, remove the city from the school name when required. I.E. SELECT school , city , TRIM(REPLACE(school, COALESCE(city,''), '')) as short_school FROM school; +------------------------------+----------------+----------------------+ | school | city | short_school | +------------------------------+----------------+----------------------+ | Bloomington North | Bloomington | North | | Columbus East | Columbus | East | | Fort Wayne Bishop Dwenger | Fort Wayne | Bishop Dwenger | | Neil Armstrong High | NULL | Neil Armstrong High | | Davy Crockett School | San Antonio | Davy Crockett School | | Kokomo | NULL | Kokomo | | Logansport | NULL | Logansport | | Marion | NULL | Marion | | Jefferson | Lafayette | Jefferson | | McCutcheon | Lafayette | McCutcheon | | Harrison | West Lafayette | Harrison | | Carmel Greyhounds | NULL | Carmel Greyhounds | | Zionsville Eagles | NULL | Zionsville Eagles | | Fishers Tigers | NULL | Fishers Tigers | | Noblesville Millers | NULL | Noblesville Millers | | Westfield Shamrocks | NULL | Westfield Shamrocks | | Hamilton Southeastern Royals | Hamilton | Southeastern Royals | +------------------------------+----------------+----------------------+
-
Are you sure they are NULL and not some with empty strings or other whitespace?