Jump to content

[SOLVED] using a hyperlink within echo


mediasix

Recommended Posts

i have written a function that grabs some data from my 'mysql' database and outputs it to a page, the problem i am having is hyperlinking the outputted data, find my code below:

 

<?php

    mysql_connect("xxxxxx","xxxxxx","xxxxxx");

  mysql_select_db("xxxxxx");

  $query ="SELECT title, content,";

  $query.=" DATE_FORMAT(date, '%D %M, %Y') AS date";

  $query.=" FROM weblog ORDER BY date DESC LIMIT 2";

  $result=mysql_query($query);

  while (list($title,$content,$date) =

    mysql_fetch_row($result)) {

echo "<a href"/weblog/weblogentry.php?=<?php echo "$title"?>"> $title </br>($date)</a>";

echo "<h5>$content</h5>";

  }

?>

 

the error i am getting is this:

 

Parse error: syntax error, unexpected '=' in /othercontent2.php on line 10

 

if i take out the <a href" etc etc, the data displays fine, as soon as i try to hyperlink it breaks!

please help!!

 

thanks mediasix

 

Link to comment
Share on other sites

try this

 

<?php
    mysql_connect("xxxxxx","xxxxxx","xxxxxx");
   mysql_select_db("xxxxxx");
   $query ="SELECT title, content,";
   $query.=" DATE_FORMAT(date, '%D %M, %Y') AS date";
   $query.=" FROM weblog ORDER BY date DESC LIMIT 2";
   $result=mysql_query($query);
   while (list($title,$content,$date) = 
    mysql_fetch_row($result)) {
      echo '<a href="/weblog/weblogentry.php?='.$title.'"> $title </br>($date)[/url]';
      echo "<h5>$content</h5>";
   }
?>

 

Also your not defining a variable here weblogentry.php?= should it not be weblogentry.php?title=

 

Regards

Liam

Link to comment
Share on other sites

To explain what the actual problem is, you can't use double quotes inside of double quotes like that.  So you have to either have single quotes inside of double quotes or double quotes inside of single quotes.  If you don't have any variables inside the quotes, then either is fine, but if you do have variables, you have to use them correctly.

 

i have written a function that grabs some data from my 'mysql' database and outputs it to a page, the problem i am having is hyperlinking the outputted data, find my code below:

 

<?php

    mysql_connect("xxxxxx","xxxxxx","xxxxxx");

   mysql_select_db("xxxxxx");

   $query ="SELECT title, content,";

   $query.=" DATE_FORMAT(date, '%D %M, %Y') AS date";

   $query.=" FROM weblog ORDER BY date DESC LIMIT 2";

   $result=mysql_query($query);

   while (list($title,$content,$date) =

    mysql_fetch_row($result)) {

echo "<a href"/weblog/weblogentry.php?=<?php echo "$title"?>"> $title </br>($date)</a>";

echo "<h5>$content</h5>";

   }

?>

 

the error i am getting is this:

 

Parse error: syntax error, unexpected '=' in /othercontent2.php on line 10

 

if i take out the <a href" etc etc, the data displays fine, as soon as i try to hyperlink it breaks!

please help!!

 

thanks mediasix

 

Link to comment
Share on other sites

echo "<a href"/weblog/weblogentry.php?=<?php echo "$title"?>"> $title </br>($date)[/url]";

 

What exactly are you trying to output here?

 

First off, you have <? ?> inside existing <? ?> tags.  That isn't necessary. 

 

if you are trying to echo this?

 

<a href="/weblog/weblogentry.php?=$title"> $title </br>($date)</a>"

 

This construct inside your parent <? ?> tags would be:

 

echo "<a href=\"/weblog/weblogentry.php?=".$title."\"> ".$title." </br>(".$date.")</a>";

 

notice that the string is broken up into literal strings (between " ") and variable strings ($title) with periods (.).  Also notice that the double qoutes in the hyperlink are escaped with a \ to make php treat them as a text character.  Also, for the hyperlink to work, you do need to define the variable name in the URL.  weblogentry.php?=$title is malformed.  weblogentry.php won't know what variable to assign $title and you will have no way of pulling it into weblogentry.  If you are trying to pass $title to var title then the full line would be:

 

echo "<a href=\"/weblog/weblogentry.php?title=".$title."\"> ".$title." </br>(".$date.")</a>";

 

Link to comment
Share on other sites

OK, with a liitle help from all of your comments and a bit of jiggery pokery i have fixed it, code as follows:

 

<?php

    mysql_connect("xxxxxx","xxxxxx","xxxxxx");

  mysql_select_db("xxxxxx");

  $query ="SELECT title, content,";

  $query.=" DATE_FORMAT(date, '%D %M, %Y') AS date";

  $query.=" FROM weblog ORDER BY date DESC LIMIT 2";

  $result=mysql_query($query);

  while (list($title,$content,$date) =

    mysql_fetch_row($result)) {

echo '<a href="/weblog/weblogentry.php?id='.$title.'"> '.$title.' </br>('.$date.')</a>';

echo "<h5>$content</h5>";

  }

?>

 

the effect that i wanted to achieve can be found on my page www.mediasix.co.ukand look on the right hand bar, the following file is a php include.

 

thanks for your help

mediasix :)[tt][/tt]

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.