Jump to content

Dynamic form from array. or better solution ????


Recommended Posts

Hi all.

 

I am currently trying to build a site that will allow users to manage a self build house project.  This is my first php MySQL project but I am getting to grips with it ok.

 

I am currently building this for my own benefit at the moment as I am about to undertake a self build. 

 

I have a few questions to put to you guys.

 

This is where I am at the moment

 

I have a database table named materials which contains a list of materials , quantities, unit of measurement, id.

 

My site has a user control panel that has separate forms for each stage of the build where the user enters the quantities of the materials, and the data is displayed as an array for each stage.

 

So, heres question 1.

 

I want to take the data and use it to generate a form which will be completed by several merchants....

 

so it will display similar to the array but will be a form with a text box for price where the merchant will insert a price.

 

i.e

 

<?php 


mysql_connect ("localhost", "test", "test") or die ('I cannot connect to the database becuase: ' . mysql_error());

mysql_select_db ("matquant");

// query
$query = mysql_query("SELECT * FROM `quantites` ORDER BY `id` ASC"); 



// results
while ($row = mysql_fetch_array($query)) {
   
   echo  "<br />"   .$row ['id'].      "  "  .$row ['material'].       "     "    .$row['quantity'].  "   "    .$row['unit']. "<br />";}
echo mysql_error();
?>

 

I want to add a text input box form which will allow the merchant to add a price to each item that is added to the array. so there the merchant will be able to give me a price for all the items in my list.  Heres where I get stuck....  as I am constantly adding to the list I have no fixed amount of items so the form for the merchants needs to be built dynamically, ie each time I add a new material, I want the merchants form to get a new input.

 

I am not very good at describing my problems but I am hoping that someone can help me out from the information given.

 

Thats the first question.  i like to get issues sorted in small bites. 

 

the second is the next big bite,  I want to send the form above to several merchants, so I intend to have a user database with a user for each merchant, and link the tables so that each merchant can provide a price for each material, ideally the form would highlight the materials that have not had  a price yet.

 

I'm looking for advice on the best method of doing this basically but if I can get the first question sorted that would be great..

 

I have the test site local but can upload it it to the server if it will help you see what im trying to do.

 

Any help that anyone can give will be greatly appreciated.

To generate the form you'd simply output your individual form fields within the while loop, For example

<form action="" method="post">
<?php
// results
while ($row = mysql_fetch_array($query)):
    $id = $row['id'];
?>
<p>
    Material: <input type="text" name="materials[<?php echo $id; ?>][name]" value="<?php echo $row['material']; ?>" /><br />
    Qantity: <input type="text" name="materials[<?php echo $id; ?>][quantity]" value="<?php echo $row['quantity']; ?>" /><br />
    Price: <input type="text" name="materials[<?php echo $id; ?>][price]" value="<?php echo $row['unit']; ?>" /><br />
</p>
<?php endwhile; ?>
<input type="submit" name="submit" value="Submit" />
</form>

Now I have setup the form fields to be grouped together within the materials array.

 

This will allow us to easily process the form results using a simple foreach loop eg

foreach($_POST['materials'] as $material_id => $material_data)
{
    extract($material_data);

    echo "Material #$material_id :: $name :: $quantity :: $price<br />";
}

  • 2 weeks later...

still have questions on the above....

 

I want different users to submit seperate prices.  how would you guys arrange the database.

 

I have a table for materials and quantities, a table for users, a table for prices.  so in the table for prices there will be a column for the userid and a column for the material id, and a column for price.

 

This is all clear enough, but how do I query several tables and share data between them.

 

I have the form above which will allow the merchants to post prices for each material (whilst showing the quantity's and material type that I have allready set up using a material control panel with forms for this.)

 

It is difficult to ask the correct question when I have a few so many....  :-\

 

So... I will try again.

 

I want the form above to only have an input field for price, which will insert the user name entered at a the login screen in the "price" table alongside the prices entered?  Quite complicated but the final piece of this jigsaw for my application site.

 

Not sure what other code I am using to let you guys see but would appreciate any further input that you can give me.  I have learned a lot but still learning. 

 

I have the login system working, and the control panel displaying the database contents and allowing me to insert a data into the materials table.  I can post that to my server and let you all see it working if that would help.  Let me know if there is anything else i can say that will help you help me....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.