Page 1 of 1

MySQL result to HTML table (I know it must be simple, but...

Posted: Thu Jan 27, 2005 2:22 am
by mhenke
I'm trying to create an HTML table like this, data comes from MySQL:

<table>
<tr>
<td>image1</td>
<td>image2</td>
<td>image3</td>
<td>image4</td>
</tr>
<tr>
<td>link</td>
<td>link</td>
<td>link</td>
<td>link</td>
</tr>
<tr>
<td>image5</td>
<td>image6</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>link</td>
<td>link</td>
<td> </td>
<td> </td>
</tr>
</table>

I need to use this loop: foreach (glob("$img", GLOB_BRACE) as $image). I now have this, but it doesn't format the table as I need. Who can help me?

Code: Select all

$id = $_SESSION&#1111;'id']; 
$path = "a/"; 
$img = $path . $id . "_" . "*" . ".jpg"; 
$count = 0; 
$count2 = 0; 
echo "<table cellpadding="0" cellspacing="5"><tr>"; 
foreach (glob("$img", GLOB_BRACE) as $image) &#123; 
$count++; 
list($w, $h) = getimagesize($image); 
$image2 = str_replace('a/', 's/', $image); 
$image3 = str_replace('a/', '', $image); 
         
    echo "<td class="td2" align="center">"; 
    echo "<img src="$image2" border="0" onClick="MM_openBrWindow('showpic.php?img=$image3','','status=yes, width=$w, height=$h')">"; 
    echo "</td>"; 
     
        if($count == 4)&#123; 
        echo "</tr><tr>"; 
             
        foreach (glob("$img", GLOB_BRACE) as $image) &#123; 
        $image4 = str_replace('a/', 's/', $image); 
        $count2++; 
            echo "<td class="td2" align="center">"; 
            echo "$image4"; 
            echo "</td>"; 
             
            if($count2 == 4)&#123; 
            echo "</tr><tr>"; 
                 
            &#125; 
            &#125; 
                 
            &#125; if($count == 4)&#123; 
        echo "</tr><tr>"; 
        &#125; 
        &#125; 
       &#125;  
     

echo "</tr></table>";

Posted: Thu Jan 27, 2005 7:31 am
by John Cartwright

Posted: Thu Jan 27, 2005 8:11 am
by mhenke
Yes I did that search too and found the same. My question is, how do I fit that code with mine? I need to use: glob("$img", GLOB_BRACE) as $image to produce a filled table... Anyone?

Posted: Thu Jan 27, 2005 8:33 am
by feyd
your counting logic has errors in it. Specifically the if involving count at 4

Posted: Thu Jan 27, 2005 8:49 am
by mhenke
I guess there are errors as my code doesn't produce what I need ; )

Does anyone know how to produce the needed code using:

glob("$img", GLOB_BRACE) as $image

This should return all images with a certain name in a dir, formatted in the described table...

Posted: Thu Jan 27, 2005 8:51 am
by feyd
the code posted does not use braces in the file pattern you are searching for. Maybe this is a problem?

Posted: Thu Jan 27, 2005 9:29 am
by mhenke
My code works fine, the only problem is that the table isn't generated properly. I need it to be generated like this:

image | image | image |
link | link | link |

break (tr) and repeat after an x amount of colums (3 here)

image | image | image |
link | link | link |

etc

Posted: Thu Jan 27, 2005 9:44 am
by feyd
I know I've posted how to do that several times.. I'll see if I can find the post (I always have trouble finding it)

in the mean time, I can tell you it involves using the modulo operator.

Posted: Thu Jan 27, 2005 9:57 am
by mhenke
THX looking forward to it. Been playing with modular/loops within loops for hours now : (

Posted: Thu Jan 27, 2005 11:07 am
by feyd
viewtopic.php?t=25105
PHP & MySQL formatting problem

finally found it.. I had to perform backend sql queries to find it..

I have too high a post rate :lol:

Posted: Thu Jan 27, 2005 11:54 am
by mhenke
THX!!!! Gonna play with this now, am sure it'll be fun : )