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
https://forums.phpfreaks.com/topic/257214-changing-loop-data/
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
https://forums.phpfreaks.com/topic/257214-changing-loop-data/#findComment-1318480
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
https://forums.phpfreaks.com/topic/257214-changing-loop-data/#findComment-1318484
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
https://forums.phpfreaks.com/topic/257214-changing-loop-data/#findComment-1318515
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.