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());

?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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