-
Posts
24,550 -
Joined
-
Last visited
-
Days Won
821
Everything posted by Barand
-
Hvae you looked at the page's html source code?
-
Php and Laravel prevent duplicate entries help please
Barand replied to PNewCode's topic in PHP Coding Help
No hablo Laravelese. Sorry, I've never used it. -
You only need one query. For example TABLE : product +----+-------------+--------------+--------+ | id | productName | category | status | +----+-------------+--------------+--------+ | 1 | Room 1 | Guestroom | Active | | 2 | Room 2 | Guestroom | Active | | 3 | Room 3 | Guestroom | Active | | 4 | Room 4 | Guestroom | Active | | 5 | Function 1 | Functionroom | NULL | +----+-------------+--------------+--------+ code $sql = "SELECT SUM(status='Pending')as pending , SUM(status='Active') as active FROM product"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($result); echo "Pending : <input type=\"button\" class=\"button\" value=\"{$row['pending']}\"> Active : <input type=\"button\" class=\"button\" value=\"{$row['active']}\"> "; output
-
The code you posted should show 0 if there are no pending orders. Post your actual code.
-
To display the value of a variable inside a string, the string needs to enclosed in double-quotes. You need echo "<input type='button' class='button' value='$rowcount'>"; or echo "<input type=\"button\" class=\"button\" value=\"$rowcount\">"; If you are only interested in the number of records, and not the individual records, it is far more efficient to get SQL to count them and return the total intead of returning the data then counting them. EG SELECT COUNT(*) as rowcount FROM orders WHERE status='Pending' or SELECT SUM(status='Pending') as rowcount from orders
-
I suspect that the culprit is your IDE trying to be helpful. Check its settings.
-
This one - in millions of database tables the world over. But if you don't have the sense to follow the best-practice approach then be prepared for slower queries, more date problems and more work.
-
It's as meaningful as "cell1" - but column names like that ring other warrning bells, like unnormalised tables with data stored like spreadsheets
-
There are several FETCH modes in PDO. The most usual is to set the default to PDO::FETCH_ASSOC so the row arrays are indexed by field name. This is normally set in your connect options. However, even if the default is set it can be overridden when required. So... $row = $result->fetch(PDO::FETCH_NUM) will allow you to use $row[6].
-
You can do it in the query... SELECT id , newdatetime AS stored_date , DATE_FORMAT(newdatetime, '%d/%m/%Y %H:%i') AS pretty_date FROM orders ORDER BY stored_date; +----+---------------------+------------------+ | id | stored_date | pretty_date | +----+---------------------+------------------+ | 2 | 2024-10-28 09:00:00 | 28/10/2024 09:00 | | 9 | 2024-10-28 12:00:00 | 28/10/2024 12:00 | | 14 | 2024-10-28 18:00:00 | 28/10/2024 18:00 | | 6 | 2024-10-29 00:00:00 | 29/10/2024 00:00 | | 10 | 2024-10-29 06:00:00 | 29/10/2024 06:00 | | 4 | 2024-10-29 11:00:00 | 29/10/2024 11:00 | | 5 | 2024-10-29 14:00:00 | 29/10/2024 14:00 | | 11 | 2024-10-29 18:00:00 | 29/10/2024 18:00 | | 7 | 2024-10-30 00:00:00 | 30/10/2024 00:00 | | 1 | 2024-10-30 06:00:00 | 30/10/2024 06:00 | | 15 | 2024-10-30 10:00:00 | 30/10/2024 10:00 | | 13 | 2024-10-30 11:00:00 | 30/10/2024 11:00 | | 8 | 2024-10-30 16:00:00 | 30/10/2024 16:00 | | 12 | 2024-10-30 20:00:00 | 30/10/2024 20:00 | | 3 | 2024-10-31 01:00:00 | 31/10/2024 01:00 | +----+---------------------+------------------+
-
Random dates test data (TABLE: orders) +----+---------------------+ | id | ordertimestamp | +----+---------------------+ | 1 | 06:00:00 10.30.2024 | | 2 | 09:00:00 10.28.2024 | | 3 | 01:00:00 10.31.2024 | | 4 | 11:00:00 10.29.2024 | | 5 | 14:00:00 10.29.2024 | | 6 | 00:00:00 10.29.2024 | | 7 | 00:00:00 10.30.2024 | | 8 | 16:00:00 10.30.2024 | | 9 | 12:00:00 10.28.2024 | | 10 | 06:00:00 10.29.2024 | | 11 | 18:00:00 10.29.2024 | | 12 | 20:00:00 10.30.2024 | | 13 | 11:00:00 10.30.2024 | | 14 | 18:00:00 10.28.2024 | | 15 | 10:00:00 10.30.2024 | +----+---------------------+ Now add a proper datetime column. ALTER TABLE orders ADD COLUMN newdatetime DATETIME; Now transfer the old timestamp data to new column, reformatting the data UPDATE orders SET newdatetime = STR_TO_DATE(ordertimestamp, '%H:%i:%s %m.%d.%Y'); Now we can list the data in date order SELECT * FROM orders WHERE newdatetime > NOW() - INTERVAL 24 HOUR ORDER BY newdatetime; +----+---------------------+---------------------+ | id | ordertimestamp | newdatetime | +----+---------------------+---------------------+ | 1 | 06:00:00 10.30.2024 | 2024-10-30 06:00:00 | | 15 | 10:00:00 10.30.2024 | 2024-10-30 10:00:00 | | 13 | 11:00:00 10.30.2024 | 2024-10-30 11:00:00 | | 8 | 16:00:00 10.30.2024 | 2024-10-30 16:00:00 | | 12 | 20:00:00 10.30.2024 | 2024-10-30 20:00:00 | | 3 | 01:00:00 10.31.2024 | 2024-10-31 01:00:00 | +----+---------------------+---------------------+
-
YYYY-MM-DD dates are far more efficient (store data for functionality, not prettiness). They ... can be sorted can be compared (earlier/later than) can be used directly by the dozens of MySql date/time functions without prior conversion (Beaten to the post)
-
When storing dates in database always use yyyy-mm-dd format, and store in a DATE type column (or date time type if you need the time too. See https://forums.phpfreaks.com/topic/325220-date-help/?do=findComment&comment=1638885
-
Always store dates in yyyy-mm-dd format. Unlike your format, this format is sortable. In addition, use a DATE or DATETIME type column to enable the use of the many excellent datetime functions. There is a workaround - STR_TO_DATE() function will allow reformatting of the date string SELECT STR_TO_DATE('15/09/2024', '%d/%m/%Y') as reformatted; +-------------+ | reformatted | +-------------+ | 2024-09-15 | +-------------+ So you can... select * from test_92; +----+------------+ | id | paydate | +----+------------+ | 1 | 29/10/2024 | | 2 | 27/10/2024 | | 3 | 15/09/2024 | +----+------------+ SELECT id -> , paydate -> FROM test_92 -> ORDER BY STR_TO_DATE(paydate, '%d/%m/%Y'); +----+------------+ | id | paydate | +----+------------+ | 3 | 15/09/2024 | | 2 | 27/10/2024 | | 1 | 29/10/2024 | +----+------------+
-
If the user selects "All", leave the "lorry = '$lorry'" out of the query. Only include that bit when a specific lorry is selected. You need to normalize your data. The lorry name should be repeated in the sales table records - the id of the lorry should be used. Therefore your lorry options should be <option value='id'>lorry name </option>
-
If "all" is selected you search your data for "WHERE lorry = 'all' " Do you have any data with that lorry name?
-
How are you processing the data when a lorry or "All" is selected?
-
mea culpa. I missed the closing ">" on the "</option>" echo "<option $sel >{$row['lorry']}</option\n"; ^
-
Not your best idea to publish a password in a public forum. I've obscured it but you should consider changing it. Try <select name="lorry" id="lorry" class="custom-select select-2"> <option value="all">All Lorries</option> <?php $mysqli = new mysqli('localhost', 'wwwbeechwoodsolu_collrystmusr', '********', 'wwwbeechwoodsolu_coal-lorry-system'); $sql = "SELECT id, lorry FROM lorries"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $sel = ($row['lorry'] == $_GET['lorry']) ? 'selected' : ''; echo "<option $sel >{$row['lorry']}</option\n"; } } ?> </select>
-
Sort a 8x2 array based on values at 2nd column. How?
Barand replied to oslon's topic in Other Programming Languages
You need to use a custom sort function with usort() usort($array['wh2d'], fn($a,$b)=>$a[1]<=>$b[1]); or, if you are uncomfortable with that syntax, ... usort($array'wh2d', 'mysort'); function mysort($a, $b) { return $a[1] <=> $b[1]; } -
You can't reference alias in the field list part of the query (they aren't allocated until the later output phase of the query. For instance, you can order by final), You need SELECT v.Total AS total, SUM(vt.numberofstudents) AS alunos, (v.Total/SUM(vt.numberofstudents)) AS final ... [edit] P.S. To summarize +---------+---------------------------+ | I/O | Query Clauses | +---------+---------------------------+ | | | | INPUT | SELECT ... | Column aliases | | FROM ... | defined but not | | WHERE ... | referenced | | | +---------+---------------------------+ | | | | | GROUP BY ... | Column aliases | OUTPUT | HAVING ... | can be referenced | | ORDER BY ... | here | | | +---------+---------------------------+
-
Well, one use would be to identify which cells are in each 9x9 square
-
If the individual cells are numbered 0 - 80 in 9 rows of 9 Then the index (0-8) of the large 3x3 square is given by function gridSquare($n) { return floor(floor($n/9) / 3) * 3 + floor($n % 9 / 3); } where $n i the nunber of the individual cell (0-80)
-
Connect via WordPress "wpdb" to another database vs "mysqli_connect"
Barand replied to dbareis's topic in PHP Coding Help
If both databases are on the same server then you only need a single connection (A connection is made to the server, not a specific database. The database name in the connect function is just the default to use). This allows you you access two databases in a single query. Specify the database.tablename when referencing the tables. Suppose you want to copy tableA from DB1 to DB2... CREATE TABLE DB2.tableA LIKE DB1.tableA; INSERT INTO DB2.tableA SELECT * FROM DB1.tableA; If DB1 is the default, it can be omitted EG INSERT INTO DB2.tableA SELECT * FROM tableA; -
How is your data expected to look when received by the API? You've shown two separate sets of data so far - is that just to confuse us?