PHP - Importing A .txt File Into An Array
Hello I am tring to import a .txt file into an array. I am stuck will somone please help me.
?<php $fp = fopen("ArrayTestFile.txt", 'wb'); $X = fread("ArrayTestFile.txt" 'w'); for ($j = 0 ; $j < 6 ; ++$j) { $written = fwrite($fp, "ArrayTestFile.txt"); if ($written == FALSE) break; } print $fp[0] . " " . $fp[1] . " " . $fp[2] . " " . $fp[3] . " " . $fp[4]; fclose($fp); ?> Similar TutorialsHi friends i need a small help in importing csv files into mySQL database table i tried all possible options but i am no where... here is the example for what i am trying to do Mysql DB name = raw Table name is = dump +--------------------+--------------------+ + Account + Bal + +--------------------+--------------------+ + + + +--------------------+--------------------+ CSV Format 50************13, 11095 When they upload the file it should automatically get inserted into appropriate fields any help would be great i tried all possible scripts found in the net could nothing is happening I'm attempting to import a mysql database backup via php unfortunately I keep getting this error: Code: [Select] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SOURCE file.sql' at line 1 Code: mysql_query("SOURCE file.sql") or die (mysql_error()); I've researched this online and this seems to be the way everyone suggests to use, unfortunately it isn't working. If it helps here is the first 4 lines of file.sql as that may be the problem for all I know: -- phpMyAdmin SQL Dump -- version 3.2.4 -- http://www.phpmyadmin.net -- Am trying to import a csv file into mysql. below is the code am using.
<?php //connect to the database include('mysql_connect.php');//select the table // if ($_FILES[csv][size] > 0) { //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r"); //loop through the csv file and insert into database do { if ($data[0]) { mysql_query("INSERT INTO requisition (department, contact_last, contact_email) VALUES ( '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."' ) "); } } while ($data = fgetcsv($handle,1000,",","'")); // //redirect header('Location: import.php?success=1'); die; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Import a CSV File with PHP & MySQL</title> </head> <body> <?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> Choose your file: <br /> <input name="csv" type="file" id="csv" /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> ........................................................................................................................................... On page load, it gives some erros Notice: Use of undefined constant csv - assumed 'csv' in C:\wamp\www\orango\upload_data.php on line 7 Notice: Use of undefined constant size - assumed 'size' in C:\wamp\www\orango\upload_data.php on line 7 Notice: Undefined index: csv in C:\wamp\www\orango\upload_data.php on line 7 Notice: Use of undefined constant success - assumed 'success' in C:\wamp\www\orango\upload_data.php on line 43 I try to ignore the errors to see if it would work but it didn't save in the DB. please help me out Hello,
I am trying to import a CSV file into an existing mysql table but it doesn't seem to work well.
Here is how my mysql is looking like:
1 a_id int(11) AUTO_INCREMENT 2 a_lobecoid int(7) 3 a_code varchar(30) utf8_general_ci 4 a_omschint varchar(60) utf8_general_ci 5 a_beveiligingniv int(5) 6 a_type varchar(5) utf8_general_ci 7 a_assortiment int(5) 8 a_discipline varchar(30) utf8_general_ci 9 a_brutoprijs varchar(50) utf8_general_ci 10 a_status varchar(5) utf8_general_ci 11 a_levcode varchar(10) utf8_general_ci 12 a_omschr_nl varchar(60) utf8_general_ci 13 a_omschr_fr varchar(60) utf8_general_cithese are some lines from my CSV file 16158|-H|Factory installed heater|10|S|400|CCTV|45.0|E| 1829|Factory installed heater|Factory installed heater 16159|-IR|Factory installed IR LED ring|10|S|400|CCTV|50.0|E| 1829|Factory installed IR LED ring|Factory installed IR LED ring 9001|00-SBN2|Smoke box niet geaspireerd,230VAC|10|S|267|BRAND|1587.03|D| 642|Smoke box niet geaspireerd,230VAC|Smoke box pas aspiré,230VCA 9003|00-TP1|Telescopische verlengstok voor Smoke box,2,4-4,6m|10|S|267|BRAND|644.09000000000003|D| 642|Telescopische verlengstok voor Smoke box,2,4-4,6m|Rallonge téléscopique pour Smoke box,2,4-4,6m 9004|00-TP2|Telescopische verlengstok voor Smoke box,2,4-9,2m|10|S|267|BRAND|944.64999999999998|D| 642|Telescopische verlengstok voor Smoke box,2,4-9,2m|Rallonge téléscopique pour Smoke box,2,4-9,2m 12161|001-0081|Thermistor probe,rood, high temp. 0-+150°C|10|S|52|INBRAAK|136.91|D| 1731|Thermistor probe,rood, high temp. 0-+150°C|Sonde température,rouge,high temp. 0-+150°CHere you have the code for the import: $upload_article_query = "LOAD DATA INFILE 'ARTIKELS.CSV' INTO TABLE artikelen FIELDS TERMINATED BY '|' LINES TERMINATED BY '\\r\\n' (a_lobecoid, a_code, a_omschint, a_beveiligingniv, a_type, a_assortiment, a_discipline, a_brutoprijs, a_status, a_levcode, a_omschr_nl, a_omschr_fr)"; $upload_article_stmt = $dbh->prepare($upload_article_query); $upload_article_stmt->execute();If i use the code then in the MYSQL table the first line is filled in but where the second line has to start it just writes it into the last column and doesn't start a new line. Also if i edit that first line it shows a lot of "?" (questionmarks) into a window symbol. Anyone has an idea what i am doing wrong? In attachment some printscreens of my table after the insert. Apologies for the Dutch language. Thanks in advance Attached Files pic1.jpg 6.13KB 0 downloads pic2.jpg 37.9KB 0 downloads Haven't really used traits much and am confused whether to import other classes in the trait or the class that uses the trait (might also be an annotation question but not sure). Must import statements (i.e. use Foo\Bar;) be included in the class or can they be included in just the trait which is used by a class? Does it depend and if so on what? Does having one class use another class impact the decision? Is it okay for one trait to use another and another trait to just use the first trait or should it use both? What is odd is I apparently must (to not get an error) import ApiProperty to the trait but doing so is not required for the class, and must import Groups, SerializedName, and Mapping in the class but doing so is not required for the trait. Instead of just a yes/no answer, would appreciate an explanation why it is what it is. Thanks! <?php namespace App\Entity\Document; use ApiPlatform\Core\Annotation\ApiResource; use Doctrine\ORM\Mapping as ORM; use App\Entity\MultiTenenacy\PublicIdTenantTrait; // Must Groups, SerializedName, Owner be imported in this class or just PublicIdTenantTrait // use Symfony\Component\Serializer\Annotation\Groups; // use Symfony\Component\Serializer\Annotation\SerializedName;use // use App\Entity\Owner\Owner; /** * @ORM\Entity() * @ApiResource() */ class Document { use PublicIdTenantTrait; }
<?php namespace App\Entity\MultiTenenacy; use ApiPlatform\Core\Annotation\ApiProperty; use Doctrine\ORM\Mapping as ORM; trait PublicIdTenantTrait { use PublicIdExtendedTenantTrait; /** * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") * @ApiProperty(identifier=false) * @ORM\Column(type="integer") */ private $id; public function getId(): ?int { return $this->id; } }
<?php namespace App\Entity\MultiTenenacy; use ApiPlatform\Core\Annotation\ApiProperty; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\SerializedName; use Doctrine\ORM\Mapping as ORM; trait PublicIdExtendedTenantTrait { use TenantTrait; /** * @ORM\Column(type="integer") * @ApiProperty(identifier=true) * @Groups({"tenant_entity:read"}) * @SerializedName("id") */ private $publicId; public function getPublicId(): ?int { return $this->publicId; } public function setPublicId(int $publicId): PublicIdTenantInterface { $this->publicId = $publicId; return $this; } public function generatePublicId(): PublicIdTenantInterface { $this->getOwner()->setPublicId($this); return $this; } public function getPublicIdTag(): string { return lcfirst((new \ReflectionClass($this))->getShortName()).'Id'; } }
<?php namespace App\Entity\MultiTenenacy; use Doctrine\ORM\Mapping as ORM; use App\Entity\Owner\Owner; trait TenantTrait { /** * inversedBy performed in child where required * @ORM\ManyToOne(targetEntity=Owner::class) * @ORM\JoinColumn(nullable=false, onDelete="CASCADE") */ private $owner; public function getOwner(): ?Owner { return $this->owner; } public function setOwner(?Owner $owner): TenantInterface { $this->owner = $owner; return $this; } }
Hey guys! So I have this script to import a CSV file into my database:
<?php $databasehost = "localhost"; $databasename = "import"; $databasetable = "import"; $databaseusername="username"; $databasepassword = "password"; $fieldseparator = ","; $lineseparator = "\n"; $csvfile = "test.csv"; if(!file_exists($csvfile)) { die("File not found. Make sure you specified the correct path."); } try { $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", $databaseusername, $databasepassword, array( PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); } catch (PDOException $e) { die("database connection failed: ".$e->getMessage()); } $pdo->exec("TRUNCATE TABLE `$databasetable`"); $affectedRows = $pdo->exec(" LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable` FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES"); echo "Loaded a total of $affectedRows records from this csv file.\n"; ?>So that script basically imports a csv replacing all of the other data. The issue I am having is that the data inside one of the columns contains commas, they are wrapped in quotations. Here is an example: ,"1 Registered Keeper, Full Service History, Central Locking, Electric Windows, Electric Mirrors, ABS, Traction Control, Climate Control, Power Steering, Drivers Airbag, Passenger Airbag, Side Airbags, Cruise Control, Alarm, Immobiliser, Half Leather Interior, Alloy Wheels", How could I make the script determine the text within those quotation marks as a whole value instead of separating them into individual columns? Thanks any help on this would be great! I need to write a code were users can login to his/her email account from my site to import contacts to be selected and used. I know you need to use api's for this. Has anyone done this before who can give me some useful tips? Hello, I've got two questions. Users can fill in a form with information that has to be in the following format: Value 1, Value 2, Value 3, Value 4 Value 1, Value 2, Value 3, Value 4 Now I want to return an error if ANY line isn't in that format. For example when it's like this: Value 1, Value 2, Value 3, Value 4 Value 1, Value 3, Value 4 I've got no clue how to do that... The second question is, how can I get a specific line of the posted information? I've already gotten an answer on how to count those lines, which is working. But I'm going to insert them into the database using a for loop, so I need to insert one row at a time. Thanks for your time and help, Chris I have a site where I have an admin panel set up to import an XML data feed into a database, but it's a large import and it takes a lot of time to update. I'm looking to upgrade the admin panel and offer the ability to be able to see the number of records as they're being imported instead of it hanging there until the query is completed. The tough part about a progress bar is obviously you have to know the # of items being imported beforehand, and that varies each time the feed is downloaded. So I'm just trying to get some suggestions from someone who may have written something similar. Thanks in advance! Hi everyone, I am currently making a page for a friend to upload a bunch of photos at a time. I was quite pleased that after a bit of googling and trial and error, I figured out how to do this so that multiple records could be added to my table with one submit button. However, my form has 10 browse iconcs. A few tests have revealed that my problem is that if only one picture is uploaded, I still get 9 entries in my database, which I don't want. My question is how can I alter the code so that a row is only populated in the database if an image is uploaded. I guess something that sort of says : if($imgx!="") { populate that row in the table } else { don't } ...and the same for $imgx002 through to $imgx010 The current query is below. Any pointers are much appreciated. Code: [Select] $query = "INSERT INTO photo_uploads (date, photo_name)" . "VALUES (NOW(), '$imgx'), (NOW(), '$imgx002'), (NOW(), '$imgx003'), (NOW(), '$imgx004'), (NOW(), '$imgx005'), (NOW(), '$imgx006'), (NOW(), '$imgx007'), (NOW(), '$imgx008'), (NOW(), '$imgx009'), (NOW(), '$imgx010')"; Hi all,
I'm working on a project involving an orders set of APIs, and having trouble coming up with the best logic to handle this - with the smallest amount of overhead and client facing slowness. Here's the jist of what I'm looking at...
There are 21 API methods to pull different types of orders, and each of them handle paging by requesting a number of records (10 - 500), and a page number. So I could say in my URI, "page-no=1&no-of-records=50", followed by "page-no=2&no-of-records=50", to get subsequent records. The API will return in each call the total records returned as well as the total records in their database.
Now, I want to insert all of this into my database, and so that means something like 21 APIs * 3 SQL requests each = 63 sql queries (check it exists, if so update - else insert).
This seems to me like way too much work for a client facing application. Any suggestions?
I need someone to look over this and just see if they can see why this won't work anymore? It worked when the site was in development but when it came down to the day it's not working. Basically it reads a CSV and inserted the driver into a Quali or Results table in the correct order but it's not working anymore? If someone could have a read over it and suggest what might be going wrong that would be great. Edit: It basically just added the same driver 24 times in the table. Code: [Select] Public Function ProcessUpload($UR, $RaceID, $RaceName) { Global $MDB; // Configuration - Your Options $allowed_filetypes = array('.csv'); // These will be the types of file that will pass the validation. $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB). $upload_path = './resultsfiles/'; // The place the files will be uploaded to (currently a 'files' directory). $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension). $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename. If (strpos($filename, "Quali") == True) { $tbl_name = "QualiPositions"; $FN = " - Qualifying"; } Else { $tbl_name = "RacePositions"; $FN = " - Race"; } $SQL = "SELECT count(*) FROM $tbl_name WHERE RaceID = $RaceID"; $Count = $MDB->prepare($SQL); $Count->execute(); $Count = $Count->fetchColumn(); If ($Count == 24) { $Msg = "<div class='error'>Error: We already have 24 Results for this Session.</div>"; } // Check if the filetype is allowed, if not DIE and inform the user. if(!in_array($ext,$allowed_filetypes)) $Msg = "<div class='error'>Error: The file you attempted to upload is not allowed.</div>"; // Now check the filesize, if it is too large then DIE and inform the user. if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) $Msg ="<div class='error'>Error: The file you attempted to upload is too large.</div>"; // Check if we can upload to the specified path, if not DIE and inform the user. if(!is_writable($upload_path)) $Msg ="<div class='error'>Error: You cannot upload to the specified directory, please CHMOD it to 777.</div>"; If (strpos($Msg, "Error:") == False) { // Upload the file to your specified path. $filename = str_replace(" ", "", $RaceName)."$FN.csv"; if (move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) { $FilePath = $upload_path . $filename; if (($handle = fopen($FilePath, "r")) !== FALSE) { $Pos = 0; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { $Pos++; If ($Pos <= 24) { $Driver = explode(" ", $data[$c]); $FName = $Driver[0]; $LName = $Driver[1]." ".$Driver[2]." ".$Driver[3]; $STH = $MDB->query("SELECT * FROM Drivers WHERE FirstName = '$FName' AND LastName = '$LName'"); $STH->setFetchMode(PDO::FETCH_OBJ); while($row = $STH->fetch()) { $DriverID = $row->DriverID; $ChassisID = $row->ChassisID; $EngineID = $row->EngineID; } $STH = $MDB->prepare("INSERT INTO $tbl_name(RaceID, DriverID, ChassisID, EngineID, Position, DSQ) VALUES (:RID, :DID, :ChaID, :EngID, :Pos, :DSQ)"); $STH->execute(array('RID' => $RaceID, 'DID' => $DriverID, 'ChaID' => $ChassisID, 'EngID'=>$EngineID, 'Pos'=>$Pos, 'DSQ' => 0)); $Msg = "<div class='team_confirm'>The Results have been uploaded for the $RaceName</div>"; } Else { If ($tbl_name == "RacePositions") { $Driver = explode(" ", $data[$c]); $FName = $Driver[0]; $LName = $Driver[1]." ".$Driver[2]." ".$Driver[3]; $STH = $MDB->query("SELECT * FROM Drivers WHERE FirstName = '$FName' AND LastName = '$LName'"); $STH->setFetchMode(PDO::FETCH_OBJ); while($row = $STH->fetch()) { $DriverID = $row->DriverID; } $sql = "UPDATE RacePositions SET FastLap = 'Yes' WHERE RaceID=? AND DriverID = ?"; $q = $MDB->prepare($sql); $q->execute(array($RaceID, $DriverID)); } } } } fclose($handle); } } else { $Msg = 'There was an error during the file upload. Please try again.'; // It failed :(. } } Return $Msg; } That is the code, the CSV is basically like this: Code: [Select] Lewis Hamilton, Jenson Button, Romain Grosjean, Michael Schumacher, Mark Webber, Sebastian Vettel, Nico Rosberg, Pastor Maldonado, Nico Hulkenberg, Daniel Ricciardo, Jean-Eric Vergne, Fernando Alonso, Kamui Kobayashi, Bruno Senna, Paul di Resta, Felipe Massa, Kimi Raikkonen, Heikki Kovalainen, Vitaly Petrov, Timo Glock, Charles Pic, Sergio Perez, Pedro de la Rosa, Narain Karthikeyan Thanks for any help! Hi Guys Just need some advice to go in the right direction. I'm working on a csv upload script (part of a bigger thing i'm building), so i read in the csv to a multipdimensional array and then build a query that inputs all rows in one query - i read this is the most efficient way to import multiple rows of data at once(rather than multiple insert statements). Just for illustration here's the code i use to build the query so you understand what i'm on about: Code: [Select] $sql = "INSERT INTO teams (company, teamname, teamnum, amountraised, country, president) VALUES "; // $rows is a count of the rows in the csv for($i=1; $i<$rows; $i++){ $sql.="('{$myarray[$i][0]}','{$myarray[$i][1]}','{$myarray[$i][2]}','{$myarray[$i][3]}','{$myarray[$i][4]}','{$myarray[$i][5]}')"; echo $i . "<br/>"; if($i >= 1 && $i < $rows - 1) { $sql.= ","; } } Anyway, the issue is that one of the fields("teamnum") needs to be unique - so i've set this as unique on the table in mysql. But when i run my query it doesn't import anything if one of the records isn't unique. What i really want is for it to import the ones it can and catch the ones it cant import to present to the user. So my question is - to acheive the above would i need to rewrite the query so that it inserts each row one at a time, instead of all together? Or can someone point me in the right direction for a better solution? Probably something very simple i've missed i am sure... Thanks chaps! This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=321126.0 say I have a file, it has information in it in this format 123489 ABDFDS 232356 DSFKWE and so on, new number and letters on each line separated by a space, what do I have to do so that like 123489 and 232356 would be in the aray numbers[] and the ABDFDS and DSFKWE would be phrases[] any ideas? Thanks Ok I need to write an array to a file here is the code if($place[2] < 10){ echo "Account Invalid"; $account = var_dump($account_data[$i][0]); echo $account; $fp3=fopen('invalid.txt', 'a+'); fwrite($fp3, $account); fclose($fp3);} when I try if($place[2] < 10){ echo "Account Invalid"; $account = var_dump($account_data[$i][0]); echo $account; $fp3=fopen('invalid.txt', 'a+'); fwrite($fp3, $account_data[$i][0]); fclose($fp3);} I just get "Array[1]" written to the file.. What can I do to write the array to the file? Thanks I've been working with PHP for only a few weeks now. I have learned a significant amount by creating a custom web app. I can usually figure things out by massive Googling and trial and error but this one has got me stumped. I have a form for this web app that I'd like to use as a configuration page. It houses all of the settings that the web app needs. On the backend, I'm housing all of this data in a XML file. So far, I've been able to parse the XML file to populate all of the appropriate fields in the HTML form. However, I'm having trouble taking the user's input and saving it back to the same XML. To do this, I have a processconfig.php page as the form's action. In this process.php page, I'm using the elements from the $_POST array to bring over all of the data in the form's fields. I want to take these $_POST array elements and insert them into the same XML that originally populated the form in the first place. With the code below I've been able to get the SimpleXML object to add data to the XML file but it is simply appended to the end with no formatting. As you can see, I've tried to name the form fields the same name as it's POST array element so that I can do the loop but it's not working. Is there an easier way to do this or is there another recommended way to retrieve and store configuration items for a web app? Here's my code: config.xml snippet Code: [Select] <Config> <General> <Logging> <Enabled>No</Enabled> </Logging> <QueryBuybackSites> <Enabled>Yes</Enabled> </QueryBuybackSites> <BuildFBA> <Enabled>No</Enabled> </BuildFBA> <BuildBB> <Enabled>No</Enabled> </BuildBB> <FBARankCutoff> <Value>1000000</Value> </FBARankCutoff> <Amazon0Offer> <DefaultPrice> <Value>47.77</Value> </DefaultPrice> <AutoKeep> <Enabled>Yes</Enabled> </AutoKeep> </Amazon0Offer> </General> </Config> configview.php snippet Code: [Select] <?php //Load all configuration data $configxml = simplexml_load_file('config.xml'); echo '<html> <head> <title>Easy Receive Configuration</title> </head> <body> <form action="processconfig.php" enctype="multipart/form-data" id="emf-form" method="post"> <table bgcolor="#FFFFFF" border="0" cellpadding="2" cellspacing="0" style="text-align: left;"> <tbody> <tr> <td colspan="2" style=""> <font color="#000000" face="Verdana" size="2"><b style="font-size: 20px;">Easy Receive Configuration Settings</b><br /> <label style="font-size: 15px;">This is where you can customize Easy Receive to your personal preferences</label></font> </td> </tr> <tr valign="top"> <td colspan="2" style=""> <hr /> <h3 id="element_0" name="element_0" style="font-size: 14px; padding: 10px 0pt 6px; margin: 14px 0pt; position: static;"> <span style="font-size: 24px;"><font color="#000000" face="Verdana">General Configuration Options</font></span></h3> </td> </tr> <tr valign="top"> <td align="" style=""> <p> <strong>Enable Logging</strong> <input name="General->Logging->Enabled" type="checkbox" ' . htmlify($configxml->General->Logging->Enabled) . ' /></p> </td> </tr> <tr valign="top"> <td align="" style=""> <p> <font color="#000000" face="Verdana" size="2"><strong>Build FBA shipment on the fly</strong> <input name="General->BuildFBA->Enabled" type="checkbox" ' . htmlify($configxml->General->BuildFBA->Enabled) . ' /></font></p> </td> </tr> </tbody> </table> </form></body> </html>'; function htmlify($input) { if ($input == "Yes") return "checked"; else return ""; } ?> processconfig.php snippet Code: [Select] <?php $xmlstring = file_get_contents('config.xml'); $configxml = new SimpleXMLElement($xmlstring); foreach ($_POST as $attrib=>$value){ $configxml->$attrib = $value; } $configxml->asXML('config.xml'); ?> Hello I was trying from days to create an array from a txt file by i can't make it happen. I am so desperate now and i want your help guys The code create a txt file from a textarea feild after it's submited each on a line. After that the php code need collect these data from the txt file and create an array from them. Example: if i type in the textarea: test test1 test2 I want these usernames to be inserted in the array so it would be like this: $names = array('test','test1','test2'); Anyone can help me This is the code <html> <head> <title></title> <style type="text/css"> body{ background-color:#1166aa; } #mainbox{ width:inherit; background-color:#FFFFFF; padding:14px; border:solid 3px #000000; float:left; } #mainbox2{ background-color:#CCCCCC; border:solid 2px #000000; padding:11px; } #contaner{ border:solid 0px #00FF00; } h1:hover{ color:#0000CC; cursor:pointer; } #msg{ cursor:pointer; } table td{ border-top:outset 1px #999999; } #amount{ color:#FF0000; cursor:crosshair; } #to{ color:#3300FF; } #from{ color:#3300FF; } #subject{ color:#3300FF; } </style> </head> <body alink="#003399" vlink="#003399"> <?php ///////////////////////////////////////////////// function create_list(){ $list= ($_GET ['name_list']); $file= "list.txt"; $file_pointer = fopen($file ,'w'); fwrite($file_pointer , $list); fclose($file_pointer); } ////////////////////////////////////////////////// $list= ""; create_list(); ////////////////////////////////////////////////// function explodeRows($data) { $rowsArr = explode("\n", $data); return $rowsArr; } $filename = "list.txt"; $handle = fopen($filename, 'r'); $data = fread($handle, filesize($filename)); $rowsArr = explodeRows($data); for($i=0;$i<count($rowsArr);$i++) { $name = explodeRows($rowsArr[$i]); function name(){ $names = array('the usernames submited to be inserted here','the usernames submited to be inserted here',.......); } } //////////////////////////////////////////////////// if ($_GET ['submit'] == "submit"){ create_list(); name(); } ?> <script type="text/javascript"> function foo(textarea,limit){ var val=textarea.value.replace(/\r/g,'').split('\n'); if(val.length>limit){ alert('You can not enter\nmore than '+limit+' lines'); textarea.value=val.slice(0,-1).join('\n') } } </script> <table align="center" id="contaner"> <tr><td> <form action="<?php $_SERVER [SERVER_NAME]; ?>" method="get"> <div id="mainbox"> <div id="mainbox2"> <table> <tr> <td>name List :</td> </tr> </table> <textarea id="name_list" name="name_list" cols="60" rows="9" onkeyup="foo(this,50)"></textarea> </div> <tr> <td><center><input name="submit" type="submit" value="submit"/></center></td> </tr> </div> </form> </tr></td> </table> </body> </html> Hi all, I'm at a dead-end here. I have a php file that I can query to return an array containing a bunch of data about a job listing. I include and query it by calling it like so within another file on my site. Quick Notes: The original file is in a different directory on my site than the page it's being included in. I have tried returning the array at the end of the script. I can print_r the array on the page I'm including the script on (so the URL path is correct). If I go to this URL directly and print_r on the array, I see the entire array as expected. Also, if I include the file on page 2 like I did above with print_r on the array in the original file, I see the array printed out on page 2. Code: [Select] include 'http://www.example.com/php/job-details.php?job=jobname&city=thecity'; However, if I do not print_r on the array in the original file and just include it on page 2, I cannot do anything with it and the array variable isn't found. Is this a case of variable scope? I'm so frustrated... Here is the code I have in my original file: Code: [Select] <? include('functions.php'); $jobtitlematch = $_GET["job"]; $jobcitymatch = $_GET["city"]; //echo $jobtitlematch; //echo $jobcitymatch; $url = 'http://somesite.com/something'; $xml = simplexml_load_file($url); foreach($xml->job as $job) { $jobtitle = makeURL((string)$job->title); $jobcity = makeURL((string)$job->region); if ($jobtitle == $jobtitlematch && $jobcity == $jobcitymatch) { $jobdata[] = array( Title => ((string)$job->title), URL_Title => makeURL((string)$job->title), Location => ((string)$job->region), URL_Location => makeURL((string)$job->region), Department => ((string)$job->department), URL_Department => makeURL((string)$job->department), Overview => ((string)$job->joboverview), Responsibilities => ((string)$job->responsibilities), Qualifications => ((string)$job->qualifications), Keywords => ((string)$job->metakeywords) ); } return $jobdata; //I have also tried return global $jobdata; } print_r($jobdata); ?> Thanks in advance for any help you can provide... I am trying to use the variable $RATETHESE to include an array into file A. The array is in file B. PhP File A include_once(dirname(__FILE__) . '/PhpFileB.php'); global $RATETHESE; $VALID_RATING_IDS = $RATETHESE; Php File B $RATETHESE = "array("1", "2");" For some reason it is printing the array on the page, and the array is not being used to run the script. Did i do anything wrong? |