Jump to content

how to get items from your database into your php form


dario

Recommended Posts

"I have a database with a lot of items in it"

 

My experience shows that the results of a database query is an indexed array of associative arrays. Thus, to iterate through the first dimension of the result, a foreach() loop can be used.

 

"I want to use radiobuttons connected to the items"

 

Within the loop, the necessary HTML can be built up to form the input element.

 

"Is there a way to do that, or do I have to do it in a different manner?"

 

There are many ways to solve this. You may need to provide a bit more detail in your desired approach.

 

 

// Assume $results is the recordset from the query.
$radiobutton_list = "";
foreach($results as $record) {
  $radiobutton_list .= '<input type="radio" name="some_name" value="'.$record['id'].'" />'.$record['item_name'].'<br />';
}
Edited by bsmither
Link to comment
Share on other sites

Im making a webshow as task for school. this webshop lets you build a PC wich you can assemble yourself.

I was thinking that I could use radiobuttons for every different piece of the computer. example:

 

Processor:

-i3 (linked to  the processor in my DB)

-i5 (linked to  the processor in my DB)

-i7  (linked to  the processor in my DB)

 

is there a way that i can add sql commands in my value of the radiobutton?

Link to comment
Share on other sites

"Could I use radiobuttons for every different piece of the computer?"

 

Yes. Using radiobuttons for mutually exclusive options is one solution. Another is a drop-down selector for each option type. The choice depends on how much web page display area you have available. That is, a stack of ten radiobuttons takes up more space than a drop-down selector with ten options.

// Assume $results is the recordset from the query.
$radiobutton_list = "";
foreach($results as $record) {
  // ['item_type'] is 'processor', 'memory', 'harddrive', etc. ['item_name'] is '-i7', '4GB', '1TB', etc.
  $radiobutton_list .= '<input type="radio" name="'.$record['item_type'].'" value="'.$record['id'].'" />'.$record['item_name'].'<br />';
}

"Is there a way that I can add SQL commands in my value of the radiobutton?"

 

Yes, but I think what it is that you want to do will not work doing it that way.

Edited by bsmither
Link to comment
Share on other sites

i prefer using the radiobuttons because i only have like 3 options for every piece of the computer.

 

where do i have to define the variables because i have an error if i use your code it says : 

 Notice: Undefined variable: results in C:\wamp\www\gip\pp3.php on line 63

Warning: Invalid argument supplied for foreach() in C:\wamp\www\gip\pp3.php on line 63

line 63 is:  foreach($results as $record) {

Edited by dario
Link to comment
Share on other sites

The statements above illustrate the concept of iterating through a recordset and building an HTML string from that recordset. What is shown above is not complete.

 

Assuming you have all the code needed to capture the data sent from the database, I suggested a variable named $results. Your code probably uses a different variable to hold the database query results.

Link to comment
Share on other sites

Here's what I do to solve my problems: I use other people's complete applications.

 

For example, if I need an online store to sell computer parts, I get a copy of a free eCommerce solution (osCommerce, OpenCart, CubeCart Lite, etc).

 

"I am making a webshop as a task for school."

 

You need to ask yourself: Must I do all the coding? Am I not allowed to use something that is already built and ready to use? How much money is my time worth? How soon does the school want the store?

Link to comment
Share on other sites

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.