Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:35:15+00:00 2026-06-18T08:35:15+00:00

I’ve written a code that converts a mysql data into csv file. The problem

  • 0

I’ve written a code that converts a mysql data into csv file. The problem is that it shows result horizontally as

DATE        AUTHOR  LIKES   COMMENTS`
1/31/2013   WTF Facts   211 3
1/31/2013   WTF Facts   211 23
1/31/2013   WTF Facts   211 23
1/31/2013   WTF Facts   211 23
1/31/2013   WTF Facts   211 23

DATE        AUTHOR  LIKES   COMMENTS`
1/31/2013   WTF Facts   211 3
1/31/2013   WTF Facts   211 23
1/31/2013   WTF Facts   211 23
1/31/2013   WTF Facts   211 23
1/31/2013   WTF Facts   211 23

however i want it vertically as

DATE        AUTHOR  LIKES   COMMENTS`   DATE    AUTHOR     LIKES    COMMENTS`
1/31/2013   WTF Facts   211 3           1/31/2013   WTF Facts  211      23
1/31/2013   WTF Facts   211 23          1/31/2013   WTF Facts  211      23
1/31/2013   WTF Facts   211 23          1/31/2013   WTF Facts  211      3
1/31/2013   WTF Facts   211 23          1/31/2013   WTF Facts  211      23
1/31/2013   WTF Facts   211 23          1/31/2013   WTF Facts  211      23

Here is my code:

      <?php
      if(isset($_POST['submit'])):
        set_time_limit(99999999);
        ini_set('memory_limit', "9999M");
        $filename_csv = "export_".rand(1000000,9999999)."_".rand(1000000,9999999).".csv";
        $path = "serp_csvs_page/".$filename_csv;
        $fp = fopen($path, 'w');
        $page_ids = $_POST['page_id'];
        $date_from =  $_POST['date_from'];
        $date_to = $_POST['date_to'];

        foreach($page_ids as $page_id):
            $brline = array("\n");
            $page_name = get_page_name_by_id($page_id);
            $dates = checkCsvQuotes("DATE: FROM {$date_from} TO {$date_to}") ;
            $page_name = checkCsvQuotes("PAGE ID: {$page_name['page_id']}");
            $header = array($dates, "\n", $page_name);
            fputcsv($fp, $header);
            fputcsv($fp, $brline);
            $query = sprintf("SELECT post_date AS `DATE`, post_from AS `AUTHOR`, message AS `MESSAGE`, total_likes AS `TOTAL LIKES`, total_comments AS `TOTAL COMMENTS` FROM tbl_contents_pages WHERE `pid_id` = '$page_id' AND `post_date` >= '$date_from' AND `post_date` <= '$date_to'");
            $result = mysql_query($query, $con) or die(mysql_error($con));
            $rows = mysql_fetch_assoc($result);
            if ($rows) {
                $arr_key = array_keys($rows); 
                fputcsv($fp, $arr_key);
                while($rows = mysql_fetch_assoc($result))
                {
                    $date_row = checkCsvQuotes($rows['DATE']);
                    $author_row = checkCsvQuotes($rows['AUTHOR']);
                    $message_row = checkCsvQuotes($rows['MESSAGE']);
                    $likes_row = checkCsvQuotes($rows['TOTAL LIKES']);
                    $comments_row = checkCsvQuotes($rows['TOTAL COMMENTS']);
                    $arr_value = array();
                    $arr_value[] = $date_row;
                    $arr_value[] = $author_row;
                    $arr_value[] = $message_row;
                    $arr_value[] = $likes_row;
                    $arr_value[] = $comments_row;
                    //print_r($arr_value);
                    fputcsv($fp, $arr_value);
                }
            }
            else
            {
                $noData = array("DO RECORDS FOUND.");
                fputcsv($fp, $noData);
            }
            fputcsv($fp, $brline);
        endforeach;
        fclose($fp);
        echo "<a href=serp_csvs_page/".$filename_csv." class='btn btn-primary'> View File </a>";  
        endif;
      function get_page_name_by_id($id)
        {
            $query = mysql_query("SELECT page_id FROM tbl_pages WHERE `id` = '$id'");
            $result = mysql_fetch_assoc($query);
            return $result;
        }
    function checkCsvQuotes($string) 
    {
        if (strpos($string,'"') !== false) {
            return '"'.str_replace('"','""',$string).'"';
        } elseif (strpos($string,',') !== false) {
            return '"'.$string.'"';
        } else {
            return $string;
        }
    }
      ?>

Im not sure even if its possible, since im new with csv. I would greatly appreciate if you guys have any idea bout it and sharae with me. thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:35:16+00:00Added an answer on June 18, 2026 at 8:35 am

    You need to keep extending the array, not instantly spit the elements out. I’ve changed your code to do so, but as I’m unable to test it, it may very likely not work. Either way, compare the code to get the idea of what needs to be done.

    <?php
    if(isset($_POST['submit'])):
        set_time_limit(99999999);
        ini_set('memory_limit', "9999M");
        $filename_csv = "export_".rand(1000000,9999999)."_".rand(1000000,9999999).".csv";
        $path = "serp_csvs_page/".$filename_csv;
        $fp = fopen($path, 'w');
        $page_ids = $_POST['page_id'];
        $date_from =  $_POST['date_from'];
        $date_to = $_POST['date_to'];
    
        $data_ar = array(); //create a "master" array
        $i = $j = 0;
    
        foreach($page_ids as $page_id):
            $brline = array("\n");
            $page_name = get_page_name_by_id($page_id);
            $dates = checkCsvQuotes("DATE: FROM {$date_from} TO {$date_to}") ;
            $page_name = checkCsvQuotes("PAGE ID: {$page_name['page_id']}");
            $header = array($dates, "\n", $page_name);
            fputcsv($fp, $header);
            fputcsv($fp, $brline);
            $query = sprintf("SELECT post_date AS `DATE`, post_from AS `AUTHOR`, message AS `MESSAGE`, total_likes AS `TOTAL LIKES`, total_comments AS `TOTAL COMMENTS` FROM tbl_contents_pages WHERE `pid_id` = '$page_id' AND `post_date` >= '$date_from' AND `post_date` <= '$date_to'");
            $result = mysql_query($query, $con) or die(mysql_error($con));
            $rows = mysql_fetch_assoc($result);
            if ($rows) {
                $arr_key = array_keys($rows); 
                fputcsv($fp, $arr_key);
                $i = 0; //set to 0 on each iteration over $page_ids
                while($rows = mysql_fetch_assoc($result))
                {
                    $date_row = checkCsvQuotes($rows['DATE']);
                    $author_row = checkCsvQuotes($rows['AUTHOR']);
                    $message_row = checkCsvQuotes($rows['MESSAGE']);
                    $likes_row = checkCsvQuotes($rows['TOTAL LIKES']);
                    $comments_row = checkCsvQuotes($rows['TOTAL COMMENTS']);
                    if ($j === 0) //note: this assumes all files have the same amount of data
                        $data_ar[$i] = array();
                    $data_ar[$i][] = $date_row; //these will add to array data instead of creating new array rows
                    $data_ar[$i][] = $author_row;
                    $data_ar[$i][] = $message_row;
                    $data_ar[$i][] = $likes_row;
                    $data_ar[$i][] = $comments_row;
                    //print_r($arr_value);
                    $i++;
                }
            }
            else
            {
                $noData = array("DO RECORDS FOUND.");
                fputcsv($fp, $noData);
            }
            $j++;
        endforeach;
        for ($q = 0; $q < count($data_ar); $q++) { //insert data to file once the data has been gathered
            fputcsv($fp, $data_ar[$q]);
            fputcsv($fp, $brline);
        }
        fclose($fp);
        echo "<a href=serp_csvs_page/".$filename_csv." class='btn btn-primary'> View File </a>";  
        endif;
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I am currently running into a problem where an element is coming back from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am using JSon response to parse title,date content and thumbnail images and place

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.