How to get result in three columns? - php

This is my code....
<?php
$arr1 = array();
$arr2 = array();
$arr3 = array();
$intial_amount = 1000;
$sum = 0;
for($j = 3; $j<=24; $j = $j + 3)
{
if($j == 3)
{
$arr1[$j] = "";
$sum = $sum + 1000*3;
}
else
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr1[$j] = $sum;
}
}
for($j = 27; $j<=48; $j = $j + 3)
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr2[$j] = $sum;
}
for($j = 51; $j<=72; $j = $j + 3)
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr3[$j] = $sum;
}
$arr = array($arr1,$arr2,$arr3);
foreach($arr as $key => $val)
{
foreach($val as $k => $v)
{
echo $k." ".$v."<br>";
}
echo "<br>";
}
?>
i want the result in three columns..

I'm not following your code so well, as it's unclear what you're doing, but if you want to display a list in three columns, use CSS's columns:
<html>
<ul style="columns: 3; -webkit-columns: 3; -moz-columns: 3;">
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
</ul>
</html>

I think you want something like that
if you do use the following code:
$arr1 = array();
$arr2 = array();
$arr3 = array();
$intial_amount = 1000;
$sum = 0;
for($j = 3; $j<=24; $j = $j + 3)
{
if($j == 3)
{
$arr1[$j] = "";
$sum = $sum + 1000*3;
}
else
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr1[$j] = $sum;
}
}
for($j = 27; $j<=48; $j = $j + 3)
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr2[$j] = $sum;
}
for($j = 51; $j<=72; $j = $j + 3)
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr3[$j] = $sum;
}
$arr = array($arr1,$arr2,$arr3);
foreach($arr as $key => $val)
{
echo '<div style=" width:33%; float:left">';
foreach($val as $k => $v)
{
echo $k." ".$v."<br>";
}
echo '</div>';
}

Related

Convert array outputted from DCT to an image in PHP

Using the following code I can get the DCT of an image in PHP. Then I need to convert this back in to the compressed image. How can I achieve that?
<?php
$results = array();
$image1 = "baboon.jpg";
$ima = ImageCreateFromJPEG($image1);
$N1 = imagesx($ima);
$N2 = imagesy($ima);
$rows = array();
$row = array();
for ($j = 0; $j < $N2; $j++) {
for ($i = 0; $i < $N1; $i++)
$row[$i] = imagecolorat($ima, $i, $j);
$rows[$j] = dct1D($row);
}
for ($i = 0; $i < $N1; $i++) {
for ($j = 0; $j < $N2; $j++)
$col[$j] = $rows[$j][$i];
$results[$i] = dct1D($col);
}
print_r($results);
function dct1D($in) {
$results = array();
$N = count($in);
for ($k = 0; $k < $N; $k++) {
$sum = 0;
for ($n = 0; $n < $N; $n++) {
$sum += $in[$n] * cos($k * pi() * ($n + 0.5) / ($N));
}
$sum *= sqrt(2 / $N);
if ($k == 0) {
$sum *= 1 / sqrt(2);
}
$results[$k] = $sum;
}
return $results;
}
?>
Also I need to know how can I add some extra details like another message to this image too.. (image steganography). Please help. Thanks

Converting binary array to decimal string strange behaviour

