Forum Moderators: coopster

Message Too Old, No Replies

import text file to PHP and allow dynamic sorting of results

         

euchrid

9:11 pm on Aug 28, 2003 (gmt 0)



Wondering if someone can help me convert this script to import data from a .txt or .csv instead of a database.

<?PHP
/* set the allowed order by columns */
$default_sort = 'last_name';
$allowed_order = array ('joindate', 'last_name','first_name');

/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ¦¦
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}

/* connect to db */
mysql_connect ('localhost','user','pass');
mysql_select_db ('test');

/* construct and run our query */
$query = "SELECT * FROM sometable ORDER BY $order";
$result = mysql_query ($query);

/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}

/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
echo "<TD><b>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
} else {
echo $heading;
}
echo "</b></TD>\n";
}
echo "</TR>\n";

/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
}
echo "</TABLE>\n";
?>

jatar_k

9:22 pm on Aug 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld euchrid,

Have you taken a look at fgetcsv [ca.php.net]?

It parses lines from a csv file into an array.

justageek

2:25 am on Aug 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think fgetcsv brings the records into an array not the lines. I'm pretty sure file() would be the one to use.