Kodundan, o $data2
DB 1 satır verileri temsil söyleyebilirim.
Yani, $data2
o göstermek istediğiniz şeyleri içerir olmalıdır.
Sizin foreach döngü yararsız: Eğer 1 hattı verileri görüntülemek isterseniz, sadece doğrudan $data2
kullanabilirsiniz.
If you want to go line by line, you must not iterate over $data2, but loop while mysql_fetch_array returns data.
Böyle bir şey, ben söylemek istiyorum:
$result2 = mysql_query("SELECT id,fp_thumb,title FROM media") or die(mysql_error());
while ($data2 = mysql_fetch_array($result2)) {
echo '<li><a href="media.php?id='.$data2['id'].'"><img src="'.$data2['fp_thumb'].'" alt="'.$data2['title'].'" /></a></li>';
}
Ayrıca, sizin emin HTML / javascript enjeksiyon herhangi bir tür yok olmak, çıktılamak olan verileri kaçmak isteyebilirsiniz - en azından src ve başlık, derim; Bunun için, size htmlspecialchars
a> kullanabilirsiniz
Sizin yankı, sonra, muhtemelen bu gibi görünecektir:
echo '<li><a href="media.php?id='
. $data2['id']
. '"><img src="'
. htmlspecialchars($data2['fp_thumb'])
. '" alt="'
. htmlspecialchars($data2['title'])
. '" /></a></li>';
(I considered id is an integer, auto-incremented or something like that, so not much of a risk)
Edit after seeing the comment on another answer
İlk kullanılan ne:
$ Veri2 bu gibi görünen bir ilişkisel dizidir:
array
'id' => int 152
'title' => string 'this is the title' (length=17)
'fp_thumb' => string 'http://...../' (length=13)
Foreach döngü olacak üç kez demektir, hangi $ val olacak:
int 152
string 'this is the title' (length=17)
string 'http://...../' (length=13)
(One value for each iteration)
yani, $ val bir dizi skaler bir değer değildir.
When you do that :
$a = 152;
var_dump($a['id']);
Alacağınız
null
Yaparken
$a = 'this is the title';
var_dump($a['id']);
Alacağınız:
string 't' (length=1)
Bir dize dizi-erişimini kullanırken, yani sadece bir dize bir anahtar ile, ilk karakteri olsun.
Note that array-access on a string, with a numeric key, is used to access one character of the array :
$a = 'this is the title';
var_dump($a[3]);
seni alır:
string 's' (length=1)
For more informations, see the manual page about string, especially String access and modification by character, which states :
Characters within strings may be
accessed and modified by specifying
the zero-based offset of the desired
character after the string using
square array brackets, as in $str[42].
Think of a string as an array of
characters for this purpose.
Ve:
Non-integer types are converted to
integer.
And converting 'title' to an integer gives 0 ; so, Alacağınızthe first character of the string.
Hope this helps, and you understood "why" ;-)