blueSky
blueSky

Reputation: 247

only last array element is written properly

$str = "5ad46120704cf.jpg
5ad46120708b7.jpg
5ad4612070c9f.jpg
5ad46120700e7.jpg";

$arr = explode("\n", $str);
foreach ($arr as $el) {
    $sqlb = "select * from banners where fname = '" . $el . "' limit 1";
    $stb = $db->prepare($sqlb);
    $stb->execute();
    $row = $stb->fetch();
    $items .= "<img class='bsingle' src = '../banners/" . $row['fname'] . "' alt='img'>\n";
}
echo $items;

Result:

<img class='bsingle' src = '../banners/' alt='img'>
<img class='bsingle' src = '../banners/' alt='img'>
<img class='bsingle' src = '../banners/' alt='img'>
<img class='bsingle' src = '../banners/5ad46120700e7.jpg' alt='img'>

Why only lyast bsingle has its full source value?

I checked in table - there is no empty cells.

Any help?

Upvotes: 0

Views: 37

Answers (2)

Kapsonfire
Kapsonfire

Reputation: 1033

I guess new line is maybe \r\n and if you just split by the \n the \r is still in it

Work with constant EOL would fix it otherwise try trim()

depending on system the end of line is \r\n or \n php uses \n most systems use \r\n

example:

 $str = 'LINE1'.EOL.'LINE2'.EOL;
 $arr = explode(EOL, $str);

also you could use preg_split instead of explode and work with both variants

http://php.net/manual/de/function.preg-split.php

 $lines = preg_split("/(\r\n|\n|\r)/",$content);

Upvotes: 1

Kapsonfire
Kapsonfire

Reputation: 1033

I guess new line is maybe \r\n and if you just split by the \n the \r is still in it

Work with constant EOL would fix it otherwise try trim()

Upvotes: 1

Related Questions