-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
The only condition you have for not inserting is if the reason has an error. You should prevent insertion if there are any errors.
-
I'm encountering an error when creating a multiuser login
Barand replied to Bramdon3000's topic in PHP Coding Help
RTFM https://www.php.net/else -
merging forums gets 1366 incorrect interger value
Barand replied to Cheagavara's topic in PHP Coding Help
Hmm.. how about the project's code? -
using for loop to display elements from array
Barand replied to webdeveloper123's topic in PHP Coding Help
There is always more than one way to solve a problem. Instead of array_merge() you could add them individually, using array_push() as above or $table[] = array ( 'fname' => 'Jon', 'lname' => 'Atkins', 'age' => '27' ); $table[] = array ( 'fname' => 'Phil', 'lname' => 'Jones', 'age' => '14' ); $table[] = array ( 'fname' => 'Frank', 'lname' => 'Lampard', 'age' => '48' ); $table[] = array ( 'fname' => 'Toney', 'lname' => 'Brentford', 'age' => '25' ); or you could loop through the newdata array to add them to $table foreach ($newdata as $rec) { $table[] = $rec; } -
using for loop to display elements from array
Barand replied to webdeveloper123's topic in PHP Coding Help
How many arrays do you now have? $table = array ( 0 => array ( 'fname' => 'Peter', 'lname' => 'Smith', 'age' => '37' ), 1 => array ( 'fname' => 'Paul', 'lname' => 'Hartley', 'age' => '48' ), 2 => array ( 'fname' => 'Mary', 'lname' => 'Baker', 'age' => '42' ), 3 => array ( 'fname' => 'Jane', 'lname' => 'Doe', 'age' => '51' ) ); $newdata = array ( 4 => array ( 'fname' => 'Jon', 'lname' => 'Atkins', 'age' => '27' ), 5 => array ( 'fname' => 'Phil', 'lname' => 'Jones', 'age' => '14' ), 6 => array ( 'fname' => 'Frank', 'lname' => 'Lampard', 'age' => '48' ), 7 => array ( 'fname' => 'Toney', 'lname' => 'Brentford', 'age' => '25' ) ); $table = array_merge($table, $newdata); foreach ($table as $row => $data) { echo "<p><b>Row number $row</b></p>"; echo "<ul>"; foreach ($data as $col) { echo "<li>$col</li>"; } echo "</ul>"; } giving -
using for loop to display elements from array
Barand replied to webdeveloper123's topic in PHP Coding Help
Use foreach() to iterate through arrays. foreach ($table as $row => $data) { echo "<p><b>Row number $row</b></p>"; echo "<ul>"; foreach ($data as $col) { echo "<li>$col</li>"; } echo "</ul>"; } -
merging forums gets 1366 incorrect interger value
Barand replied to Cheagavara's topic in PHP Coding Help
Look for the problematic query. Does the MyBB Group have anything to offer? -
merging forums gets 1366 incorrect interger value
Barand replied to Cheagavara's topic in PHP Coding Help
Change the empty string value to an integer or NULL (if NULL is permitted). -
merging forums gets 1366 incorrect interger value
Barand replied to Cheagavara's topic in PHP Coding Help
Looks like you were trying to write an empty string value into an column whose type is integer. -
Existing offers that will be affected by the new offer are those ... WHERE item_id = new_tem_id AND start_time <= new_end_time AND end_time >= new_start_time new start new end | | | | |--------------| | | not affected | | | | |-------------| not affected | | | | |--------------------------------------------| change | | |-------------------| | change | | | |-----------------| change | | | |---------| | superseded | | | |
-
selecting distinct items and listing vehicles last cleaned based on criteria
Barand replied to PatrickO's topic in MySQL Help
Your input dates will be in the $_POST array. Don't put user input data directly into your sql, use prepared queries. <form action="vehicle_cleaning-stats.php" method="post"> <input type="date" name="date_from"><input type="date" name="date_to"><input type="submit" value="Search"> </form> php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $stmt = $pdo->prepare("SELECT bus_no , COUNT(*) as total FROM `vehicle_cleaning` WHERE date_cleaned BETWEEN ? AND ? GROUP BY bus_no "); $stmt->execute( [ $_POST['date_from'], $_POST['date_to'] ] ); echo "<table>"; foreach ($stmt as $row) { echo "<tr>"; echo "<td>" . $row['bus_no'] . "</td>"; echo "<td>" . $row['total'] . "</td>"; echo "</tr>"; } echo "</table>"; } -
selecting distinct items and listing vehicles last cleaned based on criteria
Barand replied to PatrickO's topic in MySQL Help
Have you tried it with COUNT(*) in php? foreach($dbh->query("SELECT bus_no , COUNT(*) as total FROM `vehicle_cleaning` WHERE date_cleaned BETWEEN '2022-04-25' AND '2022-05-01' GROUP BY bus_no") as $row) { echo "<tr>"; echo "<td>" . $row['bus_no'] . "</td>"; echo "<td>" . $row['total'] . "</td>"; echo "</tr>"; } Re your datetime problem, what is the timezone setting on your mysql server? -
That message is telling you there is not a column called "message" in the contact_data table. Check your database - could be a spelling mistake in your table definition. BTW, dont use "-" in SQL identifiers (table and column names) but use "_" instead. Hyphens can be interpreted as minus signs in queries. For example SELECT first-name FROM user will be interpreted as column first minus column name.
-
"comment" table should not contain the user's name (that should only be in the "users" table) - you have the user_id therefore you know the name. I'd go with CREATE TABLE `news` ( `news_id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(200) DEFAULT NULL, `item_text` text, `category` varchar(20) DEFAULT NULL, `item_update` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `author` varchar(50) DEFAULT NULL, PRIMARY KEY (`news_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `users` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, `email` varchar(50) DEFAULT NULL, `username` varchar(50) DEFAULT NULL, `password` varchar(300) DEFAULT NULL, `status` enum('admin','user') DEFAULT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `comment` ( `comment_id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) DEFAULT NULL, `news_id` int(11) DEFAULT NULL, `comment` text, `comment_update` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`comment_id`), KEY `idx_comment_user_id` (`user_id`), KEY `idx_comment_news_id` (`news_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
selecting distinct items and listing vehicles last cleaned based on criteria
Barand replied to PatrickO's topic in MySQL Help
The "days_ago" will evaluate to a negative number if the date_cleaned or date_inservice is in the future. date_cleaned should always be historical (or it's an error) but it is feasible that there may be new buses in the table which come into service in the next few days. We can check that only current buses are reported by WHERE CURDATE() BETWEEN date_inservice AND IF(date_outservice=0, '9999-12-31', date_outservice) If you only want to list buses where cleaning is required, the condition would be WHERE DATEDIFF(CURDATE(), COALESCE(last_cleaned, date_inservice)) >= frequency If you only want to show the bus no and date_last_cleaned, only select those. Then the query becomes SELECT v.bus_no as 'Bus No' , COALESCE(last_cleaned, date_inservice) as 'Last Cleaned' FROM vehicle_specs v LEFT JOIN ( SELECT bus_no , MAX(date_cleaned) as last_cleaned FROM vehicle_cleaning GROUP BY bus_no ) c USING (bus_no) WHERE CURDATE() BETWEEN date_inservice AND IF(date_outservice=0, '9999-12-31', date_outservice) AND DATEDIFF(CURDATE(), COALESCE(last_cleaned, date_inservice)) >= frequency +--------+--------------+ | Bus No | Last Cleaned | +--------+--------------+ | EV-02 | 2022-04-22 | | EV-03 | 2022-04-22 | | EV-04 | 2018-01-01 | +--------+--------------+ -
selecting distinct items and listing vehicles last cleaned based on criteria
Barand replied to PatrickO's topic in MySQL Help
Just this once <?php const HOST = 'localhost'; const USERNAME = '????'; const PASSWORD = '????'; const DATABASE = 'test'; // default db function pdoConnect($dbname=DATABASE) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $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); return $db; } $pdo = pdoConnect(); $result = $pdo->query("SELECT v.bus_no as BusNo , v.rego as RegNo , COALESCE(last_cleaned, date_inservice) as last_cleaned , DATEDIFF(CURDATE(), COALESCE(last_cleaned, date_inservice)) as days_ago , v.frequency , CASE WHEN DATEDIFF(CURDATE(), COALESCE(last_cleaned, date_inservice)) >= frequency THEN 'REQUIRED' ELSE 'OK' END as cleaning FROM vehicle_specs v LEFT JOIN ( SELECT bus_no , MAX(date_cleaned) as last_cleaned FROM vehicle_cleaning GROUP BY bus_no ) c USING (bus_no)"); $row = $result->fetch(); $tdata = ''; $theads = "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n"; do { $tdata .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } while ($row = $result->fetch()) ; ?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <head> <title>Example</title> <style type='text/css'> body { font-family: calibri, sans-serif; font-size: 12pt; } table { border-collapse: collapse; width: 600px; } th { background-color: black; color: white; padding: 8px; } td { padding: 4px 8px; } </style> </head> <body> <table border='1'> <?= $theads ?> <?= $tdata ?> </table> </body> </html> -
selecting distinct items and listing vehicles last cleaned based on criteria
Barand replied to PatrickO's topic in MySQL Help
Try SELECT v.bus_no , v.rego , COALESCE(last_cleaned, date_inservice) as last_cleaned , DATEDIFF(CURDATE(), COALESCE(last_cleaned, date_inservice)) as days_ago , v.frequency , CASE WHEN DATEDIFF(CURDATE(), COALESCE(last_cleaned, date_inservice)) >= frequency THEN 'REQUIRED' ELSE 'OK' END as cleaning FROM vehicle_specs v LEFT JOIN ( SELECT bus_no , MAX(date_cleaned) as last_cleaned FROM vehicle_cleaning GROUP BY bus_no ) c USING (bus_no); +--------+--------+--------------+----------+-----------+----------+ | bus_no | rego | last_cleaned | days_ago | frequency | cleaning | +--------+--------+--------------+----------+-----------+----------+ | EV-01 | XQ63MV | 2022-04-18 | 12 | 5 | REQUIRED | | EV-02 | 914XZP | 2022-04-22 | 8 | 5 | REQUIRED | | EV-03 | 913XZP | 2022-04-22 | 8 | 5 | REQUIRED | | EV-04 | 915XZP | 2018-01-01 | 1580 | 5 | REQUIRED | | EV-05 | 916XZP | 2022-04-17 | 13 | 15 | OK | +--------+--------+--------------+----------+-----------+----------+ Note EV-04 hasn't been cleaned since it came into service - so must be due by now! -
selecting distinct items and listing vehicles last cleaned based on criteria
Barand replied to PatrickO's topic in MySQL Help
Two-thirds of the way there. What have you tried so far? -
mysqli has a fetch_all() method and PDO has a fetchAll() method. Both of the above would create that array for you. Does ADODB have the equivalent?
-
I don't understand why you would end offers at random times during they day. You buy four items for which the offer label said 50% off. Part way through your checkout the offer finishes, so you you get one of items half price and pay full price for the other three. 1 unhappy customer. You may as well do away with checkout tills and instal a roulette wheel as customers, if they carry on shopping there, won't know what they will be charged.
-
If the terms column always contained a qty and a value I would agree with you, but supermarkets have many different types of offer. Buy one get one free Buy 2 cabbages, get an Easter egg half price Buy 1 item from the main course range, one from the desserts range and one from the specified wine selection all for $10. In these cases the attributes are different for the various offer types, which could be handled by an EAV model but they are a PITA to query. JSON is another way to do it.
-
With more than a little rewriting (avoiding missing variable definitions, mis-spelt function names and illegal syntax) <?php const HOST = 'localhost'; const USERNAME = 'root'; const PASSWORD = ''; const DATABASE = 'test'; // default db function pdoConnect($dbname=DATABASE) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $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); return $db; } ### CODE ABOVE BELONGS IN AN INCLUDED FILE ################################################################### $pdo = pdoConnect('Water Tower 2000 r'); if ($_SERVER['REQUEST_METHOD']=='POST') { $stmt = $pdo->prepare("SELECT type FROM login WHERE username = ? AND password = ? "); $stmt->execute( [ $_POST['username'], $_POST['pwd'] ] ); $type = $stmt->fetchColumn(); switch ($type) { case 'Admin': header("Location: admin.html"); exit; case 'Parent': header("Location: parent.html"); exit; case 'Swimmer': header("Location: swimmer.html"); exit; default: exit("Unknown user type"); } }