Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,383 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,115 people online right now. Registration is fast and FREE... Join Now!




recursive loopback with mysql

 
Reply to this topicStart new topic

recursive loopback with mysql

didgy58
8 Oct, 2008 - 05:06 AM
Post #1

D.I.C Head
**

Joined: 23 Oct, 2007
Posts: 170



Thanked: 1 times
My Contributions
Hi all got a little trouble trying to get my head around something,

im creating a webshop and at the moment im getting a little brain tied and stuck it goes a little something like this

a user of the cms can input categories, now there are 5 main categories, this will never change, and each on of these categories can have sub categories and within these sub categories is where the products will stay. now of course a sub category can exist across different categories, and here lies one of the problems, as this is the first time out cms in house has had to come across this we havent had time to readjust this, so for me to enter a subcategory across 2 different main categories i have to create two versions of the sub instead of a nice little checkbox where i could just choose which one it belongs to,

i can get over this for now its just majorly time consuming inputting around 30 sub categories that most belong to 4 out of the 5 categories, and it really isnt how it should be done.

now if a person wants to search by one of those main categories i need it to putt all bottom level of the sub categories that belong to it, not the individual products as thats the next click through, but i would like it to list the bottom level sub category and show an image of this, so i guess i need to scan through the whole categories in the db and then find the bottom level one that belongs to say category cat.

so ive been trying to work my head around a nice way of doing this. i must admit i sometimes struggle turning things into reusable functions, which i believe this is what this should be

so this is what ive got so far and it works, but i mean it doesnt seem like its going to be fast when i fill the thing with loads more categories etc
CODE

#as we are searching by sector the first thing we need to do is to pull all the subcategories of that sector out and show them, so for instance
  #tactical has sub categories of MJM, PPE we need to show these with a small description of tactical and the cats under
  #from there they click into the product list page and then into individual pages.
  #first thing is to loop through all the categories and then we can work through them all and find the relevant bottom level cats
  $categories = array ();
  $q = "SELECT * FROM shop_categories";
  $x = $db->Select($q);

  if ($x){
    #if there are some results we will loop through them all and add them to an array
    foreach ( $x as $category){
          if ($category['parent'] != '0' ){
            #the parent category will only be 0 if its a top level category

          if ($category['parent'] == $_GET['parent']){
          #if the category parent equals the current parent then add it to the categories array
                $categories[] = $category['id'];
          } #end of if category = parent
          else {
          #next thing to do is go through and find the higher level category they belong to until they reach the parent then add them
          $q = "SELECT * from shop_categories where id = ".$category['parent']."";
          $x = $db->Select($q);
          if ($x) {

          #if there is a match then we have found the level above so need to track back until parent = 0
                foreach ($x as $uplevel){

                 $q = "SELECT * from shop_categories where id = ".$uplevel['parent']."";
                  $x = $db->Select($q);
                  if ($x) {

                      #if we get this far then we are at the bottom level
                      $categories[] = $x[0]['id'];
                  }
                  }
          }
                  else {
                  # there are not subs after this and we have reached the parent category
                        $categories[] = $uplevel['id'];
                        }
                }
          }
           }



    foreach ($categories as $product){
      echo $product;
      }
  }


now i look at this right now and i think ohh my god its ugly!! if i had a child like this i wouldnt know what i would do with it!! lol. kidding.

but i hope you can understand what im trying to do here, and if anybody might have a better idea of traversing up the tree then please let me know.

thanks

Dan
User is online!Profile CardPM
+Quote Post

didgy58
RE: Recursive Loopback With Mysql
8 Oct, 2008 - 07:38 AM
Post #2

D.I.C Head
**

Joined: 23 Oct, 2007
Posts: 170



Thanked: 1 times
My Contributions
ok i got the code wrong and ive fixed it to work, but it has its limitation, it only goes back 2 levels now this is where i would love to be able to convert it into a reusable function that i can call within it and reuse, but im finding it very difficult to do this

CODE

