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 11 of 11
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    text link to a URL variable from a database

    Hello all! I am somewhat new to PHP and SQL, but I think I have a basic understanding of how the language works. I have read through a million different websites and forums, and have tried a million different ways but am just not getting anywhere!

    This is what I am trying to do... I have a text link that I want to click through to an outside URL from my website. This outside URL was already entered into an SQL database, and I just cant seem to get the text link to bring up the outside URL from the database. This is what I have so far...

    1.) My webpage that I am working on is www.mywebsite.com/articles.php
    2.) I want the text "Read more..." on this webpage to click through to an outside URL, like "http://www.google.com", which was already entered into an SQL database under the title of "article_link" in a new window
    3.) So I would like for "Read more..." to click through the outside URL that was entered into "article_link" in the database

    PHP Code:
    <?php
    echo <p><a href="\' . $article_link . '\" target="_blank">Read more...</a></p>;
    ?>
    Nothing I have tried has worked so far, nothing clicks through to the outside URL in the "article_link" entry from the database. Sorry if I am being redundant, I have been at this all day! Please help and thank you in advance!!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It's hard to see what may be wrong without seeing your code. The one line of code you did provide will throw a syntax error, but did you just throw it into your post as an example of what you are trying to do? Anyway, show us the code where you are selecting data from your database and then echoing the data to your browser.

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    can you paste in the rest of the script? like the part where you query the database? or is this the part thats missing?

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    <?php
    echo "<p><a href=\" ' . $article_link . '\" target=\"_blank\">Read more...</a></p>";
    ?>
    Misplaced \?

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply! Yes, I posted that code to show one of the several ways I had tried to make the database item called "article_link" become the actual click through URL where it says "Read more...".

    I found some code online, courtesy of Adam Khoury and his tutorials on You Tube!, and I want each new entry to generate a new row where one of the variables will click through to a URL entered into the database.

    Below is an example of what I do have successfully working on another one of my webpages. I want to duplicate exactly this, but just have the "Read more..." text link click through to the URL that was entered into the database instead of the id's page.


    PHP Code:
    <?php 
    // Run a select query to get my letest 6 items
    // Connect to the MySQL database  
    include "admin/storescripts/connect_to_mysql.php"
    $dynamicList "";
    $sql mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 50");
    $productCount mysql_num_rows($sql); // count the output amount
    if ($productCount 0) {
        while(
    $row mysql_fetch_array($sql)){ 
                 
    $id $row["id"];
                 
    $product_name $row["product_name"];
                 
    $price $row["price"];
                 
    $date_added strftime("%b %d, %Y"strtotime($row["date_added"]));
                 
    $dynamicList .= '<table width="349" height="170" border="0" cellspacing="0" cellpadding="6">
            <tr>
              <td width="175" valign="top" align="left"><a href="admin/product.php?id=' 
    $id '"><img src="admin/inventory_images/' $id '.jpg" alt="' $product_name '" width="146" border="0" style="border:#c28a37 2px solid; margin:0 5px 35px 20px;" /></a></td>
              <td width="150" valign="top" align="center"><p span class="align_center">Pet Name:</span><br />
                
                <p span class="align_center">' 
    $product_name ' </span>
                
                <p class="lineheight5">&nbsp;</p>
                <p span class="align_center">Breed:</span>
               
                  <br />
                <p span class="align_center">' 
    $price '</span>
               
                <p class="lineheight5">&nbsp;</p>
              <p span class="align_center">
                <a href="admin/product.php?id=' 
    $id '">Read More...!</a></span></td>
            </tr>
    </table>'
    ;
        }
    } else {
        
    $dynamicList "We have no pets submitted yet, won't you submit your pet's photo today?";
    }
    mysql_close();
    ?>
    So my question now is, how to get that text link to click through to the actual URL entered into the database, not to the ID page. Thats why I was trying to put the "article_link" variable in the <a href> tag. But no luck, I cant get it to work! Thank you!!

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    <?php 
    // Run a select query to get my letest 6 items
    // Connect to the MySQL database  
    include "mysqlconnectscript.php"
    $sql mysql_query("SELECT * FROM articles ORDER BY date_added DESC LIMIT 50");
       
    $row mysql_fetch_array($sql); 
                 
    $article_name $row["article_name"];
                 
                 
    $output '<table width="349" height="170" border="0" cellspacing="0" cellpadding="6">
            <tr>
                <p class="lineheight5">&nbsp;</p>
              <p span class="align_center">
                <a href="www.yoursite.com/articledir/' 
    $article_name '">Read More...!</a></span></td>
            </tr>
    </table>'
    ;
        
    mysql_close();
    ?>
    Tried something like that?

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by rafiki View Post
    PHP Code:
    <?php 
    // Run a select query to get my letest 6 items
    // Connect to the MySQL database  
    include "mysqlconnectscript.php"
    $sql mysql_query("SELECT * FROM articles ORDER BY date_added DESC LIMIT 50");
       
    $row mysql_fetch_array($sql); 
                 
    $article_name $row["article_name"];
                 
                 
    $output '<table width="349" height="170" border="0" cellspacing="0" cellpadding="6">
            <tr>
                <p class="lineheight5">&nbsp;</p>
              <p span class="align_center">
                <a href="www.yoursite.com/articledir/' 
    $article_name '">Read More...!</a></span></td>
            </tr>
    </table>'
    ;
        
    mysql_close();
    ?>
    Tried something like that?
    That doesn't use a link from his table though.

    Jewelsmac66, use the value of the link from your table, which you referred to in your first post as $article_link, and stick it in your HTML's <a> tag.

    Also, you need to always check your queries for errors. If the code you posted was from an online tutorial, then shame on that tutorial for not teaching you this very important and basic requirement.

    PHP Code:
    $query "SELECT * FROM articles ORDER BY date_added DESC LIMIT 50";
    $sql mysql_query("$query");
    if (!
    $sql) {
        die(
    "Query Error! Query: $query<br />Error: ".mysql_error());


  • #8
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by Fumigator View Post
    That doesn't use a link from his table though.

    Jewelsmac66, use the value of the link from your table, which you referred to in your first post as $article_link, and stick it in your HTML's <a> tag.

    Also, you need to always check your queries for errors. If the code you posted was from an online tutorial, then shame on that tutorial for not teaching you this very important and basic requirement.

    PHP Code:
    $query "SELECT * FROM articles ORDER BY date_added DESC LIMIT 50";
    $sql mysql_query("$query");
    if (!
    $sql) {
        die(
    "Query Error! Query: $query<br />Error: ".mysql_error());

    True.
    Always add error checking.
    Shame on me too for not enforcing

  • #9
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh I think I see what you are saying. And my apologies, I didn't copy/paste the code for the connection to the database and the error reporting. Below is what I did but now I am not getting anything on the webpage.

    the variables in the "articles" table are as follows: id, article_title, article_source, article_author, article_link, date_added

    Underneath the <head></head> in the body tag, i have this code to call on the database and display the information and link:
    <?php echo $dynamicList; ?>

    Nothing is displaying with the below code. What did I miss when I tried to implement all of the suggestions together? Thank you!!

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">


    <?php 
    // This file is www.developphp.com curriculum material
    // Written by Adam Khoury January 01, 2011
    // http://www.youtube.com/view_play_list?p=442E340A42191003
    // Script Error Reporting
    error_reporting(E_ALL);
    ini_set('display_errors''1');
    ?>
    <?php 
    // Run a select query to get my letest 6 items
    // Connect to the MySQL database  
    include "admin/storescripts/connect_to_mysql.php"
    $dynamicList "";
    $sql mysql_query("SELECT * FROM articles ORDER BY date_added DESC LIMIT 50");
    $articleCount mysql_num_rows($sql); // count the output amount
    if ($articleCount 0) {
        while(
    $row mysql_fetch_array($sql)){ 
        
    $id $row["id"];
        
    $article_title $row["article_title"];
        
    $article_source=$row["article_source"];
        
    $article_author=$row["article_author"];
        
    $article_link=$row["article_link"];
    $date_added=$row["date_added"];

    $output '<table width="350" height="150" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td height="108" align="left" valign="top">
              <p class="article_title">' 
    $article_title '</p>
              <p class="pets_story_homepage">' 
    $article_source ', ' $article_author '</p>
              
    <p><a href="<?php #
    echo "<a href=\"$article_link\">$article_title</a>";
    #
    ?>">Read more...</a></p></table>'
    ;
        
    mysql_close();
        }
    } else {
        
    $dynamicList "There aren't any articles yet, submit one today!";
    }
    ?>

    <?php 
    // Run a select query to get my letest 6 items
    // Connect to the MySQL database  
    include "admin/storescripts/connect_to_mysql.php"
    $dynamicList "";
    $sql mysql_query("SELECT * FROM articles ORDER BY date_added DESC LIMIT 50");
       
    $row mysql_fetch_array($sql); 
                 
    $article_title $row["article_title"];
    $article_source=$row["article_source"];
    $article_author=$row["article_author"];
    $article_link=$row["article_link"];
    $date_added=$row["date_added"];

                 
                 
    $output '<table width="350" height="150" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td height="108" align="left" valign="top">
              <p class="article_title">' 
    $article_title '</p>
              <p class="pets_story_homepage">' 
    $article_source ', ' $article_author '</p>
              
    <p>
                echo "<p><a href=\"$article_link\">Read More...</a></p>";
    </table>'
    ;
        
    mysql_close();
    ?>

  • #10
    New Coder
    Join Date
    Feb 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah ha! After a little more playing around with all your feedback and also going back and forth between the other code that is successfully working from another table in my database, it is finally working perfectly! I posted the working code below for everyone to see.

    Since I am just getting my feet wet with more advanced coding like this, I think the problem was that all of the variables weren't setup in the code first before I was actually calling on them in the output table.

    I can finally breathe now! Thank you for all your help!!!!

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    
    <?php 
    // This file is www.developphp.com curriculum material
    // Written by Adam Khoury January 01, 2011
    // http://www.youtube.com/view_play_list?p=442E340A42191003
    // Script Error Reporting
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    ?>
    <?php 
    // Run a select query to get my letest 6 items
    // Connect to the MySQL database  
    include "admin/storescripts/connect_to_mysql.php"; 
    $dynamicList = "";
    $sql = mysql_query("SELECT * FROM articles ORDER BY date_added DESC LIMIT 50");
    $articleCount = mysql_num_rows($sql); // count the output amount
    if ($articleCount > 0) {
    	while($row = mysql_fetch_array($sql)){ 
                 $id = $row["id"];
    			 $article_title = $row["article_title"];
    			 $article_source = $row["article_source"];
    			 $article_author = $row["article_author"];
    			 $article_link = $row["article_link"];
    			 $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
    			 $dynamicList .= '<table width="300" height="170" border="0" cellspacing="0" cellpadding="6">
            <tr>
              <td width="288" valign="top" align="center">
              <p>' . $article_title . '            </p>
              <p>' . $article_source . ' ' . $article_author . '</p>
              <p><a href="' . $article_link . '" target="_blank">Read More...!</a></p></td>
      </tr>
    </table>';
        }
    } else {
    	$dynamicList = "We have no pets submitted yet, won't you submit your pet's photo today?";
    }
    mysql_close();
    ?>

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You are still not checking for errors

    Oh well... I tried. You can lead a horse...


  •  

    Tags for this Thread

    Posting Permissions

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