Jump to content

view the category and subcategory in multidimensional array


rajesh

Recommended Posts

<?php

$con = mysql_connect("localhost","root","");

mysql_select_db("test");

 

 

function childVal($catId=0){

    $selectSql = "SELECT catId,catName,parentId from category  where parentId =$catId";

  $result= mysql_query($selectSql);

  if(mysql_num_rows($result)==0){

  return NULL;

  }

  else{

  while($row=mysql_fetch_array($result)){

  $arr = childVal($row["catId"]);

  if(!$arr){

  $categoryArray[$row["catId"]] = $row["catName"];

  }

  else{

  $categoryArray = array($row["catName"]=>$arr);

  }

    }

   

    return $categoryArray ;

    }

    }

   

 

    echo "<pre>";print_r(childVal());

?>

see the table structure

 

--

-- Table structure for table `category`

--

 

CREATE TABLE IF NOT EXISTS `category` (

  `catId` int(12) NOT NULL auto_increment,

  `catName` varchar(244) NOT NULL,

  `parentId` int(12) NOT NULL,

  PRIMARY KEY  (`catId`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

 

--

-- Dumping data for table `category`

--

 

INSERT INTO `category` (`catId`, `catName`, `parentId`) VALUES

(1, 'sports', 0),

(2, 'test', 0),

(3, 'tennis', 1),

(4, 'cricket', 1),

(6, 'paes', 3);

 

please find the updated coding

 

 

<?php

$con = mysql_connect("localhost","root","");

mysql_select_db("test");

 

 

function childVal($catId=0){

    $selectSql = "SELECT catId,catName,parentId from category  where parentId =$catId";

  $result= mysql_query($selectSql);

  if(mysql_num_rows($result)==0){

  return NULL;

  }

  else{

  while($row=mysql_fetch_array($result)){

  $arr = childVal($row["catId"]);

  if(!$arr){

  $categoryArray[$row["catId"]] = $row["catName"];

  }

  else{

  $categoryArray[$row["catName"]] = $arr;

  }

    }

   

    return $categoryArray ;

    }

    }

   

 

    echo "<pre>";print_r(childVal());

?>

 

 

/// Database details

 

INSERT INTO `category` (`catId`, `catName`, `parentId`) VALUES

(1, 'sports', 0),

(2, 'test', 0),

(3, 'tennis', 1),

(4, 'cricket', 1),

(6, 'paes', 3),

(7, 'sachin', 4);

 

 

First you rename the file  manuallly test.xls to  test.csv after use the below code to read the file content

 

<?php

$row = 1;

$handle = fopen("test.csv", "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

    $num = count($data);

    echo "<p> $num fields in line $row: <br /></p>\n";

    $row++;

    for ($c=0; $c < $num; $c++) {

        echo $data[$c] . "<br />\n";

    }

}

fclose($handle);

?>

 

After 4 posts you still havn't twigged that we havn't got a clue what you're asking?

 

While I commend the fact that you've posted your code and database structure (albeit, without the use of


tags), actually describing the problem is rather essential for us to be able to help you.

Archived

This topic is now archived and is closed to further replies.

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