回显多维数组时使用表单数据作为已知键值
我有一个 php 脚本,它将输入到表单中的各个字符作为 mysql 查询的输入,然后使用 foreach 循环遍历结果数组,以回显代表每个字符的艺术品图像(即花的六个图像)。
但是,如何在同一个 foreach 中创建第二个 echo,以便每个输入字母之后的下一个 div 填充仅该字母的整个组合(例如 db 中字母“r”的六个图像记录)。正如我目前所拥有的,每个 div 都填充了所有输入字符的组合。我如何获得它,以便字母 r 之后的 div 将只是字母“r”的六个图像?
感谢您的帮助!在过去的几周里,我对所有这些脚本编写都是新手,所以请对我宽容一些。我真的很感激任何见解。
$lettertype = str_split($lettertype);
$lettertype = "'" . implode("','", $lettertype) . "'";
$query = "SELECT * FROM Photos WHERE letter IN ($lettertype)";
$result = mysqli_query($cxn, $query)
or die ("No good");
$alpharray = array();
while($row = mysqli_fetch_assoc($result)){
$alpharray[$row['letter']][] = $row;
}
foreach(str_split($_POST['search_term']) as $alpha){
echo "<img class='clickable' img src='../Letterproject/images/{$alpharray[$alpha][0]['photoPath']}' width='100' height='140'></src>";
echo '<div class="editimages">';
foreach ($alpharray as $tempvar){
foreach($tempvar as $oneletter){
echo "<img class='editable' img src='../Letterproject/images/{$oneletter['photoPath']}' width='100' height='140'></src>";
}
}
echo '</div>';
}
I have a php script that takes the individual characters entered into a form as the input to a mysql query and then loops through the resulting array with foreach to echo one image of artwork representing each of these characters (i.e. six images for f-l-o-w-e-r).
But how do I create a second echo within the same foreach such that the next div after each inputted letter is populated with the entire portfolio of just that letter alone (e.g. six records of images for letter "r" in db). As I currently have it, each div is populated with the portfolios of all characters inputted. How do I get it so the div after the letter r will be only six images of the letter "r"?
Thanks for any help! I'm new to all this scripting stuff in the past few weeks, so go easy on me. I really appreciate any insight.
$lettertype = str_split($lettertype);
$lettertype = "'" . implode("','", $lettertype) . "'";
$query = "SELECT * FROM Photos WHERE letter IN ($lettertype)";
$result = mysqli_query($cxn, $query)
or die ("No good");
$alpharray = array();
while($row = mysqli_fetch_assoc($result)){
$alpharray[$row['letter']][] = $row;
}
foreach(str_split($_POST['search_term']) as $alpha){
echo "<img class='clickable' img src='../Letterproject/images/{$alpharray[$alpha][0]['photoPath']}' width='100' height='140'></src>";
echo '<div class="editimages">';
foreach ($alpharray as $tempvar){
foreach($tempvar as $oneletter){
echo "<img class='editable' img src='../Letterproject/images/{$oneletter['photoPath']}' width='100' height='140'></src>";
}
}
echo '</div>';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
告诉我这是否有效。我将最后一个 foreach 更改为对每个字母图片进行 foreach 。 (假设每个字母的查询返回多个结果)
无论如何,它就是这样。
Tell me if that works. I changed the last foreach to do a foreach for every letterpicture there is. (assumes multiple results come back from the query for each letter)
Anyways here it is.