Jump to content

Muddy_Funster

Members
  • Posts

    3,372
  • Joined

  • Last visited

  • Days Won

    18

Everything posted by Muddy_Funster

  1. got a bit more detail on what it is that you want to achieve?
  2. Quite coincidentaly I just finished making some functions to do something rather simmilar to this for my current project, and was just jumping on here to see if someone could refine it down enough to add it to the snipit/repo section of the forum. It's a bit clumsy compared to what Barand and the others can do, but it takes any number of parent->child associations and nests them appropriately. It's actualy designed to build a series of dynamicaly generated menus based on a given contaner div and the currently logged in users roll level (or group as I have chosen to call it). It's split over a few functions and I have commented it so that each step is prettey well broken down. I'm posting this on here for you and anyone else to use for free (that's free as in free beer). The comments should make the process fairly simple to follow and the code fairly easy to adapt as you need. Have a look, and good luck. <?php require_once 'db_con.php'; /* A series of functions to create a dynamicly generated, infinately recursive menu using a tree structure. This runs by getting data from table with a fairly standard layout of ______________________________________________________________________________ |FieldName | DataType | Note | |lID | int() | LinkId, PrimaryKey, Unsigned, AutoInc | |pID | int() | parentID, Unsigned, default of 0-(zero) for top level| |dest | varchar()| destination that the links points to | |txt | varchar()| Display Text for link | |title | varchar()| text for the title attribute of the anchor tag | |order | int() | display order, numbered from left to right** | |lnkStat | tinyint()| used to controll display state of link*** | |accessGrp | int() | used to determin what links are available by group | |contID | varchar()| text value of div the links are to be shown in**** | ------------------------------------------------------------------------------ ** order is a reserved word in MySQL, you may want to change this for your own tables *** I use a default value of 1, with 1 = active and 0 = inactive. This can be expanded uppon using a CASE in the SQL to, for example, change the destination of the link when set to three to a stock "under maintenance", or when -1 point the link to a default "under construction" page etc. **** This must include the # and . symbols for id and class names respectivly */ function getLinks($grp){ /* Creates a PDO conection to the database by calling my own custom builder function. Replace the first line with your own PDO connction details to connect and retrieave the list of links for the applicable account group sent in $grp. Package links into a muti-dim' array in format: containingDiv->parentID->linkID->{linkDisplayTxt, linkDestination, linkTitle, linkDisplayOrder} */ $con = pdoCon('nsite'); $sql = "SELECT contID, pID, lID, dest, txt, title, `order` FROM t_links WHERE lnkStat = 1 AND accessGrp IN(SELECT agID FROM t_map_lnk_access WHERE gID = :grp ) ORDER BY contID, pID, `order`, lID ASC"; $stmt = $con->prepare($sql); $stmt->bindParam(':grp', $grp, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach($result as $row){ $links[$row['contID']][$row['pID']][$row['lID']] = json_encode( array( "txt"=>$row['txt'], "dest"=>$row['dest'], "title"=>$row['title'], "ord"=>$row['order'] ) ); } return $links; } function buildLinks($links, $cont){ /* The function takes in the $links array produced by getLinks and the $cont string variable that relates to the containerDiv that you want to populate the links into, it then calls the nonRecurseLinks() function for the relevent div. This is called independantly of the getLinks() function to reduce needles database transactions. As long as you do not have too much in the way of links you can actualy add the $links array into the $_SEESION and carry it about for use between pages without needing to call the database again (larger datasets can be stored in browser storage using javascript as well, but that's off topic) */ foreach($links as $container=>$val){ if($container == $cont){ $pids=array_keys($val); $return = nonRecurseLinks(&$val, $pids); return $return; } } } function nonRecurseLinks($val, $pids){ /* Creates the top level menu items by calling prepLink() and builds the recursive menu items by calling recurseLink() whenever an item is found to have a parentID that is in the list. This does potentialy infinate recursion although I have only tested with a four deep nest i.e top->child1->child2->child3 and found that the raleationships were intact. $vals is the array of links that relate to the containerDiv and $pids is the array of parentIDs that exist for the links in $val. If there is no matching linkID for a given parentID then none of the links with that parentID will be # displayed. This lets you cut off a branch and not worry about any sub-branches showing up where they don't belong. */ $return = ""; foreach($val as $p=>$l){ ; foreach($l as $id=>$data){ if(in_array($id, $pids)){ $return .= prepLink($data); unset($val[$p]); $return .= recurseLink($id, &$val, $pids); } elseif($p == 0){ $return.=prepLink($data)."</li>"; } } } return $return; } function recurseLink($p, $val, $pids){ /* This recusively builds the child links from the top down. taking in the current parentID for the link in use, the full remaining array of links to display and the parentID array for all parentIDs. Each link is checked to see if its parentID is in the $pids array and if it is another level is added to the tree. Once a link has been resolved to it's final position it is sent out through the prepLink function and removed from the $val array. This prevents spurious instances of the links apearing under other parent headings or on the top level. */ $return = ""; foreach($val as $pr=>$l){ if($p == $pr){ $return .="<ul>\n\r"; foreach($l as $id=>$data){ if(in_array($id, $pids)){ $return .= prepLink($data); unset($val[$pr]); $return .= recurseLink($id, &$val, $pids); } elseif($p != 0){ $return.=prepLink($data); } } $return .= "</ul></li>"; } } return $return; } function prepLink($data){ /* directly applies the html unordered list and list item wrappers to each link element and returns the sting to the buildLinks() function. */ $return = ""; $lob = json_decode($data); $return .="<li><a href='{$lob->dest}' title='{$lob->title}'>{$lob->txt}</a>\n\r"; return $return; } ?>
  3. you still don't seem to be setting any content-type in the mail header. also, unless $_POST[message'] contains the standard <html><head><body> tag layout as well as your array data, you are also still not building the page properly. Please use code tags when posting code in the forum, and try to avaid linking to files as most people will not follow links from stangers.
  4. it's not just an array, it's a combination of objects and arrays. You can manipulate and stylise these as you would any other object or array values before doing any output.
  5. as well as the above, your logic checks to see if the value of $_POST['shape'] is set or is not an empty string, this will never fire, as you will always have a value in it, even if it is "no", you need to either change your form value to be "" for the default, or change your logic to check for != "no" or the WHERE is going to include WHERE shape = "no" and I would like to assume that's not going to return any results.
  6. I dont see anything in that code that sets the content type as html, or anything else for that matter. You don't show any other html deffinitions or tags. If you want to send html mails you need to set the content-type and form the page properly. Edt: typo
  7. $clt is the name of the variable, you still havn't clarified what you meen by "text field". Oh, and please don't use SELECT *
  8. could you define "text fields"?
  9. and the code is......?
  10. I'm not a big fan of online tests - for anything. If you actualy want help you will need to give a full and proper desciption of the problem whcih covers all aspects of what is going wrong as well as a description of each of the elements used in the process, such as the database column datatypes. any and all Error/Warning/Notice messages should also be included.
  11. please post up your actual code. people on this site (myself included) generaly avoid clicking on links that others whome we do not know post up. oh, and make sure you use the code tags to enclose the code that you post, to make everyones life easier.
  12. and while you define not working, you may also want to define $tbl_name....
  13. yeah, you're probbly right on that one
  14. last line should read WHERE flav IN(cafelatte,chocolate,cremebrulee)"
  15. remeber to use descriptive names! good luck
  16. you need to use a unique identifier in the where clause to update a single record or else every record will be updated. some examples of uptate syntax: UPDATE table SET name='muddy_funster' WHERE id = 1; UPDATE table SET name='muddy_funster' WHERE id = (SELECT id from table as t where email = 'muddy_mail@your.net'); UPDATE table INNER JOIN table2 ON (table.id=table2.id) SET table.value = (table2.value / 100) * 10; does that help any?
  17. not really, no. What is it you want to do (and why do you have a box on your head? )?
  18. nest another loop and use array_serach() to check the values, count the results found and error on condition of count
  19. to delete : DELETE FROM <tablename> WHERE <value> = <condition> and to change a record UPDATE <tablename> SET <columnName> = <value> WHERE <anotherValue> = <condition>
  20. you would o the trim in the Javascript : success: function (result){ var myResult = trim(result); alert(myResult); //<---test to see what's actualy coming back from the php page, delete this line when all is working modal.......//<<the rest of your code
  21. could possably be a white space issue - try trimming the result variable before checking it
  22. That normally happens when you try to put a string value into an int field which has a default value of 0. check the datatype and the variable value being inserted.
  23. If that is indead a tab delimited file, I still think using the LOAD DATA IN FILE syntax within the MySQL database is the best way to do this.
  24. Yeah, let's see what you've got, that is taking waaaaaaayyyy too long, and to be honest I would have expected something to timeout by then.
  25. there's too much wrong with that script. I suggest you bin it and start again, paying attention to the followiing advice: Do NOT run queries inside loops Do not use SELECT * Learn about SQL JOIN syntax If after that you are still having problems, explain your desired outcome and post up your table structures along with updated code and we'll have another look.
×
×
  • 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.