Jump to content

Manipulating a spreasheet


alanl1
Go to solution Solved by DavidAM,

Recommended Posts

I have the follwoing code which basically opens up a spreasheet and displays the rows from a passed in csv file.

 

I there a way to get the headings of the spreasheet into variables?

 

thanks in advance

 

<?php
/*

*/

$newname = $_GET['newname'];  //The file that has been moved into the Uploads/ folder will need to be stripped here to retreive just the filename...
$filename = basename($newname,"/");  //basename function strips off filename based on first forward slash "/"

$row =3;
if (($handle = fopen($filename, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
       echo "<p> <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c];
   echo "    ";
   
   
        }
    }
    fclose($handle);
}

?>

 

Link to comment
Share on other sites

Please use

 ... 

tags for posting.

 

If there are headings in the file, you would need to capture them on the first pass of the loop:

<?php
/*

*/

$newname = $_GET['newname'];  //The file that has been moved into the Uploads/ folder will need to be stripped here to retreive just the filename...
$filename = basename($newname,"/");  //basename function strips off filename based on first forward slash "/"

$row =3;
if (($handle = fopen($filename, "r")) !== FALSE) {
    $headings = array(); # MAD - Added
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if (empty($headings)) $headings = $data; # MAD - Added
        $num = count($data);
       echo "<p> <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c];
   echo "    ";
   
   
        }
    }
    fclose($handle);
}
Link to comment
Share on other sites

Hi DavidAM

 

I have managed to pull the variable values out but its very messy I only want the headings once and it seems to be repeating them for every row

 

here is my code to date, I have added headings[1] through to [26] manually as i know the spreadsheet has 26 columns, but what it it has only 5 or 27 or something like that

 

if (($handle = fopen($filename, "r")) !== FALSE) {
    $headings = array(); # MAD - Added
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if (empty($headings)) $headings = $data; # MAD - Added
  echo $headings[0];
  echo $headings[1];
  echo $headings[2];
  echo $headings[3];
  echo $headings[4];
  echo $headings[5];
  echo $headings[6];
  echo $headings[7];
  echo $headings[8];
  echo $headings[9];
  echo $headings[10];
  echo $headings[11];
  echo $headings[12];
  echo $headings[13];
  echo $headings[14];
  echo $headings[15];
  echo $headings[16];
  echo $headings[17];
  echo $headings[18];
  echo $headings[19];
  echo $headings[20];
  echo $headings[21];
  echo $headings[22];
  echo $headings[23];
  echo $headings[24];
  echo $headings[25];
  echo $headings[26];
  
        $num = count($data);
       echo "<p> <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c];
   echo "    ";
  
  
        }
    }
    fclose($handle);
}
 

Link to comment
Share on other sites

Are there headings? They'd be the first row in that CSV. You should actually see them already.

As requinix said, your original code should have already been displaying the headers since they are the first row of data you read. I just added a line to "capture" the headings into an array variable. That's what you said you wanted to do.

 

Why are you echoing the headings inside the loop for every row? What are you trying to accomplish?

 

To answer your question exactly, $headings is an array, just like $data. So you can echo the headings the same way you echo the data:

for ($c = 0; $c < count($headings); $c++) echo $headings[$c] . " ";.

But it makes no sense to do that in the read loop, since you are already printing them in the for loop on the first read.

Link to comment
Share on other sites

ok all i want to do is show the column headings in a dropdown box which I have working, then I want to show the rest of the column rows underneath the drop down box

 

I have the headings captured in the dropdown but the headings and rows are outputting to when I only want the rows without the heading as I am capturing that in the dropdown

 

if that does not make sense I will try and explain in steps

 

step 1: drop down populated working,

 

step 2: then the headings again which i do not need anymore

 

step 3: and finally the row

 

step 4 : step 2 and step 3 repeating.  I only need step 3 to show the rest of the rows

 

HERE is my code

 

 

<?php

if (($handle = fopen($filename, "r")) !== FALSE) {
    $headings = array(); # MAD - Added
      ?><select name="Firstvalue" width="100" align="center" selected="Software Manufacturer"><option value='Software Manufacturer' selected="SELECTED">Choose</option><?php
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
        if (empty($headings)) $headings = $data; # MAD - Added
  
      for ($c = 0; $c < count($headings); $c++)
       { 
        echo "<option value='" .$headings[$c] ."'>" .$headings[$c] ."</option>"; //Only need first row in drop down box.  EG the headings
       }
      ?></select><?php
        $num = count($data);
       echo "<p> <br /></p>\n";
        $row++;
  
          for ($c=0; $c < $num; $c++) {
              echo $data[$c];  //Output all the rows
       echo "    ";
   
        }

    }

    fclose($handle);
}

?>

Link to comment
Share on other sites

  • Solution

This shows the importance of asking the entire question the first time. This is not the way I would handle it, but I'm not going to rewrite it at this point.

 

The if(empty($headings)) statement will only be true on the first pass through the loop. So everything you want to do with the headings can be done there.

 

	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
		// Add headings to combobox
		if (empty($headings)) {
			$headings = $data; # MAD - Added
			for ($c = 0; $c < count($headings); $c++)
			{ 
				echo "<option value='" .$headings[$c] ."'>" .$headings[$c] ."</option>"; //Only need first row in drop down box.  EG the headings
			}
	  ?></select><?php
		} // END IF(empty($headings))
		
		$num = count($data);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.