Jump to content

3-dimensional arrays.


boo_lolly

Recommended Posts

let's just say that this page i'm writing is called 'registry.php'. this page will be more-or-less a template, however contents of this page will vary depending on one variable. the variable is the ID number of a newlywed couple (in the database).

the content of registry.php will contain tables. each table is titled a certain category, each category has items that fall under that category, and each item has attributes. that will always be showen on registry.php, but each newlywed couple has different amounts of different items, and i need to express that if a user happens to click on a certain newlywed couple on the previous page. 'The Smiths' link was clicked, therefore the contents of registry.php will loop through all the items that match their associated ID number.

so, i've got a few things i need to be able to cycle through on command, obviously. i've got a 3-dimensional array that looks something like this...
[code=php:0]
$category = array($linens, $china, $kitchen, $lighting, etc...etc...etc...);

//example of element inside category
$linens = array($napkins, $towels, $sheets, $drapes, etc...etc...etc...);

//example of element inside linens. these are associative arrays.
$napkins = array("qtyReq" => $cmsVar1, "stillNeed" => $cmsVar2, "price" => $cmsVar3, etc...etc...etc...);
[/code]
so, my question is, is there anyway to perform the same loop, or nested loops to loop through all of these arrays, but change the content of these arrays by changing one variable? (the unique ID number associated with each newlywed couple)

how would i design the structure of my arrays to allow this to happen? how would i design my MySQL tables in order to call these variables in the right fashion?
Link to comment
Share on other sites

Sounds like you might be in need of Nested Sets. [url=http://www.phpriot.com/d/articles/php/application-design/nested-trees-1/index.html]Here[/url] is about the most comprehensive tutorial I can find, good luck, its pretty full on stuff.
Link to comment
Share on other sites

Mysql tables

[pre]
category                                                couple
----------                                              ----------
cat_id  ---+                                      +---  couple_id
category    |                                      |    name
            |    subcategory                      |     
            |    ------------                      |     
            |    subcat_id  --+                  |
            |    subcategory  |                  |   
            +--  cat_id        |                  |
                              |    item          |
                              |    ----------    |
                              |    item_id        |
                              |    couple_id  ----+
                              +--- subcat_id
                                    qty_reqd
                                    still_need
                                    price
[/pre]
Link to comment
Share on other sites

so, i decided that it would be much better to use a two-dimensional array to handle this situation. i'm going to let the SQL database table that i call in the beginning of 'registry.php' to refer to the correct variables. so here we have 3 pages.
[code]
//seach.php
<form method="POST" action="results.php">
Search Word: <input type="text" name="query">
<input type="SUBMIT" value="Search">
</form>
[/code]

[code]
<?php
//results.php rough draft
//imagine each result is a link. if you click the link,
//it will go to registry.php and a unique ID# will
//be sent as well

$sql = mysql_query("SELECT column1, column2 FROM myTable WHERE column1 LIKE %$query% OR column2 LIKE %$query%") or die (mysql_error());

while(list($column1, $column2)=mysql_fetch_array($sql))
{
echo "Result: $column1, $column2 <br />";
}
?>
[/code]

[code]
<?php
//registry.php

/* here i will write a function to connect to the correct SQL table using the newlywed's ID# as the locator.*/

$categories = array("Flatware", "China Room", "Crystal Room", "Kitchenware", "Silver, Stainless", "Pewter", "Serverware & Entertaining", "Gourmet", "Lighting", "Smells", "Paper Goods", "Baby & Kids",
"Housekeeping", "Religious", "Holidays", "Bath", "Body & Fragrance", "Luggage & Accessories", "Mens", "Womens", "Jewelry", "Collectibles", "The Wall");


/*unfinished array containing 23 'category' elements, and another 7 array elements within each category element.*/
$twoDarray = array(array(......; 

for($c = 0; $c < count($categories); $c++)
{
$twoDarray[$c] = $categories[$c];

if(!$twoDarray[$c])
{
exit;
}
else
{
echo "<TABLE BORDER=1><TR><TH COLSPAN=7>Category: ". $categories[$i] ."</TH></TR>";
echo "<TR><TH>Item</TH><TH>Quantity Requested</TH><TH>Still Needs</TH><TH>Price</TH><TH>View</TH><TH>Quantity</TH><TH>Buy</TH></TR>";

for($i = 0; $i < count($twoDarray[$c]); $i++)
{
if(!$twoDarray[$c][$i])
{
exit;
}
else
{
echo "<TR><TD>";
echo $twDarray[$c][$i];
echo "</TD><TD>Add to Cart Button</TD></TR>";
}
}
echo "</TABLE><br /><br />";
}
}

?>
[/code]
this is more of the 'idea' behind what i want to do. what i'm most worried about is registry.php. does anybody see any problems with this code? will this do what i want it to? what do you think, barand?
Link to comment
Share on other sites

One of the reasons for using a database is so you don't have to hard-code array contents like that

[code]<?php

$sql = "SELECT c.category, s.subcat_id, s.subcategory
        FROM category c
        INNER JOIN subcategory s ON c.id = s.cat_id
        ORDER BY c.catname, s.subcategory";
$res = mysql_query($sql) or die(mysql_error());

$lastCat = '';
echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
while (list($cat, $sid, $subcat) = mysql_fetch_row($res)) {
    if ($lastCat != $cat) {
        echo "<tr><th colspan='2'>$cat</th></tr>\n";
    }
    echo "<tr><td>$subcat</td><td><a href='mypage.php?id=$sid'>Link</a></td></tr>\n";
    $lastCat = $cat;
}
echo "</table>\n";
?>[/code]
Link to comment
Share on other sites

what do you think about this jazz...
[code]
<?php
//registry.php

@ $db = mysql_connect("host", "registry_DB", "pass");
if(!$db)
{
echo "Error: Could not connect to the database. Please try again later.";
exit;
}

//newlywed's info
$sql = mysql_query("SELECT ". $uID ." FROM my_search_DB WHERE uID") or die(mysql_error());
$result = mysql_query($sql);

$row = mysql_fetch_array($result);

echo "<B>Bride and Groom's Name: </B>". $row['brideFname'] ." ". $row['brideLname'] ." & ". $row['groomFname'] ." ". $row['groomLname'] ."<br /><br />";
echo "<B>Event Date: </B>:". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."<br /><br />";
echo "<B>Preferred Shipping Address: </B>:". $row['ship_street'] .", ". $row['ship_city'] .", ". $row['ship_state'] .", ". $row['ship_zip'] ."<br /><br />";
mysql_close($sql);



$sql = mysql_query("SELECT * FROM ". $uID ." ORDER BY category") or die(mysql_error());
$row = mysql_query($sql);

$num_rows = mysql_num_rows($result);

if(!$result)
{
echo "There are no items in the registry for this couple at this time. Please try again later.";  //connect to search_DB to add newlywed's names instead of 'this couple'
}
else
{
$row = mysql_fetch_array($result);

for($i = 0; $i < $numrows; $i == $i) //<-- no increment here. look inside while loop for increment.
{
echo "<TABLE BORDER=1><TR><TH COLSPAN=7>Category: ". $row['category'] ."</TH></TR>";
echo "<TR><TH>Item</TH><TH>Quantity Requested</TH><TH>Still Needs</TH><TH>Price</TH><TH>View</TH><TH>Quantity</TH><TH>Buy</TH></TR>";

while($row['category'] === $row['category']) //<-- pretty sure this will stop the while loop if it hits a new category.
{
echo "<TR><TD>". $row['item'] ."</TD><TD>". $row['qty_req'] ."</TD><TD>". $row['still_need'] ."</TD><TD>". $row['price'] ."</TD><TD>". $row['view'] ."</TD><TD>Input Field</TD><TD>Add to Cart</TD></TR>";
                                $i++;
}
echo "</TABLE><br /><br />";
}
}
mysql_close($sql);
?>
[/code]
not bad huh?
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.