我刚刚制作了一个从这段代码中提取信息的随机数组。它确实可以工作,但我试图在图像中添加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专家。
我期待你的回信。
詹姆斯我基本上发现我在这行代码后面漏了一个句号。
echo "<img src='".$funfacts[$id_funfacts]['image']. "' alt='".$funfacts[$id_funfacts]['alt'] "' >";
正确的代码是:
echo "<img src='".$funfacts[$id_funfacts]['image']. "' alt='".$funfacts[$id_funfacts]['alt']. "' >";
如果我错过了这样简单的事情,那么是时候睡觉了!!