Page 1 of 1

[SOLVED] Dynamic Text Links -- MySQL/PHP

Posted: Sun Aug 08, 2004 9:17 pm
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]

Posted: Sun Aug 08, 2004 9:32 pm
by paladaxar
$num=mysql_numrows($result);
should be: $num=mysql_num_rows($result);

(Notice the additional "_" between "num" and "rows")

...

Posted: Mon Aug 09, 2004 2:19 am
by ripcurlksm
How do I get the dynamic link to pull info when clicked? Im new to this.

Posted: Mon Aug 09, 2004 2:57 am
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)

...

Posted: Sat Aug 14, 2004 8:06 pm
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?

Posted: Sat Aug 14, 2004 8:10 pm
by tim
if you pass variable(s) thru the URL, you would use $_GET to retrieve them.

...

Posted: Sat Aug 14, 2004 9:37 pm
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!

Posted: Sun Aug 15, 2004 12:49 am
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.";
}

?>

...

Posted: Thu Aug 19, 2004 11:11 pm
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

Posted: Thu Aug 19, 2004 11:37 pm
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> | ";

...

Posted: Fri Aug 20, 2004 11:48 am
by ripcurlksm
Thanks feyd, you rock! Check it out:
http://www.onestopauctionshop.net/spinco/ddtest.php

Kind Regards,
Kevin McCormick