PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1847
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
PHP beginner DB design questions Posted by WestChi on 14 Apr 2008 at 4:20 PM
I am trying to make a database driven website about a collecting hobby. The problem I am having is how do you design it properly.

Example:
Table A
companyID, companyName, companyBio

Table B
itemID, itemName, colorID, price

Table C
colorID, colorName

How do I design the input and output forms so that instead of having to know the companyID and colorID for an item they can select the companyName or see the colorName on the query results form?

Thanks

Report
Re: PHP beginner DB design questions Posted by tvienti on 23 Apr 2008 at 1:59 PM
: I am trying to make a database driven website about a collecting
: hobby. The problem I am having is how do you design it properly.
:
: Example:
: Table A
: companyID, companyName, companyBio
:
: Table B
: itemID, itemName, colorID, price
:
: Table C
: colorID, colorName
:
: How do I design the input and output forms so that instead of having
: to know the companyID and colorID for an item they can select the
: companyName or see the colorName on the query results form?
:
: Thanks
:
:

I like to write a function that takes an array of ID => Name and turns it into a list of <option ...> elements. For example:

function AssocToOptions($assoc)
{
    $options = array();
    foreach ($assoc as $ID => $Name)
    {
        $ID = htmlentities($ID);        // just to be safe
        $Name = htmlentities($Name);    // again...
        $options[] = "<option value=\"$ID\">$Name</option>";
    }
    
    /* I join them with a \n for source readability */
    return implode("\n", $options);
}


Now it's just a question of selecting the ID's and names from the database, building them into an assoc array of ID => Name, and passing it to this function. Then wrap the results of the function in a <select ...> and you're good.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.