I am working on a script where a database containing link Id's and names is queried, the result(s) placed into an array and then formatted to display as a list. I am getting some errors regarding the "foreach" loop. The error states that there is an invalid argument in the "foreach" loop. Could anybody help me out?
Here is the code:
(The problem loop appears in the "display_categories" function.)
// Returns an array of categories within the system
function get_categories()
{
// query database for a list of the categories/page links
$conn = db_connect();
$query = 'select catid, catname
from Categories';
$result = @mysql_query($query);
if (!$result)
return false;
$num_cats = @mysql_num_rows($result);
if ($num_cats == 0)
return false;
$result = db_result_to_array($result);
return $result;
}
// Function that converts a MySQL result identifier into an array of results
function db_result_to_array($result)
{
$res_array = array();
for ($count=0; $row = mysql_fetch_array($result); $count++)
$res_array[$count] = $row;
return $res_array;
}
// This function displays an array of categories as a list of links to those categories
function display_categories($cat_array)
{
if (is_array($cat_array))
{
return false;
}
echo '<ul>';
foreach ($cat_array as $row)
{
$url = 'show_link.php?linkid='.($row['catid']);
$title = $row['catname'];
echo '<li>';
do_html_url($url, $title);
echo'</li>';
}
echo '</ul>';
}