Jump to content

php arrays as sql fetch array


funbinod

Recommended Posts

how can I print php arrays as we print sql arrays using while loop.?

 

print_r ( ) returns array as

Array ([1] => A [2] => B [3] => C [4] => D [5] => E)

 

But I wish to print as

 

A

B

C

D

E

 

That we get using sql query like...

 

while {$row = mysqli_fetch_array($query) {

...................

..................

}

 

 

Link to comment
Share on other sites

PHP has a few iterating functions. Such as each(), current(), and next().

 

Here's an example

while($item = each($arr)) {    echo $item . '<br>';}

But you can print elements in an array without them, the more common way:

foreach($arr as $item) {    echo $item . '<br>';}
  • Like 1
Link to comment
Share on other sites

solved! thank u.

 

now will u take me more deeper?

 

I wonder if there is some method of calculation between arrays!! :(

 

meaning, I want to calculate value(s) of this row with the value of previous row. below is the example..

while ($row = ....................) {    $ttl = ($row['a']+$row['b']-$row['c']);    echo $row['a'] . ' | ' . $row ['b'] . ' | ' . $row['c'] . ' | ' . $ttl . '<br />';}

but after each row I wish the value of $ttl on each rows be calculated in respect with the previous row..

lets look with practical example...

lets assume some data derived from above query...

 

 

$row['a'] | $row['b'] | $row['c'] | $ttl | THIS

------------|-------------|------------|------|------

9 | 2 | 3 | 8 | 8

------------|-------------|------------|-------|------

7 | 1 | 5 | 3 | 11

------------|-------------|------------|-------|------

8 | 6 | 4 | 10 | 21

 

 

what I wish is the calculation of THIS column...

 

on first row the value of $ttl is put on THIS column. on second row value of $ttl is added with value of THIS column of first row and put on THIS column of second row and so on third row...

 

this is something like prev() function on an array...

 

possible????? :(

Edited by funbinod
Link to comment
Share on other sites

Here is the list of array functions:http://php.net/manual/en/ref.array.phpThey are general-purpose functions for the most part. If you want to do something specific like what you're asking then just use a loop to go through and print the array, and have a set of variables to keep track of whatever you want to keep track of.

Link to comment
Share on other sites

understood nothing on the link that can guide me through what I wish..

 

I understand of "having a set of variables to keep track of whatever I want" as u suggested. but my problem is for calculation using the value derived from the calculation between the values of previous row.... :(

Link to comment
Share on other sites

How is that a problem? The far right column in your example starts at 0, and every time you process a record you add that record's TTL value to the variable. It's just a running total of one of the columns in the result, it's just addition.

Link to comment
Share on other sites

ok! let me guess and try something.

assuming $bal is the rightmost column

$bal = $prebal // this assumes the first ever row starts with some value calculated previously$bal += $svalue - $pvalue // preceding columnsecho $bal;

is this the method you suggested me?

 

but this calculated value for each row only. like, $prebal + $svalue - $pvalue. it didn't continued adding to further rows.

 

:(

Edited by funbinod
Link to comment
Share on other sites

ok! I got it now and solved it.

 

I did the mistake. I had placed the initial value of $bal inside the loop. now I placed the initial value of $bal (i.e. $bal = $prebal) before starting a loop.

$bal = $prebal;foreach ($period as $month) {    ......................;    ......................;        $bal += ($svalue - $pvalue);    echo $bal;}

this solved the problem. thank u both for your company....

Edited by funbinod
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...