Subtracting DB values via php - php

I am trying to subtract values of a db table $activationFee from a users total balance . How may i add the subtraction in the code below:
<div class="number">
<strong>
<?php echo $settings->currency; ?>
<?php BalanceSystem($user_id); ?>
</strong>
</div>
function BalanceSystem($user_id){
$user_bank = DB::table('bank')->where('userid', $user_id)->first();
echo $user_bank->balance;
}
Can someone please point me in the right direction?
I tried <?php BalanceSystem($user_id) - $activationFee ; ?> but did not work ... hides

You should not echo inside a function, it will prevent situations like this, where you try to substract a value to an output.
<div class="number">
<strong>
<?php echo $settings->currency; ?>
<?php echo BalanceSystem($user_id); ?>
</strong>
</div>
function BalanceSystem($user_id){
$user_bank = DB::table('bank')->where('userid', $user_id)->first();
return $user_bank->balance;
}
//Substract
<?php echo (BalanceSystem($user_id) - $activationFee); ?>

This is how I managed to solve it:
function BalanceSystem($user_id){
$user_bank = DB::table('bank')->where('userid', $user_id)->first();
$total = $user_bank->balance - 100;
echo $total;
}

Related

<br> does not work in my while

I come to you because I encounter a problem with my while loop.
I did a while loop to get the id of tickets and ticket titles.
Except that, I want to add a <br> but it does not work, it does not move.
I tried with two <br> <br> it's the same, can you help me please?
Here is my code :
<?php while ($row = $req_5->fetch()) { ?>
<?php echo $row['titre']."<br><br>"; ?>
<?php } ?>
<?php while ($row = $req_5->fetch()) { ?>
<?php echo $row['titre']?>
<br><br>
<?php } ?
just put the break tag outside of the anchor element

how to put div in a for loop

For parsing a list of articles, i have this code to parse all the articles:
while($article = $articles->fetch())
{
$date = strtotime($article['createdAt']);
$formatted_date = date("F Y",$date);
?>
<br />
<div class="news-content">
<div class="news-image">
<?php echo $article['title']; ?>
</div>
<div class="news-article">
<h3>
<span><?php $date = strtotime($article['createdAt']); echo /*date("F j",$date);*/ strftime('%e %B',$date) ?></span>
<br />
<?php echo $article['title']; ?>
</h3>
</div>
</div>
<?php
} //end while loop
?>
What i want to achieve: only the first 5 <div class="news-content">...</div> should be shown.
I know i have to do something with a for loop
but i do not know exactly how to use the for loop for this situation...
Can someone help me with that?
There are a lot of different ways to limit a loop. One possibility is to use a for loop instead of a while loop. for is often a good option if you want something to happen a specific number of times. Adding something else like fetch into the continuation condition will mean it happens up to a specific number of times.
for ($i = 0; $i < 5 && $article = $articles->fetch(); $i++) {
// output article
}

How to clean up this strange PHP code?

this script outputs the birthdays as headlines and the customer(s) below.
So the output is "grouped" by the date of birth.
echo "<div>";
foreach(pdo_query("SELECT customer, birthday FROM table ORDER BY birthday ASC", array($empty)) as $row)
{
if(!isset($birthday) or $birthday != $row['birthday'])
{
unset($drawline);
echo "</div>";
echo "<div class=title><h1>".$row['birthday']."</h1></div><div class=customer>";
}
if(isset($drawline)){echo "<hr>";}
echo $row['customer']."<br>";
$drawline = 1;
$birthday = $row['birthday'];
}
echo "</div>";
'birthday' is a DATE-field in the database.
Between the customers there's always a line (hr), but not after the last customer of a d.o.b.
Example output:
<div class=title><h1>1986-10-08</h1></div>
<div class=customer>
Don Foo<br>
<hr>
Joe Bar<br>
</div>
<div class=title><h1>1988-03-18</h1></div>
<div class=customer>
Jane Fonda<br>
<hr>
Elvis Burns<br>
</div>
Is it possible to remove the <div> and </div> outside the foreach()?
It produces always an empty <div></div>.
I've decided that it's too much hassle on the programmer's mind to see what's happening here, so here is much cleaner version of your code.
<?php
$people = pdo_query("SELECT customer, birthday FROM table ORDER BY birthday ASC", array($empty));
if (!empty($people)) {
// Find out people with the same birthday and group them.
$birthdays = [];
foreach ($people as $man) {
$birthday = $man['birthday'];
if (empty($birthdays[$birthday])) {
$birthdays[$birthday] = [];
}
$birthdays[$birthday][] = $man['customer'];
}
// Now let's output everything!
?>
<div>
<?php foreach ($birthdays as $birthday => $customers): ?>
<div class=title><h1><?= $birthday ?></h1></div>
<div class="customer">
<?= implode('<br><hr>', $customers) ?>
</div>
<?php endforeach;?>
</div>
<?php } // endif (!empty($people)) ?>
Main point of this solution - very simple to understand, uses somewhat templating (you could create one for this little snippet), easy to debug and modify. And yes, it does not output the empty <div></div> anymore!

How can I mix PHP and HTML code?

I have a while loop and I have a HTML code inside the while loop. Inside this HTML code there is PHP code reading a variable that depends on the condition of the while loop.
This variable should be evaluated by PHP. I wrote the code below to solve this but it doesn't work.
$row[0] depends on the condition of while. My code only outputs an empty space for this variable.
<?php
while ($row = mysql_fetch_row($stt)) {
$html='<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0 <span>likes</span>
</p>
<p>
_____________________
</p>
</div>';
echo $html;
}
?>
If you want to display something row by row, do this:
<?php
while($row = mysql_fetch_row($stt)){
$html="<div class='data'>" . nl2br($row[0]) . "\n<p>comment<a href=''>like</a><a href=''>share</a></p><p>0<span>likes</span></p><p></p></div>";
echo $html;
}
?>
> <?php echo nl2br($row[0]."\n"); ?>
this part should be outside of $html.
The interpreter on server side couldnt run.Accordin to php, this part is not a code block. It is just a text.
I found the answer of my question!!!
This is the solution:
<?php
while($row = mysql_fetch_row($stt)){ ?>
<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0
<span>likes</span>
</p>
<p>
_____________________
</p>
</div>
<?php
}
?>

Displaying content but not the latest one

I have these codes to display content on my website.
Index.php
$rest = new rest;
$list = $rest->fetch_all();
<?php foreach ($rest->fetch_all() as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
And include.php
class rest {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM podcast ORDER BY cast_id DESC");
$query->execute();
return $query->fetchAll();
} }
Please can someone tell me how I can get this to show results but not the latest result?
All fields are numbered using an ID tag in mysql.
On my site I have the latest entry listed above this list so I do not require the latest one appearing here.
Please help.
Thank you.
The easiest way to do this is to discard the row in the application. You can do it in SQL by using a calculated limit. Calculate it as (SELECT COUNT(*) FROM podcast) - 1.
Or:
SELECT *
FROM podcast
EXCEPT
SELECT TOP 1 *
FROM podcast
ORDER BY cast_id ASC
Excluding the last row.
You can either use array_shift or array_pop depending on your query sorting as shown bellow:
Assuming the latest result is the first raw on your query result.
$rest = new rest;
$list = $rest->fetch_all();
$latest = array_shift($list);
<?php foreach ($list as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
If it's the last raw that you need to hide, then use array_pop

Categories