budimir Posted December 25, 2010 Share Posted December 25, 2010 Hey guys, I wrote a code for displaying images in columns, but for some reason it's not working. Can you take a look and tell me what I'm doing wrong? $i = 0; if($i % 4 == 0){ echo ' <td width="25%" align="center" valign="top"> <a href= "'.$slika_velika.''.$slika_v.'" rel="lytebox" title="'.$naziv.' - '.$mjesto.' - '.$medalja.'" target="_blank"><img src="'.$slika_mala.''.$slika_m.'"width="200" height="150" border="0"></a><br> </td> <td width="25%" align="center" valign="top"> '.$naziv.' - '.$mjesto.' </td> <td width="25%" align="center" valign="top"> '.$medalja.' </td><br /> '; echo '</tr><tr>'; } $i++; } The output I'm getting now is one image below another. I would like to display them one next to another. Please help!!! Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 25, 2010 Share Posted December 25, 2010 need to see the code that precedes what you have shown Quote Link to comment Share on other sites More sharing options...
Solar Posted December 25, 2010 Share Posted December 25, 2010 Your missing a <tr> tag in your first echo. That's all I see. Quote Link to comment Share on other sites More sharing options...
budimir Posted December 26, 2010 Author Share Posted December 26, 2010 This is the complete code I'm using <?php $slika_velika = "slike_medalja/"; $slika_mala = "slike_medalja/thumbs/"; $upit = "SELECT * FROM medalje_slike ORDER BY datum_dodavanja ASC"; $rezultat = mysql_query($upit,$veza) or die ("Nije uspilo"); while($row = mysql_fetch_array($rezultat)){ $slika_v = $row["slika_velika"]; $slika_m = $row["slika_mala"]; $mjesto = $row["mjesto_osvajanja"]; $naziv = $row["naziv_natjecanja"]; $vrsta = $row["vrsta_medalje"]; if ($vrsta == 1) { $medalja = "Zlato"; } elseif ($vrsta == 2) { $medalja = "Srebro";} elseif ($vrsta == 3) { $medalja = "Bronca"; } $i = 0; if($i % 4 == 0){ echo ' <td width="25%" align="center" valign="top"> <a href= "'.$slika_velika.''.$slika_v.'" rel="lytebox" title="'.$naziv.' - '.$mjesto.' - '.$medalja.'" target="_blank"><img src="'.$slika_mala.''.$slika_m.'" border="0"></a><br> </td> <td width="25%" align="center" valign="top"> '.$naziv.' - '.$mjesto.' </td> <td width="25%" align="center" valign="top"> '.$medalja.' </td><br /> '; echo '</tr><tr>'; } $i++; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 26, 2010 Share Posted December 26, 2010 As posted, where is your opening TR? Quote Link to comment Share on other sites More sharing options...
budimir Posted December 26, 2010 Author Share Posted December 26, 2010 Hey revraz, I did it like this, now, but still not working <?php $slika_velika = "slike_medalja/"; $slika_mala = "slike_medalja/thumbs/"; $upit = "SELECT * FROM medalje_slike ORDER BY datum_dodavanja ASC"; $rezultat = mysql_query($upit,$veza) or die ("Nije uspilo"); while($row = mysql_fetch_array($rezultat)){ $slika_v = $row["slika_velika"]; $slika_m = $row["slika_mala"]; $mjesto = $row["mjesto_osvajanja"]; $naziv = $row["naziv_natjecanja"]; $vrsta = $row["vrsta_medalje"]; if ($vrsta == 1) { $medalja = "Zlato"; } elseif ($vrsta == 2) { $medalja = "Srebro";} elseif ($vrsta == 3) { $medalja = "Bronca"; } $i = 0; if($i % 4 == 0){ echo '<tr> <td width="25%" align="center" valign="top"> <a href= "'.$slika_velika.''.$slika_v.'" rel="lytebox" title="'.$naziv.' - '.$mjesto.' - '.$medalja.'" target="_blank"><img src="'.$slika_mala.''.$slika_m.'" border="0"></a><br> </td> <td width="25%" align="center" valign="top"> '.$naziv.' - '.$mjesto.' </td> <td width="25%" align="center" valign="top"> '.$medalja.' </td><br /> '; echo '</tr>'; } $i++; } ?> I would like to display the images horizontaly in columns, but I'm getting them verticly in rows??? What am I doing wrong? Quote Link to comment Share on other sites More sharing options...
Thauwa Posted December 26, 2010 Share Posted December 26, 2010 Heya! Just replace your <td>'s with <tr>'s and vice versa. <td> <tr> </tr> <tr> </tr> <tr> </tr> <tr> </tr> </td> I hope that that's what you wanted with your previous question. Regards, Thauwa Quote Link to comment Share on other sites More sharing options...
budimir Posted December 26, 2010 Author Share Posted December 26, 2010 Hey guys, Thanks for you're effort, but I made it. I tried to play with code and this is what it acctualy made it work. <table width="100%" border="1" cellspacing="4" cellpadding="4" align="center"> <tr> <?php $slika_velika = "slike_medalja/"; $slika_mala = "slike_medalja/thumbs/"; $upit = "SELECT * FROM medalje_slike ORDER BY datum_dodavanja ASC"; $rezultat = mysql_query($upit,$veza) or die (mysql_error()); while($row = mysql_fetch_array($rezultat)){ $slika_v = $row["slika_velika"]; $slika_m = $row["slika_mala"]; $mjesto = $row["mjesto_osvajanja"]; $naziv = $row["naziv_natjecanja"]; $vrsta = $row["vrsta_medalje"]; if ($vrsta == 1) { $medalja = "Zlato"; } elseif ($vrsta == 2) { $medalja = "Srebro";} elseif ($vrsta == 3) { $medalja = "Bronca"; } $i = 0; if($i % 4 == 0){ echo ' <td width="25%" align="center" valign="top"> <a href= "'.$slika_velika.''.$slika_v.'" rel="lytebox" title="'.$naziv.' - '.$mjesto.' - '.$medalja.'" target="_blank"><img src="'.$slika_mala.''.$slika_m.'" border="0"></a><br /> '.$naziv.' - '.$mjesto.' <strong>'.$medalja.'</strong> </td>'; } $i++; #echo '</tr><tr></tr><tr>'; } ?> </table> I hope someone will make a use of this. Again, thanks!!! Quote Link to comment Share on other sites More sharing options...
budimir Posted December 26, 2010 Author Share Posted December 26, 2010 Not working correctly. Now I have a situation that images are display horizontally, but for indefinite. What ever I do, it will not go to new row after 4 images. Please help. This is the code. <table width="100%" border="1" cellspacing="4" cellpadding="4" align="center"> <tr> <?php $slika_velika = "slike_medalja/"; $slika_mala = "slike_medalja/thumbs/"; $upit = "SELECT * FROM medalje_slike ORDER BY datum_dodavanja ASC"; $rezultat = mysql_query($upit,$veza) or die (mysql_error()); while($row = mysql_fetch_array($rezultat)){ $slika_v = $row["slika_velika"]; $slika_m = $row["slika_mala"]; $mjesto = $row["mjesto_osvajanja"]; $naziv = $row["naziv_natjecanja"]; $vrsta = $row["vrsta_medalje"]; if ($vrsta == 1) { $medalja = "Zlato"; } elseif ($vrsta == 2) { $medalja = "Srebro";} elseif ($vrsta == 3) { $medalja = "Bronca"; } $i = 0; if($i % 4 == 0){ echo ' <td width="25%" align="center" valign="top"> <a href= "'.$slika_velika.''.$slika_v.'" rel="lytebox" title="'.$naziv.' - '.$mjesto.' - '.$medalja.'" target="_blank"><img src="'.$slika_mala.''.$slika_m.'" border="0"></a><br /> '.$naziv.' - '.$mjesto.' <strong>'.$medalja.'</strong> </td>'; } #echo '</tr><tr>'; $i++; } ?> </tr> </table> Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 26, 2010 Share Posted December 26, 2010 here is an example ... (NOTE: I have not included any pagination to this) <?php $max_columns = 5; /* set max columns for table */ $i = 0; /* initially set counter to use in starting and ending rows */ ?> <table border ="2"> <?PHP include('db.php'); /* make connection to database here */ $query = "Select * FROM album"; /* define query */ $result = mysql_query($query); /* execute query */ $total_cells = mysql_num_rows($result); /* count total cells to be displayed */ $total_rows = ceil($total_cells / $max_columns); /* calculate total rows needed */ $junk1 = $total_rows * $max_columns; /* calculate number of empty cells in last row */ $junk2 = $junk1 - $total_cells; if($junk2==0) { $last_row = "</tr>"; }else{ $j = 0; while($j<$junk2){ $last_row = $last_row . "<td></td>"; $j ++; } $last_row = $last_row . "</tr>"; } while($row = mysql_fetch_array($result)) { /* begin looping thru the results */ $image = "thumbs/" . $row['image']; if($i == 0){ echo "<tr>"; $i ++; } ?> <td align="center" valign="center"><IMG src = "<?PHP echo $image; ?>"></td> <?PHP $i ++; if($i > $max_columns) { /* check if need to close row */ echo "</tr>"; $i=0; } } echo $last_row . "</table>"; /* clean up last row */ ?> Quote Link to comment Share on other sites More sharing options...
the182guy Posted December 26, 2010 Share Posted December 26, 2010 Try seperating the PHP logic and the HTML (presentation). You'll find it's much easier to read and debug. Quote Link to comment Share on other sites More sharing options...
budimir Posted December 26, 2010 Author Share Posted December 26, 2010 Thanks litebearer, You're code worked like a charm, atlhough I would like to know what I messed up with my code. I'm realy curious!!!! I had to correct one mistake in you're code, but it worked perfectly. Thanks for you're effort. Thanks. Thanks. Thanks. You made my day. I spent whole day trying to figure out what's wrong with my code, because the same one is working on antoher page I have, so that's why I don't understand!!! If you could clarify that for me I would appreciate I lot. I need to understand. Quote Link to comment 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.