www.webdeveloper.com
Results 1 to 3 of 3

Thread: Looping problem on display of images to php table

  1. #1
    Join Date
    Dec 2012
    Posts
    2

    Looping problem on display of images to php table

    I want to display different images in a 5 by 3 table.

    W1 W1 W1
    W2 W2 W2
    W3 W3 W3
    W4 W4 W4
    W5 W5 W5

    Here is my code
    Code:
    $ds ='\image';
    $imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
    echo "<pre>"; print_r($imagefile);
    $file = 'blank.png';
    $d = $ds.$file;
    
    echo "<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">";
    for($row=1;$row<=5;$row++){
       echo "<tr>";
    
          for($col=1;$col<=3;$col++){
    
               // echo"<td height=60px>W$row</td>";
               //if()
               echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
    
          }
       echo "</tr>";
    
    }
    echo "</table>";     
    ?>
    my desired table is to display the image base on the middle file name and blank.png if no name in the array

    EX_W1_01.png | EX_W1_02.png | blank.png |
    EX_W2_01.png | Blank.png | blank.png |
    EX_W3_01.png | Blank.png | Blank.png |
    Blank.png | Blank.png | blank.png |
    Blank.png | Blank.png | blank.png |
    Last edited by jedaisoul; 04-14-2014 at 06:28 AM. Reason: code tags added

  2. #2
    Join Date
    Mar 2012
    Posts
    1,724
    1. Why are you using PHP to do this??? Is it supposed to use a database of file names? If not, what is the purpose of using PHP? You could hand write the table:
    Code:
    <table border = "1" width="540" cellspacing="0" cellpadding="0">
    <tr>
    <td><img src="/image/EX_W1_01.png" width="120" height="120"></td>
    <td><img src="/image/EX_W1_02.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    </tr>
    <tr>
    <td><img src="/image/EX_W2_01.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    </tr>
    <tr>
    <td><img src="/image/EX_W3_01.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    </tr>
    <tr>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    </tr>
    </table>
    Notes:
    a). I have assumed you meant "/image/" instead of "\image". If you meant "image/" omit the first "/" in each url.
    b) In HTML the widths etc. are assumed to be in pixels. Explicitly stating "px" does not work (you seem to be confusing HTML and CSS usage).
    c) The stated height of the images (120px) does not match that of the <td> tags (60px). I've omitted the explicit height on the TDs.
    d) The stated widths of the images (120px) and the overall width of 540px do not match. I'll leave you to sort that out!

    2. Assuming that the array IS to contain variable file names, I'll make no assumptions as to what those names are. I will simply assume that an array $imagefile exists and each entry is either a file name, or blank. In the latter case you want the file name "blank.png" to be used. The code then becomes something like:
    Code:
    <table border = "1" width="540" cellspacing="0" cellpadding="0">
    
    <?php
    $ds ='/image/';
    for ($row=0; $row<=3; $row++) {
    ?>
    
    <tr>
    
    <?php
        for ($col=0; $col<=2; $col++) {
            if(isset($imagefile[$row][$col])) {
                $file=$imagefile[$row][$col]; }
            else {
                $file='blank.png';
            } // end if
    ?>
    
    <td><img src="<php echo $ds.$file; ?>" width="120" height="120"></td>
    
    <?php
        ;}  // end columns
    ?>
    
    </tr>
    
    <?php
    ;} // end rows
    ?>
    
    </table>
    Note:
    e) I have not tested this code. So it may require tweaking.
    f) The above notes a) to d) apply.

  3. #3
    Join Date
    Dec 2012
    Posts
    2
    Quote Originally Posted by jedaisoul View Post
    1. Why are you using PHP to do this??? Is it supposed to use a database of file names? If not, what is the purpose of using PHP? You could hand write the table:
    Code:
    <table border = "1" width="540" cellspacing="0" cellpadding="0">
    <tr>
    <td><img src="/image/EX_W1_01.png" width="120" height="120"></td>
    <td><img src="/image/EX_W1_02.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    </tr>
    <tr>
    <td><img src="/image/EX_W2_01.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    </tr>
    <tr>
    <td><img src="/image/EX_W3_01.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    </tr>
    <tr>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    <td><img src="/image/blank.png" width="120" height="120"></td>
    </tr>
    </table>
    Notes:
    a). I have assumed you meant "/image/" instead of "\image". If you meant "image/" omit the first "/" in each url.
    b) In HTML the widths etc. are assumed to be in pixels. Explicitly stating "px" does not work (you seem to be confusing HTML and CSS usage).
    c) The stated height of the images (120px) does not match that of the <td> tags (60px). I've omitted the explicit height on the TDs.
    d) The stated widths of the images (120px) and the overall width of 540px do not match. I'll leave you to sort that out!

    2. Assuming that the array IS to contain variable file names, I'll make no assumptions as to what those names are. I will simply assume that an array $imagefile exists and each entry is either a file name, or blank. In the latter case you want the file name "blank.png" to be used. The code then becomes something like:
    Code:
    <table border = "1" width="540" cellspacing="0" cellpadding="0">
    
    <?php
    $ds ='/image/';
    for ($row=0; $row<=3; $row++) {
    ?>
    
    <tr>
    
    <?php
        for ($col=0; $col<=2; $col++) {
            if(isset($imagefile[$row][$col])) {
                $file=$imagefile[$row][$col]; }
            else {
                $file='blank.png';
            } // end if
    ?>
    
    <td><img src="<php echo $ds.$file; ?>" width="120" height="120"></td>
    
    <?php
        ;}  // end columns
    ?>
    
    </tr>
    
    <?php
    ;} // end rows
    ?>
    
    </table>
    Note:
    e) I have not tested this code. So it may require tweaking.
    f) The above notes a) to d) apply.
    Thanks for your help. Actually this array is from a database table and the php table is much bigger so I have to use looping variables.

    for($i=0;$i<count($imagefile); $i++) {

    $ww = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
    }
    This loop able to get me the middle file eg W1, W2 but cannot put to the exact row/col

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles