Tan
Tan

Reputation: 33

Using form data as known key value when echoing a multidimensional array

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>';         
}

Upvotes: 0

Views: 94

Answers (1)

jakx
jakx

Reputation: 758

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>';         
}

Upvotes: 1

Related Questions