我刚刚制作了一个从这段代码中提取信息的随机数组。它确实有效,但我试图在图像中加入alt属性,以便屏幕阅读器等人更容易使用它。
<?php
$funfacts[0]['image']="img/fun-fact-white-potato.jpg";
$funfacts[0]['paragraph']="White potatoes were first cultivated by local Indians in the Andes Mountains of South America.";
$funfacts[0]['alt']="White Potato";
$funfacts[1]['image']="img/fun-fact-strawberries.jpg";
$funfacts[1]['paragraph']="Strawberries are not really a fruit or a berry but the enlarged receptacle of the flower. ";
$funfacts[1]['alt']="A pair of Strawberries";
$funfacts[2]['image']="img/fun-fact-oranges.jpg";
$funfacts[2]['paragraph']="British sailors used to be called “Limeys” because they ate citrus to prevent scurvy on long sea voyages.";
$funfacts[2]['alt']="A cut-up Orange";
$funfacts[3]['image']="img/fun-fact-carrots.jpg";
$funfacts[3]['paragraph']="The original carrots used to be purple. Farmers in Holland started planting orange carrots in the sixteenth century.";
$funfacts[3]['alt']="A pile of carrots";
$id_funfacts = array_rand($funfacts);
echo "<img src='".$funfacts[$id_funfacts]['image']. "' alt='".$funfacts[$id_funfacts]['alt'] "' >";
echo "<p>".$funfacts[$id_funfacts]['paragraph']. "</p>";
?>
它告诉我带
的行echo "<img src='".$funfacts[$id_funfacts]['image']. "' alt='".$funfacts[$id_funfacts]['alt'] "' >";
是罪魁祸首,但我不确定它有什么问题。如果我无法解决这个问题,你可以看到我不是真正的PHP大师。
我期待着您的回复。
詹姆斯
答案 0 :(得分:3)
我基本上发现在这行代码之后我错过了一个句号。
echo "<img src='".$funfacts[$id_funfacts]['image']. "' alt='".$funfacts[$id_funfacts]['alt'] "' >";
正确的代码是:
echo "<img src='".$funfacts[$id_funfacts]['image']. "' alt='".$funfacts[$id_funfacts]['alt']. "' >";
如果我错过了那种简单的东西,那么就该睡觉了!