$categories = array ();
  $q = "SELECT * FROM shop_categories";
  $x = $db->Select($q);

  if ($x){
    #if there are some results we will loop through them all and add them to an array
    foreach ( $x as $category){
          if ($category['parent'] != '0' ){
            #the parent category will only be 0 if its a top level category

          if ($category['parent'] == $_GET['parent']){
          #if the category parent equals the current parent then add it to the categories array
                $categories[] = $category['id'];
          } #end of if category = parent
          else {
          #next thing to do is go through and find the higher level category they belong to until they reach the parent then add them
          $q = "SELECT * from shop_categories where id = ".$category['parent']."";
          $x = $db->Select($q);
          if ($x) {

           $currentitem = $category['id'];
           if ($x[0]['parent'] == '0'){

           }
           else {
          #if the parent isnt 0 we need to find the level back
                foreach ($x as $uplevel){

                  $q = "SELECT * from shop_categories where id = ".$uplevel['parent']."";
                  $x = $db->Select($q);
                  if ($x) {
                      if ($x[0]['parent'] == '0'){
                        }
                        else {
                      #i think we need to step again here, but i need to make this into a recursive function that i can reuse

                      #if we get this far then we are at the bottom level, so add the current item variable to it
                      $categories[] = $currentitem;
                      }
                  }
                  }
                  }
          }
                  else {
                  # there are not subs after this and we have reached the parent category
                        $categories[] = $uplevel['id'];
                        }
                }
          }
           }




  }
  else {
    echo "<h1>No Current Categories</h1>";
    }


i guess im not sure where i would go about doing it.
User is online!Profile CardPM
+Quote Post

CTphpnwb
RE: Recursive Loopback With Mysql
8 Oct, 2008 - 07:41 AM
Post #3

D.I.C Regular
***

Joined: 8 Aug, 2008
Posts: 399



Thanked: 24 times
My Contributions
It would be helpful to know what your database structure looks like. What tables are we dealing with, and what are the columns?



User is offlineProfile CardPM
+Quote Post

DilutedImage
RE: Recursive Loopback With Mysql
8 Oct, 2008 - 01:55 PM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Nov, 2006
Posts: 642



Thanked: 6 times
Dream Kudos: 25
My Contributions
The PHP documentation has a sample of how to create a recursive function. See example 4.

As for storing the data, you've got several options. Your parent field could contain a comma-delimited list of parent categories, which is queried using a LIKE clause, and then parsed out in PHP. This is a rather resource-intensive approach though.

A better approach would be to ditch the parent field, and create a new table for storing category relationships. It could be a simple two-field table (cat_id, parent_id), with a single entry for each relationship. While it won't eliminate the need for a row for every relationship, it will eliminate the need to make category updates to all of the rows. And with a little coding, creating the relationship entries could be a simple matter of checking some boxes in an HTML form.


User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Recursive Loopback With Mysql
8 Oct, 2008 - 04:25 PM
Post #5

D.I.C Regular
***

Joined: 8 Aug, 2008
Posts: 399



Thanked: 24 times
My Contributions
QUOTE(DilutedImage @ 8 Oct, 2008 - 02:55 PM) *
A better approach would be to ditch the parent field, and create a new table for storing category relationships. It could be a simple two-field table (cat_id, parent_id), with a single entry for each relationship.

Here's an example I wrote recently that demonstrates what DilutedImage is talking about. I use four fields in the second table (custom_fields) instead of two, but that's so I can sort the fields according to different criteria.

The code seems long, but that's because it creates and populates the tables before sorting them. Don't let that scare you!
CODE
<html>
<head>
<title>An Addressbook Example</title>
<link rel="stylesheet" type="text/css" href="my.css" />
</head>
<?php
session_start();
if ($_POST['destroy'] == "Yes")
    {
    session_unset();
    session_destroy();
    session_start();
    }

if ($_POST['state'] == "up")
    {
    $ch1 = 'checked="checked"';
    } else
    {
    $ch2 = 'checked="checked"';
    $thedir="DESC";
    }
    
