SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation MySQL database for web links !!!!

    I want to create a database with mySQL that has links and a description of the link aswell, but i can't seem to figure out how do i make the link from the database an active link using PHP, i'm using PHP/mySQL - obviously

    Help is appriciated

    Thanks!

    -AlBERT

  2. #2
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone??

  3. #3
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Well, you would need to create a table with four fields:

    Code:
    CREATE TABLE links (
    link_id int(5) not null auto_increment,
    link_title varchar(100) not null,
    link_loc varchar(100) not null,
    link_desc text not null,
    primary key(link_id)
    )
    You would then insert your links with their description and using PHP would pull them out and display them.

  4. #4
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I already have that done, i have already create a table with the fields, id, description, url, and alt. But i want to be able to get all the links in the database from the first till the last and have them ordered into an HTML table on my site, dynamicilly, but i want the links to be active meaning, i want them to work, which is what i can't do!!!!

  5. #5
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simply wrap the url column in the HTML link tag (<a href="url" title="alt">Description</a>

  6. #6
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <a href="url" title="alt">Description</a>

    so would this be something like this: <p>click here: <a href="<?php echo $row_links['id']; ?>">HERE</a> </p>???

  7. #7
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the initial 'click here:' text is not required
    the 'HERE' text should be replaced with the contents of the description field.
    the $row_links['id'] should probably be $row_links['ur'] assuming the 'utl' field contains the URL

  8. #8
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doesn't seem to work, would be able to upload a working example or give me a link to a working example????

  9. #9
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by user_A
    doesn't seem to work, would be able to upload a working example or give me a link to a working example????
    Forget the database for a moment. Take it one step at a time. Can you get it to work with just variables?

    eg:

    PHP Code:
    $url 'http://www.example.com' ;
    $desc 'This is an example description' ;
    $alt 'This is the alt string' ;

    echo 
    "<a href=\"$url\" alt=\"$alt\">$desc</a>" 
    Once you have that working, you can work out how to get info for a single record from the database into a single link, then try several records for several links.

    Mike

  10. #10
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok that works fine

  11. #11
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good. Are you okay getting the data out of the database?

    Mike

  12. #12
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this code

    PHP Code:
     // I assume that you have connected to the MySQL server
     // and have selected the correct database
     
     // select the columns you require from the database
     
    $sql 'SELECT'
          
    ' url, description, alt'
          
    ' FROM '
          
    ' links'            // this is the name of your table
          
    ;
     
     
    // run the query
     
    $result mysql_query($sql) or die('Failed to execute ' $sql ' due to ' mysql_error());
     
     
     
    $start_link '<a href="';
     
    $end_link '</a>';
     
    // read the data from the database
     
    while ($row mysql_fetch_***0c($result))
     {
       
    // build the link tag
       
    $link $start_link                   // start of link tag
             
    $row['url']                   // URL
             
    '" title="'                   // title attribute
             
    $row['alt']                   // title data from the database
             
    '">'                          // complete <a ...> tag
             
    $row['description']           // description from database
             
    $end_link;                    // close the anchor tag
     
       // display the link tag
       
    echo $link;
     } 

  13. #13
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Error: Parse error: parse error, unexpected '{' on line 35


    <?php require_once('intranet_db.php'); ?>

    <?php
    mysql_select_db($database_intranet_db, $intranet_db);
    $query_weblinks = "SELECT * FROM media";
    $weblinks = mysql_query($query_weblinks, $intranet_db) or die(mysql_error());
    $row_weblinks = mysql_fetch_assoc($weblinks);
    $totalRows_weblinks = mysql_num_rows($weblinks);
    ?>

    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body bgcolor="#CCCCCC">
    <?php // I assume that you have connected to the MySQL server
    // and have selected the correct database

    // select the columns you require from the database
    $sql = 'SELECT'
    . ' url, description, alt'
    . ' FROM '
    . ' links' // this is the name of your table
    ;

    // run the query
    $result = mysql_query($sql) or die('Failed to execute ' . $sql . ' due to ' . mysql_error());


    $start_link = '<a href="';
    $end_link = '</a>';
    // read the data from the database
    while ($row = mysql_fetch_***0c($result))
    {
    // build the link tag
    $link = $start_link // start of link tag
    . $row['url'] // URL
    . '" title="' // title attribute
    . $row['alt'] // title data from the database
    . '">' // complete <a ...> tag
    . $row['description'] // description from database
    . $end_link; // close the anchor tag

    // display the link tag
    echo $link;
    }
    ?>
    </body>
    </html>

  14. #14
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's my Db syntax:

    CREATE TABLE `media_archive` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `day` varchar(15) NOT NULL default '',
    `month` varchar(15) NOT NULL default '',
    `Year` varchar(15) NOT NULL default '',
    `link` varchar(255) NOT NULL default '',
    PRIMARY KEY (`id`)
    ) TYPE=MyISAM MAX_ROWS=255



    Also see attached image:
    Attached Images Attached Images

  15. #15
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok - I assume that you only want to display links where there is an actual link in the media_archive table.

    PHP Code:
      $start_link '<a href="';
      
    $end_link '</a>';
      
      
    // select the columns you require from the database
      
    $sql 'SELECT'
           
    ' link'
           
    ' FROM '
          
    ' media_archive'         // this is the name of your table
           
    ' WHERE'
           
    'link IS NOT NULL'
           
    ;
      
      
    // run the query
      
    $result mysql_query($sql) or die('Failed to execute ' $sql ' due to ' mysql_error());
      
      
    // read the data from the database
      
    while ($row mysql_fetch_assoc($result))
      {
         
    // build the link tag
     
    $link $start_link                 // start of link tag
             
    $row['link']                 // URL
             
    '">'                         // complete <a ...> tag
             
    $row['link']                 // description from database
             
    $end_link;                 // close the anchor tag
      
         // display the link tag
         
    echo $link;
      } 

  16. #16
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that worked well, although i had to remove:
    // select the columns you require from the database
    $sql = 'SELECT'
    . ' link'
    . ' FROM '
    . ' media_archive' // this is the name of your table
    . ' WHERE'
    . 'link IS NOT NULL'
    ;

    I had to remove these two lines of code:. ' WHERE' . 'link IS NOT NULL' as i was getting an error saying that line one was NOT NULL or something like that, umm the links appear connected side by side horizontally, can i seperate them and for example can i have them arranged in an HTML table????

    Thanks very much for the help

  17. #17
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops -

    change
    PHP Code:
      'link IS NOT NULL' 
    to

    PHP Code:
       ' link IS NOT NULL' 
    i.e. add a space before the word link

    You can arrange the links in any fashion you want to. Just add the appropriate HTML tags where you need them, and then echo the $link variable in the appropriate place.

  18. #18
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WoW! Thank you so much! Mind if i ask, where could i possibly get more information or perhaps tutorials to further help me out with advanced code like the above?

  19. #19
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    More information - just keep looking in these forumns, especially the PHP forum for PHP stuff.
    Also http://www.w3schools.com is another good site.
    Search the PHP forum (lnik above) as there are more resources listed there

  20. #20
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's wrong with this code?

    <?php print "<table>";
    $while ($row = mysql_fetch_array( $rs))
    } <--- I keep getting an error on this line!! says unexpected syntax?
    print "<tr>";
    print "<td><a href=\"$row["url"]\" title=\"$row["alt"]>$row["description"]</a></td>";
    print "</tr>";
    {
    print "</table>";

    ?>

  21. #21
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The unexpected syntax is from the row above you want to use WHILE not $while.

  22. #22
    SitePoint Addict user_A's Avatar
    Join Date
    Mar 2004
    Location
    Vancouver, BC
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php print "<table>";
    while ($row = mysql_fetch_array( $rs))
    {
    print "<tr>";
    print "<td><a href=\"$row["url"]\" title=\"$row["alt"]>$row["description"]</a></td>";
    print "</tr>";
    }
    print "</table>";

    ?>

    after i fixed that, i get another error!
    Attached Images Attached Images

  23. #23
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are getting the error because you are using double quotes within a print statement that is double quoted.

    You can't use "<tr> "$myvariable"</tr>" for instance but need to use "<tr> '$myvariable' </tr>"

    After the initial open double quotes the next one you use closes the entire string. As far as PHP knows you are done with that line and is expecting nothing more except a semi-colon. You will need to tidy up any other areas of code you have where you are using double quotes around variables and whatnot.

  24. #24
    Apache Expert i_like_php's Avatar
    Join Date
    Nov 2001
    Location
    Dallas, Texas
    Posts
    1,342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey can you guys help me as well, since you're already here. ok i'm trying to display the id of the links stored within the mysql database.

    for example http://dfhsdka/index.php?id=1 and so forth

    i will try my best to explain what i want, so let's say they click on the link photos ok. i want it to display as photos?id=1 or ?id=photos and have it go the photos link. did i lose anyone lol


    have a look at the code to see what i am doing wrong

    PHP Code:
     //pulling the links from the database
    $sql_query "SELECT * FROM site_links WHERE cat like 'main'";
    //store the SQL query in the result variable
    $result mysql_query($sql_query);
    if(
    mysql_num_rows($result))
    {
    //output as long as there are still available fields
    while($row mysql_fetch_row($result))
    {
    echo (
    "{<a href=\"$row[2]\">&nbsp;$row[3]</a>}&nbsp;");
    }
    }
    //if no fields exist
    else
    {
    echo 
    "no values in the database";

    where i have it bold, that's where i want to be able to add the id to the end of the link, or the name of the link. so how do i accomplish this?
    i love php


Bookmarks

Posting Permissions

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