PHP - Im Confused With Large Csv Reading Line By Line
i have to read a single line from a csv, its a really big file and i only need one column.
i need the response to be a string ,i made a search and found the following code but i dont have any idea how to get a single line from a single string per run .
<?php $row = 1; //open the file if (($handle = fopen("file.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); } ?> Edited by bores_escalovsk, 16 May 2014 - 06:38 PM. Similar TutorialsI have a script that reads a .gz file into an array and prints the name of each record but will not work on larger files. Is there a way to read 1 line at a time? Here is the code I have so far. Code: [Select] <?php if ($handle = opendir('.')) { print "<ol>"; while (false !== ($file = readdir($handle))) { if($file != '..' && $file!="." && $file!="start_update.php" && $file!="sharons_dbinfo.inc.php" && $file!="root.php" && $file!="read_directory.php" && $file!="read_dir.php" && $file!="new_category.php" && $file!="index.php" && $file!="file_count.php" && $file!="dir_loop2.php" && $file!="dir_loop1.php" && $file!=".htaccess" && $file!="Answer.txt" && $file!="Crucial_Technology-Crucial_US_Product_Catalog_Data_Feed.txt"){ $filename = $file; $go = filesize($filename); if($go >= 1){ $filename2 = explode("-", $filename); $filename2 = $filename2[0]; echo str_replace("_"," ",$filename2) . ' | Filesize is: ' . filesize($filename) . ' bytes<br>'; $gz = gzopen($filename, 'r'); $lines = gzfile($filename,10000); foreach ($lines as $line) { $line2 = explode(",", $line); $line2 = str_replace("," , "-" , $line2); echo "<li>".str_replace("," , "-" , $line2[4])."</li><br>"; } } } } closedir($handle); } ?> </ol> what im trying to do is take a youtube embed code find the URL code for that video and remove all other parts of the code keeping only the URL of the video after i get the URL by it self then replace the http://www.youtube.com/ part of the URL with http://i2.ytimg.com/vi/ to do this i know that i need something like this to get the URL of the video http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+) but how to only return just the URL is something idk how to do then use str_replace http://www.youtube.com/(?:v|cp)/" with http://i2.ytimg.com/vi/ so in the end im asking if anyone know how to remove all other codes i dont want and only keep the URL this may have to be done using "regex" im not sure as i dont know to much about regex and what it can do but does sound like it would help lol Hi. I want a simple textbox, that when submited, will replace very every new line, with the <br> tag. What will happen, when it submits, it will take the contents of a textbox, add the <br> tag where a new line is suposed to be, and save the string to a MySQL Database. I think this is the easiest way of allowing a user to edit what appears on a website when logged in, but if there is a easier way, then please tell me. What I am trying to do, is a login page, and once logged in, you enter new text into the textbox, click submit, and on the website homepage, the main text will change to what was submitted. But if there is a new line, I think the only way in HTML to make one is to put a <br> tag, so I want the PHP to but that tag wherever there is a new line. Sorry if I am confusing, I am not that advanced at PHP, but I would be very happy if you could supply me with the correct code. Time is running out... If you do not understand me, please tell me -- PHPLeader (not) Dear All Good Day, i am new to PHP (a beautiful server side scripting language). i want to send a mail with line by line message i tried with different types like by placing the things in table and using <br /> but the thing is the tags are also visible in the message of the mail. Here is my code: $message1 = "Name :". $_REQUEST['name']."<br />"; $message1 .= "Surname :". $_REQUEST['surname']."<br />"; $message1 .= "Cellphone :". $_REQUEST['mobileno']."<br />"; $message1 .= "Telephone :". $_REQUEST['landno']."<br />"; $message1 .= "Fax :". $_REQUEST['fax']."<br />"; $message1 .= "Company :". $_REQUEST['company']."<br />"; $message1 .= "Email :". $_REQUEST['email']."<br />"; $message1 .= "Country :". $_REQUEST['country']."<br />"; $message1 .= "Enquity :". $_REQUEST['enquiry']."<br />"; $message1 .= "Date&Time :". $date."<br />"; For this code if try to print/echo it it is working fine it is displaying line by line, but using this variable ($message1) in the mail these <br /> are also visible. Can any one guide me to resolve(to remove these tags from the message part) this issue. Thanks in Advance. :confused: How to get this echo line to display as one line? No matter what I have done it displays as two lines. I even tried <nobr></nobr> Teachers Name: John Jones $userid = mysql_real_escape_string($_GET['user_id']); $sql = "select * from users where `id`='$userid' "; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { echo "<h3>Teachers Name: </h3>" . $row["first_name"] . " " . $row["last_name"] ; } Thanks for your help. I'm so sorry to ask such a "newbee question," but believe me I have been on Google for the better part of the week trying to find an answer.
I'll start with the brief question.
Then I'll give an example to show what I mean.
Then, I'll give the BESTEST "pseudo-code" I could come up with (to PROVE that I've really given it my best).
Question:
How do I make PHP loop through a big file, make the changes line by line, and to save the resultant file.
Example: I have a 100mb text file ("animals.txt") with 500,000 lines. If any lines have the word "cat" in it, I want to add "Be careful with cats!" to the end of the line:
From this:
A fish and his tank.
A cat and his toy.
A bird and her cage.
A frog and his lilly.
A cat and her friend.
To this:
A fish and his tank.
A cat and his toy. Be careful with cats!
A bird and her cage.
A frog and his lilly.
A cat and her friend. Be careful with cats!
The best "pseudo-code" I can come up with is:
<?php
$data = file_get_contents("animals.txt");
$lines = explode(PHP_EOL,$data); I want my application will send a email after 10 minutes of sending another email. In my application A user completes registration with payment Application sends the user a payment confirmation emailNow I want to send another email 10 minutes After payment confirmation email with welcome tipsBelow is the function where for user setup .
public function finishUserSetup($Sub){ if($Sub == 0){ $subscription = SubscriptionPlans::where('identifier', '=', "Monthly")->first(); $expiry = date('Y-m-d', strtotime('+' . $subscription->months . ' months')); $sub_period = "monthly"; } else{ $subscription = SubscriptionPlans::where('identifier', '=', "Annually")->first(); $expiry = date('Y-m-d', strtotime('+' . $subscription->months . ' months')); $sub_period = "annually"; } $this->expiry_date = $expiry; $this->user_type = "SUB"; $this->subscription_period = $sub_period; $this->update(); $replaceArray = array( 'fullname' => $this->forename . " " . $this->surname, 'subscriptionName' => $subscription->name, ); EmailTemplate::findAndSendTemplate("paymentconfirm", $this->email, $this->forename . " " . $this->surname, $replaceArray); } In the above function the last line of code is the one which sends a payment confirmation email to the user which is EmailTemplate::findAndSendTemplate("paymentconfirm", $this->email, $this->forename . " " . $this->surname, $replaceArray); I want to execute the following line of code 10 minutes after the above one
EmailTemplate::findAndSendTemplate("WelcomeTips", $this->email, $this->forename . " " . $this->surname, $replaceArray);
How to do that. that is running the last line of code 10 minutes after gday all! just starting to learn php but having a liitle problem with \n forcing a line break. in my situation it simply doesnt. looking at this what have i done wrong? Code: [Select] echo "<div class=\"imgcontainer\"><a href='$row[filename]' rel='shadowbox[thingo]'><img class= 'thumbnail' img src='$row[filename]'/></a><div class=\"thetext\">Artwork: $row[name]\nArtist:$row[artist]</div></div> "; Thanks I'm trying to insert the contents of a textarea into a MySQL database but I am wondering what the best way is to preserve the users line breaks. I know that you can use nl2br() to convert "\n" into "<br />" when the retrieve data from the database but I am looking at a way to insert these line breaks when I enter the data. Thanks for any help. echo "<div id='wrap'><div id='header'>" . implode("</div>", array_keys($sports)) . ; I get the following error: Parse error: syntax error, unexpected ';' in /hermes/bosweb/xxxxx/xxx/xxxxxxxxxxxxxxxx/xxxxx/xxx_xxxx/xxx_xxxxxx.php on line 75 The above line is 75. I have changed the quotes using ' ' or " " and no matter what I do I continue to get errors.. where am I going wrong here? Thanks! I've looked it, and I guess it's right in front of me but I don't see it. Parse error: syntax error, unexpected T_STRING, expecting ']' in /home/a8079066/public_html/view.php on line 24 echo "<b>Name:</b> ". $query['name'] ."<br/><b>Description:</b> ". $query['description'] ."<hr><a href='/mods/". $query['id'] .".zip'><h2>[DOWNLOAD]</h2></a><hr>"; Hi.. I have a php file where i have only one line which says header('Location: index.php'); How do i run this php file in the terminal on fedora 14? Thanks, Rohit Honestly I've been staring at this for 3 hours LOL and I just can't get it to work.. A little help please would be so awesome. $link = '<a rel="lightbox[social]" title="DCC Videos" href="echo (str_replace("&feature=youtube_gdata","", $video['link']))">'; To be more precise this is actually the part that's not working... href="echo (str_replace("&feature=youtube_gdata","", $video['link']))">'; That line right there is my problem... whew... thrown myself a little I think... Thank you for the help! I want to have a line break between the lastname and the age. Please help. echo $line["firstname"].' '.$line["lastname"].' Age is'.$line["age"].'!'; Code: [Select] $result = mysql_query("SHOW COLUMNS FROM ".$table.""); $i = 0; if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $csv_output .= $row['Field'].","; $i++; } } $csv_output .= "\n"; $values = mysql_query("SELECT * FROM ".$table.""); while ($rowr = mysql_fetch_row($values)) { for ($j=0;$j<$i;$j++) { $csv_output .= $rowr[$j].", "; } $csv_output .= "\n"; } print $csv_output; I'm using the above to get a csv output in my browser, but it appears as a string of text eg a1,a2,a3,a4,b1,b2,b3,b4,... I'd like it to appear as a1,a2,a3,a4 b1,b2,b3,b4 c1,c2,c3,c4 Any ideas how?
I am getting an error with this line... I tried to do this: explode(" ",$cp['size'])[0] and it isn't working obviously. The reason I want it in one line is its in the middle of a big string and I'd like to keep all the code in one area. Is their a right way to do that other than setting it to a variable first? ok... so my page.php works perfectly when it is receiving date ie the url is page.php?id=x, but how do i set a default for the page? for example if someone browses to page.php at the moment i just get an error message: "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 '' at line 1" shouldnt it echo Result not found? what have i done wrong?? php below: <?php $max_columns = 3; include_once "include/globals.php"; //globals contains db connection if (isset($_GET['cat-id'])) { $id = $_GET['cat-id']; } ?> <?php //Get database Results $result = mysql_query("SELECT * FROM Products WHERE is_active = 1 AND catagory_id = $id ") or die(mysql_error()); if(mysql_num_rows($result)===0) { $output = "No records found.\n"; } else { $output = "<table>\n"; //keeps getting the next row until no more records $recNo = 0; while($row = mysql_fetch_array($result)) { $recNo++; //Start new row when needed if($recNo%$max_columns==1) { $output .= "<tr>\n"; } //Create TD for record $output .= "<td><div class=\"table-bg\">"; $output .= "<div class=\"title\">{$row['Name']}</div>"; $output .= "<div class=\"image\">"; $output .= "<a href=\"product_detail.php?id=\"><img src=\"{$row['image']}\" width=\"100\" alt=\"\" border=\"0\"></a>"; $output .= "</div>"; $output .= "<div class=\"tag_line\">{$row['Tag_Line']}</div>"; $output .= "<div class=\"price\">Now Only: £{$row['Website_Price']}</div>"; $output .= "<div class=\"prod-footer\"><a href=\"product_detail.php?id={$row['ID']}\">more info</a></div>"; $output .= "</div></td>\n"; //Close row when needed if($recNo%$max_columns==0) { $output .= "</tr>\n"; } } //Close final row if needed if($recNo%$max_columns!=0) { $output .= "</tr>\n"; } $output .= "<table>\n"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <link href="styles/stylesheet.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="Wrapper"> <?php include "include/header.php"; ?> <?php include "include/thinnav.php"; ?> <?php include "include/nav.php"; ?> <div id="main"> <?php //echo $output; ?> </div> <?php include "include/right-nav.php"; ?> <?php include "include/footer.php"; ?> </div> </body> </html> pleasse help this is doing my nuts in........... |