-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
How do I combine values of same col inside a foreach loop?
Barand replied to imgrooot's topic in PHP Coding Help
No mention of balances over 100 or wallets when I asked what you wanted to do. Is there any relationship between user_id and recipient? -
How do I combine values of same col inside a foreach loop?
Barand replied to imgrooot's topic in PHP Coding Help
I had no intention of rewriting your application - just showing you how to query the data you wanted. -
Need help streamlining multi table select and output
Barand replied to darkone2022's topic in PHP Coding Help
Like this, maybe $res = $con->query("SELECT system_id , status_text_color , status_text , icon_color , status_img , link FROM status ORDER BY system_id "); foreach ($res as $row) { echo <<<OUTPUT <div class="col-xl-3 col-sm-6 grid-margin stretch-card"> <div class="card"> <div class="card-body"> <div class="row"> <div class="col-9"> <div class="d-flex align-items-center align-self-start"> <h6 class="mb-0"><?= $row['system_id']; ?></h6> <h6 class="{$row['status_text_color']} ml-2 mb-0 font-weight-medium">{$row['status_text']}</h6> </div> </div> <div class="col-3"> <div class="{$row['icon_color']}"> <span class="{$row['status_img']}"></span> </div> </div> </div> <h6 class="text-primary font-weight-normal"><a href="{$row['link']}" target="_blank">Access System</a></h6> </div> </div> </div> OUTPUT; } -
Need help streamlining multi table select and output
Barand replied to darkone2022's topic in PHP Coding Help
Could you not simply do $result = $conn->query("SELECT system_id , whatever_else FROM `status` ORDER BY system_id "); foreach ($result) { // get results for output } -
How do I combine values of same col inside a foreach loop?
Barand replied to imgrooot's topic in PHP Coding Help
Assuming the latested posted_date may not be the same for every recipient we first need to know what each recipient's latest date is. This is done with a table subquery... ( SELECT recipient , MAX(posted_date) as posted_date FROM user_earnings GROUP BY recipient ) latest We then query the earnings table joined to this subquery matching on the recipient and date so we only process the earnings with the latest date $res = $db->query("SELECT e.recipient , e.balance , e.posted_date FROM user_earnings e JOIN ( SELECT recipient , MAX(posted_date) as posted_date FROM user_earnings GROUP BY recipient ) latest USING (recipient, posted_date) "); $results = $res->fetchAll(); echo '<pre>' . print_r($results, 1) . '</pre>'; // view results $total_balance = array_sum(array_column($results, 'balance')); // get the total balance printf('$%0.2f', $total_balance) ; //-> $300.00 if you are are not interesting in seeing the individual latest records, you can straight to the total with mysql> SELECT SUM(balance) as total_balance -> FROM user_earnings e -> JOIN -> ( -> SELECT recipient -> , MAX(posted_date) as posted_date -> FROM user_earnings -> GROUP BY recipient -> ) latest USING (recipient, posted_date); +---------------+ | total_balance | +---------------+ | 300.00 | +---------------+ -
How do I combine values of same col inside a foreach loop?
Barand replied to imgrooot's topic in PHP Coding Help
You have shown us several queries that don't give you what you want. Thus we know what you don't want. We need to know what you do want. What output would you want to see from this test table and why... +----+-----------+---------+-------------+ | id | recipient | balance | posted_date | +----+-----------+---------+-------------+ | 1 | 1 | 80.00 | 2022-01-09 | | 2 | 2 | 100.00 | 2022-01-09 | | 3 | 3 | 120.00 | 2022-01-09 | | 4 | 1 | 85.50 | 2022-01-16 | | 5 | 2 | 101.25 | 2022-01-16 | | 6 | 3 | 113.25 | 2022-01-16 | | 7 | 1 | 70.00 | 2022-01-23 | | 8 | 2 | 80.00 | 2022-01-23 | | 9 | 3 | 150.00 | 2022-01-23 | +----+-----------+---------+-------------+ -
How do I combine values of same col inside a foreach loop?
Barand replied to imgrooot's topic in PHP Coding Help
Do you mean SELECT SUM(balance) as total FROM user_earnings; which gives ... -
Why don't you use a php function?
-
What do you see if you echo $sql; Is there a value at the end where the staff id should be?
-
Curly braces and endif don't play nice together. Use one syntax or the other. <?php if($chk == 0) { ?> <div id='showMe'> <div class="container"> <div class="row" style="color:red; padding-top:6rem; text-align:center;"> <h1>Database Update Failed</h1> </div> </div> </div> <?php } else { ?> <div id='showMe'> <div class="container"> <div class="row" style="padding-top:6rem; text-align:center;" > <h1><center>Database Update Successful</center></h1> </div> </div> </div> <?php } ?> </body> </html>
-
using php read from weather api and insert into mysql database
Barand replied to AJM2's topic in PHP Coding Help
Can you post the data returned by the api without all the var_dump junk in it? Can you post your code in a reasonably formatted manner? -
Where is the value for $userLoggedin coming from?
-
Example $arr = [ [ 'A', 'Jan. 22, 22'], [ 'B', 'Dec. 25, 21'], [ 'C', 'Feb. 22, 22'], [ 'D', 'Jan. 2, 22'] ]; usort($arr, function($a, $b) { $da = DateTime::createFromFormat('M. j, y', $a[1]); $db = DateTime::createFromFormat('M. j, y', $b[1]); return $db <=> $da; }); echo '<pre>' . print_r($arr, 1) . '</pre>'; outputs Array ( [0] => Array ( [0] => C [1] => Feb. 22 22 ) [1] => Array ( [0] => A [1] => Jan. 22 22 ) [2] => Array ( [0] => D [1] => Jan. 2 22 ) [3] => Array ( [0] => B [1] => Dec. 25 21 ) )
-
COALESCE() comes in useful here SELECT ... FROM tablename WHERE COALESCE(colname, '') = '';
-
Use PHP Variable as Value for Inline Style Property
Barand replied to braddalton's topic in PHP Coding Help
If you do, tell us what your real problem is. At present you are telling us a solution that isn't working - because it's the wrong solution, perhaps? -
Use PHP Variable as Value for Inline Style Property
Barand replied to braddalton's topic in PHP Coding Help
Use outer double quotes to insert a variable into a string. $cat_list = "<a href='blah'>cat 1</a>, <a href='yadayada'>cat 2</a>"; $cat_color = '#8F1FCF'; $cat_list = str_replace('<a', "<a style='color:$cat_color'", $cat_list); -
SImply by using the fact that there are 60 minutes in an hour and some very basic arithmetic. Therefore 0.67 hrs is 60 * 0.67 minutes (= 40 min) $t = 12.67; printf('%d hrs %d min', intval($t), ($t - intval($t))*60); //--> 12 hrs 40 min
-
It does center the text, but your inline heading is only as wide as its content (as adding a border shows)