[SOLVED] Dynamic Text Links -- MySQL/PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

[SOLVED] Dynamic Text Links -- MySQL/PHP

Post by ripcurlksm »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have an 'items for sale' mysql database of categories (garden, outdoors, automotive, etc.) and their status (sold or selling) thats loads dynamic text links for each active category. My problem is querying the database, when a unique category is selected (or clicked on), I want to generate a page where categories = 'selection' and status = 'selling.' So it would list all of the items in the database.

Here is an example of what I have so far, however, they arent linked to anything, Im having problems generating a page from this point. Here is a link to the page:
http://www.onestopauctionshop.net/preview/ddtest.php

And here is the code:
==================================

Code: Select all

<?
$username="name";
$password="pass";
$database="db";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM bookmark WHERE status='selling'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$category=mysql_result($result,$i,"category");


echo "<a href='http://onestopauctionshop.com/preview/category.php'>$category</a> | ";

++$i;
}

?>


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Post by paladaxar »

$num=mysql_numrows($result);
should be: $num=mysql_num_rows($result);

(Notice the additional "_" between "num" and "rows")
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

...

Post by ripcurlksm »

How do I get the dynamic link to pull info when clicked? Im new to this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

generally, by passing some way of knowing which category to grab on the other side.. (I do it through the url like: http://foo.com/somepage.php?somevar=somedata)
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

...

Post by ripcurlksm »

Ok, so I should create a php page that will query the database, based on the data passed through the url. Right? What would the category.php page look like?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

if you pass variable(s) thru the URL, you would use $_GET to retrieve them.
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

...

Post by ripcurlksm »

So the category.php script would look like:

get category from url
loop/query the database where * = 'category'




Using the info for from my above script, what someone quickly write out what the script should look like. I am having trouble tring to find a get/post tutorial. Please help!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would generally do something like


catagory.php

Code: Select all

<?php

//make sure the var in the url is iset
if (!empty($_GET["catagory"])) {
    //get all the items from the select catagory
    $result = mysql_query("SELECT FROM `items` WHERE catagory='"$_GET["catagory"]."'") or die("mysql error: ". mysql_error());

    //check if there are any items in the catagory
    if (mysql_num_rows($result) == 0)
    {
        echo "No items found in this catagory";
    }
    //if there are items, display them
    else
    {
    //show your items here
    }
//if the var was not set means there is an error, or the user access this file directly
}
else
{
    echo "You cannot directly access this page.";
}

?>
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

...

Post by ripcurlksm »

Ok so I have my first page which pulls the items from the database by unique category. Here is what it looks like http://www.onestopauctionshop.net/spinco/ddtest.php

And Here is the code:

Code: Select all

<?phpmysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM bookmark WHERE status='selling'";
$result=mysql_query($query);

$num=mysql_num_rows($result); 



$i=0;
while ($i < $num) {

$category=mysql_result($result,$i,"category");


echo "<a href='http://onestopauctionshop.com/spinco/category.php?category='>$category</a> | ";

++$i;
}


?>
Right now the problem is that it is not it is not posting the url correctly. When it loads "category.php" the url does not contain the category. So I know that this is the first problem I have to deal with. Try clicking on a category from the above link http://www.onestopauctionshop.net/spinco/ddtest.php and lets solve the url problem. This is my first adventure into custom programming, and I have purchased at least 4 books and refrenced them in detail, so I appreciate your help on this. :D

-Kevin
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you didn't include the $category variable inside your url :oops:

Code: Select all

echo "<a href="http://onestopauctionshop.com/spinco/category.php?category=$category">$category</a> | ";
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

...

Post by ripcurlksm »

Thanks feyd, you rock! Check it out:
http://www.onestopauctionshop.net/spinco/ddtest.php

Kind Regards,
Kevin McCormick
Post Reply