Jump to content

create links from table entries?


gk20

Recommended Posts

I have a table called item with fields category and item. I can display all distinct categories but I want to display them as links on my home page, so that when u click a category it displays all items in that category on a new page?

Here my code for displaying each individual category:

<?php
include 'db.inc.php';

$cats = @mysql_query("SELECT DISTINCT category from item ORDER BY category");

echo '<b>&nbsp Category List<br></b>';

while ($row = mysql_fetch_array($cats))
{
echo '<font color=red size="4">';
echo '&nbsp;&nbsp;';
echo $row['category'] . '<br/>';
echo '</font>';
}
?>

Can anyone help?
Link to comment
https://forums.phpfreaks.com/topic/5410-create-links-from-table-entries/
Share on other sites

[!--quoteo(post=356919:date=Mar 21 2006, 05:39 PM:name=gk20)--][div class=\'quotetop\']QUOTE(gk20 @ Mar 21 2006, 05:39 PM) [snapback]356919[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I have a table called item with fields category and item. I can display all distinct categories but I want to display them as links on my home page, so that when u click a category it displays all items in that category on a new page?

Here my code for displaying each individual category:

<?php
include 'db.inc.php';

$cats = @mysql_query("SELECT DISTINCT category from item ORDER BY category");

echo '<b>&nbsp Category List<br></b>';

while ($row = mysql_fetch_array($cats))
{
echo '<font color=red size="4">';
echo '&nbsp;&nbsp;';
echo $row['category'] . '<br/>';
echo '</font>';
}
?>

Can anyone help?
[/quote]
if you are trying to link them then
add two more echos inside of the while
that is
while ($row = mysql_fetch_array($cats))
{
//like file is
$link = "$row['category'] "."php"


echo '<font color=red size="4">';
echo '&nbsp;&nbsp;';
echo '<a href='$link'>';
echo $row['category'] . '<br/>';
echo '</a>';
echo '</font>';
}

i hope this will work as per your needs.

Thank you,
Karthikeyan
I've changed the code for displaying the different categories to this:

while ($row = mysql_fetch_array($cats))
{
session_register('category');
$_SESSION['category'] = $category;

$category = $row['category'];
echo '<font color=red size="4">';
echo '&nbsp;&nbsp;';
echo '<a href="viewads.php">';

echo $row['category'] . '<br/>';
echo '</a>';
echo '</font>';
}

My page for displaying the different items is called viewads.php, i dont have any files called
"categortyname".php, just this viewads.php file. At the moment it displays all items in the database,
but I want it so that when a user clicks on a category name on the home page it will just display the items
in that particular category??

Is that possible?
Heres the relevant code from my viewads.php file:

session_register('cat');
$cat = $_SESSION['category'];

$getcats = @mysql_query("SELECT DISTINCT category from item
where category = '$cat' order by category asc");

echo '<br>';
while ($row = mysql_fetch_array($getcats))
{
echo '<b><font color=red>Category: ';
echo $row['category'];
echo '<br><br></font></b>';
$catagain = $row['category'];

$getitems = @mysql_query("SELECT itemname, cost, description, itemowner
from item
where category = '$catagain'
order by itemname asc");
.......
...... etc display results

}


But it still doesnt work!! Does anybody understand what I'm trying to say???









Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.