SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello. I have been going through the Sitepoint tutorial on using PHP and MySql. I am trying to change the sample scripts around for my own edification and potential use on my web site. What I want to do is create a database of URLs that can be listed by category. I have created one table (called "links") for the URLs and another one (called "categories") for the categories. There is a CID (category ID) field in the "links" table. So far so good. But now I am trying to write code to handle all this. My first project was to create a web page that would list all the URLs in a specific category, and have a drop-down menu to change to a new category. This has proved much harder than I thought. Right now I am still sticking with the tutorial's format of having the selector be in a seperate view on the same page, rather than combined with the list. I also am getting all kinds of errors about my code. Although I understand the basics of PHP, it seems that moving from a tutorial to one's own code is not so easy. If someone has the time I would appreciate some comments on my messy code. (Be nice!). Thank you!

    kerim

    PS: the ****** marks the name of my server which I didn't want to share, as well as the password.


    Old Script removed - see new script in my most recent post below.
    Last edited by kerim; May 22, 2001 at 06:47.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (censored for being absolutely WRONG!...sitepoint administrator)
    Last edited by 7stud; May 21, 2001 at 22:58.

  3. #3
    SitePoint Member
    Join Date
    May 2001
    Location
    Holland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the problem is that u paste to much and should type code more...

    I'm afraid I can't help u with the code alas (i'm sure somebody will) but I think that u should first find out where the problem lies, and then try to solve it (wether it is you to solve it, or with help from sitepoint)

    I'm afraid that my first sentence states the real problem... Perhaps work through the tutorial again except instead of a jokes database, you're own site..

  4. #4
    SitePoint Member
    Join Date
    May 2001
    Location
    Holland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is the problem..
    u should try to add this in the sql_output:
    <a href='$PHP_SELF?selectcat=$cid'>select entry</a>

    This way the selectcat will use the cid value in the $cid variable.. Makes sense eh? no?

    This way a variable $cid wil be created when someone's choosing the category with the cid value of the category in your database.

    then you should use this $cid to join the 2 as written in the tutorial it'self

  5. #5
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to all those who responded!

    I finally got it working just the way I wanted. Although I would appreciate help with two things: (1) How to go back to the initial page that appears after the value has already been set? It seems like this (ifset=xxxx) type structure doesn't allow that, but I'm sure there is a way. (2) General suggestions for cleaning up the code would be appreciated as well.

    kerim

    <HTML>
    ...
    <body>
    <center>
    <?php

    // If the user selects a category
    if (isset($selectcat)):

    include("login.php");

    $result = mysql_query(
    "SELECT category FROM categories WHERE ID=$selectcat");

    if (!$result) {
    echo("<P>Error performing query: " .
    mysql_error() . "</P>");
    exit();
    }

    while ($row = mysql_fetch_array($result)) {
    $category = $row["category"];
    echo("<h3>Here are all the URLs from:" .
    "$category</h3>");
    }

    $result = mysql_query(
    "SELECT name, url, notes " .
    "FROM links WHERE CID=$selectcat");

    if (!$result) {
    echo("<P>Error performing query: " .
    mysql_error() . "</P>");
    exit();
    }

    while ( $row = mysql_fetch_array($result) ) {
    $name = $row["name"];
    $url = $row["url"];
    $notes = $row["notes"];
    $category = $row["category"];
    echo("<P><A href='http://$url'>$name</a></p> " .
    "<p>$notes </p>");
    }

    // When clicked, this link will load this page
    // with the new categtory value instead of the current one.




    ?>
    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <select name=selectcat>
    <option value=0 >Select a category</option>
    <?php
    $catlist = mysql_query(
    "SELECT * FROM categories");

    while ($row = mysql_fetch_array($catlist)) {
    $ID = $row["ID"];
    $category = $row["category"];

    echo "<option value=$ID>$category</option>";
    }
    ?>

    </select>
    <input type="submit" value="Change Category" name="submit">
    </FORM>

    <?php
    else:

    include("login.php");


    // List all the Categories, ordered by ID.

    $result = mysql_query("select * from categories order by ID");

    if (!$result) {
    echo("<P>Error performing query: " .
    mysql_error() . "</P>");
    exit();
    }


    echo("<P> Here are all the Categories in our database: </P>");

    while ( $row = mysql_fetch_array($result) ) {
    $categories = $row["category"];
    $ID = $row["ID"];

    // Click to set the Category ID

    echo("<A HREF='$PHP_SELF?selectcat=$ID'>" .
    "$categories</A></P>");

    }



    endif;

    ?>
    </center>
    </BODY>
    </HTML>
    Last edited by kerim; May 23, 2001 at 07:10.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •