alanl1 Posted May 23, 2013 Share Posted May 23, 2013 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);} ?> Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/ Share on other sites More sharing options...
requinix Posted May 23, 2013 Share Posted May 23, 2013 Are there headings? They'd be the first row in that CSV. You should actually see them already. Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/#findComment-1431762 Share on other sites More sharing options...
DavidAM Posted May 23, 2013 Share Posted May 23, 2013 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); } Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/#findComment-1431856 Share on other sites More sharing options...
alanl1 Posted May 23, 2013 Author Share Posted May 23, 2013 which loop the while loop or the for loop as i want to capture the headings in variables, how would i do this thanks Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/#findComment-1431953 Share on other sites More sharing options...
DavidAM Posted May 23, 2013 Share Posted May 23, 2013 The code in my previous response included two lines I added to capture the headings. Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/#findComment-1431971 Share on other sites More sharing options...
alanl1 Posted May 24, 2013 Author Share Posted May 24, 2013 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);} Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/#findComment-1432007 Share on other sites More sharing options...
DavidAM Posted May 24, 2013 Share Posted May 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/#findComment-1432020 Share on other sites More sharing options...
alanl1 Posted May 28, 2013 Author Share Posted May 28, 2013 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);} ?> Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/#findComment-1432827 Share on other sites More sharing options...
Solution DavidAM Posted May 29, 2013 Solution Share Posted May 29, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/278308-manipulating-a-spreasheet/#findComment-1433029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.