News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Undefined offset error

Started by Jade Elizabeth, January 03, 2015, 05:56:24 AM

Previous topic - Next topic

Jade Elizabeth

So for some reason I keep getting an Undefined offset: 0 error for the lines under the comments regarding types and subtypes (it's upset with the child level part) of my file. Everything is working as it should I just get 44 errors a page load lol.

Can someone please help me understand why I'm getting this error? This is my first time working with multidimensional arrays that I've built myself from scratch so I would really love to fully understand what's going on :).


$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();

// Build the array of cats and types
while ($row = $smcFunc['db_fetch_assoc']($result_work_cats_and_types))
{
// Make sure we haven't seen the cat yet then grab it
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() ,
);
}

// Make sure to grab the types
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' => $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() ,
);
}

// Grab the syubtypes
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' => $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>',
);
}
}
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Arantor

Because you're doing things that make no sense. And I seem to recall mentioning this before.

child_level is something at board level yet you're trying to use it at category level.

$work_categories is an array of categories. You're trying to use the level of indentation (as in, number of levels of indentation, A > B > C would see C having child_level of 2 because it's two levels in) as a category id and naturally this doesn't work.

I don't understand what this code is supposed to achieve or why you couldn't use the code in SMF that does this for you.

Jade Elizabeth

By board you mean type in this case.

I couldn't adapt the code to work for me how I wanted. It works exactly how I wanted it to this way. Also I changed the row child to go into the types and it didn't help.
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Arantor

Aside from the fact that it clearly doesn't work as intended and only works by sheer luck, it works how you wanted it?

Also, maybe, just maybe, next time how about stating what you want to achieve rather than be fixated on how you think you should achieve it. It really does help to know what you're trying to build rather than what tool you think you need to build it. As in, multiple different options were provided to you for this code and you never actually said why any of them were not appropriate for the job at hand.

Jade Elizabeth

I wanted to build an array that had categories with their types and subtypes from the database and came out like this:
http://pastebin.com/raw.php?i=jjjUnK2W

And I did that with this code:
http://pastebin.com/Mxqx0rxt

SQL:
http://pastebin.com/raw.php?i=LhMKZi2Q


The goal was to build the array and have it work. It is used to build a menu....but I am going to change it to take into account the type id in the URL so that it will only show subtypes (and sub subtypes) for that type and on the front page show only types without subs. Right now it shows all subtypes etc.
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Advertisement: