944,118 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 10907
  • PHP RSS
Nov 13th, 2006
0

three dimensional arrays

Expand Post »
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...
php Syntax (Toggle Plain Text)
  1. $category = array($linens, $china, $kitchen, $lighting, etc...etc...etc...);
  2.  
  3. //example of element inside category
  4. $linens = array($napkins, $towels, $sheets, $drapes, etc...etc...etc...);
  5.  
  6. //example of element inside linens. these are associative arrays.
  7. $napkins = array("qtyReq" => $cmsVar1, "stillNeed" => $cmsVar2, "price" => $cmsVar3, etc...etc...etc...);
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?
Last edited by boo_lolly; Nov 13th, 2006 at 6:34 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
boo_lolly is offline Offline
35 posts
since Nov 2006
Nov 13th, 2006
0

Re: three dimensional arrays

Hi,

Just to be clear, a multidimensional array looks something like:

[PHP]$array = array(array(0, 1)); // 2 dimensional since you need two indexes to get the values eg: $array[0, 0];[/PHP]

I believe whats limiting the code right now is the notation of your arrays. (that may just be an example by the way, but anyways..) Since the arrays will be dynamic, you do not want to use a "static" notation such as:

[PHP]$array = array($item1, $item2);[/PHP]

In the above example:
This would assume that the array has a fixed length of 2 every time.
It also assumes that its an indexed array, with indexed 0 and 1.

IF you use something like:

[PHP]$array = array('item1'=>$item1, 'itme2'=>$item2);[/PHP]

Its even more static, since you're assuming the same for the first array, and also that the associative indexes have to be item1 and item2.

For dynamic arrays use the notation:

[PHP]$array = array(); // instantiate the array

$array[$index] = $value; // if you want to add a single dynamic index and value

// or as many as you need...[/PHP]

Since a database is dynamic, it will give you back a dynamic array. It does not need you to instantiate your arrays (like other programming languages). In php, $array[0] = 'soemthing'; inistantiates an array, and gives it the property 'something' with the index 0.

In short:


You do not need to define your arrays.
The database functions will return the arrays for you in the format you specify.
In fact (to my knowledge) mysql (and thus php) only returns 1 dimensional arrays, either associative or indexed numerically. (you can create multidimensional arrays out of this, but thats just complicating things).
(even using SELECT with JOIN or UNION will create new indexes on the same dimension of the 1-dimensional array)

see the mysql docs on PHP.net. http://us2.php.net/mysql

For returning numerically indexed arrays: http://us2.php.net/manual/en/functio...-fetch-row.php

Returning associative arrays: http://us2.php.net/manual/en/functio...etch-assoc.php