if ($_POST['UName'] != '')
    {
    $uname=escape($_POST['UName']);
    $pswd=escape($_POST['pwd']);
    } else
    {    
    if ($_SESSION['UName'] != '')
        {
        $uname=$_SESSION['UName'];
        $pswd=$_SESSION['pwd'];
        } else
        {
        mylogin();
        }
    }

if ($uname != '')
    {
    $newaddresses = new Addresses();
    $newaddresses->setup($uname,$pswd);
    if ($newaddresses->connected)
        {
        echo "<h1>Note that subgroups and their contents are sorted too, but the contents are sorted according<br>to the field_position field in the database, allowing field positions to be moved at will.</h1>";
        ?>
        <form method="post" action="<?php echo $php_self ?>">
        <input type="radio" name="state" value="up" <?php echo $ch1; ?> onclick="this.form.submit();">Sort in Ascending order <br>
        <input type="radio" name="state" value="DESC" <?php echo $ch2; ?> onclick="this.form.submit();" >Sort in Descending order
        </form>
        
        
        <form method="post" action="<?php echo $php_self ?>">
        Reset session<input type="radio" name="destroy" value="Yes" onclick="this.form.submit();" >Yes
        <input type="radio" name="destroy" value="No" checked="checked" onclick="this.form.submit();">No <br>
        </form>

        
        <?php
        $newaddresses->sortbylastname($thedir);
        } else
        {
        mylogin();
        }
    }
    
    
$_SESSION['UName']=$uname;
$_SESSION['pwd']=$pswd;
?>
</body>
</html>
<?php
function mylogin()
    {
    ?>
    <form action="<?php echo $php_self ?>" method="post">
    MySQL User Name: <input type="text" size= 15 name="UName"  />
    Password: <input type="password" size= 15 name="pwd"  />
    <input type="submit" value="Submit" />
    </form>
    <?php
    }

function escape($values)
{
if(is_array($values))
    {
    $values = array_map(array(&$this, 'escape'), $values);
    } else
    {
    if ( !is_numeric($values) || $values{0} == '0' )
        {
        $values = mysql_real_escape_string($values);
        }
    }
return $values;
}

