Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Results in a link question

    Hello...I'm new to programming with PHP, but as I'm going through tutorials and reading books I've come across a question I have not been able to figure out.

    I have a table with 2 fields....one field is just text and the other is for a URL.

    I want to have a link created that uses the url for the link and the text field for the text.

    Here is the code I have so far, but I cannot get it to work:

    Code:
    <html>
    <head>
    <title>Test Page</title>
    </head>
    <body>
    <?php
    mysql_connect('hostname','username','password');
    mysql_select_db('db_name') or die (mysql_error());
    $result = mysql_query("SELECT * from patterns");
    $row = mysql_fetch_array($result);
    echo <a href="$row[lnktxt]">$row[dsptxt]</a>;
    }
    ?>
    
    </body>
    </html>
    Any help would be appreciated

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,269
    Thanks
    10
    Thanked 277 Times in 276 Posts
    Looks like just the echo is wrong. It should be:
    PHP Code:
    echo "<a href=".$row['lnktxt'].">".$row['dsptxt']."</a>"
    There are lots of ways to deal with the various quotes. Keep in mind just output text (like <a href) needs quotes around it (i.e. to make it a string).

    Dave

  • #3
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Dave,

    Thanks for the reply. When I updated my code and try to view it, all it shows me in the web browser is this:

    "$row['dsptxt'].""; } ?>

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,269
    Thanks
    10
    Thanked 277 Times in 276 Posts
    Could you copy in the code you're using again, including a few lines before and after this particular one?

    Thanks
    Dave

  • #5
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    mysql_connect('hostname','username','password');
    mysql_select_db('db_name') or die (mysql_error());
    $result = mysql_query("SELECT * from patterns");
    $row = mysql_fetch_array($result);
    echo "<a href=".$row['lnktxt'].">".$row['dsptxt']."</a>";  
    
    }
    ?>

  • #6
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,269
    Thanks
    10
    Thanked 277 Times in 276 Posts
    I forgot the quotes around the URL, try this:
    PHP Code:
    echo "<a href=\"".$row['lnktxt']."\">".$row['dsptxt']."</a>"

  • Users who have thanked tracknut for this post:

    mharrison (12-12-2012)

  • #7
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Same thing....when I replace the exact line from the code I showed you with what you gave me, the webpage only displays:

    ".$row['dsptxt'].""; } ?>

  • #8
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Actually, I think I got it figured out. I renamed the page to .php instead of .html and removed the } before the ?> and it worked. Just have to add a loop into it so it displays all of the table records. Thanks a bunch for your help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •