aml Posted September 12, 2009 Share Posted September 12, 2009 Hi all. Wondering if I can achieve this goal, with your help of course I have a table (4 rows x 4 lines) with 16 images; each one has a link to open in a bigger size or an html page. I want my images change the position, randomly in that table, each time the page is loaded or refreshed. Is that possible using PHP? Should I use another different technology? By the way, I’m using Dreamweaver for design. Thanks in advance aml Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/ Share on other sites More sharing options...
Alex Posted September 12, 2009 Share Posted September 12, 2009 Yes, it's possible. You'll have to put the images in an array and randomly place them in the table. You'll have to show us your source for more help. Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917273 Share on other sites More sharing options...
aml Posted September 12, 2009 Author Share Posted September 12, 2009 Hi Alex, first of all thanks for your reply. Basically, this is on row of my table: <tr> <td><div align="center"><a href="mariza.html" target="_blank"><img src="imagens/mariza/main.jpg" width="220" height="240" border="0"></a></div></td> <td><div align="center"><a href="anaraquel.html" target="_blank"><img src="imagens/anaraquel/main.jpg" width="220" height="240" border="0"></a></div></td> <td><div align="center"><a href="joicy.html" target="_blank"><img src="imagens/joicy/main.jpg" width="220" height="240" border="0"></a></div></div></td> <td><div align="center"><a href="rita.html" target="_blank"><img src="imagens/rita/main.jpg" width="220" height="240" border="0"></a></div></td> </tr> As I told you before, the idea is to have all images displayed in a different position each time the page loads… Hope this helps to better understand the idea Cheers, aml Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917277 Share on other sites More sharing options...
Alex Posted September 12, 2009 Share Posted September 12, 2009 You could do something like.. <?php $imgs = Array(Array('mariza', 'main.jpg'), Array('anaraquel', 'main.jpg'), Array('joicy', 'main.jpg'), Array('rita', 'main.jpg')); shuffle($imgs); ?> <tr> <td><div align="center"><a href="<?php echo $imgs[0][0]; ?>.html" target="_blank"><img src="imagens/<?php echo $imgs[0][0] . '/' . $imgs[0][1]; ?>" width="220" height="240" border="0"></a></div></td> <td><div align="center"><a href="<?php echo $imgs[1][0]; ?>.html" target="_blank"><img src="imagens/<?php echo $imgs[1][0] . '/' . $imgs[1][1]; ?>" width="220" height="240" border="0"></a></div></td> <td><div align="center"><a href="<?php echo $imgs[2][0]; ?>.html" target="_blank"><img src="imagens/<?php echo $imgs[2][0] . '/' . $imgs[2][1]; ?>" width="220" height="240" border="0"></a></div></div></td> <td><div align="center"><a href="<?php echo $imgs[3][0]; ?>.html" target="_blank"><img src="imagens/<?php echo $imgs[3][0] . '/' . $imgs[3][1]; ?>" width="220" height="240" border="0"></a></div></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917290 Share on other sites More sharing options...
aml Posted September 12, 2009 Author Share Posted September 12, 2009 Hi Alex. And when I have a ton of images (50 or 60) is there some other way to do the things more simple and add and remove images of my table? cheers, aml Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917305 Share on other sites More sharing options...
Alex Posted September 12, 2009 Share Posted September 12, 2009 Yea, something like this.. <?php $imgs = Array(Array('mariza', 'main.jpg'), Array('anaraquel', 'main.jpg'), Array('joicy', 'main.jpg'), Array('rita', 'main.jpg')); shuffle($imgs); echo "<tr>"; foreach($imgs as $img) { echo '<td><div align="center"><a href="' . $img[0] . '.html" target="_blank"><img src="imagens/' . $img[0] . '/' . $img[1] . '" width="220" height="240" border="0"></a></div></td>'; } echo "</tr>"; Then just add more content to the Array to display more. The thing is, you'll have to edit that so it'll work for adding more rows. Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917309 Share on other sites More sharing options...
RussellReal Posted September 12, 2009 Share Posted September 12, 2009 <?php $imgs = Array( Array('mariza', 'main.jpg'), Array('anaraquel', 'main.jpg'), Array('joicy', 'main.jpg'), Array('rita', 'main.jpg') ); shuffle($imgs); echo "<table><tr>"; $width = 4; $cw = 1; foreach($imgs as $img) { if ($cw == $width) { echo "</tr><tr>"; $cw = 1; } echo '<td><div align="center"><a href="' . $img[0] . '.html" target="_blank"><img src="imagens/' . $img[0] . '/' . $img[1] . '" width="220" height="240" border="0"></a></div></td>'; $cw++; } while ($cw < 4) { echo "<td> </td>"; } echo "</tr></table>"; ?> building off of alex's post Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917310 Share on other sites More sharing options...
aml Posted September 13, 2009 Author Share Posted September 13, 2009 Hi all. sorry to bother again cannot put this code working properlly... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <?php $imgs = Array( Array('a', 'main.jpg'), Array('b', 'main.jpg'), Array('c', 'main.jpg'), Array('d', 'main.jpg') ); shuffle($imgs); echo "<table><tr>"; $width = 4; $cw = 1; foreach($imgs as $img) { if ($cw == $width) { echo "</tr><tr>"; $cw = 1; } echo '<td><div align="center"><a href="' . $img[0] . '.html" target="_blank"><img src="imagens/' . $img[0] . '/' . $img[1] . '" width="220" height="240" border="0"></a></div></td>'; $cw++; } while ($cw < 4) { echo "<td> </td>"; } echo "</tr></table>"; ?> <html> <head> </head> <body> <table width="920" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#312F30"> <tr> <td><div align="center"><a href="<?php echo $imgs[0][0]; ?>.html" target="_blank"><img src="imagens/<?php echo $imgs[0][0] . '/' . $imgs[0][1]; ?>" width="220" height="240" border="0"></a></div></td> <td><div align="center"><a href="<?php echo $imgs[1][0]; ?>.html" target="_blank"><img src="imagens/<?php echo $imgs[1][0] . '/' . $imgs[1][1]; ?>" width="220" height="240" border="0"></a></div></td> <td><div align="center"><a href="<?php echo $imgs[2][0]; ?>.html" target="_blank"><img src="imagens/<?php echo $imgs[2][0] . '/' . $imgs[2][1]; ?>" width="220" height="240" border="0"></a></div></div></td> <td><div align="center"><a href="<?php echo $imgs[3][0]; ?>.html" target="_blank"><img src="imagens/<?php echo $imgs[3][0] . '/' . $imgs[3][1]; ?>" width="220" height="240" border="0"></a></div></td> </tr> </table></td> </tr> </table> </body> </html> What am I doing wrong? ( a lot of things I imagine... ) Cheers, aml Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917625 Share on other sites More sharing options...
mattal999 Posted September 13, 2009 Share Posted September 13, 2009 You have to put the PHP code where you want the content to be. Not in the head. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> </head> <body> <table width="920" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#312F30"> <?php $imgs = Array( Array('a', 'main.jpg'), Array('b', 'main.jpg'), Array('c', 'main.jpg'), Array('d', 'main.jpg') ); shuffle($imgs); echo "<tr>"; $width = 4; $cw = 1; foreach($imgs as $img) { if ($cw == $width) { echo "</tr><tr>"; $cw = 1; } echo '<td><div align="center"><a href="' . $img[0] . '.html" target="_blank"><img src="imagens/' . $img[0] . '/' . $img[1] . '" width="220" height="240" border="0"></a></div></td>'; $cw++; } while ($cw < 4) { echo "<td> </td>"; } echo "</tr>"; ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917631 Share on other sites More sharing options...
aml Posted September 13, 2009 Author Share Posted September 13, 2009 humm... already do that but no chance, get this error and the page didn't load: [13-Sep-2009 10:11:15] PHP Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /home/public_html/index.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917705 Share on other sites More sharing options...
RussellReal Posted September 13, 2009 Share Posted September 13, 2009 sorry.. here: while ($cw < 4) { echo "<td> </td>"; } that should be: while ($cw < 4) { echo "<td> </td>"; $cw++; } Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917858 Share on other sites More sharing options...
aml Posted September 14, 2009 Author Share Posted September 14, 2009 Hi Russel, the algorithm seems to work great sweet however, the table structure was changed.. I have the first row with 3 pictures and a second row with just only one picture... additionally, I have an extra column with four rows that present 4 pictures, one per row and all aligned at right... :S strange behavior... could you help me find out what the problem is? cheers, aml Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-917914 Share on other sites More sharing options...
aml Posted September 14, 2009 Author Share Posted September 14, 2009 Hi all. maybe I didn't explain myself correctly; what append with this code is: it's supposed to have a table with one row with the all 4 pictures, and I have a couple of rows: the first one with 3 pictures and a second row with the remaining picture of the 4 images sequence. then, I have more 4 rows and each one display one picture (with always the same sequence as the first one) can someone help me find out what the problem is? thanks in advance aml Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-918189 Share on other sites More sharing options...
RussellReal Posted September 14, 2009 Share Posted September 14, 2009 make both of $cw equal to 0 Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-918428 Share on other sites More sharing options...
aml Posted September 14, 2009 Author Share Posted September 14, 2009 I Russell. It's a little better now; the four images appear all in one row. however, there still another set of 4 images (4 rows with one picture each... ) :S Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-918536 Share on other sites More sharing options...
aml Posted September 14, 2009 Author Share Posted September 14, 2009 Hi again. I put the code in a test folder so you can check what I mean: http://motoserra.eu/beta Cheers, aml Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-918542 Share on other sites More sharing options...
RussellReal Posted September 15, 2009 Share Posted September 15, 2009 add me to msn or aim I'll help you better from there I hate the post back and forth bs Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-918701 Share on other sites More sharing options...
aml Posted September 15, 2009 Author Share Posted September 15, 2009 ok, already add you... Quote Link to comment https://forums.phpfreaks.com/topic/174011-random-images-in-a-table/#findComment-919193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.