class Addresses
    {
        var $server="127.0.0.1";
        var $mydatabase="MyContacts";
        var $mytable="addresses";
        var $myfields="custom_fields";
        var $myindex="id";
        var $myfieldindex="contact_id";
        var $Firstname;
        var $Lastname;
        var $addr1;
        var $addr2;
        var $city;
        var $thestate;
        var $zip;
        var $fields;
        var $fieldcount;
        var $connected= false;
                
    function setup($username,$password)    
        {
        $link = mysql_connect($this->server, $username, $password);// or die(mysql_error()) or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());;
        $sel= mysql_select_db($this->mydatabase);// or die(mysql_error()) or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
        if ($link && $sel)
            {
            $this->connected=true;
            }
        if ($link && !$sel)
            {
            
            $query ="create database ".$this->mydatabase;
            mysql_query($query);
            mysql_select_db($this->mydatabase);

            $query = "CREATE TABLE ".$this->mytable." ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first_name VARCHAR(25), last_name VARCHAR(25), addr1 VARCHAR(25), addr2 VARCHAR(25), City VARCHAR(25), theState VARCHAR(25), Zip VARCHAR(25)) Type=InnoDB";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());

            $query ="CREATE TABLE ".$this->myfields." (contact_id INT NOT NULL, field_name VARCHAR(20) NOT NULL, field_value VARCHAR(50) NOT NULL DEFAULT '', field_position INT NOT NULL, field_kind VARCHAR(20), PRIMARY KEY (contact_id, field_name), FOREIGN KEY (contact_id) REFERENCES addresses(id) ON UPDATE CASCADE ON DELETE CASCADE) type=InnoDB";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $q = "Insert into ".$this->mytable." (first_name, last_name, addr1, addr2, City, theState, Zip) values (";
            $query = $q."'Joe','Smith','111 Main Street','Apt 1','Middletown','CT','06457')";
                mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . ysql_errno() . ") " . mysql_error());
            $query = $q."'Jane','Doe','222 Oak St','Unit A','Middlefield','CT','06455')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'Mary','Johnson','443 Elm','','Durham','NC','12234')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());

            $q = "Insert into ".$this->myfields." (contact_id, field_name, field_value, field_position, field_kind) values (";
            $query = $q."'1', 'Cell Phone', '454-1212', '2', 'Phone')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'1', 'Disposable email', 'somebody@somewhere.com', '2', 'Email')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'1', 'Home email', 'joe@aol.com', '2', 'Email')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'1', 'Home Phone', '555-1212', '1', 'Phone')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'1', 'Work email', 'Joe@somewhere.com', '1', 'Email')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'2', 'Home Phone', '555-333', '2', 'Phone')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'2', 'Work email', 'jane@jane.com', '1', 'Email')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'3', 'Home Phone', '555-1234', '1', 'Phone')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            $query = $q."'3', 'Work Phone', '888-1212', '2', 'Phone')";
            mysql_query($query)or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
            }
        }
    function ReadContent($row,$direct)
        {
            $this->Firstname = $row['first_name'];
            $this->Lastname = $row['last_name'];    
            $this->addr1 = $row['addr1'];
            $this->addr2 = $row['addr2'];
            $this->city = $row['City'];
            $this->thestate = $row['theState'];
            $this->zip = $row['Zip'];

            $query = "SELECT * FROM $this->myfields where $this->myfieldindex = ".$row['id']." order by field_kind ".$direct.", field_position ".$direct;
            $ndbdata= mysql_query($query) or die(mysql_error());
            $i=0;
            while ($row2 = mysql_fetch_array($ndbdata))
            {
                $this->fields[$i][3]=$row2['field_kind'];
                $this->fields[$i][2]=$row2['field_position'];
                $this->fields[$i][1]=$row2['field_value'];
                $this->fields[$i][0]=$row2['field_name'];
                $i++;
            }
            $this->fieldcount = $i;        
        }
        
    function Displaycontact()
        {
        $st="<h1>".$this->Firstname." ".$this->Lastname."</h1><body>".$this->addr1."<br>".$this->addr2."<br>".$this->city.", ".$this->thestate." ".$this->zip."<br>";
        $st=str_ireplace("<br><br>","<br>",$st);
        echo $st;
        $G=$this->fields[0][3];
        echo "<h2>Group: ".$G."</h2>";
        for ($i=0; $i<$this->fieldcount; $i++)
            {
            if ($this->fields[$i][3] != $G)
                {
                $G=$this->fields[$i][3];
                echo "<h2>Group: ".$G."</h2>";
                }
            echo $this->fields[$i][0].": ".$this->fields[$i][1]."<br>";
            }
            echo "<br>---End of Contact ---<br>";
        }
    function sortbylastname($direction)
        {
        $sortquery = "SELECT * FROM $this->mytable order by last_name ".$direction;
        $sortrow = mysql_query($sortquery) or die(mysql_error());
        while ($myrow = mysql_fetch_array($sortrow))
            {
            $this->ReadContent($myrow, $direction);
            $this->Displaycontact();
            }
        
        }
    }

?>


This post has been edited by CTphpnwb: 8 Oct, 2008 - 04:25 PM
User is offlineProfile CardPM
+Quote Post

didgy58
RE: Recursive Loopback With Mysql
9 Oct, 2008 - 03:12 AM
Post #6

D.I.C Head
**

Joined: 23 Oct, 2007
Posts: 170



Thanked: 1 times
My Contributions
ok its dealing with one table, and the table has the following fields

CODE

id
name
about
intro
rank
parent
visible
thumbnail
metak
metat
meatd



it just seems im doing the loop over and over, ive now got it to pull all the bottom level categories for each main category, which is what i wanted it to do, but it does look like too much code if im honest and reading through it there must be a better way of doing this, as you can tell im not the best at refactoring my code at the moment, im just getting my head around OOP, so this is what i have got

CODE

<?php switch ($_GET['sector']){
        case "tactical":
          $color='#045827';
          break;
          case "commercial":
          $color='#6F1461';
          break;

          case "adventure":
          $color='#97151D';
          break;

          case "maritime":
          $color='#023A69';
          break;

          case "civil":
          $color='#0893B2';
          break;

          } ?>
