how to get row name from database to excel

Discussion in 'PHP' started by anisFarim, Jun 20, 2011.

  1. #1
    I can retrieve all the data from my table, but I want the title from first row on my table appear on excel too. Anyone can help me... TQVM

    
    
    <?php
    
    if (isset($_GET['evaluate']))
    {
    $stud_id=$_GET['evaluate'];
    		
    $db = mysql_connect("localhost","root") 
    	or die ("Could Not Connect to Server" . mysql_error());
    	
    /* select database */
    mysql_select_db("eol", $db)
    	or die ("Could Not Connect to Server" . mysql_error());	
    
    $query  = "SELECT * FROM evaluate WHERE stud_id = '$stud_id' ORDER BY specimen_no";
    $result = mysql_query($query) or die('Error, query failed');
    
    $tsv  = array();
    $html = array();
       
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
       $tsv[]  = implode("\t", $row);
       $html[] = "<tr><td>" .implode("</td><td>", $row) .              "</td></tr>";
    }
    
    $tsv = implode("\r\n", $tsv);
    $html = "<table>" . implode("\r\n", $html) . "</table>";
    
    $fileName = 'mysql-to-excel.xls';
    header("Content-type: application/vnd.ms-excel"); 
    header("Content-Disposition: attachment; filename=$fileName");
    
    echo $tsv;
    
    }
    
    ?>
    
    
    PHP:
     
    anisFarim, Jun 20, 2011 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2

    You are fetching as only MYSQL_NUM; you're removign the table name so just dont do that part. or specify assoc. The default is both so you should just be able to do something like this - not tested, theres probably more efficient ways of doing so....


    $count=0;
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
       if($count == 0) $data = array_keys($row);
       else	$data = array_values($row);
    
       $tsv[]  = implode("\t", $data);
       $html[] = "<tr><td>" .implode("</td><td>", $data) .              "</td></tr>";
    }
    PHP:
     
    lukeg32, Jun 21, 2011 IP
  3. anisFarim

    anisFarim Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks lukeg32. You give me an idea. I test your coding, but the every row in excel appear a title of first row in mysql. So I change it little and I get a very close what I want but still can't reach my goal. The coding like this:

    
    
    <?php
    if (isset($_GET['evaluate']))
    {
    $stud_id=$_GET['evaluate'];
    
    $db = mysql_connect("localhost","root") 
    	or die ("Could Not Connect to Server" . mysql_error());
    	
    /* select database */
    mysql_select_db("eol", $db)
    	or die ("Could Not Connect to Server" . mysql_error());	
    
    $flag = false;
    
    $query  = "SELECT * FROM evaluate WHERE stud_id = '$stud_id' ORDER BY specimen_no";
    $result = mysql_query($query) or die('Error, query failed');
    
    $tsv  = array();
    $html = array();
    
    while(false !== ($row = mysql_fetch_assoc($result)))
    {
       if(!$flag)
       {
        	$data = array_keys($row);
        	$flag = true;
       }
       
       else
       {
       		$data = array_values($row);
       }
    	
       $tsv[]  = implode("\t", $data);
       $html[] = "<tr><td>" .implode("</td><td>", $data) .              "</td></tr>";
    }
    
    $tsv = implode("\r\n", $tsv);
    $html = "<table>" . implode("\r\n", $html) . "</table>";
    
    $fileName = 'mysql-to-excel.xls';
    header("Content-type: application/vnd.ms-excel"); 
    header("Content-Disposition: attachment; filename=$fileName");
    
    echo $tsv;
    
    }
    ?>
    
    
    PHP:
    From this coding, the title appear but the data for first row not appear. It only appear for second data and so on. Do u have any idea... lukeg32
     
    anisFarim, Jun 21, 2011 IP
  4. india9

    india9 Member

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    thank lukeg32
    I need it
     
    india9, Jun 21, 2011 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Oh sorry - I must off an increment at the end of the while loop..... this should be it!

    $count=0;
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
       if($count == 0) $data = array_keys($row);
       else $data = array_values($row);
    
       $tsv[]  = implode("\t", $data);
       $html[] = "<tr><td>" .implode("</td><td>", $data) .              "</td></tr>";
      $count++;
    }
    PHP:
     
    lukeg32, Jun 21, 2011 IP
  6. anisFarim

    anisFarim Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    THANK YOU VERY MUCH lukeg32. It's work. I'm appreciated it. :)
     
    anisFarim, Jun 21, 2011 IP