回显多维数组时使用表单数据作为已知键值

发布于 2024-12-14 20:19:47 字数 1339 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2024-12-21 20:19:47

告诉我这是否有效。我将最后一个 foreach 更改为对每个字母图片进行 foreach 。 (假设每个字母的查询返回多个结果)

"SELECT * FROM Photos WHERE letter IN ($lettertype)"; 

无论如何,它就是这样。

$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[$alpha] as $tempvar){                                      
            echo "<img class='editable' img src='../Letterproject/images/{$tempvar['photoPath']}' width='100' height='140'></src>";                                      
    }              
    echo '</div>';         
}

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)

"SELECT * FROM Photos WHERE letter IN ($lettertype)"; 

Anyways here it is.

$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[$alpha] as $tempvar){                                      
            echo "<img class='editable' img src='../Letterproject/images/{$tempvar['photoPath']}' width='100' height='140'></src>";                                      
    }              
    echo '</div>';         
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文