Compare and display using Php - php
I am working on a code where i am having a file.txt which is used like a database and i want to search the content on file.txt in any txt file or json file. But the problem is whenever it searches it skips the 1st line.
Below is my code.
<?php
$gameaudiof = fopen("gameaudio.txt", "a");
$lines1 = file("file.txt");
$data2 = $_FILES['data2']['tmp_name'] or exit("Please select the file");
$data3 = file_get_contents($data2);
foreach($lines1 as $line1){
$line1 = trim($line1);
if(strpos($data3, $line1)){
echo $line1;
echo "\n<br>";
fwrite($gameaudiof, $line1);
fwrite($gameaudiof,"".PHP_EOL);}}fclose($gameaudiof); ?>
Related
Incrementing number inside bloc string using fwrite
im creating file retrieving data from mysql database. $fo = fopen($newName, 'w')or die("can't open file") After querying db, lets start writing. <?php fwrite($fo, $row['entetequestionmonochoix']. PHP_EOL);?> Correspondance to 'entetequestionmultichoix' is the following: \begin{question}{01}\scoring{b=1,e=0,m=0,V=0} im facing a small obstacle when generating file. The while loop allow me to read and then insert but inside the correspondance i need to increment {01} after each loop. \begin{question} **{01}**\scoring{b=1,e=0,m=0,V=0}
Use str_replace() to replace {01} with a string containing an incrementing variable. $i = 1; while ($row = $results->fetch_assoc()) { $line = str_replace('{01}', sprintf('{%02d}', $i++), $row['entetequestionmonochoix']); fwrite($fo, $line . PHP_EOL); }
PHP read from file line by line, declare as variable, then use in MySQL query as WHERE column_name equals variable
Good day, My aim with the script (overview) is to let PHP read from text file line by line then declare the line as variable that will be used in a MySQL statement as a where clause into an array, then ultimately writing to another text file. CODE: $lines = file('/root/prcode'); foreach($lines as $line) { $query = "select * from $db_table where code=$line"; $result = mysqli_query($conn,$query); while($row = mysqli_fetch_array($result, MYSQLI_NUM)) { $out = "$row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7], $row[8],$row[9],$row[10],$row[11],$row[12],\n"; //file_put_contents($outfile, $out); //$fp = fopen($outfile, 'w'); //fwrite($fp, $out); //fclose($fp); echo $out; //echo $line; //echo $query; //echo $result; //echo $row; }; } File prcode's first 10 lines: 60025909170 * .05005140160 0000000000000000 0000000000000001 0000000000000004 0000000000000005 0000000000000007 0000000000000010 0000000000000011 OUTPUT when script is run from Linux shell: 0010,0000,60025909170,01,,,,,,,,,, 0010,0000,60025909170,02,,,,,,,,,, 0010,0000,60025909170,03,,,,,,,,,, 0010,0000,60025909170,04,,,,,,,,,, 0010,0000,60025909170,05,,,,,,,,,, 0010,0000,60025909170,06,,,,,,,,,, 0010,0000,60025909170,07,4.000,36.960,-4.000,-36.960,,,,,, 0010,0000,60025909170,08,4.000,36.960,,,,,,,, 0010,0000,60025909170,09,4.000,36.960,,,,,,,, 0010,0000,60025909170,10,4.000,36.960,,,,,,,, 0010,0000,60025909170,11,4.000,36.960,,,,,,,, 0010,0000,0000060025909170,01,,,,,,,,,, 0010,0000,0000060025909170,02,,,,,,,,,, 0010,0000,0000060025909170,03,,,,,,,,,, 0010,0000,0000060025909170,04,,,,,,,,,, 0010,0000,0000060025909170,05,,,,,,,,,, 0010,0000,0000060025909170,06,,,,,,,,,, 0010,0000,0000060025909170,07,,,,,,,,,, 0010,0000,0000060025909170,08,,,,,,,,,, 0010,0000,0000060025909170,09,,,,,,,,,, 0010,0000,0000060025909170,10,-4.000,-.040,4.000,.040,,,,,, 0010,0000,0000060025909170,11,-4.000,-.040,,,,,,,, 0010,0000,60025909170,12,4.000,36.960,,,,,,,, 0010,0000,0000060025909170,12,-4.000,-.040,,,,,,,, From the output, it can be established that the order of the text file is not being followed as 60025909170 and 0000060025909170 are 2 different products and 0000060025909170 is at about line 32000 <- my first problem. To try to rectify this and cater for following the order, the special characters and spaces I tried: $query = "select * from $db_table where code='$line'"; \\causes empty output $query = "select * from $db_table where code=\"$line\""; \\causes empty output $query = "select * from $db_table where code=\'$line\'"; \\empty output The echos below were just to test to see which variables are set or not to try to further troubleshoot: echo $out; //echo $line; //echo $query; //echo $result; //echo $row; The second problem is, none of the attempts to write to file worked, it would either write just a single line and nothing more or would not write at all. Any advise or guidance on how to possibly fix my 2 issues?
1st issue: Likely, your file is being read in order, but your query is finding all of those rows. You can see if this is happening by adding the line number to your output. Try changing your foreach to foreach($lines as $lineNum => $line) { and your output to $out = $lineNum.implode(',', $row)."\n"; If you have the same lineNum for 60025909170 and 0000060025909170, then you know that the query is matching both. 2nd issue: You just need to add a flag of FILE_APPEND to the file_put_contents. Like this: file_put_contents($outfile, $out, FILE_APPEND);
How to export data from MySQL to CSV?
I am trying to export data from MySQL to CSV using PHP from a website. When a user clicks generate the report it will automatically create and this will be downloaded as a file on their computer. The code below I have been following an online tutorial but I have run into some issues. Currently, it will download but will only show one row with one date value from the MySQL data. I was wondering what I need to do to show all the data in the csv file and how do I ensure that the names of the Rows are printed as well. #header("Content-Disposition: attachment; filename=record.csv"); $select = "SELECT * FROM DBtable WHERE user_id=$id"; $result2 = $conn->query($select); while ($row = $result2->fetch_assoc()) { $data = $row['pain']."\n"; $data = $row['sleep']."\n"; $data = $row['mood']."\n"; $data = $row['heartrate']."\n"; $data = $row['time_of_entry']."\n"; } echo $data; exit();
Instead of setting the $row values to $data, you should echo instead #header("Content-Disposition: attachment; filename=record.csv"); $select = "SELECT * FROM DBtable WHERE user_id=$id"; $result2=$conn->query($select); while($row=$result2->fetch_assoc()){ echo $row['pain']."\n"; echo $row['sleep']."\n"; echo $row['mood']."\n"; echo $row['heartrate']."\n"; echo $row['time_of_entry']."\n"; } exit(); Your code just keeps setting $data to a rows value, without actually doing anything with it. Each line overwrites the previous, so when you do echo $data, you are echoing the last value it was set to which in your case was the last loops "time of entry" value. As for adding "names of the Rows", currently the code puts in a value then a new line. You probably want to replace "\n" with ",", so one loops data is one one line, then before the closing while loop, echo a "\n" to insert a new line character. You can then put the titles in the same way, using an echo, and comma seperated titles, followed by a new line character. For example echo $row['pain'].","; echo $row['sleep'].","; echo $row['mood'].","; echo $row['heartrate'].","; echo $row['time_of_entry'].","; echo "\n";
Can't save Arabic text from csv to txt file
I have a csv file like this: region_name, city_name, district_name, ppm_value منطقة, مدينة , حي , 220.92 ... All columns are strings in Arabic, except ppm_value which is a float number. I'm trying to process the csv file and reproduce it in json format. I tried this: $districts = array(); $fd = fopen('data2.csv', 'r'); while ($row = fgetcsv($fd)) { $region = $row[0]; $city = $row[1]; $district = new stdClass(); $district->name = "".$row[2]; $district->ppm = $row[3]; //also add region and city to $district $districts[] = $district; } $json1=json_encode($districts); file_put_contents("text.txt",$json1); This produces the following: [{"name":null,"ppm":"220.92","region":null,"city":null}, ... ] The float is saved correctly, but the Arabic strings are all null. I even tried to save it like this but I got the same result: $myfile = fopen('text.txt', "w") or die("Unable to open file!"); fwrite($myfile, json_encode($districts)); fclose($myfile);
I know next to nothing about php, but perhaps the problem is that your arabic text is being encoded as ASCII? try changing it to unicode before you add it to you JSON object, you probably know how to do this better than I do... string utf8_encode ( string $data )
Opening Text File in PHP update Database
Okay this will be my last post here for today, have asked you guys enougth questions about why my terrible code dosnt work :) I am trying to run a query to update all values of a column with a text documents contents. My database looks like follows: Attribute_id | value 64 | Data.txt 109 | other data 64 | Data2.txt 109 | other data If you look at my script you can see what I am trying to do, for now I am echoing the query the end result is here: http://dev.central-pet-supplies.co.uk/test.php it is returning UPDATE mage_catalog_product_entity_text SET value= 'Array' WHERE value = '13685.txt' however array should be the text files contents Here is the script: <?php mysql_connect ("localhost","cpsdev_mage1","********"); mysql_select_db ("cpsdev_mage1"); $sql = "select value from mage_catalog_product_entity_text WHERE Attribute_id = 64"; $result = mysql_query ($sql) or die($myQuery."<br/><br/>".mysql_error()); while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $lines = file('http://dev.central-pet-supplies.co.uk/media/import/' . $row['value']); $query3 = "UPDATE mage_catalog_product_entity_text " . " SET value= '" . $lines . "' WHERE value = '" . $row['value'] . "'"; echo $query3 ."<br>"; } ?> Edit: changed file to file_get_contents and now working.
file() reads in the entire file with each line as a new entry in an array. Since you don't want that, you can either join the lines together using implode() by putting $lines = implode( "\n", $lines); After: $lines = file('http://dev.central-pet-supplies.co.uk/media/import/' . $row['value']); Or, by using file_get_contents() instead of file(), which will return the entire file's contents as a string, instead of an array: $lines = file_get_contents( 'http://dev.central-pet-supplies.co.uk/media/import/' . $row['value']);
You dont show where $lines comes from but its an array i assume of lines in the text file. Use something like file_get_contents to get the contents of the text file as a single string instead.