rajesh Posted July 30, 2008 Share Posted July 30, 2008 <?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()); ?> Quote Link to comment Share on other sites More sharing options...
rajesh Posted July 30, 2008 Author Share Posted July 30, 2008 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); Quote Link to comment Share on other sites More sharing options...
rajesh Posted July 30, 2008 Author Share Posted July 30, 2008 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); Quote Link to comment Share on other sites More sharing options...
rajesh Posted August 1, 2008 Author Share Posted August 1, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 1, 2008 Share Posted August 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.