Jump to content

El Heso

Members
  • Posts

    14
  • Joined

  • Last visited

    Never

Everything posted by El Heso

  1. Yes i know but can you show me some code how to manage that or links to example or tutorials i´m a novice on this :-)
  2. i have a import function for the csv files and the files is different everytime so i only upload and make a temporary table with columnsname of the columns in the csv file then i want to distribute the content to the right table Hope you understand what i´m talking about :-)
  3. Hi folks! I need help how to code or links to tutorials and examples. What i want to do: I have a temporary table with the following columns: `company_id` int(11) NOT NULL AUTO_INCREMENT, `company_name` varchar(30) NOT NULL, `company_address` varchar(50) NOT NULL, `company_zipcode` int(6) NOT NULL, `company_city` varchar(30) NOT NULL, `company_telephone` varchar(20) NOT NULL, `company_addressid` int(9) NOT NULL, `person_firstname` varchar(20) NOT NULL, `person_lastname` varchar(20) NOT NULL, `person_email` varchar(30) NOT NULL, `person_emailstatus` varchar(12) NOT NULL, `person_position` varchar(50) NOT NULL, `answer_1` varchar(50) NOT NULL, `answer_2` varchar(50) NOT NULL, `answer_3` varchar(50) NOT NULL Then i have 3 tables: company `company_id` int(11) NOT NULL AUTO_INCREMENT, `company_name` varchar(30) NOT NULL, `company_address` varchar(50) NOT NULL, `company_zipcode` int(6) NOT NULL, `company_city` varchar(30) NOT NULL, `company_telephone` varchar(20) NOT NULL, `company_addressid` int(9) NOT NULL person `person_id` int(11) NOT NULL AUTO_INCREMENT, `person_firstname` varchar(20) NOT NULL, `person_lastname` varchar(20) NOT NULL, `person_email` varchar(30) NOT NULL, `person_emailstatus` varchar(12) NOT NULL, `person_position` varchar(50) NOT NULL answer `answer_id` int(11) NOT NULL AUTO_INCREMENT, `answer_1` varchar(50) NOT NULL, `answer_2` varchar(50) NOT NULL, `answer_3` varchar(50) NOT NULL, The problem: I want to insert from the temporary table to this 3 tables were i need it to be relational so in the table company one company only can be one time ( the company_addressid is a unique number for that company ) so when i insert to that table it should match this company_addressid anf if exists it only should update the data when insert to person it should be the same as company but in this case the person_email is the unique data to match from so if exists only update the record when insert to answer it should be the same as person but in this case if the person has already one answer attached to him it should only update the record Solution: I think i have to have the following extra columns: In person: company_id In answer: person_id Hope someone can help me in the right direction how to select from the temporary table and split it to this 3 tables and still avoid duplicates in the company, person, answer If you have some example please post it
  4. Hi! this is my code for the import: if (($handle = $source_file) !== FALSE) { $columns = fgetcsv($handle, 10000, ";"); foreach ($columns as &$column) { $column = str_replace(".","",$column); } $insert_query_prefix = "INSERT INTO table (".join(",",$columns).")\nVALUES"; while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) { while (count($data)<count($columns)) array_push($data, NULL); $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; mysql_query($query); } fclose($handle); } function quote_all_array($values) { foreach ($values as $key=>$value) if (is_array($value)) $values[$key] = quote_all_array($value); else $values[$key] = quote_all($value); return $values; } function quote_all($value) { if (is_null($value)) return "NULL"; $value = "'" . mysql_real_escape_string($value) . "'"; return $value; } the value i want to remove before import is (Gå till ...) it starts with this (Gå till than more value then close with ) so i need the preg_replace search the code were it starts with (Gå till then delete everything within that () example: in one row this exists: Hey (Gå till nästa steg) mom. //the value "nästa steg" can be different should be after preg_replace Hey mom. after the preg_replace the sript should import the data i have tryed to use this kind of code but cant make it work ( dont know were to put it or how to write ) $pattern = '#\(\s*Gå till[^)]+\)#'; $s = preg_replace($pattern, "", $s); $s = preg_replace('#\s{2,}#', " ", $s);
  5. hi! I figured that it cant be done, beacuse its a "fake" excel im getting out. I belived it was a real excel. I notice the PHPexcel so i need to some how figure out how to use that instead Or can it be done?
  6. Hi! Is it possible to make this happen? $sql="SELECT CONCAT(contact) Contact, CONCAT(date) Date, CONCAT(p1453_01) '1. How old are you?', CONCAT(p1453_02) '2. Do you own a car?', CONCAT(p1453_03) '3. Have your friends a car?' FROM survey WHERE p1453_02 like 'Yes%' ORDER BY datum DESC"; to this: $sql="SELECT CONCAT( get value from other place ) get value from other place , CONCAT(get value from other place ) get value from other place , CONCAT(get value from other place ) ' get value from other place ', CONCAT(get value from other place ) ' get value from other place ', CONCAT(get value from other place ) ' get value from other place ' FROM survey WHERE get value from other place like 'get value from other place %' ORDER BY datum DESC"; I would like to set the value for each field from admin side of my site and then run the query Hope you guys understand what i mean Any advice? Regards Tony
  7. I fixed the problem by this code: $source_file = fopen('http://localhost/test/murt.csv', 'r'); $target_table = 'foretag'; if (($handle = $source_file) !== FALSE) { $columns = fgetcsv($handle, 10000, ";"); foreach ($columns as &$column) { $column = str_replace(".","",$column); } $insert_query_prefix = "INSERT INTO foretag (".join(",",$columns).")\nVALUES"; while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) { while (count($data)<count($columns)) array_push($data, NULL); $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; mysql_query($query); } fclose($handle); } function quote_all_array($values) { foreach ($values as $key=>$value) if (is_array($value)) $values[$key] = quote_all_array($value); else $values[$key] = quote_all($value); return $values; } function quote_all($value) { if (is_null($value)) return "NULL"; $value = "'" . mysql_real_escape_string($value) . "'"; return $value; }
  8. hi! need help how to make the rows that are "new" ( 30 days back to present day a other color than the "old" rows ) here is my script: <?php include('config.php'); include('opendb.php'); $selected_radio = @$_POST['formradio1'] ; switch ($selected_radio) { case 1: //$sql="SELECT * FROM foretag"; $sql="SELECT CONCAT(foretagsnamn) Företagsnamn, CONCAT(adress) Adress, CONCAT(postnr) Postnr, CONCAT(ort) Ort, CONCAT(telefon) Telefon, CONCAT(fornamn,' ',efternamn) Namn, CONCAT(email) Email, CONCAT(befattning) Befattning, CONCAT(orgnr) OrgNr, CONCAT(huvudinriktning)Huvudinriktning, CONCAT(huvudbransch) Huvudbransch, CONCAT(esn) ESN, CONCAT(branschkod) Branschkod, CONCAT(anstallda) Anställda, CONCAT(anstallda_text) AnställdaIntervall, CONCAT(omsattning_text) OmsättningIntervall, CONCAT(omsattning) Omsättning, CONCAT(kommun) Kommun, CONCAT(lan) Län, CONCAT(adressid) AdressID, CONCAT(land) Land, CONCAT(datum) Datum FROM foretag ORDER BY datum DESC"; //execute query $result = @mysql_query($sql) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno()); //if this parameter is included ($w=1), file returned will be in word format ('.doc') //if parameter is not included, file returned will be in excel format ('.xls') if (isset($w) && ($w==1)) { $file_type = "msword"; $file_ending = "doc"; }else { $file_type = "vnd.ms-excel"; $file_ending = "xls"; } //header info for browser: determines file type ('.doc' or '.xls') header("Content-Type: application/$file_type"); header("Content-Disposition: attachment; filename=test.$file_ending"); header("Pragma: no-cache"); header("Expires: 0"); /* Start of Formatting for Word or Excel */ if (isset($w) && ($w==1)) //check for $w again { //define separator (defines columns in excel & tabs in word) $sep = "\n"; //new line character while($row = mysql_fetch_row($result)) { //set_time_limit(60); $schema_insert = ""; for($j=0; $j<mysql_num_fields($result);$j++) { //define field names $field_name = mysql_field_name($result,$j); //will show name of fields $schema_insert .= "$field_name:\t"; if(!isset($row[$j])) { $schema_insert .= "NULL".$sep; } elseif ($row[$j] != "") { $schema_insert .= "$row[$j]".$sep; } else { $schema_insert .= "".$sep; } } $schema_insert = str_replace($sep."$", "", $schema_insert); $schema_insert .= "\t"; print(trim($schema_insert)); //end of each mysql row //creates line to separate data from each MySQL table row print "\n----------------------------------------------------\n"; } }else{ //define separator (defines columns in excel & tabs in word) $sep = "\t"; //tabbed character //start of printing column names as names of MySQL fields for ($i = 0; $i < mysql_num_fields($result); $i++) { echo mysql_field_name($result,$i) . "\t"; } print("\n"); //end of printing column names //start while loop to get data while($row = mysql_fetch_row($result)) { //set_time_limit(60); $schema_insert = ""; for($j=0; $j<mysql_num_fields($result);$j++) { if(!isset($row[$j])) $schema_insert .= "NULL".$sep; elseif ($row[$j] != "") $schema_insert .= "$row[$j]".$sep; else $schema_insert .= "".$sep; } $schema_insert = str_replace($sep."$", "", $schema_insert); //this corrects output in excel when table fields contain \n or \r //these two characters are now replaced with a space $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); $schema_insert .= "\t"; print(trim($schema_insert)); print "\n"; } } break; case 2: include("Choice_2.php"); break; case 3: include("Choice_3.php"); break; case 4: include("Choice_4.php"); break; case 5: include("Choice_5.php"); break; case 6: include("Choice_6.php"); break; } ?>
  9. same problem quet is empty here is the hole code <form method="post" enctype="multipart/fprm-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> <?php include('config.php'); include('opendb.php'); $query=''; if(isset($_POST['upload'])) { $source_file = @$_FILES['userfile']; //$source_file = fopen('http://localhost/test/upload/test.csv', 'r'); $target_table = 'foretag'; function csv_file_to_mysql_table($source_file, $target_table, $max_line_length=10000) { if (($handle = fopen("$source_file", "r")) !== FALSE) { $columns = fgetcsv($handle, $max_line_length, ","); foreach ($columns as &$column) { $column = str_replace(".","",$column); } $insert_query_prefix = "INSERT INTO $target_table (".join(",",$columns).")\nVALUES"; while (($data = fgetcsv($handle, $max_line_length, ";")) !== FALSE) { while (count($data)<count($columns)) array_push($data, NULL); $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; mysql_query($query); } fclose($handle); } } function quote_all_array($values) { foreach ($values as $key=>$value) if (is_array($value)) $values[$key] = quote_all_array($value); else $values[$key] = quote_all($value); return $values; } function quote_all($value) { if (is_null($value)) return "NULL"; $value = "'" . mysql_real_escape_string($value) . "'"; return $value; } } if( !mysql_query($query) ) { echo "<br>Query: $query<br>Returned error: " . mysql_error() . "<br>";} include('closedb.php'); echo "<br>done<br>"; ?>
  10. can this be the problem? $target_table = 'foretag'; i try to define what table in the database it should place the data
  11. but when i print out the $source_file the data exist, were can it be empty?
  12. The user dont have that permission,so i need a script that controls the import after posted a csv file
  13. Hi! I hope somebody can help me what im do wrong. i have checked that the data from the file is in $source_file but nothing imports to the database <?php include('config.php'); include('opendb.php'); if(isset($_POST['upload'])) { $source_file = @$_POST['userfile']; //$source_file = fopen('http://localhost/test/upload/test.csv', 'r'); $target_table = 'foretag'; function csv_file_to_mysql_table($source_file, $target_table, $max_line_length=10000) { if (($handle = fopen("$source_file", "r")) !== FALSE) { $columns = fgetcsv($handle, $max_line_length, ","); foreach ($columns as $column) { $column = str_replace(".","",$column); } $insert_query_prefix = "INSERT INTO $target_table (".join(",",$columns).")\nVALUES"; while (($data = fgetcsv($handle, $max_line_length, ";")) !== FALSE) { while (count($data)<count($columns)) array_push($data, NULL); $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; mysql_query($query); } fclose($handle); } } function quote_all_array($values) { foreach ($values as $key=>$value) if (is_array($value)) $values[$key] = quote_all_array($value); else $values[$key] = quote_all($value); return $values; } function quote_all($value) { if (is_null($value)) return "NULL"; $value = "'" . mysql_real_escape_string($value) . "'"; return $value; } } include('closedb.php'); echo "<br>done<br>"; ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.