SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to save xml data to file

    I'm using PHP to extract data from a MySQL database. I am able to build an XML file how would I save the XML file to a directory on the server?

    below is the code




    <?php
    include("config1.php");

    $trimmed=$_POST['search1'];
    $from_date=$_POST['t1'];
    $to_date=$_POST['t2'];

    header("Content-type:text/xml");
    print("<?xml version=\"1.0\"?>");
    //define variables from incoming values



    $posStart = 0;
    $count = 300;
    //create query to products table

    $sql="select vehicle_id,pickup_date,booking_fair,booking_id,customer_name,customer_phno,pickup_point,drop_point,created_on from dispatcher_details
    where vehicle_id='$trimmed' AND created_on between '".$from_date."' AND '".$to_date."'";
    $sno=1;

    if($posStart==0){
    $sqlCount = "Select count(*) as cnt from ($sql) as tbl";
    $resCount = mysql_query ($sqlCount);
    $rowCount=mysql_fetch_array($resCount);
    $totalCount = $rowCount["cnt"];
    }
    //add limits to query to get only rows necessary for the output
    $sql.= " LIMIT ".$posStart.",".$count;
    //query database to retrieve necessary block of data
    $res = mysql_query ($sql);

    //output data in XML format
    print("<rows total_count='".$totalCount."' pos='".$posStart."'>");

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

    $n3=$row['created_on'];
    $cd=substr($n3,0,10);
    $ct=substr($n3,10,14);


    print("<row>");

    print("<cell>");
    print($sno++);
    print("</cell>");

    print("<cell>");
    print($row['vehicle_id']);
    print("</cell>");
    print("<cell>");
    print($row['pickup_date']);
    print("</cell>");
    print("<cell>");
    print($row['booking_fair']); //value for product name
    print("</cell>");

    print("<cell>");
    print($row['booking_id']); //value for internal code
    print("</cell>");

    print("<cell>");
    print($row['customer_name']); //value for internal code
    print("</cell>");

    print("<cell>");
    print($row['customer_phno']); //value for internal code
    print("</cell>");

    print("<cell>");
    print($row['pickup_point']);
    print("</cell>");
    print("<cell>");
    print($row['drop_point']);
    print("</cell>");
    print("<cell>");
    print($cd);
    print("</cell>");
    print("<cell>");
    print($ct); //value for internal code
    print("</cell>");

    print("</row>");
    }
    print("</rows>");



    ?>


    please help me out


    thanks

    MD. Samiuddin

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should replace print() with a variable (eg $xml) and then use file_put_contents to save it's content to a file.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should I replace all print with $xml

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you should do something like the following

    PHP Code:
    $xml = "<?xml version=\"1.0\"?>";
    while(
    $row mysql_fetch_array($res)) 
    {
        
    // ....

        
    $xml .= "<row>";

        
    $xml .= "<cell>";
        
    $xml .= $sno++;
        
    $xml .= "</cell>";
        
    // ...
    }
    file_put_contents("file.xml"$xml);

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried your code but file_put_contents is not writing the data to the file

    please help me out
    <?php
    include("config1.php");

    $trimmed=$_POST['search1'];
    $from_date=$_POST['t1'];
    $to_date=$_POST['t2'];

    header("Content-type:text/xml");
    $xml ="<?xml version=\"1.0\"?>";
    //define variables from incoming values



    $posStart = 0;
    $count = 300;
    //create query to products table

    $sql="select vehicle_id,pickup_date,booking_fair,booking_id,customer_name,customer_phno,pickup_point,drop_point,created_on from dispatcher_details
    where vehicle_id='$trimmed' AND created_on between '".$from_date."' AND '".$to_date."'";
    $sno=1;

    if($posStart==0){
    $sqlCount = "Select count(*) as cnt from ($sql) as tbl";
    $resCount = mysql_query ($sqlCount);
    $rowCount=mysql_fetch_array($resCount);
    $totalCount = $rowCount["cnt"];
    }
    //add limits to query to get only rows necessary for the output
    $sql.= " LIMIT ".$posStart.",".$count;
    //query database to retrieve necessary block of data
    $res = mysql_query ($sql);

    //output data in XML format
    $xml.= "<rows total_count='".$totalCount."' pos='".$posStart."'>";

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

    $n3=$row['created_on'];
    $cd=substr($n3,0,10);
    $ct=substr($n3,10,14);


    $xml.= "<row>";

    $xml.= "<cell>";
    $xml.= $sno++;
    $xml.= "</cell>";

    $xml.= "<cell>";
    $xml.=$row['vehicle_id'];
    $xml.= "</cell>";
    $xml.= "<cell>";
    $xml.= $row['pickup_date'];
    $xml.= "</cell>";
    $xml.= "<cell>";
    $xml.= $row['booking_fair']; //value for product name
    $xml.="</cell>";

    $xml.= "<cell>";
    $xml.= $row['booking_id']; //value for internal code
    $xml.= "</cell>";

    $xml.= "<cell>";
    $xml.= $row['customer_name']; //value for internal code
    $xml.= "</cell>";

    $xml.= "<cell>";
    $xml.= $row['customer_phno']; //value for internal code
    $xml.= "</cell>";

    $xml.= "<cell>";
    $xml.= $row['pickup_point'];
    $xml.= "</cell>";
    $xml.= "<cell>";
    $xml.= $row['drop_point'];
    $xml.= "</cell>";
    $xml.= "<cell>";
    $xml.= $cd;
    $xml.= "</cell>";
    $xml.= "<cell>";
    $xml.= $ct; //value for internal code
    $xml.= "</cell>";

    $xml.= "</row>";
    }
    $xml.= "</rows>";

    file_put_contents("project/reports/dispatcher_report.xml", $xml);

    ?>


    please help me out friend

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check that directory "project/reports/" exists and is writeable by the webserver.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •