0

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

1 Answer 1

1

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>';         
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I was making too much of it in my head, I think. That's just the thing I was looking for. Thanks so much. I appreciate the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.