I have an invalid foreach on my multidimentional array...please help! :)

Started by Jade Elizabeth, December 22, 2014, 08:43:00 AM

Previous topic - Next topic

Jade Elizabeth

I'm trying to work with my multidimentional array to create a menu but I am stumped on how to make this work. I would really appreciate some pointers because everything I've looked at online is either stupid and doesn't apply or doesn't work when I try it.

All I want in the end is category link printed, then the type links printed, and then when I work that out I will print the subtypes links.


WorkIndex.php
// Retrieve the categories and boards.
require_once($sourcedir . '/Subs-WorkIndex.php');
$workIndexOptions = array(
'base_level' => 0,
'parent_id' => 0,
'include_categories' => true,
'countChildStats' => true,
);
$context['work_categories'] = getCategoriesTypes($workIndexOptions);
}


Subs-WorkIndex.php
function getCategoriesTypes($workIndexOptions)
{
global $smcFunc, $scripturl, $user_info, $modSettings, $txt;
global $settings, $options, $context, $sourcedir;

$result_work_cats_and_types = $smcFunc['db_query']('workindex_fetch_cats_and_types' , '
SELECT
c.id_cat,
c.cat_order,
c.cat_name,
c.cat_desc,
t.id_type,
t.id_cat AS parent_cat,
t.id_parent,
t.child_level,
t.type_order,
t.type_name,
t.type_desc,
t.num_works,
t.num_comments,
t.unapproved_comments
FROM
{db_prefix}works_categories AS c,
{db_prefix}works_types AS t
WHERE
c.id_cat = t.id_cat
ORDER BY
c.cat_order, t.id_parent
');

// Start with an empty array.
$work_categories = array();

while ($row = $smcFunc['db_fetch_assoc']($result_work_cats_and_types))
{
if (!isset($work_categories[$row['id_cat']]))
{
$work_categories[$row['id_cat']] = array(
'id_cat' => $row['id_cat'],
'order' => $row['cat_order'],
'name' => $row['cat_name'],
'description' => $row['cat_desc'],
'href' => $scripturl . '#c' . $row['id_cat'],
'link' => '<a href="' . $scripturl . '#c' . $row['id_cat'] . '">' . $row['cat_name'] . '</a>',
'types' => array() ,
);
}


if (($work_categories[$row['child_level']]) == 0)
{
$work_categories[$row['id_cat']]['types'][$row['id_type']] = array(
'id_type' => $row['id_type'],
'order' => $row['type_order'],
'parent_cat' => $row['parent_cat'],
'parent' => $row['id_parent'],
'child_level' => $row['child_level'],
'name' => $row['type_name'],
'description' => $row['type_desc'],
'works' => $row['num_works'],
'comments' => $row['num_comments'],
'href' => $scripturl . '?type=' . $row['id_type'] . '.0',
'link' => '<a href="' . $scripturl . '?type=' . $row['id_type'] . '.0">' . $row['type_name'] . '</a>',
'types' => array() ,
);
}

if (($work_categories[$row['child_level']]) > 0)
{
$work_categories[$row['id_cat']]['types'][$row['id_parent']]['types'][$row['id_type']] = array(
'id_type' => $row['id_type'],
'order' => $row['type_order'],
'parent_cat' => $row['parent_cat'],
'parent' => $row['id_parent'],
'child_level' => $row['child_level'],
'name' => $row['type_name'],
'description' => $row['type_desc'],
'works' => $row['num_works'],
'comments' => $row['num_comments'],
'href' => $scripturl . '?type=' . $row['id_type'] . '.0',
'link' => '<a href="' . $scripturl . '?type=' . $row['id_type'] . '.0">' . $row['type_name'] . '</a>',
);
}
}
$smcFunc['db_free_result']($result_work_cats_and_types);

// Let's return something....
return $work_categories;


}


WorkIndex.template.php - Problem HERE
function template_main()
{
global $context, $txt, $scripturl;

echo '<pre>';
//print_r($context['work_categories']);
echo '</pre>';
works_menu();

}

function works_menu()
{
foreach ($context['work_categories'] as $id_cat => $category)
{
print_r($category['link']);

foreach ($category['types'] as $id_type => $type)
{
print_r($type['link']);
}
}

}


$context['work_categories'] prints this:
http://pastebin.com/H813Srki
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Arantor


Jade Elizabeth

Crap so I forgot to global it. Far out so much to remember XD.

I will give that a go later when I load it back up. Thank you!! :D
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Advertisement: