9

The below is the php code i am using

$file_handle = fopen("products.csv", "r");
$fname = "products.csv";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$server = "**\******,1433";
$connectionInfo = array( "Database"=>"******", "UID"=>"***", "PWD"=>"*******" );
$conn = sqlsrv_connect( $server, $connectionInfo ); 

if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}

while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 1024);
    $itemco = $line_of_text[0];
    $sql = "SELECT quantity FROM Item WHERE itemlookupcode = '$itemco' "; 
    $stmt = sqlsrv_query( $conn, $sql );

    if( $stmt === false) {
        die( print_r( sqlsrv_errors(), true) );
    }

    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
        $icc = $row['quantity'];
        $content = str_replace("$line_of_text[1]", "$icc", "$content");
    }
}

$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);

str_replace isn't working in the following instance:

$content = str_replace("$line_of_text[1]", "$icc", "$content");
Jack
  • 1,901
  • 1
  • 19
  • 32
Max
  • 229
  • 2
  • 6
  • 18
  • 3
    Get rid of the quotes around your variable names inside of `str_replace()` – John Conde Mar 28 '14 at 17:37
  • 1
    @JohnConde is right, its taking the literal characters "$icc" and "$content" – Beau Bouchard Mar 28 '14 at 17:40
  • Thanks i tried it before but it doesn't work. – Max Mar 28 '14 at 17:45
  • can you add little more detail, 3 2 value examples. What comes in $line_of_text, $icc and $content. str_replace needs to do replacement and it will do whenever possible. – justnajm Apr 07 '14 at 05:52
  • 1
    The quotes would be irrelevant in this case; it's only single quotes that preserve the literal dollar sign. They still do not need to (and should not) be there however. – Matt Humphrey Apr 07 '14 at 11:54
  • Why you are using double quotes in str_replace parameters..? they are variables.. – Haroon Apr 07 '14 at 18:20
  • Double quotes are okay, its not an issue, even its better to use it in case if its not an string, it will be converted to string instead of throwing error. Can you please define: str_replace is "NOT WORKING". What do you mean by "not working"? What are you expecting and what is the value that you are getting ? You can break the loop and check the content of variables and output. – Rahul Prasad Apr 07 '14 at 20:21

5 Answers5

4

Remove quotes around variables.

$content = str_replace("$line_of_text[1]", "$icc", "$content");

Should be

$content = str_replace($line_of_text[1], $icc, $content);

//str_replace ( search, replace, subject )   This is how it works.

Next,

Use this code corresponding to the while loop

$count=1;
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$icc = $row['quantity'];
$content = str_replace($line_of_text[$count++], $icc, $content);
}
Sasanka Panguluri
  • 3,058
  • 4
  • 32
  • 54
4

First of all, you open the same file three times and only close it once. This is not a major problem but you really should make a habit of finishing/closing what you started. You can skip one of those fopen by using file_get_contents:

$content = file_get_contents("products.csv");

Now, str_replace will replace every occurrence of your string. If, for example, $line_of_text[1] is "11" and $icc is "9", other values in the file such as "110" will be affected, thus making the rest of future replacements less and less reliable each loop.

If I'm not mistaken and all you want to do is replace a single cell in each row, here's a suggestion: Create an empty variable, let's say $new_content

Now, change your loop:

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
    $line_of_text[1] = $row['quantity']; // change cell value
    $new_content .= implode(',', $line_of_text).','; // convert into string with extra
                                                     // ',' to avoid merging cells
}
$new_content = trim($new_content, ','); // remove extra ','
Gil
  • 1,794
  • 1
  • 12
  • 18
1

No debug output data.. I can only give some advice..

  • Does $line_of_text[1] and $icc output as expected values?
  • Does sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) returns a proper value? (If not it will won't do anything)
  • Hows your csv file structured?
Sam Ye
  • 184
  • 6
  • .Does $line_of_text[1] and $icc output as expected values? Yes.. Does sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) returns a proper value? Yes...Hows your csv file structured? I dont get what this is mean the only problem is when row have same $line_of_text[1] value .. like row 1 $line_of_text[1] = '1' row 2 $line_of_text[1] = '1' except this it works but only each of $line_of_text[1] has same value Thank you so much – Max Apr 04 '14 at 15:47
1

I'm not sure I understand, but there is no reason that str_replace does not work.
It may be just a matter of reasoning.
It does not work at all or only in some cases?
If it is in some cases, it may be because the replacement value no longer exists.

For example:

$content = "1234";
echo "$content\n"; // echo "1234"
$content = str_replace("1", "a", "$content");
echo "$content\n"; // echo "a1234"
$content = str_replace("2", "b", "$content");
echo "$content\n"; //  echo "ab34"
$content = str_replace("1", "c", "$content");
echo "$content\n";  // echo "ab34" => no change because '1' is not existe -> already replace

I do not know if this is your problem, but it could explain.

So may be you should do something like this:

$pos1 = stripos("$content", "$line_of_text[1]");
if ($pos1 !== false)
    $content = str_replace("$line_of_text[1]", "$icc", "$content");
else
    echo "$line_of_text[1] not found in $content \n";

Otherwise, I agree with the others, the double quotes are optional. :)

Good Luck.

doydoy44
  • 5,720
  • 4
  • 29
  • 45
1

$content = str_replace("$line_of_text[1]", "$icc", "$content");

this should be replaced with

$content = str_replace($line_of_text[1], $icc, $content);

since these are php variables and putting them in double or single quotes is incorrect, as they will be considered as normal text and str_replace will try to replace "$line_of_text[1]" with "$icc" in "$content".

kkarayat
  • 392
  • 1
  • 5
  • 14