Jump to content

Changing loop data


graham23s

Recommended Posts

Hi Guys,

 

Theres probably a really simple way to do this, but i haven't touched php in a wee while lol this code:

 

                            foreach ($menu as $elem) {
                                $selected1 = '';
                                $selected2 = '';
                                if ($elem['system_name'] == $this->system_name) {
                                    $selected1 = '<b>';
                                    $selected2 = '</b>';
                                }
                                if ($elem['system_name'] != 'home-content') {
                                    $elem['system_name'] = $elem['system_name'] . '.html';
                                } else {
                                    $elem['system_name'] = '';
                                }

                                printf('<li class="%s">%s<a href="%s/%s">%s</a>%s</li>', $elem['li_class'], $selected1, ROOT, $elem['system_name'], $elem['title'], $selected2);
                            }
                            ?>

 

Prints out the navigation on one of my sites, when the code loops through the menu i want to change one of the href targets for example:

 

if (loop through menu)
{
if (page returned for example users page)

  {
   <a href="[b]CHANGE THIS TO SOMETHING.html[/b]">
  }
}

 

So i only need to change one of the hrefs not all of them :)

 

any help would be appreciated

 

thanks guys

 

Graham

Link to comment
Share on other sites

It's not clear what to give you. You obviously know you need to put an if statement in. But, what the condition should be is up to you. You haven't provided any details about what values determine the change or what that change would be. I'm guessing that the variable to check may be: $elem['title']

 

So,

if($elem['title'] == 'userpage')
{
    //Do normal process
}
else
{
    //Do something else
}

Link to comment
Share on other sites

Hi P,

 

Sorry i should have posted the entire menu block:

 

                            <?
                            $menu = array(
                                array(
                                    'system_name' => 'home-content',
                                    'title' => 'Home',
                                    'li_class' => 'menu_home'
                                ),
                                array(
                                    'system_name' => 'apply',
                                    'title' => 'Apply',
                                    'li_class' => 'menu_apply'
                                ),
                                array(
                                    'system_name' => 'how-it-works',
                                    'title' => 'How it Works',
                                    'li_class' => 'menu_how_it_works'
                                ),
                                array(
                                    'system_name' => 'testimonials',
                                    'title' => 'Testimonials',
                                    'li_class' => 'menu_testimon'
                                ),
                                array(
                                    'system_name' => 'faq',
                                    'title' => 'FAQ\'s',
                                    'li_class' => 'menu_faq'
                                ),
                                array(
                                    'system_name' => 'privacy-policy',
                                    'title' => 'Privacy Policy',
                                    'li_class' => 'menu_privacy'
                                ),
                                array(
                                    'system_name' => 'contact-us',
                                    'title' => 'Contact Us',
                                    'li_class' => 'menu_contact'
                                ),
                            );
                            foreach ($menu as $elem) {
                                $selected1 = '';
                                $selected2 = '';
                                if ($elem['system_name'] == $this->system_name) {
                                    $selected1 = '<b>';
                                    $selected2 = '</b>';
                                }
                                if ($elem['system_name'] != 'home-content') {
                                    $elem['system_name'] = $elem['system_name'] . '.html';
                                } else {
                                    $elem['system_name'] = '';
                                }

                                printf('<li class="%s">%s<a href="%s\%s">%s</a>%s</li>', $elem['li_class'], $selected1, ROOT, $elem['system_name'], $elem['title'], $selected2);
                            }
                            ?>

 

This prints the href data out like apply.html, contact-us.html, privacy-policy.html what i'm ideally trying to do is once it brings back the apply.html page out to change it to www.anothersite.com rather than apply.html i have a mental block lol

 

cheers mate

 

Graham

Link to comment
Share on other sites

OK, that's better. In addition to your particular problem I would not use bold tags. Instead, you should set an appropriate class, add another class or set the style of the element.

 

I also think it's a good idea to separate your logic from the presentation. So, I would create the content for the links at the top of my script or a separate file entirely. Then echo the links into the body of my output document.

 

Anyway, give this a go

$nav_links = '';
foreach ($menu as $elem)
{
    $style = ($elem['system_name'] == $this->system_name) ? ' style="font-weight:bold;"' : '';

    if ($elem['system_name'] == 'home-content')
    {
        $url = ROOT . '/';
    }
    elseif($elem['system_name'] == 'apply')
    {
        $url = 'http://www.anothersite.com';
    }
    else
    {
        $url = ROOT . '/' . $elem['system_name'] . '.html';
    }

    $nav_links .= sprintf('<li class="%s"><a href="%s"%s>%s</a></li>', $elem['li_class'], $url, $style, $elem['title']);
}

//Echo this in the output document
echo $nav_links;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.