Page 1 of 1

Dynamic input w/ array usage..?

Posted: Tue Sep 16, 2008 7:07 am
by Weasel5-12
G'day...

1st post, i'm tryin 2 develop a website for drinks.. and havin some trouble with my ingredient input.

1st off, would it be better to ask users to select the number of ingredients or give a set amount.
i understand that the latter, although easier, can create un-needed code and chew memory.
but with that being said, the 1st option may disrupt the flow of my site.. (HELP !)

2ndly, im again unsure of how to store these variables.

the input will look very similar to the following:

Code: Select all

 
<form id="form1" name="form1" method="post" action="">
    
    Ingredient:
    <select name="ingredient_type">
    <?php 
        while ($row = mysql_fetch_array($result)){
            extract($row);
            echo "<option value='".$ingredientID."'>".$ingredientID;
        }
    ?>      
    </select>
    
    Amount:
    <input type="text" name="amount" id="amount" maxlength="11" width="50"/>
</form>
 
the above will be repeated as needed...

would it be best to store them in an array?

Re: Dynamic input w/ array usage..?

Posted: Tue Sep 16, 2008 11:24 am
by Christopher
First of all, though you have thought about this design a lot -- we know very little about it. So you will need to explain a little more what you are trying to do beyond a "website for drinks". I also would not worry about "create un-needed code and chew memory" at this point.

It sounds like you need to select both an ingredient and a quantity. That probably needs to be done in two steps.

Re: Dynamic input w/ array usage..?

Posted: Tue Sep 16, 2008 12:24 pm
by Weasel5-12
Okay, sorry...

What i'm trying to do for this particular stage in development, is to collect the input from users for 2 things.
1) Ingredient to be included (will be selected from a drop down list) and,
2) The amount of the ingredient used.

Within my database, i have a table which will include the specific drinkID, the specific ingredient ID and amount of the ingredient.
i have set this up as a 1:M relationship, to eliminate a M:M.

I have included a very brief ERD to help explain my ideas.
Image
http://i33.tinypic.com/2j3po9j.jpg

My problem was, 1stly taking the input from the user (as the #of ingredients vary per drink) and 2ndly, storing the data before inputting it into my database.

The ingredient itself, will be selected from a list of pre-determined ingredients, where as the amount will be purely user input

if this problem can be overcome, there will be a beta release of this very very soon.
which i will hope you will recognise how much you help really was appreciated.

Re: Dynamic input w/ array usage..?

Posted: Tue Sep 16, 2008 1:53 pm
by Christopher
Weasel5-12 wrote:My problem was, 1stly taking the input from the user (as the #of ingredients vary per drink)
A form with two fields per ingredient should work. Look into the different ways to do field naming, arrays, etc. with forms.
Weasel5-12 wrote:and 2ndly, storing the data before inputting it into my database.
Save each ingredient in a record in an ingredients table with the ID of the drink it goes with.

Re: Dynamic input w/ array usage..?

Posted: Tue Sep 16, 2008 2:00 pm
by Weasel5-12
cheers bro, just 1 last question.

with your comment on my 2nd problem you said
Save each ingredient in a record in an ingredients table with the ID of the drink it goes with.
this is what was suggested per my ERD in the drinkIngredient table..

the issue i was having was should i take the ingredients & amounts and store them in an array/vector before storing them in the DB?

and if so, how using something similar to a vector (if possible in PHP)

cheers bro

Re: Dynamic input w/ array usage..?

Posted: Tue Sep 16, 2008 2:32 pm
by Christopher
I would store each ingredient and its associated amount in an ingredients table. Each record should also contain the key of the drink that the ingredient is part of.

Re: Dynamic input w/ array usage..?

Posted: Wed Sep 17, 2008 2:10 am
by Weasel5-12
Aiight bro, thx 4 ur input. But i think i figured it out myself...

The following is what i think should work (please dont hesitate to comment/dedirect me). I did work this out purely in my head (so far)

Code: Select all

$numIng = $_POST('numIngregients');
$ingAmt[] = $_POST('ingredient[]');
$drinkID = $_POST('drinkID');
 
for($i = 0; $i < $numIng; $i++){
    $y = 0;
    $ingredientList[$i] = $ingAmt[$y];     // adds ingredients stored in $ingAmt[] to $ingredientList[]
    $y = $y+2;
}
 
 
for($i = 0; $i < $numIng; $i++){
    $y = 1;
    $amountList[$i] = $ingAmt[$y];     // adds amounts stored in $ingAmt[] to $amountList[]
    $y = $y+2;
}
 
for($i = 0; $i < $numIng; $i++){
    // Loop through query to input into database
}

Please, all comments welcome

cheers.