...

View Full Version : Hyperlinking database output



alex98uk
02-26-2010, 04:17 PM
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:


<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 :)

Shauny_B
02-26-2010, 04:22 PM
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

Nightfire
02-26-2010, 04:22 PM
Alter


while($row = mysql_fetch_array($result))
{

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

}

to


while($row = mysql_fetch_array($result))
{

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

}

alex98uk
02-26-2010, 04:26 PM
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 :)


Alter


while($row = mysql_fetch_array($result))
{

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

}

to


while($row = mysql_fetch_array($result))
{

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

}


As for you, I love you. :eek:

alex98uk
02-26-2010, 04:32 PM
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 :)

Nightfire
02-26-2010, 04:36 PM
A basic way would be:



<?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'].' ';

}

?>

alex98uk
02-26-2010, 05:10 PM
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.

Nightfire
02-26-2010, 05:22 PM
So you're looking for all atriclenames with the 'tag' anywhere in it?



select * from articlename where tag LIKE '%$tag%'


Is that right?

alex98uk
02-26-2010, 05:51 PM
This is my database at the moment:

http://i70.photobucket.com/albums/i120/alex98uk/db.jpg

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 :)

Nightfire
02-26-2010, 06:07 PM
Been ages since I've done mysql/php so a bit rusty lol



select * from article_tag where tag='$tag'


and in the while loop

you want



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

alex98uk
02-26-2010, 06:12 PM
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
$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 :)

bgallegos
02-26-2010, 09:39 PM
Nightfire already mentioned above, but you need make the following change to display the article names:


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

To


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

alex98uk
03-01-2010, 12:41 PM
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 :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum