Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ol-li Subsystem

Status
Not open for further replies.

Nightsoft

Programmer
Aug 23, 2001
50
0
0
DK
Hi everybody.

Im trying to make a form with multiply inputs, and subinputs to the inputs. So far i've accomplished to make the dynamics in making the form. See temp. Test example...
My Structure for subgrouping is like this
Code:
ol
  li_1_1    First List item
    ol
      li_11_1   Sub of first, and first in its ol
        ol
         li_111_1 Sub of first, first, and first in its ol
        /ol
      li_11_2   Sub of first, second
        ol 
          li_112_1  sub of second li in the first sub
          li_112_2  sub of second li in the first sub          
        /ol
    /ol
  li_1_2    Second list item
  li_1_3    Third list item
/ol
I then have some hidden fields to hold the amount of list items in each Ol. not important... And some id's on ol

My problem is when I want to make a list of the already saved inventory.
Heres a look at how some inventory is stored in mysql db.
Id SubId ParentId Supposed List Item
47 1 47 li_1_1
48 1 48 li_1_2
49 1 49 li_1_3
50 2 47 li_11_1
51 2 47 li_11_2
52 3 51 li_112_1
53 3 51 li_112_2
54 3 50 li_111_1

I can't figure out how to sort them, or extraxt the data and make a list as shown above, from this way of saving the substructure.. I'll try to find someway to make the list, but for now I can't see a good dynamic way to do it.
Im using standard php 4 and mysql
Any help would be great.

Dennis

Machine code Rocks:)
 
Typically, this kind of thing happens via multiple queries to the database and recursion.

Your first order of business is to fix your database. All items with no parents should have the same value in their "ParentID" columns, typically 0 (zero), not their own ids. That way, you can perform a first query using a WHERE clause like "WHERE ParentID = 0".

After that, the display can be handled via a recursive function, which will be of the form (in pseudocodea):

[tt]function display_data ($parentid)
{
print <ol>
query database for all items with ParentId = $parentid
while (getting data from the query)
{
print <li>
print item value
display_data (<Id of current record>)
print </li>
{
print </ol>
}[/tt]

The function is called the first time with an input of zero (to match what you fixed before), then the function calls itself as needed.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi again.
It worked.!!! Thank you Speipnir.

This is my final code.. I had to put in an extra argument but it does what i want..

Code:
function display_data($strParentId,$strOlId)
{
  $strLiId = 1;     

  // query database for all items with ParentId = $strParentId
  $resData = mysql_query("SELECT * FROM TApartmentInv WHERE RoomId = ".$_SESSION["RId"] ." AND ParentId = $strParentId");
   // See if any items at all
   if (mysql_num_rows($resData) > 0)
   {
     $_SESSION["ItemsInSub"][$strOlId] = 0;		   
     print("<ol id=\"ol_$strOlId\">\n");	   
     while ($arrData = mysql_fetch_assoc($resData))
     {
       printf("  <li id=\"%d_%d\">",$strOlId,$strLiId);
       printf("    <input type=\"text\" name=\"Ids[%d_%d]\" id =\"input_%d_%d\" value=\"%s\" onFocus=\"document.RoomForm.InputInFocus.value = this.id;\" />",$strOlId,$strLiId,$strOlId,$strLiId, $arrData["InvName"]);
       print($arrData["Id"]) . "\n";
       display_data($arrData["Id"],$strOlId . $strLiId);
       print("  </li>\n");
       $strLiId++;
       // Register list items in ol
       $_SESSION["ItemsInSub"][$strOlId]++;
     } 

     print ("  </ol>\n");
   }
}

display_data(0,1); // display_data(ParentId, OlId)

See example if anyones interested.
See ya

Machine code Rocks:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top