Re: creating an xls file from mysql data
| From: | Richard Kurth | Date: | Tue, 13 May 2008 06:31:44 +0000 |
| Subject: | Re: creating an xls file from mysql data | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-274225@lists.php.net to get a copy of this message | ||
Chris wrote:
Richard Kurth wrote:121,17,97,123,243,52,138,114,172,170,64,49,60,256,176,244,201,42,95,4, it is not coming across as an array so the foreach is throwing an error how can I make this an array in the proper format. This number are selected in a checkbox and passed with a javascript to the script should I be converting them to an array in the javascript. this is the javascript that is passing the numbers. function exportContacts(theform) {This script will create an xls file from the data that is sent to it When I run this it only gets one recored and it is supposet to get all the records that are past by the $_POST[selectedcontactlist] I think I have a } in the wrong place but I can not figure it out anybody have a suggestion $_POST[selectedcontactlist]="3,45,65,23,12,4,56"; //this is a sample of what is past $ExplodeIt = explode(",",rtrim($_POST[selectedcontactlist],",")); $Count = count($ExplodeIt); for ($i=0; $i < $Count; $i++) { $sql = "SELECT * FROM contacts WHERE id = '$ExplodeIt[$i]'";Instead of doing that, do this: /** * This section makes sure the id's you are going to use in your query are actually integer id's. * If they aren't, you'll get an sql error. * */ $ids = array(); foreach ($_POST['selectedcontactlist'] as $id) { if (!is_int($id)) {continue;} $ids[] = $id; } // all posted values are duds? show an error. if (empty($ids)) { echo "No id's are numeric, try again"; exit; } $sql = "select * from contacts where id in (" . implode(',', $ids) . ")"; That'll get everything for all of those id's and then you can loop over it all once: // print out the header for the csv file here. // then loop over the results: while ($row = mysql_fetch_assoc($sql_result)) { // put it into file here. } // close the file // print it out. This is what the $_POST['selectedcontactlist'] looks like
//theform.action = "cmexport.php";
theform.action="cmexport.php";
if (getSelectedContacts(theform) != "")
{
theform.submit();
}
else
{
alert("Please select contacts to export by checking the boxes to the left of the contact's name.");
}
}