How to Change href to a page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tibby.hu
Forum Newbie
Posts: 3
Joined: Sat Dec 27, 2008 8:19 pm

How to Change href to a page?

Post by tibby.hu »

Hello!

This is my First post (Here)! Yeee... :D

I have this code running multiple times on my site, collecting data out from a search.
It lists out results in links.
What I would like is to have is to run only once, not the "foreach" but only once, and instead of listing the results as links, I would like to go on the first link that founds.

What I did is I've created a simple HTML forward to another page, but it's not too good becuse the results site is showing up.

Somebody please help me to modify this code.

Can't figure out how to change the "$result->href" to the actually to process the link.

Thank you!

Code: Select all

 
<?php
        foreach( $this->results as $result ) : ?>
            <fieldset>
                <div>
                <?php if ( $result->href ) :
                    if ($result->browsernav == 1 ) : ?>
                        <a href="<?php echo JRoute::_($result->href); ?>" target="_blank">
                    <?php else : ?>
                    <meta http-equiv="Refresh" content="0; URL=<?php echo JRoute::_($result->href); ?>">
                    <a href="<?php echo JRoute::_($result->href); ?>">
                    <?php endif;
                    echo $this->escape($result->title);
                    if ( $result->href ) : ?>
                        </a>
                    <?php endif;
                    if ( $result->section ) : ?>
                        <br />
                    <?php endif; ?>
                <?php endif; ?>
                </div>
            </fieldset>
        <?php endforeach; ?>
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: How to Change href to a page?

Post by cptnwinky »

It would be nice to see the array that your actually using. If you were to do something like this...

Code: Select all

 
echo '<pre>';
print_r($this->results);
echo '</pre>';
 
before your loop and copy the results from the browser to here. This way if what you want to do is already available. Other than that if you were to call $this->results['href'] before the loop you will see that it uses the very first index.
tibby.hu
Forum Newbie
Posts: 3
Joined: Sat Dec 27, 2008 8:19 pm

Re: How to Change href to a page?

Post by tibby.hu »

cptnwinky wrote:It would be nice to see the array that your actually using. If you were to do something like this...

Code: Select all

 
echo '<pre>';
print_r($this->results);
echo '</pre>';
 
before your loop and copy the results from the browser to here. This way if what you want to do is already available. Other than that if you were to call $this->results['href'] before the loop you will see that it uses the very first index.
The output is this:

Code: Select all

Array
(
    [0] => stdClass Object
        (
            [title] => FA5753795
            [metadesc] => 
            [metakey] => 
            [created] => Tuesday, 15 December 2008
            [text] => FA5753795 - 200 ...
            [section] => FAQs/General
            [slug] => 46:fa5753795
            [catslug] => 31:general
            [sectionid] => 3
            [browsernav] => 2
            [href] => index.php?option=com_content&view=article&id=46:fa5753795&catid=31:general&Itemid=46
            [count] => 1
        )
 
)
 
Regarding the $this->results['href']
You mean it should be like this?

Code: Select all

<?php if ( $result->href ) :
                     if ($result->browsernav == 1 ) : ?>
                         <a href="<?php echo JRoute::_($result->href); ?>" target="_blank">
                     <?php else : ?>
                     <?php echo JRoute::_($result->[color=#BF0000]['href'][/color]); ?>
                     <?php endif;
...
 
I guess No, because this did not worked :banghead:

Thanks!
tibby.hu
Forum Newbie
Posts: 3
Joined: Sat Dec 27, 2008 8:19 pm

Re: How to Change href to a page?

Post by tibby.hu »

I've added this line:

Code: Select all

<?php header("Location: http://localhost/$result->href"); ?>
But it's still not the perfect one.
I'm wondering how would that be done in your suggestion?
Thisway I will have to change this host on the server side, once it's been uploaded.

Thanks,
Tibby
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: How to Change href to a page?

Post by cptnwinky »

I did make one mistake though, the first index of the array is 0 so you would actually call it with $this->results[0]['href']. Note that I left the s at the end of results. $this->result was created by your foreach loop, which you are now bypassing.

Also, you may want to think about refactoring your code so that since you only need one result you only pull one result from the db.
Post Reply