I'm currently implement left shift using int[] arrays in php and need to get back the decimal after operation. So I have written the following snippet to attempt conversion of binary array to decimal.
function bin2dec($bin)
{
$length = count($bin) - 1;
$sum = 0;
//convert using doubling
for($i = 0; $i < $length; $i++)
{
//use string_add if doubling bigger than int32
if($i >= 16)
{
$double = $this->string_add("$sum", "$sum");
$cr = $bin[$i];
if($cr == 0)
{
$sum = $this->string_add($sum, $double);
}
else{
return $i;//WHAT's UP??!
$add = $this->string_add($double, "$cr");
$sum = $this->string_add($sum, $add);
}
}
else{
$sum += ($sum * 2) + $bin[$i];
}
}
return $sum;
}
Now the weird problem is in the loop where $cr != 0, $i returns an unbelievable value already not satisfying the loop condition but I can't figure out why this is happening. Here's the rest of the relevant code.
function string_add($a, $b)
{
$lena = strlen($a); $lenb = strlen($b);
if($lena == $lenb)
{
$len = $lena - 1;//any
}
else if($lena > $lenb)
{
$b = str_pad($b, $lena, "0", STR_PAD_LEFT);
$len = $lena - 1;
}
else if($lenb > $lena){
$a = str_pad($a, $lenb, "0", STR_PAD_RIGHT);
$len = $lenb - 1;
}
$result = "";
for ($i = $len, $carry = 0; $i >= 0 || $carry != 0; $i--)
{
$add1 = $i < 0 ? 0 : $a[$i];
$add2 = $i < 0 ? 0 : $b[$i];
$add = $add1 + $add2 + $carry;
if ($add > 9) {
$carry = 1;
$add -= 10;
}
else {
$carry = 0;
}
$result .= $add;
}
return strrev($result);
}
$arr = array_pad(array(1), 62, 0);
$dec = bin2dec($arr);
return $dec;//test
I have also implemented a working version on ideone for testing. Does anyone understand why this is happening?
Thanks.
Ok, so apparently the problem was adding more than needed and unnecessarily subtracting 1 from length in bin2dec. Here's the final working version:
<?php
class MyClass{
function bin2dec($bin)
{
$length = count($bin);
$sum = 0;
//convert using doubling
for($i = 0; $i < $length; $i++)
{
//use string_add if doubling bigger than int32
if($i >= 16)
{
$sum = $this->string_add("$sum", "$sum");
$cr = $bin[$i];
if($cr != 0){
$sum = $this->string_add($sum, "$cr");
}
}
else{
$sum += $sum + $bin[$i];
}
}
return $sum;
}
function string_add($a, $b)
{
$lena = strlen($a); $lenb = strlen($b);
if($lena == $lenb)
{
$len = $lena - 1;//any
}
else if($lena > $lenb)
{
$b = str_pad($b, $lena, "0", STR_PAD_LEFT);
$len = $lena - 1;
}
else if($lenb > $lena){
$a = str_pad($a, $lenb, "0", STR_PAD_RIGHT);
$len = $lenb - 1;
}
$result = "";
for ($i = $len, $carry = 0; $i >= 0 || $carry != 0; $i--)
{
$add1 = $i < 0 ? 0 : $a[$i];
$add2 = $i < 0 ? 0 : $b[$i];
$add = $add1 + $add2 + $carry;
if ($add > 9) {
$carry = 1;
$add -= 10;
}
else {
$carry = 0;
}
$result .= $add;
}
return strrev($result);
}
}
$man = new MyClass();
$arr = array_pad(array(1), 62, 0);
$dec = $man->bin2dec($arr);
echo $dec;
Using strings...
function bin2dec($bin)
{
$length = count($bin);
$sum = "";
for ( $i = 0; $i < $length; $i++)
{
$sum = $sum . ( $bin[$i} == 0 ? '0' : '1');
}
return $sum;
}
function string_add($a, $b)
{
// not efficient, but obvious
$maxlen = ( strlen($a) > strlen($b) ? strlen($a) : strlen($b);
// make them both same length as longest
$a = str_pad($a, $maxlen, "0", STR_PAD_LEFT);
$b = str_pad($b, $maxlen, "0", STR_PAD_LEFT);
$result = "";
$carry = 0;
// start from the right end
for ($i = $maxlen - 1; $i >= 0; $i--)
{
$val = $a[$i] + $b[$i] + $carry;
$result = ( $val % 10 ) . $result;
$carry = $val / 10;
}
// handle final carry if present
if ( $carry > 0 )
{
$result = $carry . $result;
}
return $result;
}
$arr = array_pad(array(1), 62, 0);
$dec = bin2dec($arr);
return $dec;//test

print number vertically and grouping it

I am trying to print number vertically and it must be in group
here is my code
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= 24; $j = $j + $rows) {
$count++;
if($count>$nums){
break;
}
echo "<div class='fleft'>$count</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
out of above
but i want output like for the first column
and next group number will start from where first group number end. in this case next group start from 25
please ask if any doubt
$nums = 105;
$rows = 8;
$colsize = 3;
$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
$modulo = 0;
$correction = 0;
$rest = $nums - $count;
if ($rest < $groupsize) {
$empty = $groupsize - $rest;
$correction = floor($empty / $colsize);
$modulo = $empty % $colsize;
}
for ($i = 1; $i <= $rows; $i++) {
$colind = 0;
for ($j = $i; $j <= $groupsize; $j = $j + $rows) {
$count++;
if ($count > $nums) {
break;
}
$val = $j + ($g * $groupsize);
$val -= $colind * $correction;
$modcor = $colind - ($colsize - $modulo);
if ( $modcor > 0 ) {
$val -= $modcor;
}
echo "<div class='fleft'>" . $val . "</div>";
$colind++;
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
This works:
Also, you can change number of digits, columns or size of column
for($group = 0; $group < 3; $group++){
for($row =1 ; $row <= 8; $row++){
for($col = 0; $col <= 2; $col++){
echo ($group*24)+ $row + 8 * $col; echo " ";
}
echo "\n";
}
}
This code will print the number in the requested format. You need to modify according to your need.
may be i am mad , made a simple alter .... try this
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0; //added a counter
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
$letCounter=0; //reset counter on each loop
for ($j = $i; $j <= 24; $j = $j + $rows)
{
$count++;
if($count>$nums)
{break;}
//made an alter in the below line , some math :)
echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
Thanks !
This May work
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{
if($c%$group== 1)
{
echo "Group Start";
$flag = false;
}
for ($i = 1; $i <= $rows; $i++) {
$count++;
echo "<div class='fleft'>$count</div>";
echo "<div class='clear'></div>";
}
echo "Line End";
if($c%$group == 2&& $flag)// Check here for your requirement
echo "Group End </br>";
$flag = true;
}

PHP combine rows

I am working on a PHP project in which I need to combine rows (the rule is: if the first two numbers matched then add the rest numbers)
I have this array :
array_b4_combine= [
[2,15,1,1,0],
[2,15,3,3,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0],
[3,24,1,1,0]];
the output should be :
combined= [
[2,15,5,5,0],
[2,21,2,2,0],
[2,24,9,9,0],
[3,15,8,8,0],
[3,24,1,1,0]];
This is my code :
$num1 = $array_b4_combine[0][0];
$num2 = $array_b4_combine[0][1];
$sum1 = 0;
$sum2 = 0 ;
$sum3 = 0 ;
$combined ;
for ( $i = 0 ; $i < count($array_b4_combine) ; $i++)
{
if ($num1 == $array_b4_combine[$i+1][0] && $num2 == $array_b4_combine[$i+1][1])
{
$sum1 = $sum1 + $array_b4_combine[$i][2];
$sum2 = $sum2 + $array_b4_combine[$i][3];
$sum3 = $sum3 + $array_b4_combine[$i][4];
}
else
{
$combined[] = array($num1 , $num2 , $sum1, $sum2, $sum3);
$day = $array_b4_combine[$i][0];
$time = $array_b4_combine[$i][1];
$sum1 = $array_b4_combine[$i][2];
$sum2 = $array_b4_combine[$i][3];
$sum3 = $array_b4_combine[$i][4];
}
}
the output for my code is this:
combined=
[[2,15,4,4,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0]];
Am I doing the reset clause in wrong order.. can someone figure out what is the problem here.
Thanks
I really cannot understand your code, try this one:
$combined = [];
foreach ($array_b4_combine as $i => $arrayRow) {
$k = $arrayRow[0].' '.$arrayRow[1];
if (isset($combined[$k]))
for ($i=2; $i<5; $i++)
$combined[$k][$i] += $arrayRow[$i];
else
$combined[$k] = $arrayRow;
}
$combined = array_values($combined);
Here is how i solved your problem . code can be optimized .
<?php
$array_b4_combine = [
[2,15,1,1,0],
[2,15,3,3,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0],
[3,24,1,1,0]];
$j =0;
for ( $i = 0 ; $i < count($array_b4_combine) -1 ; $i++)
{
if($i == 0)
{
$sum1 = $array_b4_combine[$i][2];
$sum2 = $array_b4_combine[$i][3];
$sum3 = $array_b4_combine[$i][4];
}
if(($array_b4_combine[$i][0] == $array_b4_combine[$i+1][0]) && ($array_b4_combine[$i][1] == $array_b4_combine[$i+1][1]) )
{
$sum1 = $sum1 + $array_b4_combine[$i+1][2];
$sum2 = $sum2 + $array_b4_combine[$i+1][3];
$sum3 = $sum3 + $array_b4_combine[$i+1][4];
$combined[$j][0] = $array_b4_combine[$i][0];
$combined[$j][1] = $array_b4_combine[$i][1];
$combined[$j][2] = $sum1;
$combined[$j][3] = $sum2;
$combined[$j][4] = $sum3;
}
else
{ $j++;
$combined[$j][0] = $array_b4_combine[$i+1][0];
$combined[$j][1] = $array_b4_combine[$i+1][1];
$combined[$j][2] = $array_b4_combine[$i+1][2];
$combined[$j][3] = $array_b4_combine[$i+1][3];
$combined[$j][4] = $array_b4_combine[$i+1][4];
$sum1 = $combined[$j][2];
$sum2 = $combined[$j][3];
$sum3 = $combined[$j][4];
}
}
echo "<pre>";
print_r($combined);
echo "</pre>";
?>

Sum the value of variable return by for loop in php

I have this code that is working perfectly. My question is how can I sum all the value of $manpowerCost and $otherCharges?
for ($i = 1; $i<=9; $i++) {
$m = '0'.$i;
$sumMc = array();
$revenue = revenue($project, $year.'-'.$m);
$manpowerCost = manpowerCost($project, $year.'-'.$m);
$otherCharges = otherCharges($project, $year.'-'.$m);
$netIncome = netIncome($revenue, $manpowerCost, $otherCharges);
if ($manpowerCost<=0 && $m <= $projected_date){
$manpowerCost = round(($projected_mc * $percentage_mc),2);
$projected_total_mc += $manpowerCost;
$pt = $pt + $manpowerCost;
} else {
$projected_mc = $manpowerCost;
$manpowerCost = $projected_mc;
}
if ($otherCharges<=0 && $m <= $projected_date){
$otherCharges = round(($projected_oc * $percentage_oc),2);
$projected_total_oc += $otherCharges;
$pt = $pt + $otherCharges;
} else {
$projected_oc = $otherCharges;
$otherCharges = $projected_oc;
}
echo '<td>RE:'.number_format($revenue, 2, '.', '').'<br />MC:'.number_format($manpowerCost, 2, '.', '').'<br />OC:'.number_format($otherCharges, 2, '.', '').'<br />NI:'.$netIncome.'</td>';
}
U can Try like this
$powercost_total = '0';
for ($i = 1; $i<=9; $i++) {
$manpowerCost = manpowerCost($project, $year.'-'.$m);
$powercost_total = $manpowerCost + 4powercost_total;
....
Declare $TotalManPowerCost and $TotalOtherCharges variables outside of the loop, and add $manpowercost and $otherCharges to them in each iteration of the loop

Categories