SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how should i approach this?

    Hello, on and off I have been working on a script. I am trying to build a piece of it, but I currently can't, and was hoping someone could tell me another approach I could take.

    If you are familiar with RPG games, this will help a ton!



    Ok, in RPGs, there are usually classes (like a warrior, paladin, etc) and items (like a sword).

    The script is to display search results (the search is finished). Ok, so say I search "a sword", i click "A Giant Sword". This is where I am confused, I want to display a list of classes that can use the sword.


    Currently I have a list for the item. The list looks like this: 1 4 6 7 10
    Each number represents a class that can use the item.
    It doesn't work ATM. I can get it to work, but only with 1 number in the list.

    So my question is: Are there any other ways I could make a list of classes that can use the item?

    Thanks for reading.

  2. #2
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this isn't a must, but personally, i'd make this script database driven. this way you'll also have a lot of room for expansion

  3. #3
    SitePoint Addict loadx's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne, VIC, Australia
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why dont you do something similiar to a dmflag in quake

    for example

    elves have a value of 1
    knights 6
    orks 7

    combined u get 167 - in effect Orks, knights and elves

    otherwise make it an array like
    $arrayname ('orksvalue','elvesvalue',knightsvalue');
    because then you can just check it by array index

    gl

  4. #4
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using a DB, ill try to explain this better.
    There are 2 tables, table_items & table_classes.

    I use my script to search through table_items, i find an item, i click it for more info. There it displays a list of classes.

    How is the list displayed?
    Currently I have a column called class_list. Lets say the item I found was A Giant Sword. The Giant Sword has a class_list of 5 7 2 4 6.

    So back to the display page. A SQL ,shown at the end of the post, selects the classes from class_list. Table_classes might have this row:
    ID(AI) --- NUM --- NAME
    1 ------- 5 ----- Warrior

    Since 5 was in the class_list, and 5 is found in column NUM, ill echo 5's NAME. But I currently have to use a foreach and extract. Since I am a beginner to PHP, its somewhat hard. I have tried so many combinations to the implode, never could get it to work.

    Heres the code:
    PHP Code:
    $result mysql_query("SELECT class_list FROM table_items WHERE NUM = '.$itemsid.'") or die ('Query was not run'); 
    while(
    $grow mysql_fetch_array($result)) 

    /* because the results in an array, for future purpose, i have to extract */ 
    extract($growEXTR_PREFIX_ALL"Ta"); 
    foreach (array(
    $Ta_classlist) as $value) { 
    /* now, for each number in classlist, i am going to run an SQL */ 
    $newval implode("','",$value);
    echo 
    $newval
    }    


  5. #5
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $result mysql_query("SELECT class_list FROM table_items WHERE NUM = '.$itemsid.'") or die ('Query was not run'); 
    result set is a bunch of 'class_list's

    PHP Code:
    while($grow mysql_fetch_array($result)) 
    take one class_list at a time

    PHP Code:
    extract($growEXTR_PREFIX_ALL"Ta"); 
    extract single class_list from array $grow

    PHP Code:
    foreach (array($Ta_classlist) as $value
    put this single variable into an array. loop through this array (containing only 1 item)

    PHP Code:
    $newval implode("','",$value); 
    can't do this, you only have one item and it's a string, not an array

    if you're trying to list all results, try this:
    PHP Code:
    $result mysql_query("SELECT class_list FROM table_items WHERE NUM = '.$itemsid.'") or die ('Query was not run'); 
    while(
    $grow mysql_fetch_array($result)) 

    /* feed each number into an array */
    $class_list_array[] = $grow['class_list'];
    /* implode array into 1 comma delimited string */
    $newval implode(', ',$class_list_array);
    echo 
    $newval;


  6. #6
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The tab button is your friend.

  7. #7
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if I were you, I would possibly setup the database like this:

    Table_Character_Classes:

    character_id PRIMARY KEY (auto increment)
    character_name (ex: Paladin)
    character_weapon_class (ex: 1 for Great Swords)
    character_armor_class (ex: 1 for Heavy Armor)
    etc

    Table_Weapon_Classes

    weapon_id PRIMARY KEY (auto increment)
    weapon_name (ex: Masamune)
    weapon_class (ex: 1 for Great Swords)
    etc

    So when you went to query the database to find the weapon type that i character can use, you'd use a query like this:
    PHP Code:
    $select "SELECT character_name, weapon_name"// returns a column of character names and weapon names
    $from " FROM table_character_classes, table_weapon_classes"// joins the two tables based on the following condition
    $where " WHERE character_weapon_class = weapon_class";

    $query $select .$from .$where;

    $results = @mysql_query($query);
    while(
    $row mysql_fetch_array($results)) {
    $char $row['character_name'];
    $weapon $row['weapon_name'];

    echo 
    'The ' .$char .' can use a ' .$weapon .'<br />';

    }
    // end while 

  8. #8
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the posts.

    Gib, wouldn't that work only for weapons that had one class? Most weapons have like 5-10 classes. That's why I am using a list.

    Darchangel, I messed up on my SQL, /sigh. It's actually supposed to be like this:
    PHP Code:
    $result mysql_query("SELECT class_list FROM table_items WHERE items_id = '.$itemsid.'") or die ('Query was not run'); 
    The items_id is auto increment. $itemsid is a variable in the url like this:
    mydot.com/display_page.php?item_id=5

    So the SQL would actually be like this:

    PHP Code:
    $result mysql_query("SELECT class_list FROM table_items WHERE items_id = '5'") or die ('Query was not run'); 


    For the array thing, how do i use that in the foreach()?

    $class_list_array[] = $grow['class_list'];

    I am trying to do it like this:

    foreach($number_in_class_list as $value) {do an SQL using that number}

    If I can figure this method out, ill use it, and unfortuneately I think it's the only one I could use.

    --
    Thanks

  9. #9
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gib was correct. use that join he suggested. also, after the $where line, add:
    $where .= " AND items_id = '$itemsid' ";

    be careful. above you mistakenly have:
    $result = mysql_query("SELECT class_list FROM table_items WHERE items_id = '.$itemsid.'") or die ('Query was not run');
    this will try to look up '.5.' not '5'. i think what you meant to do is this:
    $result = mysql_query("SELECT class_list FROM table_items WHERE items_id = '" . $itemsid . "'") or die ('Query was not run');

  10. #10
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what gibb meant now. I think...

    If you have ever played EverQuest, there are around 20-30 thousand items. I think I would then have to have around that many rows?

    I am trying to do something like this...
    I search an item, i click the items name, it shows detailed info on it, including: What classes & races can use it, if its used in a quest, if so, which, and where it's found (could be many zones).

    Since EverQuest items are almost all unique, i would almost have to have a column for every item, as items don't go off of what "weapon class" (like BLUNT), they say specific classes. Ill show an example item below.


    Item name: The Impaler from the Depths
    Statistics: Strength +5, Agility +10, Wisdom -5
    Usable Races: Elf, Human, Dwarf
    Usable Classes: Warrior, Paladin
    Weapon Type: Spear
    Weapon Weight: 12 lbs.
    Found In: The Black Forest & The Green Forest

    Used in quests: none
    From Quest: The Long Quest

    Another person suggested making a column for each class, saying YES or NO. I was originally going to do this, but if i did, i would probably have 10k columns.

    Used in the Long Quest: YES
    Used in the Short Quest: NO

    **I may not understand gibb's idea yet, but I think i do.

  11. #11
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    10k columns really isn't that much. sql servers serve one purpose and they do it well: store/retrieve info. you're not going to break it or even slow it down with 10k columns (how you handle efficient script writing will be your hindering factor). at work i created a php/mysql intranet. it holds all of our deliverable info for 1500+ clients. there are 60 tables and over 200,000 rows of data. and that's only considered a small to medium sized business application. mysql is still as fast on that server as the moment i installed it

    if you need help understanding joins, try these:
    http://www.w3schools.com/sql/sql_join.asp
    http://www.sqlcourse.com

  12. #12
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was told a single table could only hold 1k columns. But even if it is possible, myself actually writing up 10k columns would take a LONG time.

    Since I am a beginner to PHP, would any of you be willing to help me on my original method?

    I have tried at least 40-50 combinations with the foreach(), and obviously never got it to work.

    I could however get it to work like this, foreach(array("4, 3, 2")) , but when i put a variable in, it changes like this:

    $the = "5, 3, 2, 1";
    foreach(array($the)) {}

  13. #13
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by augathra
    foreach(array("4, 3, 2"))
    if this worked then php was being very forgiving. quotes in a function divide input. the above should yield an unnamed array (if that's even possible) with [0] = 4, 3, 2
    this should be:
    $some_array_name = array(4, 3, 2);
    or
    $some_array_name = array('4', "3', '2');
    or
    $some_array_name = array("4", "3", "2");

    quotes can also be used as string values by escaping them
    $arr = array("\"4", "3", "2");
    yields:
    [0] = "4
    [1] = 3
    [2] = 2

    when quotes are used in a string, they aren't separating values, they're just characters. same with commas. a string variable can only be treated as a single value. that's why no matter what you stuff into the following, it'll only ever occupy one array cell:
    $the = "5, 3, 2, 1, stuff, \"quoted stuff\", more stuff";

  14. #14
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was a bit off with my post. You are right augatha in that my setup would only work if a certain character class could only weird one class of weapon. In the case that you have a Paladin who can use Swords, Great Swords, Maces, etc, you would need to have 3 tables. One table would describe the paladin, another would describe a specific weapon, and the 3rd table would link the two together.

    TABLE_CHARACTER

    char_id
    char_name

    TABLE_WEAPON

    weapon_id
    weapon_name

    TABLE_LINK

    link_id
    link_char (references the character table)
    link_weapon (references the weapon table)

    Then when you wanted to display all the weapons that a paladin could use, you would have a query like this:
    PHP Code:
    $select "SELECT char_name, weapon_name";
    $from " FROM table_character, table_weapon, table_link";
    $where " WHERE char_id = link_char AND weapon_id = link_weapon AND char_name = 'Paladin' ";

    $query $select .$from .$where
    Your Link table would look pretty uninformative by itself, merely containing rows that look like this:

    1, 1, 1
    2, 1, 2
    3, 1, 3
    4, 2, 4
    etc

    Assuming that the 1's in the 2nd column refer to the paladin, and the 3rd column refers to different weapon classes, then you can see that the paladin can use weapons of class 1, 2, and 3.

    So basically the reason for joining all the tables together is to allow you to easily output the name of the chracter, and the name of the weapon.

    You could technically put the actually character names and weapon names in the Link table, but the point is that you only have to write out "Paladin" one time, then use a number to reference him in the background.

  15. #15
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would mean i would have to duplicate the item around 300 times, X around 20k. Since the game has everything unique.

    The array, gees, I placed another error, i used $some_array_name = array("4", "3", "2"); and
    $some_array_name = array(4, 3, 2);

    If it does not look like a huge task, would either of you try to get the array method to work? I don't understand why I can't put $variables into the array(). Is it because they are already an array? I have tried foreach($variable as $v) {}..

    Thanks

  16. #16
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dunno if i understand what you're trying to do with arrays yet. Are you trying to make an array to hold all the various types of weapons that I character can use? So in your DB you'd have a row in your character table like this maybe?:

    1, Paladin, weaponArray, armorArray, etc

    and weaponArray would be like this?
    weaponArray = array('Sword','Great Sword','Mace');

    If that's the case, then i suppose that would work, tho my college SQL teacher would probably keel over and die at the thought of it.

    What types of values are you trying to store into an array?

  17. #17
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lol no, this is how i do it.
    Ok, i have these tables:
    items
    classes
    races
    quests

    etc


    Items table:
    ID - Name - Wisdom - STR - etc - class_list - race_list - quest_list
    1 --Sword-----0------5----------5 4 3 2 1----3 1 2------0-------

    If 0, no quest is used.

    It's somewhat like that.
    ---------------

    classes:
    ID - NUM - Name
    1 ----57---Warrior---


    Ok, you probably get the drift now.

    Then I SELECT class_list FROM Items WHERE ID = " . $itemsid;
    Now i have the list of classes, but they are all one.

    So now I am trying to cut them all up into single numbers, then do another SQL like this:

    SELECT Name FROM classes WHERE NUM = " . $one_number_from_list

  18. #18
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no reason to keep lists in a single cell. harness the power of the database and of joins
    Code:
    Items
    ID  Name   wisdom  STR  ...etc
    1   Sword  0       5
    
    classes
    ID  NUM  Name
    1   57   Warrior
    
    Races
    ID  Race
    1   Elf
    
    RaceItemRestrictions
    ID  RaceID  ItemID
    1   3       1
    2   1       1
    3   2       1
    
    ClassItemRestrictions
    ID  ClassID  ItemID
    1   5        1
    2   4        1
    3   3        1
    4   2        1
    5   1        1

  19. #19
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darchangel
    no reason to keep lists in a single cell. harness the power of the database and of joins
    I agree. Unfortunately any way you decide to accomplish this task will take along time because of what you're doing. Because all the items are unique, you're going to have to enter in the data for every single of of them. If you do it right tho, you'll only have to do it once, and can easily group information together with the right table joins.

  20. #20
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gibb
    I agree. Unfortunately any way you decide to accomplish this task will take along time because of what you're doing. Because all the items are unique, you're going to have to enter in the data for every single of of them. If you do it right tho, you'll only have to do it once, and can easily group information together with the right table joins.
    yup. this is when you bite the bullet and create your own interface. just create the code once so in the future you can complete a form and let it do all the hard work for you. most effective and efficient db schema are not easy on the eyes. normalization is ugly stuff

  21. #21
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would anyone be willing to help me out on how this would be written? Or perhaps a premade script that I could take some lines of code...
    The insert & the display....

    If I insert it, the ItemID in table ClassItemRestrictions has to be the same number as ID in table items. Im not sure how I would do this? Since i can't "get" the ID from items while inserting it...

    On the display.php page, I would have to use a foreach(), correct?

    Ill be working on display.php after this post. Try to configure it the way Darchangel said.

    Thanks for all the posts.

  22. #22
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I came up with on the display.php page. Would say 10 SQL's like this slow the site down a lot?

    PHP Code:
    <?php 
    $itemsid 
    "1";
    $SQL = ("SELECT classID FROM class_restriction WHERE itemID = " $itemsid);
    $connect mysql_connect("localhost""asdf""asf") or die(mysql_error());
    $datab mysql_select_db("asdf");
    $results mysql_query($SQL) or die(mysql_error());

    while(
    $row mysql_fetch_array($results)) {
    $p $row['classID'];
    $SQL_2 = ("SELECT classname FROM classes WHERE classid = " $p);
    $result mysql_query($SQL_2) or die();
    while(
    $grow mysql_fetch_array($result)) {
    echo 
    $grow['classname'];
    }
    }

    ?>
    --- I'm guessing the insert is the only hard part now...

    Lets say i am making a new item, i am a user to the site.


    I have the item: A Hairy Spear, Usable by: Warrior & Paladin

    A Hairy Spear goes to table: items
    since auto_increment is on, it's ID will be "1"

    Then the user clicks checkboxes or something for the classes.
    The class names are sent to table: class_restriction
    It too get's its ID
    The tricky part: How does it get it's classID?

    The classID is the same as the items ID...but since it's not made yet...it can't really fetch it.

  23. #23
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i suggest making a form with several drop down (select) menus
    Code:
    New Item Form
    Item name:
    [input field]
    Allowed by classes:
    [dropdown 1]
    [dropdown 2]
    ...
    [dropdown 10]
    
    Allowed races
    [dropdown 1]...
    have the values for each option be the appropriate ID #s. when the form is submitted, you create the item field in its table. next (assuming you're using mysql) use mysql_insert_id to retrieve the item's ID that you just created. next, build the rows for each class/race/etc

    the key here is learning the wonders of mysql_insert_id. it's a glorious tool that allows you to "Get the ID generated from the previous INSERT operation"

  24. #24
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, would you be able to show me a short, short piece of code using it? I'm not sure how to "harness" the previous ID, like into a variable.
    If i need to...

    Would I basically do something like this?
    (table: race_restriction) itemID= $previousID ++;


    I was thinking perhaps a "Continue" button...all "unfinished" items, ones that were continued, but not all the way, are marked with a UNFINISHED. I run a script at the end of the week to delete all UNFINISHED rows.

    Although, I think i'd like your method more, heh.

  25. #25
    SitePoint Guru augathra's Avatar
    Join Date
    Jul 2004
    Location
    united states
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nvm
    Last edited by augathra; Aug 14, 2004 at 12:17.


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
  •