01hanstu Posted September 23, 2009 Share Posted September 23, 2009 Hi, I have a booking system incorporate into our intranet. The problem is that we have to manually time the names of the teachers in the fields then the class names. My idea is to utilise the MIS Server we have and do an export of the room timetables and then when we open the manage timetable page we can click on import csv the point to where it is then it will put the data onto the form the we can just click submit. Is this possible? Thanks Stuart Link to comment https://forums.phpfreaks.com/topic/175260-csv-imports/ Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 yes it's possible simply way is to use fgetcsv from an uploaded csv file. Manual http://us.php.net/manual/en/function.fgetcsv.php I can show you an example of how I do it if you get stuck. Link to comment https://forums.phpfreaks.com/topic/175260-csv-imports/#findComment-923697 Share on other sites More sharing options...
01hanstu Posted September 23, 2009 Author Share Posted September 23, 2009 Hi nuttycoder, Thanks for the reply. Had a look at the site. The problem I have is that the name of the csv is always a different name as its generated by the MIS Server, So how would you add a "Browse" Button. Thanks nuttycoder, Stuart. Link to comment https://forums.phpfreaks.com/topic/175260-csv-imports/#findComment-923704 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 so can you not see if on your computer through a network? I supose you could just the filename to a function to import the file, but you would need to know the file name to pass, not sure how else you could go about it. Link to comment https://forums.phpfreaks.com/topic/175260-csv-imports/#findComment-923709 Share on other sites More sharing options...
01hanstu Posted September 23, 2009 Author Share Posted September 23, 2009 Hi, Thanks again, I Guess i could simply rename the csv file to suit when its emailed to me. and copy it to the web directory the run the script. Are you able to show me how, using a fixed name, to fill the textfield from a csv and if you can show me one i can try to replicate it to do them all (Bit of copy N paste) Thanks - Stuart Link to comment https://forums.phpfreaks.com/topic/175260-csv-imports/#findComment-923714 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 okay I've commented a script to help show you how to use it: <?php //include db connection; //error function function errors($error){ if (!empty($error)) { $i = 0; $showError.= "<p><span class=\"error\" id=\"message\">"; while ($i < count($error)){ $showError.= $error[$i].'<br />'; $i ++;} $showError.= "</span></p>"; echo $showError; }// close if empty errors } // close function //function to detect file extension function get_file_extension($file_name) { return end(explode('.',$file_name)); } if (isset($_POST['upfile'])){ // check feilds are not empty //if file is not a csv generate an error if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv') { $error[] = 'Only CSV files accepted!'; } if (!$error){ // $_FILES['uploaded']['tmp_name']; $tot = 1; $handle = fopen($_FILES["uploaded"]["tmp_name"], "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { for ($c=0; $c < 1; $c++) { //echo "<pre>".print_r($data)."</pre>"; //import into databsse $sql = mysql_query("INSERT INTO table( col1, col2, col3, col4 )VALUES( '$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]' )")or die(mysql_error()); $tot++; } } fclose($handle); echo "CSV File Imported, $tot records added"; }// end no error }//close if isset upfile //show any errors errors($error); //show upload form echo "<form enctype=\"multipart/form-data\" action=\"\" method=\"post\"> File:<input name=\"uploaded\" type=\"file\" maxlength=\"20\" /><input type=\"submit\" name=\"upfil\e" value=\"Upload File\"> </form>"; ?> if your getting the file emailed to you the filename can be anything since you can upload it through a form. there is a function called errors which will collect any errors from an array also another function to determine the extension if the file being uploaded. You'll need to chnage the database table name and the column names each column data comes back as an array $data[] put the column section number inside the array like $data[$1] which gets the second segment as arrays start at 0 you should be able to see what I mean from the script. Hope this is clear. Link to comment https://forums.phpfreaks.com/topic/175260-csv-imports/#findComment-923733 Share on other sites More sharing options...
01hanstu Posted September 24, 2009 Author Share Posted September 24, 2009 Hi, This is looking Promising, but i have an error and cant get rid of it My Connect.php is as follows: <?php $hostname = "xx.xx.xx.xx"; $database = "db_name"; $username = "u_name"; $password = "p_word"; $conn= mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); ?> But i get an error saying "No database selected" Can you help Thanks Stuart Link to comment https://forums.phpfreaks.com/topic/175260-csv-imports/#findComment-924264 Share on other sites More sharing options...
nuttycoder Posted September 24, 2009 Share Posted September 24, 2009 yes you need to select the database to use like this; $conn = mysql_select_db ($database)or trigger_error(mysql_error(),E_USER_ERROR); Link to comment https://forums.phpfreaks.com/topic/175260-csv-imports/#findComment-924266 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.