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 6371617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:06:04+00:00 2026-05-25T01:06:04+00:00

Okay, I’ve spent several hours on this problem and I’m not sure what’s going

  • 0

Okay, I’ve spent several hours on this problem and I’m not sure what’s going on. I think I just need a fresh perspective on this problem especially since I’ve been up for over 24 hours and the deadline for this is in five hours.

I am getting an Undefined offset notice for every single offset (0 to 907) when I try to use the data I pulled from a CSV. (It probably means I am not successfully pulling the data, but I am exhausted and would appreciate some help)

Does anyone know what I’m doing wrong?

$lines = array();
$lines2 = "";

$one = array();
$two = array();
$three = array();
$four = array();
$five = array();
$six = array();

$header = "";

$footer = "";

$countLines = 0;

/*
* Open the file and store its data into an array
*/
$fp = fopen('db.csv','r') or die("can't open file");

while($lines = fgetcsv($fp)) {

        for ($k = 0, $m = count($lines) - 1; $k < $m; $k++) {

            $one[$k] = $lines[0];
            $two[$k] = $lines[1];
            $three[$k] = $lines[2];
            $four[$k] = $lines[3];
            $five[$k] = $lines[4];
            $six[$k] = $lines[5];
    }
    $countLines++;

}




fclose($fp) or die("can't close file");


/*
* Set up file header
*/
$header = "Header"
    ;


/*
* Set up file footer
*/
$footer = "Footer";

/*
* Prepare data for export
*/
for ($i = 0, $j = $countLines - 1; $i < $j; $i++) {


            $lines2 .= $one[$i] ." ". 
            $two[$i] ." ". 
            str_pad($three[$i], 3) ." ". 
            str_pad($four[$i], 30) ." ". 
            str_pad($five[$i], 30) ." ". 
            str_pad($six[$i], 30) ."\r\n";

}

/*
* Store data in file
*/
$fp = fopen('db2.csv', 'w') or die("can't open file");

fwrite($fp, $header);

fwrite($fp, $lines2);

fwrite($fp, $footer);

fclose($fp) or die("can't close file");

The CSV file is a standard comma-delimited file so I don’t see any reason to post that data here.

  • 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-05-25T01:06:04+00:00Added an answer on May 25, 2026 at 1:06 am

    The statement

    while($lines = fgetcsv($fp)) {
    

    fgetcsv will return a single line in the form of an array containing the elements on the line;

    Therefore the following is wrong and needs to be removed as you are iterating over a single line in the CSV

       for ($k = 0, $m = count($lines) - 1; $k < $m; $k++) {
    

    So, after revision the reading loop should (I think) be like this:

    $fp = fopen('db.csv','r') or die("can't open file");
    $k=0;
    while($lines = fgetcsv($fp)) {
                $one[$k] = $lines[0];
                $two[$k] = $lines[1];
                $three[$k] = $lines[2];
                $four[$k] = $lines[3];
                $five[$k] = $lines[4];
                $six[$k] = $lines[5];
        $k++;
        $countLines++;
    }
    

    After this use, e.g. print_r($one) for debug to view the arrays.

    To output it I’m relying heavily on guesswork as to what you want to achieve, because you are outputting to db2.csv, but without commas (to seperate). however try something like the following

    /*
     * Store data in file
     */
    $fp = fopen('db2.csv', 'w') or die("can't open file");
    fwrite($fp, $header);
    
    /*
     * Data for export
     */
    for ($i = 0, $j = $countLines - 1; $i < $j; $i++) {
        fprintf($fp, "%s %s %-3s %-30s %-30s %-30s\r\n", /* possibly add commas here? */
                $one[$i], $two[$i], $three[$i],$four[$i], $five[$i], $six[$i]);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay i have this problem with every page i make. im not sure what
Okay, so I'm not even sure how to ask this question (much less search
Okay, This may be really simple or it may not even be possible, or
Okay, I think I'm just making a stupid mistake here, but I want to
Okay, before you guys go nuts -- this is just a small site, temporary
Okay people, I'm sure someone has had this issue and can help me out.
Okay, so the answer to my question might not be the problem but here's
Okay, not quite sure how to explain it but I'll try my best... I
Okay I have spent the last 2 days trying to sort this one out.
Okay i know i have to use the JDBC etc, but im not sure

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.