Zerpex Posted October 3, 2011 Share Posted October 3, 2011 I'm trying to create a website, that echo's out a bunch of groups, where each group contains a group of checkboxes, containing A value, and a label for the checkbox, the way it is created right now, is foreach that echo's out a bunch of php arrays, which was easier than the static way before - But still, it's static in some way, or not very user friendly at the moment.. My problem is that I really want to write it in a database when I have the option. Is there anyone that can give some tips how to do? At the moment, my foreach looks like this: echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">'; /* NEXT WE CREATE OUR FOREACH LOOPS TO ECHO THE HTML FOR LOOKS AND CHECKBOXES */ $totalID=0; // this is a counter we use to build our check box names foreach ($items as $list){ $totalID++; // add one to the checkbox name counter echo "<h2>{$list['title']}</h2>\n"; // and echo out our section header foreach ($list['items'] as $cbox){ // now for each item in the list, call it $cbox // $cbox now holds the item name, and point value echo "<label class='checkbox'><input type='checkbox' name='totals[$totalID][]' value='{$cbox[1]}'> {$cbox[0]}</label>\n"; } } echo "</form>"; And my array is something like this: $items['computers']['title']='Computer Brand'; $items['computers']['items'][]=array('Apple iMac',1); $items['computers']['items'][]=array('Apple Macbook',.5); $items['phones']['title']='Phone Brand'; $items['phones']['items'][]=array('iPhone',1); $items['phones']['items'][]=array('HTC',1); As said, I can write this, but takes time. I want to get it into a database, that data above, but I'm having problems about echo'ing it out, I really can't see how I should do. My current database looks like this: Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/248347-creating-multidimensional-array-containing-database-output/ Share on other sites More sharing options...
cunoodle2 Posted October 3, 2011 Share Posted October 3, 2011 Well you need to first write a mysql query to gather all of the results. I honestly don't know why you need to store them in an array unless you need to some how modify the order but that too should be able to be done in the query. Then after you do the query loop through all results and echo them out to the screen. Try writing out this code and let me know what you come up with. Quote Link to comment https://forums.phpfreaks.com/topic/248347-creating-multidimensional-array-containing-database-output/#findComment-1275328 Share on other sites More sharing options...
Zerpex Posted October 3, 2011 Author Share Posted October 3, 2011 Well you need to first write a mysql query to gather all of the results. I honestly don't know why you need to store them in an array unless you need to some how modify the order but that too should be able to be done in the query. Then after you do the query loop through all results and echo them out to the screen. Try writing out this code and let me know what you come up with. Ahh yeah, ur right, I could just make the foreach echo out the stuff, instead of putting them in an array, and then echo out Quote Link to comment https://forums.phpfreaks.com/topic/248347-creating-multidimensional-array-containing-database-output/#findComment-1275330 Share on other sites More sharing options...
cunoodle2 Posted October 3, 2011 Share Posted October 3, 2011 If there was a LIKE button for your comment I'd press it. Quote Link to comment https://forums.phpfreaks.com/topic/248347-creating-multidimensional-array-containing-database-output/#findComment-1275333 Share on other sites More sharing options...
Zerpex Posted October 3, 2011 Author Share Posted October 3, 2011 Hmm okay, I can see, that when I use the $items and foreach to echo out the checkboxes, it doesn't even work.. Hmmz gotta find a better way, with a starting point of my static checkboxes Quote Link to comment https://forums.phpfreaks.com/topic/248347-creating-multidimensional-array-containing-database-output/#findComment-1275337 Share on other sites More sharing options...
Zerpex Posted October 3, 2011 Author Share Posted October 3, 2011 Okay, so at the moment, I've tried out this code: <?php error_reporting(-1); mysql_connect('localhost', 'user', 'pass') or die(mysql_error()); mysql_select_db('wafcalculator') or die(mysql_error()); $query = "SELECT title, brand, points, brands.id FROM titles INNER JOIN categories ON titles.categoryid = categories.categoryid INNER JOIN brands ON brands.categoryid = categories.categoryid"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<h2>{$row['title']}</h2>"; foreach ($row['id'] as $int) { echo '<label class="checkbox"><input type="checkbox" name="totals['.$int.'][]" value="'.$row['points'].'"> '.$row['brand'].'</label>'; $int++; } //echo "{$row['brand']} "; //echo "{$row['points']} "; //echo "{$row['id']} "; //echo '<br />'; } ?> Only problem is that I'm getting an "invalid argument" on the foreach :S Quote Link to comment https://forums.phpfreaks.com/topic/248347-creating-multidimensional-array-containing-database-output/#findComment-1275358 Share on other sites More sharing options...
Drummin Posted October 3, 2011 Share Posted October 3, 2011 I had made this earlier today based on your first post and not knowing your table name. Yes is has the dreaded double query (oh my) but it worked when I tested it. I see now that you've had some help and have also done some yourself. Might help anyway. //Assuming you wish do something with the posted results IF (isset($_POST['submit'])){ foreach ($_POST as $k => $v){ IF ($k!="submit"){ echo "Selected item :$k Point Value:$v<br />"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </style> </head> <body> <?PHP //print_r($_POST); //I don't use '.$_SERVER['PHP_SELF'].' echo '<form method="post" action="index.php">'; $getcategories = mysql_query("SELECT DISTINCT(title) FROM items ORDER BY title ASC"); while($gtcategories = mysql_fetch_array($getcategories)){ $category=$gtcategories['title']; echo "<h2>$category</h2>\n"; $getitems = mysql_query("SELECT id,title,brand,point FROM items WHERE title='$category' ORDER BY brand ASC"); while($items = mysql_fetch_array($getitems)){ $itemid=$items['id']; $itemtitle=$items['title']; $itembrand=$items['brand']; $itempoint=$items['point']; echo "<label class='checkbox'><input type='checkbox' name='$itemid' value='$itempoint'>$itembrand</label>\n"; } } echo "<br /><input type=\"submit\" name=\"submit\" value=\"Submit\" />"; echo "</form>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/248347-creating-multidimensional-array-containing-database-output/#findComment-1275433 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.