Page 1 of 1

create a menu

Posted: Thu Jun 12, 2008 7:46 am
by abhikerl
calculate average from rows in a database

hi, can anyone tell me how to calculate the average of data retrieves from a database. The situation is I have a table "number" which consist of several data like "2,5,4,8,6...." I want to calculate the average and display it on the browser. thanks in advance for your help.

http://www.islandinfo.mu

Hi,
I have a 2 tables: category, subCategory
Can anyone explained me how to create a menu like a tree menu except there won't be any collapsible feature.
for example

Food(category)
meat(sub)
fruits(sub)
vegetables(sub)

Furniture(category)
chair(sub)
tables(sub)
bed(sub)

as you see, I want to display the category and under the category, I want to display the sub. I am using a database for that so that I can put other items without going through the code.
I tried a lot but couldn't do it. Any help will be grateful. Thanks a lot

http://www.islandinfo.mu

Re: create a menu

Posted: Thu Jun 12, 2008 7:56 am
by superdezign
Two posts in one...? Okay... o_O
abhikerl wrote:hi, can anyone tell me how to calculate the average of data retrieves from a database. The situation is I have a table "number" which consist of several data like "2,5,4,8,6...." I want to calculate the average and display it on the browser.
Get each entry from the database, run a total of how many entries you retrieve and the total of the entries, and get your average.

Code: Select all

$totalAmount = 0;
for ($totalEntries = 0; $data = mysql_fetch_object($queryResult); $totalEntries++) {
  $totalAmount += $data->number;
}
 
if ($totalEntries > 0) {
  $average = $totalAmount / $totalEntries;
} else {
  $average = 0;
}
abhikerl wrote:I have a 2 tables: category, subCategory
Can anyone explained me how to create a menu like a tree menu except there won't be any collapsible feature.
Make sure the subCategory table has a field for the id of the parent category. Then, just output each parent category and output the children.

Code: Select all

$output = '<ul>';
foreach ($categories as $category) {
  $output .= '<li>' . $category->name . '<ul>';
  foreach ($subcategories as $subcategory) {
    if ($subcategory->parentCategoryId = $category->id) {
      $output .= '<li>' . $subcategory->name . '</li>';
    }
  }
  $output .= '</ul></li>';
}