Jump to content

Creating multidimensional array containing database output


Zerpex

Recommended Posts

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:

Rj0wQ.png

 

Thank you!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

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.