<?php   $q="SELECT * FROM shop_categories where name='".ucfirst($_GET[sector])."'";
        $c=$db->Select($q);
        echo "<img src='".ROOT."images/categories/".$c[0]['thumbnail']."' alt='' title='' style='float:right' />"; ?>
<h2>Sector - <span style="color:<?=$color?>"><?=$_GET['sector']?></span></h2>

      <?php
        echo $c[0]['intro'];

?>
<div id="breaker"></div>

<?php
if (($_GET['record']=='0') || (!isset($_GET['record']))){

  #as we are searching by sector the first thing we need to do is to pull all the subcategories of that sector out and show them, so for instance
  #tactical has sub categories of MJM, PPE we need to show these with a small description of tactical and the cats under
  #from there they click into the product list page and then into individual pages.
  #first thing is to loop through all the categories and then we can work through them all and find the relevant bottom level cats


  $q = "SELECT * FROM shop_categories";
  $x = $db->Select($q);

if ($x){
#if there are some results we will loop through them all and add them to an array
    foreach ( $x as $category){
          #the parent category will only be 0 if its a top level category so no need to add
          if ($category['parent'] != '0' ){

            if ($category['parent'] == $_GET['parent']){

                #if the category parent equals the current parent then we need to see if it has any children if it doesn't add it, else don't and loop down
                $q = "SELECT id from shop_categories where parent = '".$category['id']."'";
                $x = $db->Select($q);

                if ($x) {
                        #dont add it as it has children
                        }
                else {
                    #if the category parent equals the current parent and no children add it to the array
                    $categories[] = $category['id'];
                    }
            } #end of if category = parent
            else {

                #next thing to do is go through and find the previous level category they belong to until they reach the parent then add them
                $q = "SELECT * from shop_categories where id = ".$category['parent']."";
                $x = $db->Select($q);
                if ($x) {

                    $currentitem = $category['id'];
                    if ($x[0]['parent'] == '0'){

                          # this is the bottom category
                    }
                else { #for if $x

                #if the parent isnt 0 we need to find the level back
                foreach ($x as $uplevel){

                  $q = "SELECT * from shop_categories where id = ".$uplevel['parent']."";
                  $z = $db->Select($q);
                  if ($z) {
                      if (($z[0]['parent'] == '0') && ($z[0]['id'] == $_GET['parent'])){
                           $categories[] = $currentitem;
                      }

                  }
                }
                }
          }
          else {
                # there are not subs after this and we have reached the parent category
                $categories[] = $uplevel['id'];
          }
    }
}
}
$_SESSION['categories'] = $categories;
}
  else {
    echo "<h1>No Current Categories</h1>";
    }
   # print_r($_SESSION);
if ($_SESSION['categories']){
echo "<ul id='itemlist'>";

foreach ($_SESSION['categories'] as $product){

$q= "SELECT * FROM shop_categories where id = '".$product."'";
#$q="SELECT  *,shop_products.name as prodname FROM shop_products  JOIN shop_product_images on shop_product_images.shop_products_id=shop_products.id JOIN shop_products_cats on shop_products.id=shop_products_cats.products_id JOIN  shop_categories on shop_products_cats.products_categories_id = shop_categories.id where shop_categories.name ='".$_GET['sector']."' GROUP BY shop_products.id";
$x=$db->Select($q);
if ($x){
  //create the list of categories here and the image list below;

    foreach ($x as $item){
      echo "<a href='".ROOT."$item[name]/sector_$item[name]/page_product/product_$item[products_id]/index.html'><li>".$item['name']."</li></a>";
      }

      }

}
echo "</ul><br /><br />";
       }
      }
      #need to now use the array again and draw the images