If you prefer objects: http://us2.php.net/manual/en/functio...tch-object.php
(returns the default php object (new stdClass())

If you really need to create multidimensional arrays out of the returned results, you can use the array functions: http://us3.php.net/array

Hope that helps. I'm not sure I got the question fully though...
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 14th, 2006
0

thanks for the reply

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.
php Syntax (Toggle Plain Text)
  1. //seach.php
  2. <form method="POST" action="results.php">
  3. Search Word: <input type="text" name="query">
  4. <input type="SUBMIT" value="Search">
  5. </form>
php Syntax (Toggle Plain Text)
  1. <?php
  2. //results.php rough draft
  3. //imagine each result is a link. if you click the link,
  4. //it will go to registry.php and a unique ID# will
  5. //be sent as well
  6.  
  7. $sql = mysql_query("SELECT column1, column2 FROM myTable WHERE column1 LIKE %$query% OR column2 LIKE %$query%") or die (mysql_error());
  8.  
  9. while(list($column1, $column2)=mysql_fetch_array($sql))
  10. {
  11. echo "Result: $column1, $column2 <br />";
  12. }
  13. ?>
php Syntax (Toggle Plain Text)
  1. <?php
  2. //registry.php
  3.  
  4. /* here i will write a function to connect to the correct SQL table using the newlywed's ID# as the locator.*/
  5.  
  6. $categories = array("Flatware", "China Room", "Crystal Room", "Kitchenware", "Silver, Stainless", "Pewter", "Serverware & Entertaining", "Gourmet", "Lighting", "Smells", "Paper Goods", "Baby & Kids",
  7. "Housekeeping", "Religious", "Holidays", "Bath", "Body & Fragrance", "Luggage & Accessories", "Mens", "Womens", "Jewelry", "Collectibles", "The Wall");
  8.  
  9.  
  10. /*unfinished array containing 23 'category' elements, and another 6 array elements within each category element.*/
  11. $twoDarray = array(array(......; 
  12.  
  13. for($c = 0; $c < count($categories); $c++)
  14. {
  15. $twoDarray[$c] = $categories[$c];
  16.  
  17. if(!$twoDarray[$c])
  18. {
  19. exit;
  20. }
  21. else
  22. {
  23. echo "<TABLE BORDER=1><TR><TH COLSPAN=7>Category: ". $categories[$i] ."</TH></TR>";
  24. 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>";
  25.  
  26. for($i = 0; $i < count($twoDarray[$c]); $i++)
  27. {
  28. if(!$twoDarray[$c][$i])
  29. {
  30. exit;
  31. }
  32. else
  33. {
  34. echo "<TR><TD>";
  35. echo $twoDarray[$c][$i];
  36. echo "</TD><TD>Add to Cart Button</TD></TR>";
  37. }
  38. }
  39. echo "</TABLE>\n\n";
  40. }
  41. }
  42.  
  43. ?>
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, digital-ether?
Last edited by boo_lolly; Nov 14th, 2006 at 10:04 am.
Reputation Points: 10
Solved Threads: 1
Light Poster
boo_lolly is offline Offline
35 posts
since Nov 2006
Nov 14th, 2006
0

Re: thanks for the reply

What is the structure of your database table(s)?


You don't need to create the arrays $twoDarray and $category.

You can just select this from the DB, uless the categories will never change..
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 14th, 2006
0

Re: thanks for the reply

What is the structure of your database table(s)?


You don't need to create the arrays $twoDarray and $category.

You can just select this from the DB, uless the categories will never change..
here's the thing. i'm also creating a CMS with this. the administrator will be able to go into a GUI that i created, and add, edit, delete, modify information. i believe i'm going to have to have one table in the DB for the search query. and another new table for each newlywed couple's registry. everytime a new couple is entered into the database by the administrator, it will give that couple a unique ID number (random) and it will automatically create a new table with that ID number as the name. the ID number will be in the searchtable, with the rest of the couple's information.

after the search is conducted and has returned results, a user can click a couple's name and they will be taken to registry.php. registry.php will then query the database for a table matching their ID# (which was sent to registry.php via $_POST) and output the information into a table, or however i see fit.

is this ok?

here's the new search.php
php Syntax (Toggle Plain Text)
  1. <!-- SEARCH.PHP -->
  2. <form method="POST" action="results.php">
  3. First Name:<input type="text" name="fname"><BR>
  4. Last Name:<input type="text" name="lname"><FONT COLOR="FF0000" SIZE="-1">(required)</FONT><BR>
  5. <SELECT NAME="regDate">
  6. <OPTION VALUE="">Select an Event Date
  7. <OPTION VALUE="">Month | Year
  8. <OPTION VALUE="">Month | Year
  9. <OPTION VALUE="">Month | Year
  10. <OPTION VALUE="">Month | Year
  11. <OPTION VALUE="">Month | Year
  12. </SELECT>
  13. <BR>
  14. <input type="SUBMIT" value="Search">
  15. </form>

here's the new results.php
php Syntax (Toggle Plain Text)
  1. <?php
  2. // results.php
  3. trim($lname);
  4. if (!$lname)
  5. {
  6. echo "<FONT COLOR="FF0000">You have not filled the required fields. Please try again.</FONT>";
  7. exit;
  8. }
  9.  
  10. @ $db = mysql_connect("host", "newlyWed_DB", "pass");
  11. if(!$db)
  12. {
  13. echo "Error: Could not connect to the database. Please try again later.";
  14. exit;
  15. }
  16.  
  17. $sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error());
  18. $result = mysql_query($sql);
  19. $num_result = mysql_num_rows($result);
  20.  
  21.  
  22. echo "Number of matches: ". $num_result ."\n";
  23.  
  24. if(!$result)
  25. {
  26. echo "Sorry, there were no matches for your query. Please try again.";
  27. }
  28. else
  29. {
  30. echo "<TABLE BORDER=1><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH>View Registry</TH></TR>";
  31.  
  32. for($i=0; $i < $num_result; $i++)
  33. {
  34. $row = mysql_fetch_array($result);
  35. echo "<TR><TD>". $row['brideFname'] ." ". $row['brideLname'] ."</TD><TD>". $row['groomFname'] ." ". $row['groomLname'] ."</TD><TD>". $row['eventDate'] ."</TD><TD>". $row['uID'] ."</TD></TR><br />";
  36. }
  37.  
  38. echo "</TABLE>";
  39. }
  40. mysql_close($db);
  41. ?>
Last edited by boo_lolly; Nov 14th, 2006 at 12:50 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
boo_lolly is offline Offline
35 posts
since Nov 2006
Nov 14th, 2006
0

Re: three dimensional arrays

i've got another question. if i have a SQL table with, i dunno, 7 columns in it. how do i write a script to categorize all the rows with the same name in their first column? and then write a loop to output the info into an HTML table, with the name as the title of the table, then mysql_fetch_array() the rest of the info in all of those rows. as many times as there are rows that match that first column. and then, once it's done with the first set, it will match another set of names in the first column, and do the same with those. until the last row has been called. then the loop stops.

i can write the output, no problem. but i need help writing a script to categorize the SQL rows into sets. the rows will be categorized together if the first column in their row matches another row's first column. i can pretty much do the rest myself. thanks.
Reputation Points: 10
Solved Threads: 1
Light Poster
boo_lolly is offline Offline
35 posts
since Nov 2006
Nov 14th, 2006
0

Re: three dimensional arrays

that helps a lot bro. but maybe i'm not explaining the issue properly. imagine a SQL table with 4 columns. the first column is named 'category'. the second is named 'item'. the other two, 'size', 'color'. (attributes of the item)

i want to write a script that will find all the matching categories, and output an HTML table with the title of the category, then list the items and attributes in that category. here's what the SQL table looks like...

Category Item Size Color
Plates Glass Medium Red
Linens Drapes Large Grey
Linens Bed Sheet Medium White
Plates Plastic Large White
Linens Napkins Small White

the content of the SQL table will NOT be in order. i want to write a loop of somekind (or nested loop) that will find all the categories, match them, then put out HTML tables as shown below.

Category: Plates
Item Size Color
Glass Medium Red
Plastic Large White

Category: Linens
Item Size Color
Drapes Large Grey
Bed Sheet Medium White
Napkins Small White

that's what i want the HTML tables to look like. does that make sense?
Last edited by boo_lolly; Nov 14th, 2006 at 3:45 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
boo_lolly is offline Offline
35 posts
since Nov 2006
Nov 14th, 2006
0

Re: three dimensional arrays

actually, i think i've got it. will somebody tell me if i got it right or if there are any problems with the code? thanks in advanced.
php Syntax (Toggle Plain Text)
  1. <?php
  2. //registry.php
  3.  
  4. @ $db = mysql_connect("host", "registryDB", "pass");
  5. if(!$db)
  6. {
  7. echo "Error: Could not connect to the database. Please try again later.";
  8. exit;
  9. }
  10.  
  11. $sql = mysql_query("SELECT * FROM ". $uID ." ORDER BY category") or die(mysql_error());
  12. $result = mysql_query($sql);
  13.  
  14. $num_rows = mysql_num_rows($result);
  15.  
  16.  
  17. if(!$result)
  18. {
  19. 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'
  20. }
  21. else
  22. {
  23. for($i = 0; $i < $numrows; $i == $i) //<-- no increment here. look inside while loop for increment
  24. {
  25. echo "<TABLE BORDER=1><TR><TH COLSPAN=7>Category: ". $row['category'] ."</TH></TR>";
  26. 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>";
  27.  
  28. while($row['category'] == $row['category']) //<-- pretty sure this will stop the while loop if it hits a new category. right?
  29. {
  30. 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>";
  31. $i++;
  32. }
  33. echo "</TABLE><br /><br />";
  34. }
  35. }
  36.  
  37. ?>
Last edited by boo_lolly; Nov 14th, 2006 at 4:39 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
boo_lolly is offline Offline
35 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: phpmyadmin
Next Thread in PHP Forum Timeline: Adding A Category





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC