Page 1 of 1

return a sequence of records from a function

Posted: Tue Mar 11, 2003 8:29 pm
by theclay7
if I want to write a function with a given Main_cat_id parameter and it will return its sub_cat_id, total num of topic, moderator...how can I return a list of records in the function....i wrote the function and it only gave the last record:

function chooseCAT($catID){
$db = getDBPConn();
$sql = "select cat_id, parent_cat_id, cat_name, priority, tot_topics, moderator, banner_code, rec_cr_date, last_upd_date
from game_forum_category where parent_cat_id=$catID order by priority";

$result = $db->query($sql);
if (DB::isError($result = $db->query($sql))){
die (DB::errorMessage($result));
}
if ($result->numRows() > 0){
while ($row = $result->fetchRow()) {
$cat_id = $row[0];
$parent_cat_id = $row[1];
$cat_name = $row[2];
$priority = $row[3];
$tot_topics = $row[4];
$moderator = $row[5];
$banner_code = $row[6];
$rec_cr_date = $row[7];
$last_upd_date = $row[8];
return array($cat_id, $parent_cat_id, $cat_name, $priority, $tot_topics, $moderator, $banner_code, $rec_cr_date, last_upd_date);
}
} else {
return false;
exit;
}
$result->free();
$db->disconnect();
}

Posted: Wed Mar 12, 2003 12:08 am
by patrikG
I use associative arrays for that purpose. Check if this works for you:

Code: Select all

<?php
function chooseCAT($catID){
$db = getDBPConn();
$sql = "select cat_id, parent_cat_id, cat_name,priority,tot_topics,moderator,banner_code,rec_cr_date,last_upd_date
from game_forum_category where parent_cat_id=$catID order by priority";

$result = $db->query($sql);
if (DB::isError($result = $db->query($sql))){
die (DB::errorMessage($result));
}
if ($result->numRows() > 0){
while ($row = $result->fetchRow()) {
$myArray['cat_id'][] = $row[0];
$myArray['parent_cat_id'][]= $row[1];
$myArray['cat_name'][] = $row[2];
$myArray['priority'][] = $row[3];
$myArray['tot_topics'][] = $row[4];
$myArray['moderator'][] = $row[5];
$myArray['banner_code'][] = $row[6];
$myArray['rec_cr_date'][] = $row[7];
$myArray['last_upd_date'][] = $row[8];
return $myArray;
}
} else {
return false;
exit;
} 
?>
The [] after each $myArray[abcdefg][] autoincrement the key of the array.