I am fetching data from database in two different while loops and I want to add the variable between them outside the loop. Example:
while($cash_fetch = mysql_fetch_array($cash_qur))
{
$a = 500; //suppose I am fetching from database
}
while($card_fetch = mysql_fetch_array($card_qur))
{
$b = 1000; //suppose I am fetching from database
}
$total = $a+$b;
echo $total;
I want to do this thing exactly but I am getting inappropriate results. Please help.
You can try this -
$a = $b = 0; // set to 0 by default
while($cash_fetch = mysql_fetch_array($cash_qur))
{
$a += 500; //Increment
}
while($card_fetch = mysql_fetch_array($card_qur))
{
$b += 1000; //Increment
}
$total = $a + $b;
echo $total;
You can try with this:
$a = 0;
$b = 0;
while($cash_fetch = mysql_fetch_array($cash_qur))
{
$a = 500; //suppose I am fetching from database
}
while($card_fetch = mysql_fetch_array($card_qur))
{
$b = 1000; //suppose I am fetching from database
}
$total = $a+$b;
echo $total;
You may want to do like this:
$total = 0;
while($cash_fetch = mysql_fetch_array($cash_qur))
{
++$total; //Increment
}
while($card_fetch = mysql_fetch_array($card_qur))
{
++$total; //Increment
}
echo $total; // this will give you a count of total fetched records
Related
The contents of this question have been removed due to a DMCA Takedown request by Codility Limited.
Here is the Simplets PHP solution for the Above question from the Codility test.
<?php
$A = [2,2,1,2];
$B = [1,3,4,4];
$w = [3,5,2,4,1];
$N = 5;
// $A = [1];
// $B = [3];
// $A = [1,3];
// $B = [2,4];
function solution ($N, $A, $B){
if(count($A) != count($B) || !is_int($N) )
{
return false;
}
$V = [];
$vertextCount = [];
foreach($A as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
foreach($B as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
if($vertextCount < $N)
{
$vertextCount[$N] = 0;
}
$VC = $vertextCount;
$tn = $N;
$wightArr = [];
while(count($VC) > 0){
$maxKey = current(array_keys($VC, max($VC)));
$wightArr[$maxKey] = $tn;
unset($VC[$maxKey]);
$tn--;
}
$sum = 0;
foreach($A as $k=>$val){
$sum += $wightArr[$A[$k]] + $wightArr[$B[$k]];
}
return $sum;
}
echo $sum = solution($N, $A, $B);
NOTE:- Tested against the 3 given Examples in the test, Not sure about all the test cases.
i tried to store variables which are set in a while loop in a multi dimensional arrays. Afterwarts i want to print the array out.
what i did:
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
}
When i run this code i will not get anything.
What is the reason for this?
Thank you!
Here is code-
<?php
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
$i++;
}
You need to add the index of the array you are adding to or you are just writing over it.
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[$counter] = array($a,$b);
$counter++;
}
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
if ($a != $b)
echo $a.' is not '.$b;
else
echo $a.'='.$b;
$i++;
}
My code:
$a['page'] = 1;
function change($a) {
$a['page'] = 2;
}
My output:
$a['page'] = 1;
$a['page'] = 2;
Why am I get two keys 'page'?
I was expecting the function changed the value.
you could pass $a by reference instead and it would work as expected. It would be slower but not significantly so, given the function.
$a['page'] = 1;
function change(&$a) {
$a['page'] = 2;
}
change($a);
echo "<pre>";
print_r($a);
$a['page'] = 1;
function change($a) {
return $a['page'] = 2;
}
echo change($a);
I want all of the elements in the array to be added together, but this doesn't seem to be working.
<?php
function mimic_array_sum($array) {
foreach($array as $total) {
$total = $total + $total;
}
return $total;
}
$var = array(1,2,3,4,5);
$total = mimic_array_sum($var);
echo $total;
?>
$total = $total + $total --> well, there's your problem...
The $total variable gets overwritten on each loop through the array.
Assign a separate variable for each number in the array, like so:
function mimic_array_sum($array) {
$total = 0;
foreach($array as $number) {
$total = $total + $number;
}
return $total;
}
$var = array(1,2,3,4,5);
echo mimic_array_sum($var);
Although the point of this is not clear to me... You might as well use the php-function array_sum...
$var = array(1,2,3,4,5);
echo array_sum($var);
$var = array(1,2,3,4,5);
$total = array_reduce(
$var,
function($sum, $value) {
return $sum + $value;
}
);
though why not simply use array_sum()?
You can use array_sum — Calculate the sum of values in an array
$var = array(1,2,3,4,5);
$total = array_sum($var);
echo $total;
<?php
function mimic_array_sum($array) {
$total = 0;
foreach($array as $elem) {
$total += is_numeric($elem) ? $elem : 0;
}
return $total;
}
$var = array(1,2,3,4,5);
$total = mimic_array_sum($var);
echo $total;
?>
<?php
$a = array(1,2,3,4,5);
echo "sum is:".array_sum($a);
?>
See Manual
Please try following, you have to take separate variable to do so. Or else you can use array_sum()
function mimic_array_sum($array) {
$test = 0;
foreach($array as $total) {
$test = intval($test) + intval($total);
}
return $test;
}
$var = array(1,2,3,4,5);
$total = mimic_array_sum($var);
echo $total;
?>
The following code is displaying INF as the result. How can I fix it?
<?php
function fibonacci($n)
{
$a = 1;
$b = 1;
$result = 0;
for ($i = 0; $i < $n; $i=$i+1)
{
$sum = $a + $b;
$a = $b;
$b = $sum;
if ($a % 2 == 0)
{
$result = $result + $a;
}
}
echo "<br/>" . $result;
}
echo fibonacci(400000);
?>
The number is too big to display, and INF is a pretty good guess :) (fibonacci(1000) gives you a number with 210 digits).
100: 22 digits, 110: 24 digits, 120: 25 digits, 130: 27 digits
If you extrapolate that, you would end up with about (400000 / 10) * 2 = 80000 digits.
The following implements your logic using bcmath to prevent the INF error.
function fibonacci($n)
{
$a = '1'; $b = '1'; $result = '0';
for ($i = 0; $i < $n; $i++) {
$sum = bcadd($a,$b);
$a = $b;
$b = $sum;
if (bcmod($a,'2') == '0') {
$result = bcadd($result,$a);
}
}
echo "<br />".$result;
}
As your fibonacci function doesn't actually return any value, there's no point in echo fibonacci(400000)
EDIT
However, your logic is completely flawed. The following should give you the correct result for the problem you're trying to solve (again using bcmath):
function fibonacci($n)
{
$a = '0'; $b = '1'; $sum = '0';
$sum = '0';
do {
$fib = bcadd($a,$b);
$a = $b;
$b = $fib;
if (bccomp($fib,$n) == -1) {
if (bcmod($fib,'2') == '0') {
$sum = bcadd($sum,$fib);
}
}
++$i;
} while (bccomp($fib,$n) == -1);
return $sum;
}
echo fibonacci(4000000);
Rather than simply executing it to get the result, look to see how it works and what it's actually doing