Re: Array_walk

From: Date: Mon, 26 Jun 2000 20:58:46 +0000
Subject: Re: Array_walk
Groups: php.general 
Request: Send a blank email to php-general+get-3198@lists.php.net to get a copy of this message
>Hey All > >I have an array of id numbers on which I use the array_walk to search a >database of the for the relevant price and description of the id. What I >want to do is return the total price for all the id numbers in the array. >Anyone know how to do that or maybe a better way. The code is below.. > >function bookarraycheck($item) > { > >global $conn_id; > >$result = ifx_query("select title, price from books where bookid = >'$item'",$conn_id); >$r = ifx_fetch_row($result); >ifx_free_result ($result); > >echo "<TR><TD WIDTH=\"50%\" VALIGN=\"TOP\">".$r["title"]."</TD>"; >echo "<TD WIDTH=\"50%\" VALIGN=\"TOP\">£ ".$r["price"]."</TD></TR>"; >$totalcosts += $r["price"]; > >return $totalcosts; > >} To make as little change as possible to your code, I would: 1) insert '$totalcosts=0;' at the stop of your script 2) insert '$global $totalcosts;' at the top of bookarraycheck() 3) change 'return $totalcosts;' to 'return' in bookarraycheck() There is no way for array_walk() to do anything with the return value of bookarraycheck(). Alternatively, you could use a for loop: $totalcosts=0.0; for ($i=0;i<count($book);$i++) { $item=$book[$i]; your code to get the row from the books table your code to generate HTML $totalcosts+=$r["price"]; } I don't know if either of these methods is the Best Way(tm). HTH, Carroll Barrett

« previous php.general (#3198) next »