Page 1 of 1

help! repeated key/value conflicts in my array!

Posted: Tue Aug 09, 2005 3:40 pm
by LizzyD
Hi eveyone!

I'm not quite sure how to describe my problem as I'm a complete newbie and am still trying to learn all the php terms. But if you have a quick try-out of the menu I'm trying to put together on the following page you'll see exactly what i mean:

http://www.gerrydanaher.com/index.php

I want users to be able to search by topic or by date. However, some of the topics/dates are inevitably going to cross over and it keeps messing things up!

The code i'm using for the Search by Date bit is below (the code for the Search by Topic bit is exactly the same only without the 1's after all the variables etc...

Code: Select all

<?php
				  
// menu title => menu target
$menu1 = array(

'2005'=>array(
'14 October'=>'content.php?yr=04&id=1014', 
'20 September'=>'content.php?yr=04&id=0920'),

'2004'=>array(
'19 August'=>'content.php?yr=04&id=0819', 
'18 August'=>'content.php?yr=04&id=0818'),

'2003'=>array(
'14 October'=>'content.php?yr=04&id=1014', 
'20 September'=>'content.php?yr=04&id=0920'),

'2002'=>array(
'19 August'=>'content.php?yr=04&id=0819', 
'18 August'=>'content.php?yr=04&id=0818'),

'2001'=>array(
'8 August'=>'content.php?yr=04&id=0808',
'31 July'=>'content.php?yr=04&id=0731'),

'2000'=>array(
'19 August'=>'content.php?yr=04&id=0819', 
'18 August'=>'content.php?yr=04&id=0818'),

'1995'=>array(
'8 August'=>'content.php?yr=04&id=0808',
'31 July'=>'content.php?yr=04&id=0731')

);


// alternative method to using cookies
function array_search_recursive1($needle1, $haystack1) {
    $pos1 = null;
    $keys1 = array_keys($haystack1);
    while(!$pos1 && (list($garbage1, $value1)=each($keys1))) {
        if(is_scalar($haystack1[$value1])) {
            if($haystack1[$value1] === $needle1)
                $pos1[] = $value1;
        } elseif(is_array($haystack1[$value1])) {
            if($pos = array_search_recursive1($needle1, $haystack1[$value1]))
                array_unshift($pos1, $value1);
        }
    }
    return $pos;
}

// recursive function to draw menu
function draw_menu1($menu1, $preserve1, &$id1) {
    if($id1 == 0)
        echo "<div id=\"$id1\">\r\n<ul>\r\n";
    else
        echo "<div id=\"$id1\" style=\"display:none;\">\r\n<ul>\r\n";
    $id1 += 1;

    foreach($menu1 as $key1=>$value1) {
        if(is_array($value1)) {
            if(@in_array($key1, $preserve1))
                $toggle1 = $id1;
            echo "<li class=\"top\"><a href=\"#\" onclick=\"toggle($id1);\">$key1</a></li>\r\n";
            draw_menu1($value1, $preserve1, $id1);
        }
        else {
            echo "<li>";
		    if(@in_array($key1, $preserve1))
                echo "<a href=\"$value1\">$key1</a>";
            else
                echo "<a href=\"$value1\">$key1</a>";
            echo "</li>\r\n";
        }
    }
    echo "</ul>\r\n</div>\r\n";
    if(isset($toggle1))
        echo "<script language=\"javascript\">toggle($toggle1);</script>\r\n";
}

$id1 = 0;
$base1 = basename($_SERVER['PHP_SELF']);
$self1 = isset($_SERVER['QUERY_STRING']) ? $base1.'?'.$_SERVER['QUERY_STRING'] : $base1;
$preserve1 = array_search_recursive1($self1, $menu1);
draw_menu1($menu1, $preserve1, $id1);

?>
I'd be soooo massively grateful if anyone could tell me what i'm doing wrong - or even just point me towards a tutorial that might help me. I do realise that i'm probably wasting all your time with a really stupid question, and i do want to learn myself what i should be doing, but i've just been searching for hours and i can't find a similar problem anywhere that would give me a hint, and think i could well just be searching under the wrong terms...

Would be grateful for any advice at all!

Yours hopefully,

Lizzyd

Posted: Tue Aug 09, 2005 3:52 pm
by feyd
prepend 'year' or something to the ID you are using for each pass. The issue at hand is your ID's are overlapping from the topics..

Posted: Tue Aug 09, 2005 4:04 pm
by LizzyD
thanks feyd!

but how exactly do you mean? I've already distinguised between $id and $id1. Which ID should i change? (sorry to be such a newbie!)

Lizzyd

Posted: Tue Aug 09, 2005 4:10 pm
by feyd
wherever you use $id1 add 'year' (minus the quotes) in front of it.

Posted: Tue Aug 09, 2005 4:28 pm
by LizzyD
Thanks again feyd, but that's giving me a parse error: unexpected T_STRING, expecting T_VARIABLE... I think I might need to look at my other bits of code that I'm using though below...

Code: Select all

<script type="text/javascript">
<!--
function toggle(id) {
  if(document.getElementById) {
    var el = document.getElementById(id);
    el.style.display = (el.style.display == 'none') ? 'block' : 'none';
  }
}
//-->
</script>

Code: Select all

<?php
if (!empty($_GET['yr']))  $yr = $_GET['yr'];
else $yr = $_POST['yr'];

if (!empty($_GET['id']))  $id = $_GET['id'];
else $id = $_POST['id'];

include ( $yr . "/" . $id . ".php"); 
?>
Think i might have an idea what to do - so don't worry for a bit - will post again later if i'm still stuck... Thanks again for helping me!!

LizzyD

Posted: Tue Aug 09, 2005 4:40 pm
by feyd
not the variable $yr.. I was talking about when you use $id1 in the strings.. like here:

Code: Select all

if($id1 == 0)
        echo "<div id=\"$id1\">\r\n<ul>\r\n";
    else
        echo "<div id=\"$id1\" style=\"display:none;\">\r\n<ul>\r\n";
    $id1 += 1;
should become

Code: Select all

if($id1 == 0)
        echo "<div id=\"year$id1\">\r\n<ul>\r\n";
    else
        echo "<div id=\"year$id1\" style=\"display:none;\">\r\n<ul>\r\n";
    $id1 += 1;
obviously, don't copy that code literally, as you'll get several "are"s in there...

Posted: Tue Aug 09, 2005 5:04 pm
by LizzyD
cheers again feyd - i see what you mean now. .. it's getting there i think but still not quite right.

i'm afraid i'm in london so i'm off to bed now - my brain's starting to hurt! but thanks very much for all your help. i'll def. have another look at it again tomorrow and see if i can get my brain into gear.

thanks very much again anyway and night night!

lizzyd

Posted: Fri Aug 12, 2005 4:00 am
by LizzyD
Hi again everyone,

I'm still stuck on this problem! does anyone know of any tutorials or anything that might give me a few pointers? :? it's driving me insane!!

Lizzyd

Posted: Fri Aug 12, 2005 5:49 am
by feyd
the basics are, you must have different ID's (they shouldn't be numeric either) for it to work.

Posted: Fri Aug 12, 2005 6:03 am
by LizzyD
cheers again feyd! off to the edinburgh festival for the weekend in a few mins but will have another go when i get back!

much appreciated!

lizzyd