//start of the displaying product Categories as images
/*$q= "SELECT * FROM shop_categories where parent = '".$_GET['parent']."'";
#$q="SELECT  *,shop_products.name as prodname FROM shop_products  JOIN shop_product_images on shop_product_images.shop_products_id=shop_products.id JOIN shop_products_cats on shop_products.id=shop_products_cats.products_id JOIN  shop_categories on shop_products_cats.products_categories_id = shop_categories.id where shop_categories.parent ='".$_GET['parent']."' GROUP BY shop_products.id ";
$x=$db->Select($q); */
$size=sizeof($_SESSION['categories']);
//find out how many pages we require
$pages=$size/9;
if (isset($_GET['record'])){
  $record = $_GET['record'];
  if ($record == 0){
    $end = 6;
    }
    else {
  $end = $record + 9;
        }
}
else {
    $record = 0;
    $end = 6;
}
$column = 0;
echo "<div class='row'>";
#loop through the array and pull the information
for ($i = $record; $i<$end; $i++){
  $q = "SELECT * FROM shop_categories where id = ".$_SESSION['categories'][$i]."";
  $x=$db->Select($q);
  if ($x){
    foreach ($x as $item){ ?>
        <?php if ($column == '3'){ ?>

        </div><div class='row'>

        <?php $column = 0; } ?>
            <?php #draw the information out onto the screen this being the titl and image associated with the section
            ?>
            <div class='product'>
            <h3><?=$item[name]?></h3>
            <a href='<?=ROOT?><?=$item['name']?>/sector_<?=$sector?>/page_product/product_<?=$item[products_id]?>/index.html'><img src="<?=ROOT?>images/categories/<?=$item['thumbnail']?>" alt="<?=$item['title']?>" title="<?=$item['title']?>"  style="height:135px;width:180px;"/></a>
        <?php
    }

?>

  <div class="sectors">
<?php
#now we need to pull the categories each one belongs to
echo  $q = "SELECT *
            FROM shop_categories where name LIKE '".$item['name']."'
            ";
$x = $db->Select($q);
if ($x){

#if there are some results we will loop through them all and add them to an array
    foreach ( $x as $category){
          #the parent category will only be 0 if its a top level category so no need to add
          if ($category['parent'] != '0' ){

            if (($category['parent'] == '1') || ($category['parent'] == '2') || ($category['parent'] == '3') || ($category['parent'] == '4') || ($category['parent'] == '5')){

                #if the category parent equals the current parent then we need to see if it has any children if it doesn't add it, else don't and loop down
                $q = "SELECT id from shop_categories where parent = '".$category['id']."'";
                $x = $db->Select($q);

                if ($x) {
                        #dont add it as it has children
                        }
                else {
                    #if the category parent equals the current parent and no children add it to the array
                        $categorylist[] = $category['parent'];
                    }
            } #end of if category = parent
            else {

                #next thing to do is go through and find the previous level category they belong to until they reach the parent then add them
                $q = "SELECT * from shop_categories where id = ".$category['parent']."";
                $x = $db->Select($q);
                if ($x) {

                    $currentitem = $category['id'];
                    if ($x[0]['parent'] == '0'){

                          # this is the bottom category
                    }
                else { #for if $x

                #if the parent isnt 0 we need to find the level back
                foreach ($x as $uplevel){

                  $q = "SELECT * from shop_categories where id = ".$uplevel['parent']."";
                  $z = $db->Select($q);
                  if ($z) {
                      if (($z[0]['parent'] == '0') && ($z[0]['id'] == $_GET['parent'])){
                           $categorylist[] = $z[0]['id'];
                      }

                  }
                }
                }
          }
          else {
                # there are not subs after this and we have reached the parent category
                $categorylist[] = $uplevel['parent'];
          }
    }
}
}
#loop through the category list and take out the repeats

  print_r($newcatlist);
#print_r($categorylist);
   foreach ($categorylist as $image){
     echo $image;
        switch ($image){
        case "1":
        $sectorz="Tactical & Security";
        $image="circletact.png";
        break;
        case "2":
        $sectorz="Private/Corporate";
        $image="circlepriv.png";
        break;
        case "3":
        $sectorz="Emergency Services";
        $image="circleemer.png";
        break;
        case "4":
        $sectorz="Adventure";
        $image="circleadv.png";
        break;
        case "5":
        $sectorz="Maritime";
        $image="circle.png";
        break;
        }
        }
        echo "<img src='images/framework/webshop/$image' alt='$sectorz' title='$sectorz' class='nobordercir' />";



}

/*$q="SELECT *
FROM shop_products_cats
JOIN shop_categories ON shop_categories.id = shop_products_cats.products_categories_id
JOIN shop_product_images
WHERE shop_products_cats.products_id = '".$item[products_id]."'
GROUP BY shop_products_cats.products_categories_id"; */


  ?>
  </div><p><span>&pound;<?=$item[price]?></span><a href='<?=ROOT?><?=$item['name']?>/sector_<?=$item['name']?>/page_product/product_<?=$item[products_id]?>/index.html'><img src="images/framework/webshop/side.png" alt="side Arrow" title="Side Arrow" class="noborder" /></a></p>
  </div>


<?
  }
$column++;
}
echo "</div>";
//if the size of the returned result is more than 6 then draw the navigation.
if ($size>6){  ?>

   <div id="navigation">
   <div class='block'>
   <?php
   //getting the correct color arrows depeding on sectors they are in
  switch ($sector){
    case "tactical":
    $prev='prevtact.png';
    $next='nexttact.png';
    break;

    case "civil":
    $prev='prevpriv.png';
    $next='nextpriv.png';
    break;

    case "commercial":
    $prev='nextemer.png';
    $next='prevemer.png';
    break;

    case "adventure":
    $prev='prevadve.png';
    $next='nextadve.png';
    break;

    case "maritime":
    $prev='prevmarit.png';
    $next='nextmarit.png';
    break;
    }
    ?>
   <?php if (!isset($_GET['record'])|| $_GET['record']!='0'  ){ ?>
   <?php
   if ($_GET['record']=='6'){
        $lastpage=$_GET['record']-6;
     }
     else {
        $lastpage=$_GET['record']-9;
       }     ?>
   <a class='empty' href='<?=ROOT?><?=$sector?>/sector_<?=$sector?>/page_sector/record_<?=$lastpage?>/parent_<?=$_GET['parent']?>/index.html' ><img src="<?=ROOT?>images/framework/webshop/<?=$prev?>" alt="Previous" title="Previous" /></a>
   <?} else {echo "&nbsp";}?>
   </div>
   <?php
   //loop to create the pagination for the sector products
   for ($i=0; $i<$pages; $i++){
   if ($i=='1'){
       $record = $i * 6;
   }
   else {
    $record = $i * 9;
    }
    $number = $i + 1;
    ?>
  <a class='boxes' href='<?=ROOT?><?=$sector?>/page_sector/sector_<?=$sector?>/record_<?=$record?>/parent_<?=$_GET['parent']?>/index.html' ><?=$number?></a>
<?} ?>
  <?php if ($_GET['record'] + $end < $size){    ?>
  <?php
  if (!isset($_GET['record']) || ($_GET['record']=='0')){
    $nextpage = $_GET['record'] + 6;
    }else {
    $nextpage = $_GET['record'] + 9;
  }?>
    <div class='block'><a class='empty' href='<?=ROOT?><?=$sector?>/page_sector/sector_<?=$sector?>/record_<?=$nextpage?>/parent_<?=$_GET['parent']?>/index.html' ><img src="<?=ROOT?>images/framework/webshop/<?=$next?>" alt="Next" title="Next" /></a></div>
  <?  }?>
</div>
  <?}

else {
  echo "No current Products";
  }
?>


dont shout at it now ok...
User is online!Profile CardPM
+Quote Post

CTphpnwb
RE: Recursive Loopback With Mysql
9 Oct, 2008 - 06:01 AM
Post #7

D.I.C Regular
***

Joined: 8 Aug, 2008
Posts: 399



Thanked: 24 times
My Contributions
Since you appear to only want the root of the lineage, why not store the information in the database? Every time you create a name, you store their parent, so you could easily store their root parent too.

CODE
id
name
about
intro
rank
parent
ROOT_PARENT
visible
thumbnail
metak
metat
meatd

User is offlineProfile CardPM
+Quote Post