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...

$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...);

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?

Hi,

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

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

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:

$array = array($item1, $item2);

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:

$array = array('item1'=>$item1, 'itme2'=>$item2);

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:

$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...

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/function.mysql-fetch-row.php

Returning associative arrays: http://us2.php.net/manual/en/function.mysql-fetch-assoc.php

If you prefer objects: http://us2.php.net/manual/en/function.mysql-fetch-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...

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.

//seach.php
<form method="POST" action="results.php">
Search Word: <input type="text" name="query">
<input type="SUBMIT" value="Search">
</form>
<?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 />";
}
?>
<?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 6 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 $twoDarray[$c][$i];
				echo "</TD><TD>Add to Cart Button</TD></TR>";
			}
		}
		echo "</TABLE>\n\n";
	}
}

?>

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?

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..

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

<!-- SEARCH.PHP -->
<form method="POST" action="results.php">
First Name:<input type="text" name="fname"><BR>
Last Name:<input type="text" name="lname"><FONT COLOR="FF0000" SIZE="-1">(required)</FONT><BR>
<SELECT NAME="regDate">
<OPTION VALUE="">Select an Event Date
<OPTION VALUE="">Month | Year
<OPTION VALUE="">Month | Year
<OPTION VALUE="">Month | Year
<OPTION VALUE="">Month | Year
<OPTION VALUE="">Month | Year
</SELECT>
<BR>
<input type="SUBMIT" value="Search">
</form>

here's the new results.php

<?php
// results.php
    trim($lname);
    if (!$lname)
    {
    echo "<FONT COLOR="FF0000">You have not filled the required fields. Please try again.</FONT>";
    exit;
    }

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

    $sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error());
    $result = mysql_query($sql);
    $num_result = mysql_num_rows($result);


    echo "Number of matches: ". $num_result ."\n";

    if(!$result)
    {
        echo "Sorry, there were no matches for your query. Please try again.";
    }
    else
    {
        echo "<TABLE BORDER=1><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH>View Registry</TH></TR>";
        
        for($i=0; $i < $num_result; $i++)
        {
        $row = mysql_fetch_array($result);
        echo "<TR><TD>". $row['brideFname'] ." ". $row['brideLname'] ."</TD><TD>". $row['groomFname'] ." ". $row['groomLname'] ."</TD><TD>". $row['eventDate'] ."</TD><TD>". $row['uID'] ."</TD></TR><br />";
        }
        
        echo "</TABLE>";
    }
    mysql_close($db);
?>

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.

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?

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
//registry.php
 
    @ $db = mysql_connect("host", "registryDB", "pass");
    if(!$db)
    {
    echo "Error: Could not connect to the database. Please try again later.";
    exit;
    }
    
    $sql = mysql_query("SELECT * FROM ". $uID ." ORDER BY category") or die(mysql_error());
    $result = 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
    {
        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. right?
            {
                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 />";
        }    
    }
 
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.