Jump to content

Displaying MYSQL results as html links


fantastapotamus

Recommended Posts

Hi all,

 

I'm new to php and I've been having problems implementing it into a (mock) website I'm woking on. I'm trying to implement tagging; so a user can either tag an item with what they seem fit, or click on a tag and that will retrieve a list of other items that has that tag.

 

I've managed (just!) to allow users to tag and I can retrieve all tags that an item has (should probably mention that the items - its a fake e-commerce site - aren't stored in a database, just  on seperate html pages) but don't know how to retrieve them as a HTML link, which the user can click on and then see a list of all other items with that tag!

 

It's driving me a bit bonkers! I was surprised that I couldn't find a decent tutorial of implementing tagging - which I was kind of hoping on.

 

Anyway, if anyone can help I'd be most appreciated.

 

:D

Link to comment
Share on other sites

Thanks for the response.

 

Would you be able to just help me out with that in the context of my code?

 

As I'm trying to get it to link to all pages that have that 'tag' on them, what 'link' should that point to? A new page? And what should be on that page? I thought I might have to pass a variable in the url - but I'm not entirely sure what that means (it just looked like something I had to when I was reading a php book!)

 

Ta again

Link to comment
Share on other sites

Sure thing.


<?php

//connect to MySQL
$connect = mysql_connect('localhost', 'xxx', 'xxx'
or die(mysql_error());
mysql_select_db('xxx')
or die (mysql_error());

//select tags for movie

$query = 'SELECT * FROM tags WHERE name = "enchanted"';



$result = mysql_query($query, $connect)
or die(mysql_error());

while ($nt=mysql_fetch_array($result))
{
echo '<a href=\"tags.php?id='. $nt['id'] .' ">'.$nt['tags'].'</a>';
}

?>

//for the user to insert a new tag into the database

<form action="insert.php" method="post">
Tag: <input type="text" name="name" />
<input type="submit" />
</form>

 

I tried another bit of code that I found on the forum, but I messed it up.

 

What I'm unsure about is when the user clicks the tag - I'm guessing it needs to open a new php page? If so, what should be in that page? How do I do it so taht page knows what to retrieve (in this case, all items that have the same tag?)

 

Many thanks

Link to comment
Share on other sites

<?php

//connect to MySQL
$connect = mysql_connect('localhost', 'xxx', 'xxx') or die(mysql_error());
mysql_select_db('xxx') or die(mysql_error());

//select tags for movie

$query = 'SELECT * FROM `tags` WHERE `name` = "enchanted"';



$result = mysql_query($query, $connect) or die(mysql_error());

while ($nt=mysql_fetch_array($result)) {
echo "<a href=\"tags.php?id=" . $nt['id'] . "\">" . $nt['tags'] . "</a>";
}

?>

//for the user to insert a new tag into the database

<form action="insert.php" method="post">
Tag: <input type="text" name="name" />
<input type="submit" />
</form>

 

Now in tag.php in order to find the right row in mysql, use get. Like:

 

$id = $_GET['id'];

 

$q = mysql_query("SELECT * FROM `table` WHERE `table_id` = '$id'") or die(mysql_error());

Link to comment
Share on other sites

I think I've messed up the query!

while ($nt=mysql_fetch_array($result))
{
echo '<a href=\"tags.php?id=" .$nt["id"]">[/url]';
}

Doesn't display anything at all now.

 

You posted above:

 

while ($r = mysql_fetch_array($q)) {

echo "<a href=\"" . $r['link'] . "\">Link[/url]";

}

 

Could you tell me what should be in 'link' for me? The new tags.php?

 

 

Link to comment
Share on other sites

Sorry, that should be;

 

$result = mysql_query($query, $connect)
or die(mysql_error());

while ($nt=mysql_fetch_array($result))
{
echo '<a href="tags.php?id=" .$nt["id"]"></a>';
}


 

I'm really having trouble understanding whats in that echo statement.

 

The tag table has tag_id (which is auto increment and PK) and name (name is what the user has typed in and submitted).

 

So I'm trying to get it to display name as a HTML link and when the user clicks it, it uses the id to pass into tags.php?

 

I think!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.