dario Posted April 19, 2014 Share Posted April 19, 2014 I have a database with a lot of items in it for a webshop, and i want to use radiobuttons connected to the items in the database, is there a way to do that or do i have to do it in a different manner? Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 19, 2014 Share Posted April 19, 2014 (edited) "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 April 19, 2014 by bsmither Quote Link to comment Share on other sites More sharing options...
dario Posted April 19, 2014 Author Share Posted April 19, 2014 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? Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 19, 2014 Share Posted April 19, 2014 (edited) "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 April 19, 2014 by bsmither Quote Link to comment Share on other sites More sharing options...
dario Posted April 19, 2014 Author Share Posted April 19, 2014 (edited) 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 April 19, 2014 by dario Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 19, 2014 Share Posted April 19, 2014 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. Quote Link to comment Share on other sites More sharing options...
dario Posted April 19, 2014 Author Share Posted April 19, 2014 i think i dont have a querry yet xs is it ok to send you the file i have? or dont you want to check it? I am really struggeling i have been adjusting code and copying codes all day long and no succes yet Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 19, 2014 Share Posted April 19, 2014 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? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted April 19, 2014 Share Posted April 19, 2014 Making a store isn't for beginners. I'm with bsmither by using something already done unless you are willing to spend time and actually learn how to do everything.. It's like making your own CMS plus is a store. So pretty much you want to make something like http://assembleyourpc.net/ ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.