Make an equation in php - php

I have a php file where i want to make an equation with the output i get.
It is pretty easy if the line is like this:
<?php echo round(($post['voteup']/($post['voteup'] + $post['votedown'] ))*100); ?>
But i have a php line where it goes like this, and i just can't figure out how to make the equation work:
$str='';
$data = $dbc->query($sql);
if($data!=null && $data->num_rows>0){
while( $row = $data->fetch_array(MYSQLI_ASSOC)){
$str.="<p>".$row['voteup']." + ".$row['votedown'].</p>";
}
It may be a silly question, but this really bothers me.

If you want to add the upvotes and downvotes you would do this -
$newTotal = $row['voteup'] + $row['votedown'];
$str.="<p>" . $newTotal ."</p>";

If you are trying to add $row['voteup'] and $row['votedown'] together you should addition them in a variable first then use it.
$total = $row['voteup'] + $row['votedown'];
$str .= "<p>{$total}</p>";
If you really need to make it inline you can do something like this as well.
$str .= '<p>' . ($row['voteup'] + $row['votedown']) . '</p>';

If you just want to display the "equation" simply fix the error.
$str.="<p>".$row['voteup']." + ".$row['votedown']."</p>";
Or you could actually turn it into an equation:
$str.="<p>".$row['voteup']." + ".$row['votedown']." = ".($row['voteup'] + $row['votedown'])."</p>";

Related

how can i add " to a variable in php?

so I'm trying to make a variable that contains a " sign
like if i had a variable called $x
i want my variable
$y = """ , $x , """
but i can't just write """ so what is the method of adding a " to a variable
im using php 5 but i don't think that's gonna help i mean im justwriting more because this site doesn't allow me to post but i can't explain more and i cant find my answer in another place
and again more writing yada yada , some questions are small so WHY just WHY does it think it's incomplete
You can do it like this
$x =11;
$y = '"""'.$x.'"""';
echo $y;
Result
"""11"""
now i finally figured out what i was doing wrong , i was using , instead of . between the values so the final code is
$y = '"' . $x . '"' ;

Incrementing a string by one in PHP

I am struggling to find a way to increment a specific pattern required. For each new member, they are given a unique ID such as ABC000001. Each new members ID should increment by one. So the second member's ID would be ABC000002. I am using PHP and MySQL to keep track of each member but I have not been able to come up with a way to properly increment using the string format above.
What is the best way to approach this?
As #axiac mentions this is probably not a good idea but it's pretty easy to manage.
$memberid = 'ABC000001';
list($mem_prefix,$mem_num) = sscanf($memberid,"%[A-Za-z]%[0-9]");
echo $mem_prefix . str_pad($mem_num + 1,6,'0',STR_PAD_LEFT);
Split your current member number into the alpha and numeric parts then put them back together bumping the number when you do it. I use this as a function and pass the previous ID and what I get back is the next ID in the sequence.
You can extract only digits using regex to increment and using str_pad for create a prefix :
$memberid = 'ABC000001';
$num = preg_replace('/\D/', '',$memberid);
echo sprintf('ABC%s', str_pad($num + 1, "6", "0", STR_PAD_LEFT));
Possible answer without regex.
Runs through each character and checks if it is a number or not.
Then uses sprintf() to make sure leading 0s are still there.
$str = "ABC000001";
$number = "";
$prefix = "";
$strArray = str_split($str);
foreach ($strArray as $char) {
if (is_numeric($char)) {
$number .= $char;
} else {
$prefix .= $char;
}
}
$length = strlen($number);
$number = sprintf('%0' . $length . 'd', $number + 1);
echo $prefix . $number;
This works for this instance but would not work if the prefix had numbers in it.
just use the PHP increment operator ++
as long as you've sufficient leading zeros in the pattern, then PHP will correctly increment the numeric component
This code:
<?php
$name = 'ABC0000098';
print ++$name . PHP_EOL;
print ++$name . PHP_EOL;
print ++$name . PHP_EOL;
Outputs:
ABC0000099
ABC0000100
ABC0000101
Read more in the PHP docs:
https://www.php.net/manual/en/language.operators.increment.php
aha - just saw there is already a link to a similar example in the comment posted by "axiac" above

PHP repeat action with adding value

I am just learning coding, and couldn't find solution for that:
I'm creating a website that shows pictures that are on its server.
$number = $_POST["NR"];
$picture1 = "{$number}.jpg";
echo '<img src="pictures/' . $picture1 . ' ">';
How can I make it to repeat the echo string, but every time add for "$picture1" calculation +1 ? So, that the maximum count would be 40?
Thanks for helping. I know that it is actually easy, but I still don't know how to do it :)
Assuming that you have images named correctly awaiting this to happen:
$number = $_POST["NR"];
$i = 0;
while ($i < 40) {
$picture = $number + $i.".jpg";
echo '<img src="pictures/' . $picture . ' ">';
$i++;
}
Use a counter and loop. The issue here is that your $number could be anything... For instance if someone passed 500, you would need images named, 500.jpg, 501.jpg, etc. all the way up to 540.jpg. Unless I misunderstood the question.

PHP code snippet decode

I was looking a craigslist for web dev jobs (I am a beginner).
I came across a Jr. job that wanted me to decode this snippet.
<?php
$f1 = 'e' . 'x' . 'p';
$f1 .= 'l' . 'o' . 'd' . 'e';
list($f2,$ext) = $f1('.',$argv[0]);
$x = array('a','H','I',
'r','Y','2',
'x','q','c',
'm','R','l',
'd','k','B',
'l','d','m',
'9','r','b',
'm','93','L',
'm','N','v',
'b','Q','');
echo $f2(implode('', $x)) . "\n";
?>
Since I do not know the answer I a a bad candidate for the job, but I would like to understand it if anyone can help me. What I do get is that you are using the string of $f1 to manipulate the array $x. But i see problems with using $argv[0] and I believe that using f2(implode('', $x)) will give a parameter error.
Any help would be greatly appreciated.
Hehe, that was kidna fun. It's the email to their HR department.
<?php
$argv[0] = 'base64_decode.';
$f1 = 'explode';
list($f2,$ext) = $f1('.',$argv[0]);
$x = array('a','H','I',
'r','Y','2',
'x','q','c',
'm','R','l',
'd','k','B',
'l','d','m',
'9','r','b',
'm','93','L',
'm','N','v',
'b','Q','');
echo $f2(implode('', $x)) . "\n";
Not really sure what they were looking for in an answer. Made me think of this though. http://blog.sucuri.net/2013/09/ask-sucuri-non-alphanumeric-backdoors.html

Display 1000 as 1k using PHP

I have beginners skills with PHP and hence need your help guys.
What i am trying to achieve is to round up a number for total shares count for a plugin.
my variable is $totalshare
i am using return $totalshare to dispaly the results
i would like to achieve results as following example:
1000 = 1k
1.230 = 1.2k
1489 = 1.5k
1.660 = 1.6k
.
.
.
15576 = 15.6k
.
.
1201200 = 12.02mil
1211200 = 12.12mil
Any help would be much appreciated.
Thanks in advance.
You can write like this,
function convertShares($shareValue){
if($shareValue < 1000){
return shareValue;
}elseif($shareValue > 100000 ){
return number_format($shareValue/100000,2) . "mil";
}else{
return number_format($shareValue/1000,2) . "k";
}
}
Simplest way:
if($n>=1000) $n = ($n/1000)."k";
if you want something more i can edit

Categories