I am getting value in this format 2.00. I want to show the value as 2.How can i do so using php? My code to get value form the database is following and i am using floor function but evrytime i get value 0.
code
while ($row = oci_fetch_array($rcv, OCI_ASSOC+OCI_RETURN_NULLS))$basic_info[]=$row;
foreach ($basic_info as $x)
{
$serial=$x['SERIAL'];
$in = floor($serial);
}
echo "$in"
value in the database:
2.00
desired output:
2
currentoutput:
0
Use number_format() for that
number_format($x['SERIAL'], 0);
You can convert your $in value in int.
while ($row = oci_fetch_array($rcv, OCI_ASSOC+OCI_RETURN_NULLS))$basic_info[]=$row;
foreach ($basic_info as $x)
{
$serial=$x['SERIAL'];
$in = floor($serial);
}
echo (int)$in; // here is change.
Use typecasting.
$in = intval($serial);
Or
$in = (int) $serial;
while ($row = oci_fetch_array($rcv,OCI_ASSOC+OCI_RETURN_NULLS))$basic_info[]=$row;
foreach ($basic_info as $x){$serial=$x['SERIAL'];
$in = (int) $serial;} echo "$in";
Related
my question is how to display string as a pattern using php loops like, if the string is computer,on first iteration it will display "c" and then second iteration it will display like "co" and so on.My following code given below.please give a solution.
<?php
$array = "computer";
$count = strlen($array);
for($i=0;$i<=$count;$i++)
{
echo $array[$i]."<br>";
}
?>
output will print like this
c
co
com
comp
compu
$array = "computer";
$count = strlen($array);
for($i=0;$i<=$count;$i++)
{
echo substr($array,0,$i+1)."<br>";
}
$array = "computer";
$count = strlen($array)-1;
$out='';
for($i=0;$i<=$count;$i++)
{
$out .= $array[$i];
echo $out."<br>";
}
Have a nice day :-)
You can use substr($array, 0, $i + 1) instead of $array[$i]. Visit to: PHP: substr for more information.
i'm realy done with this. My problem is the following:
code:
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ".$highest++;
echo $final;
?>
All i want is adding something to the number in the $string. So as a result it should echo 2. However it echos 1.
Whats wrong?
Use pre-increment:
$final = "secound value is: ".++$highest;
More info: Incrementing/Decrementing Operators
Try the below code
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ". ++$highest;
echo $final;
?>
The reason is, $highest++ is the post-increment. It will increment the value only after its usage. And ++$highest is pre-increment will increment the value first and then use it
You are doing a post increment. If you do a pre-increment, you will get 2 as your result. Is this what you want?
$final = "secound value is: ".++$highest;
Just to put an answer out of the box. You can save yourself exploding and an array that you may not need/want.
$highest = substr($str, 2) + 1;
There is a difference between pre-increment and post increment.
pre-increment-(++$highest)
At first increase the value by one then executes the statement.
post-increment-($highest++)
At first executes the statement then increase the value by one.
So in this case You have to use pre-increment.Then line will be executed after increment of the value .
Try this code:
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ".++$highest;
echo $final;
?>
I have an array.First i want to search a specific value in that array and want to return that value with multiple by a value.I was trying below way but in this way its multiple with array keys but i want it will multiple with arrays value. It can be very simple but i don't know the solution.
<?php
$arr = ['10','20','30','40'];
$searching = array_search("30", $arr);
if ($searching) {
$result = $searching*30;
echo $result;
}
?>
Output is : 60
But i want : 900
The array_search() function search an array for a value and returns
the key.
Try this:
array_search give you the index, so use $arr[$searching].
$arr = ['10','20','30','40'];
$searching = array_search("30", $arr);
if ($searching) {
$result = $arr[$searching] * 30;
echo $result;
}
in_array function will help you to determine if value exists in your array.
If it exists - return this value, multiplied by some other value.
$arr = ['10','20','30','40'];
$search_val = 20;
if (in_array($search_val, $arr)) {
echo 30 * $search_val;
}
Try it:-
<?php
$arr = ['10','20','30','40'];
$val = 20;
echo (in_array($val, $arr)) ? (30 * $val) : "";
?>
Using the following example in PHP:
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
1) I would like to iterate on the 4 values in $priv. Would 'foreach' be the correct way to do it?
2) If the value is higher than a given number, I would like to echo the index of this value. Not sure how to do it. The comparaison must be INT (not string).
Ex. using "30" it would output:
PAGE_C
PAGE_D
Is it possible? Or maybe I am not using the correct container for what I'm trying to do ?
PS. How would you call the type of "$priv" in this example ? An array ? An indexed variable ? A dictionary ? A list ?
Thank you.
basically:
<?php
function foo($var){
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
$out='';
foreach ($priv as $k=>$v){
if ($v >$var){
$out .= $k.'<br>';
}
}
return $out;
}
echo foo('30');
demo: http://codepad.viper-7.com/GNX7Gf
Just create an array with the letters to iterate over.
$letters = array('A','B','C','D');
for($i=0;$i<count($letters);$i++) {
if($priv['PAGE_' . $letters[$i]] > /*value*/) {
echo $priv['PAGE_' . $letters[$i]];
}
}
$priv is an array.
Also, it's not too clear to me what you are exactly trying to do. Are you trying to echo the value of the array element if it's greater than a constant value?
We could do it using PHP builtin array functions. Its good to use builtin functions if possible in case of performance.
array_walk will do the trick for you. In this case $priv is an associative PHP array. Following is the one line script that will do what you want to achieve:
$input = 30;
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
array_walk($priv, function($value, $key, $input){ if($value > $input) echo $key . '<br>';}, $input);
In my db there is a table that have some values like this[string]
100/100
50/100
40/80
7/70
I need to change this values in
100%
50%
50%
10%
How can i do this using only PHP/mysql code?
EDIT:this is the code:
foreach ($html->find('div.stat') as $status_raw){
$status = $tag_pic_raw->src;
$status = mysql_real_escape_string($status);
$data->query("INSERT IGNORE INTO `tb` (`value`) VALUES ('".$status."')");
}
I have used a DOM inspector to get info from another site
Used explode() combined with some math.
$str = '40/80';
$vals = explode('/', $str);
$percent = (($vals[0] / $vals[1]) * 100).'%';
echo $percent;
use explode() to divide up the values and then math them.
foreach($array as $val) // assuming all the (##/##) values are in an array
{
$mathProblem = explode("/", $val);
echo (intval($mathProblem[0]) / intval($mathProblem[1]) * 100)."%<br />";
}
You can set up a function similar to this:
function math($one)
{
$new = explode("/", $one);
$math = $new[0] / $new[1];
$answer = $math * 100;
return $answer."%";
}
Then every time you need to query something, you can do it simply by doing this:
foreach ($results as $r)
{
echo math($r);
}
This just makes it (I think) tidier and easier to read.
Use explode() :Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter( / in this case).
$arr=array('80/100','50/100','40/80');
foreach($arr as $str){
$values = explode('/',$str);
echo ((int)$values[0]/(int)$values[1])*100.'%<br/>';
}