0

This is the code I am working with:

<?php
    $rss = new DOMDocument();
    $rss->load('http://hugeriver.wordpress.com/feed/');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('encoded')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            );
        array_push($feed, $item);
    }
    $limit = count($feed);
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $description = $feed[$x]['desc'];
        $date = date('l F d, Y', strtotime($feed[$x]['date']));
        echo '<h2><a name="test">'.$title.'</a><span class="line"></span></h2>';
        echo '<small><em>Posted on '.$date.'</em></small></p>';
        echo '<p>'.$description.'</p>';
    }
?>

I am stuck specifically with this line. I am trying to make it so the title is both the name of the ancor and the link (so when clicked it scrolls to the top). This is what I tried that doesn't work. Can anyone please show me what is wrong with my syntax?

echo '<h2><a name="'.$title'" href="#'.$title'">'.$title.'</a><span class="line"></span></h2>';
1
  • Is the missing dot on your 2 code block a typo ? I will assume it is since you have the dot right on your main code block "'.$title.'" and "'.$title'" Commented Jul 15, 2013 at 14:29

6 Answers 6

3

You're currently creating a link that targets itself.

If you want the link to go to the top of the document when clicked, simply link to '#':

<a href="#" name="<?php echo $title ?>"><?php echo $title ?></a>

Also, name is deprecated on <a/> elements in HTML 5. Use id instead:

<a href="#" id="<?php echo $title ?>"><?php echo $title ?></a>
Sign up to request clarification or add additional context in comments.

2 Comments

I want the link to go to the title when clicked. So instead of ahref = # I would like ahref = $title please
You want the link to go to itself? <a href="#<?php echo $title ?>" id="<?php echo $title ?>"><?php echo $title ?></a>
1

Andre answer should solve your problem, if what you want to do is simply go to the top of the page. however if you want to go to a specific section that has id set to the value of $title then you can try this...

<a href="#<?php echo $title ?>" name="<?php echo $title ?>"><?php echo $title ?></a>

This way when the link is clicked it will jump to the exact element with ID equal to title(which may or may not be at the top). I believe this is what you want to achieve.

Comments

1
<a href="#" ID="<?php echo htmlentities($title, ENT_QUOTES); ?>">
    <?php echo htmlentities($title, ENT_NOQUOTES); ?></a>

Why is everyone forgetting htmlentities(), especially for attributes?

And why use $title as #target? When the $title is a variable with spaces and punctuation unfit for #target practice... Why not use an md5($title) since your generating the page dynamically? Like:

<a href="#" ID="<?php echo htmlentities(md5($title), ENT_QUOTES); ?>">
    <?php echo htmlentities($title, ENT_NOQUOTES); ?></a>

and later on linking to it like this:

<a href="#<?php echo htmlentities(md5($title), ENT_QUOTES); ?>">
    Go to <?php echo htmlentities($title, ENT_NOQUOTES); ?>!</a>

Comments

0

there are two missing dots.

try this:

echo '<h2><a name="'.$title.'" href="#'.$title.'">'.$title.'</a><span class="line"></span></h2>';

Comments

0

try this syntax

echo "<h2><a name=\"$title\" href=\"#$title\">$title</a><span class=\"line\"></span></h2>";

or

echo "<h2><a name='$title' href='#$title'>$title</a><span class='line'></span></h2>";

This is much cleaner and has less probability of missing a dot or closing/opening quote.

Comments

-1
echo '<h2><a name="'.$title.'" href="#'.$title.'">'.$title.'</a><span class="line"></span></h2>';

You are missing dots after $title

Also, your links link to themself. You need to define a seperate anchor and then link to it.

1 Comment

echo with . means pointless concatenation. echo accepts ,. So don't echo 'why'.'concatenate'.'this'; but do echo 'when', 'echo', 'does', 'it', 'for', 'you';.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.