Re: creating an xls file from mysql data

From: Date: Tue, 13 May 2008 02:18:13 +0000
Subject: Re: creating an xls file from mysql data
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-274211@lists.php.net to get a copy of this message
Richard Kurth wrote: > > 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. -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.general (#274211) next »