...

View Full Version : Export a Listbox



mqcarpenter
06-29-2004, 10:57 PM
Is there a way to export the contents of a listbox to say an Excel spreadsheet if it is not database driven?

I have a dynamic listbox that allows people to sort it. Once they are done, I would like to give them the option to export it for offline use.

sad69
06-30-2004, 12:11 AM
Would it be possible to see this in action online? It'd be nice to get a picture of what you're talking about (because I'm having a bit of a hard time visualizing this..) so that we can offer a solution.

Also, do you have PHP or ASP available on your webserver?

Sadiq.

MindCrafter
06-30-2004, 12:40 AM
mqcarpenter,

The simplest way is to write the content of the list to a comma delimited (CSV) file on the server.

You can use server-side scripting (ASP,ColdFusion or PHP) to write the file then provide the end user a link to the file in a download/confirmation page.

Excel will open the file without any problems

For garbage collection, simple include code or schedule a task to clean the directory dependant upon the age of the file i.e. 1+ days old.


If you explicitly need to create an Excel file, below is a link to an example of how to create an Excel spreadsheet using ASP. You could remove the ADO code and loop thru the listbox collection instead.


CreateXLS_asp.txt (http://www.mindcrafter.com/misc_code/CreateXLS_asp.txt)


MindCrafter
www.mindcrafter.com

mqcarpenter
06-30-2004, 01:19 AM
I have PHP available. A csv would be fine, and allow a download would work. Is that a difficult thing to do?

I do not have ASP available as an option.

mqcarpenter
06-30-2004, 01:28 AM
Would it be possible to see this in action online? It'd be nice to get a picture of what you're talking about (because I'm having a bit of a hard time visualizing this..) so that we can offer a solution.

I do not think I have seen this before. I just want to be able to export a list so that it can be used offline by the person sorting it. Here is my listbox that is sortable:

www.gruenebydesign.com/rotc/2004/predraft2004.html


Also, do you have PHP or ASP available on your webserver?



I have PHP, not ASP

sad69
06-30-2004, 01:54 AM
Oh I see what you're saying now!

Ok, check out this link:
http://www.phpfreaks.com/tutorials/114/0.php

I know it says it's for MySQL, but the idea is the same. Basically, you'd want to submit this page to a PHP script. There, you would parse the listbox (you may need all the elements in the listbox to be selected in order for them to all be submitted.. Javascript?) and create a comma or tab delimited string (not file) and then look at page 5 of the tutorial. See how it uses headers to tell the browser that this is an xls file? Do the same, and the print/echo your delimited string right after the headers.

That should do it for you. Let me know if you run into any troubles.

Sadiq.

mqcarpenter
06-30-2004, 06:47 PM
I have thought about this, and looked over the code, but this is not ideal. What I have is a listbox that is not populated from the db. I would rather keep it that way, mainly because I am unfamiliar with how to do this easily. I am not that PHP literate yet.

The listbox feature is here: http://www.gruenebydesign.com/rotc/2004/predraft2004.html

I just need a way to allow people to export the results in the right column, or allow them to view it in a separate window....

Hmmm, maybe that is the direction I should head?

sad69
06-30-2004, 07:33 PM
I have thought about this, and looked over the code, but this is not ideal. What I have is a listbox that is not populated from the db. I would rather keep it that way, mainly because I am unfamiliar with how to do this easily.
That's fine, you don't need to use a db, it's fine the way it is.


I am not that PHP literate yet.
Well now's as good a time as any!

Check out the attached file. Run it and analyze it as I'm pretty sure it does what you're wanting to achieve.

Let me know how you make out, or if you have any questions.

Sadiq.

mqcarpenter
06-30-2004, 08:01 PM
Sad, Thanks for the input.

I tried modifying what you did to work with the existing form. It clears the form, but does not prompt a download or view. Remember that there are some external Javascripts to handle the istbox functionality.

Any thoughts?

Here is the form code:


<?php
if(isset($_POST['submit'])) {
$html_str = '';
foreach($_POST['multi_sel'] as $option) {
$html_str .= $option.'<br>';
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print $html_str;
}
else {
?>
<script>
function selectAll(_id) {
var m_sel = document.getElementById(_id);
for(var i = 0; i < m_sel.options.length; i++) {
m_sel.options[i].selected = true;
}
}
</script>
<?php
}//else
?>
<SELECT NAME="list21" MULTIPLE SIZE=35 onDblClick="moveSelectedOptions(this.form['list21'],this.form['list11'],false)" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" onsubmit="selectAll('multi_sel');">
<select id="multi_sel" name="multi_sel[]" multiple="true">
<input name="submit" value="submit" type="submit">


</SELECT>


</TD>
</TR>
</TABLE>

</FORM>

sad69
06-30-2004, 08:38 PM
Yeah, I think you've missed the point.. it wasn't going to be a direct copy+paste.. compare this new file (you should be able to use it directly.. let me know if it doesn't quite work out..) with what you did and my original file. Hopefully you can make some connections.

One of the main issues is that your file has to have a .php extension, so the webserver knows to parse it with the PHP interpreter. BTW, you do have PHP installed, right?

Let me know how this works out for you.

Sadiq.

mqcarpenter
06-30-2004, 08:47 PM
Yes, I missed it. Thanks for looking out for me. It looks like there is an error in the "foreach" statement. It show up in the excel file when extracting. I will take a look and get back to you:

http://www.gruenebydesign.com/rotc/2004/export02.php

And yes, I have PHP, and had added the code into a PHP page.

sad69
06-30-2004, 09:06 PM
Well it looks like you got rid of the [] in the name of the select box. You have to keep it there or it won't work:
<select ... NAME="list21[]" ...>

Sadiq.

mqcarpenter
06-30-2004, 09:21 PM
OK I see what is going on. I had to add characters in front of your <BR> to see what it was doing. It is counting the options in list21, and printing the characters and breaks, not the content of the options.

This is a great start though. I will look closely to see how to add the contents.

http://www.gruenebydesign.com/rotc/2004/export02.php

sad69
06-30-2004, 09:41 PM
Basically it passes the VALUE attribute of each option. If there's no value specified, it just treats it as an empty string.

Looking at your select options, it doesn't look like you've specified any values. This might be a lot of work for you to copy+paste all the contents into the values, but that's probably the ONLY way you're going to get it to work.

Actually, one thought comes to mind, but it would require altering your external Javascript file. Basically when it copies the option over from the left listbox to the right, when creating the option on the right it should assign newOption.value=oldOption.text, ontop of what it's already doing. It should be just a one-liner, but I haven't seen the code you're working with..

Let me know how you make out.

Sadiq.

sad69
06-30-2004, 09:48 PM
Ok, try using this external Javascript file instead, it should do the trick (so you don't have to put a value in for each option in listbox11).

I changed line 158, if you're interested (or need to change it back).

Let me know how it goes.

Sadiq.

mqcarpenter
06-30-2004, 09:48 PM
Interesting.

I set the initial values to be the same as the left, and it works fine. How strange that it does not do the same thing when it starts blank. Let me play with it. Thank you so much for your help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum