5

I am trying to populate HTML table with the data comes from my database. Here I have stored "services" in my table. Each service may have multiple images. So when populating the table it should have 3 table cells one for "service name" and second for "description" and third one for images.

This is my SQL query looks like:

$prep_stmt = " SELECT s.id
                    , s.name
                    , s.description
                    , i.image
                    , i.image_path
                FROM  services s
                LEFT JOIN images i ON i.service_id = s.id";

This is how my while look like this:

while ($stmt->fetch()) {        
    $html  = "<tr>\n";  
    $html .= "  <td><input type='checkbox'></td>\n";    
    $html .= "  <td>\n";    
    $html .= "      <a href='' class='name'>{$name}</a>\n";     
    $html .= "  </td>\n";   
    $html .= "  <td class='view_html'>{$description}</td>\n";   
    $html .= "  <td>\n";    
                --- My images should be display here ---- 
    $html .= "  </td>\n";   
    $html .= "</tr>\n";                 

    //Add output to array
    $output[] = $html;  
}

My problem is How I display multiple images in one table cell? If one service have only one image then I can do it, but it has multiple images then I am not sure how to do it.

4
  • first store those values in an array, then use that array for generating the html table Commented Jun 29, 2015 at 3:47
  • @FerozAkbar, Thank you for your comment. But I an not sure how to do it. Can you kindly show me with an example? Thank you. Commented Jun 29, 2015 at 3:52
  • 1
    I'd take out the join. Pull all services then query all images at the HTML creation when needed. As is you'll get multiple rows per ID, which I don't think you want. Commented Jun 29, 2015 at 3:56
  • If I understand what you are doing in the database, @chris85 is correct. Using a join here is just going to give you multiple lines for your services for each picture associated with the service. Using 2 queries will allow you to place multiple images in the final column for each service. Commented Jun 29, 2015 at 3:59

1 Answer 1

7

Change your sql code as below and try

 $prep_stmt = " SELECT s.id
                    , s.name
                    , s.description
                    , (select group_concat(i.image_path,'/',i.image)  from images i where i.service_id = s.id) as img
                FROM  services s";

Then use this Html code

    while ($stmt->fetch()) {        
    $html  = "<tr>";  
    $html .= "  <td><input type='checkbox'></td>";    
    $html .= "  <td>";    
    $html .= "     <a href='' class='name'>{$name}</a>";     
    $html .= "  </td>";   
    $html .= "  <td class='view_html'>{$description}</td>";   

    $html .= "  <td>";
    $img_array=explode(",",$img);
    foreach($img_array as $im){
        if($im==''){
         $html .= " <img src='default.jpg'>";   
        }else{
         $html .= " <img src='{$im}'>";   
        }
    }
    $html .= "  </td>";

    $html .= "</tr>";                 

    //Add output to array
    $output[] = $html;  
}
Sign up to request clarification or add additional context in comments.

5 Comments

GROUP_CONCAT is cool. I didn't know you could return an array like that.
Changed sql query. Try again
Can you kindly explain updated mysql query and why it didn't work earlier one?
Previous sql that I updated; group all images thats why u got one row . But actually u want to get images that are associated with service ID.
@Sameeraa4ever, one more question. Is there way to display a default image if $im is null?

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.