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 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Hyperlinking database output

    Hi, a bit of a complex one (well, for me at least ). Basically, I am making a small tag cloud for University work. Now, I have it outputting all relevant tags which are stored in a MySQL database in a table called article_tag.

    This is the code I use:

    PHP Code:
    <h3>Tags Submitted for this article</h3>
    <?php
    $con 
    mysql_connect("xxxxxx""xxxxxx""xxxxxx");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    $db_selected mysql_select_db("db_xxxxxx",$con);

    $result mysql_query("SELECT * FROM article_tag");

    while(
    $row mysql_fetch_array($result))
      {

      echo 
    $row['tag'];
      print 
    " ";

      }

    ?>
    This just outputs it in a list like so:
    loves hard Testing the inputs is annoying gdfggh hgffgfg ghxfghh hgxdhxgh xfghhgf the big black dog went the tester1 sdfdfsg fggfdgfdfgd fgdddddddd dfggfddfgd dfgdfhgdh dfgdfhhh
    What I want to do is hyperlink each separate word to a search. So, if the user were to click the tag "inputs", it would search for every article with the tag "inputs" which (as explained earlier), is held in the article_tag table.

    Is there a realistic way to do this? Thank you
    Last edited by alex98uk; 02-26-2010 at 04:22 PM.

  • #2
    New Coder
    Join Date
    Feb 2010
    Location
    UK, North West
    Posts
    36
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Hello,
    The way I'd go about doing such a task would be to use jQuery and AJAX, for example say if you "mouseover" on a specific word it then searches the database for any relavant data which matches it.

    To do that, I'd have an array of words which you're matching, seperate the output where the " " is, then if there's a matching word within it, put it within a <span></span> attribute.

    Then jQuery the mouseover and mouseout events over the span attributes to search the database with the inner html value

    Hope this helps,
    Shaun

  • Users who have thanked Shauny_B for this post:

    alex98uk (02-26-2010)

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Alter
    PHP Code:
    while($row mysql_fetch_array($result))
      {

      echo 
    $row['tag'];
      print 
    " ";

      } 
    to
    PHP Code:
    while($row mysql_fetch_array($result))
      {

      echo 
    '<a href="searchpage.php?tag='.$row['tag'].'">'.$row['tag'].'</a> ';

      } 

  • Users who have thanked Nightfire for this post:

    alex98uk (02-26-2010)

  • #4
    New Coder
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Shauny_B View Post
    Hello,
    The way I'd go about doing such a task would be to use jQuery and AJAX, for example say if you "mouseover" on a specific word it then searches the database for any relavant data which matches it.

    To do that, I'd have an array of words which you're matching, seperate the output where the " " is, then if there's a matching word within it, put it within a <span></span> attribute.

    Then jQuery the mouseover and mouseout events over the span attributes to search the database with the inner html value

    Hope this helps,
    Shaun
    Heh, thanks for the help sir, but it looks a little complicated, especially as I am what one might refer to as a "noob". But I appreciate you taking your time to reply

    Quote Originally Posted by Nightfire View Post
    Alter
    PHP Code:
    while($row mysql_fetch_array($result))
      {

      echo 
    $row['tag'];
      print 
    " ";

      } 
    to
    PHP Code:
    while($row mysql_fetch_array($result))
      {

      echo 
    '<a href="searchpage.php?tag='.$row['tag'].'">'.$row['tag'].'</a> ';

      } 
    As for you, I love you.

  • #5
    New Coder
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Heh, OK so that works good. It should do a search depending on what word you click. So, I created a searchpage.php, but i'm not sure what code I need to display the output.

    How do I tell it to search the database for the word that was clicked? I have only really covered searching from an input box (not a hyperlink).

    Thanks

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    A basic way would be:

    PHP Code:
    <?php
    $con 
    mysql_connect("xxxxxx""xxxxxx""xxxxxx");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    $db_selected mysql_select_db("db_xxxxxx",$con);
    $tag mysql_real_escape_string($_GET['tag']);
    $result mysql_query("SELECT * FROM article_tag WHERE tag='$tag'");

    while(
    $row mysql_fetch_array($result))
      {

      echo 
    $row['tag'].' ';

      }

    ?>

  • Users who have thanked Nightfire for this post:

    alex98uk (02-26-2010)

  • #7
    New Coder
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks I don't mean to be annoying, but there is one more thing. I would, upon clicking a word, for all articles in the database that have it as a tag to be listed.

    So, I need to tell it to select * from articlename where tag = '$tag' correct? How do I put this into working code?

    EDIT:

    or select articlename from article_tag where tag = '$tag'. Doesn't seem to work though.
    Last edited by alex98uk; 02-26-2010 at 05:15 PM.

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    So you're looking for all atriclenames with the 'tag' anywhere in it?

    PHP Code:
    select from articlename where tag LIKE '%$tag%' 
    Is that right?

  • #9
    New Coder
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    12
    Thanked 0 Times in 0 Posts
    This is my database at the moment:



    As you can see, article #2 & #3 both have the tag "foxes". I want it so that if the user clicks on the word "foxes" (the hyperlinked words we worked on earlier), the search page displays all articles with that tag associated to it.

    So, it would show something like this:

    Foxes

    3) Passing and ball distribution
    2) Sixteen Hits
    Does that make sense? Thanks for helping me

  • #10
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Been ages since I've done mysql/php so a bit rusty lol

    PHP Code:
    select from article_tag where tag='$tag' 
    and in the while loop

    you want

    PHP Code:
    echo $row['articlename'].' '
    Last edited by Nightfire; 02-26-2010 at 06:10 PM.

  • Users who have thanked Nightfire for this post:

    alex98uk (03-01-2010)

  • #11
    New Coder
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Heh, no problem, you know more than me. I'm doing an IT and business degree, but i'm more interested in the latter subject. However, we have to have a basic knowledge of how programming and scripting works so as to enable us to effectively manage IT projects.

    Anyway, the code you gave is:

    PHP Code:
                                                                                                          <?php
    $con 
    mysql_connect("xxxxxx""xxxxxx""xxxxxx");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    $db_selected mysql_select_db("db_xxxxxx",$con);
    $tag mysql_real_escape_string($_GET['tag']);

    $result mysql_query("SELECT * FROM article_tag WHERE tag='$tag'");

    while(
    $row mysql_fetch_array($result))
      {

      echo 
    $row['tag'].' ';

      }

    ?>
    What that does it just display the value of $tag (which is whatever word I clicked on). I just need it to print the names of the article (held in the articlename column) where the relevant tag is present.

    IF you can't remember, no worries, i'm sure I can find out elsewhere, cheers for trying though, it is appreciated

  • #12
    New Coder
    Join Date
    Jul 2008
    Posts
    45
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Nightfire already mentioned above, but you need make the following change to display the article names:

    PHP Code:
    echo $row['tag'].' '
    To

    PHP Code:
    echo $row['articlename'].' '

  • Users who have thanked bgallegos for this post:

    alex98uk (03-01-2010)

  • #13
    New Coder
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    12
    Thanked 0 Times in 0 Posts
    I swear I had tried that and it hadn't worked, but I just did it again and it's all good.

    Thanks a lot for your help everyone, i'm sure i'll be back


  •  

    Posting Permissions

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