Englyst Posted July 8, 2008 Share Posted July 8, 2008 Hi folks. My first post here ever I'm not the best at coding but I try at the best of my ability. So please beware any obvious blunders coming! My problem is: I've got a bunch of articles from a database that needs to get distributed into a 2 column layout. But it seems my code only processes the first record and then stops. The first part: Generate HTML code for a 2-column layout based on articles from a database. In this case there are 2 articles but only one shows up. Seems like the while doesn't loop. It only processes the first record bLeft is for switching from putting an article in the left or right HTML variable. while ($row_rs_artikler) { if($bLeft == 1) { $LeftHTML .= '<tr><td>'; if (($row_rs_artikler['filename']) != "") { $LeftHTML .= '<img src="images/articleimages/'.$row_rs_artikler['filename'].'" alt="'.$row_rs_artikler['imagedescription'].'" class="image" /><br /><br />'; } $LeftHTML .= $row_rs_artikler['description'].'<br /><br ></td></tr>'; $bLeft = 0; } else { $RightHTML .= '<tr><td>'; if (($row_rs_artikler['filename']) != "") { $RightHTML .= '<img src="images/articleimages/'.$row_rs_artikler['filename'].'" alt="'.$row_rs_artikler['imagedescription'].'" class="image" /><br /><br />'; } $RightHTML .= $row_rs_artikler['description'].'<br /><br /></td></tr>'; $bLeft = 1; } } ?> Heres the code for outputting the generated HTML nested in a master table with 2 columns: <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="article"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <?php echo $LeftHTML; ?> </td> </tr> </table> </td> <td class="article"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <?php echo $RightHTML; ?> </td> </tr> </table> </td> </tr> </table> I have been staring at this for too long and there's probably a MUCH better way at doing this. If anyone can help me out I'd be a happy camper. I haven't been using arrays or such very much so in some areas I'm drawing a blank You can see a working version of this code here: http://carpenta.englyst.eu/konceptet.php Thanks! / Michael Englyst Link to comment https://forums.phpfreaks.com/topic/113662-solved-problems-trying-to-generate-html-data-for-a-2-column-layout/ Share on other sites More sharing options...
BillyBoB Posted July 8, 2008 Share Posted July 8, 2008 I would suggest something like: $query = mysql_query("SELECT * FROM table"); while ($row_rs_artikler = mysql_fetch_array($query)) { if($bLeft == 1) { $LeftHTML .= '<tr><td>'; if (($row_rs_artikler['filename']) != "") { $LeftHTML .= '<img src="images/articleimages/'.$row_rs_artikler['filename'].'" alt="'.$row_rs_artikler['imagedescription'].'" class="image" /><br /><br />'; } $LeftHTML .= $row_rs_artikler['description'].'<br /><br ></td></tr>'; $bLeft = 0; } else { $RightHTML .= '<tr><td>'; if (($row_rs_artikler['filename']) != "") { $RightHTML .= '<img src="images/articleimages/'.$row_rs_artikler['filename'].'" alt="'.$row_rs_artikler['imagedescription'].'" class="image" /><br /><br />'; } $RightHTML .= $row_rs_artikler['description'].'<br /><br /></td></tr>'; $bLeft = 1; } } ?> I don't know exactly how you have done the rest of your code because this is clearly not all of it. Link to comment https://forums.phpfreaks.com/topic/113662-solved-problems-trying-to-generate-html-data-for-a-2-column-layout/#findComment-584243 Share on other sites More sharing options...
Englyst Posted July 8, 2008 Author Share Posted July 8, 2008 Thanks a lot. It actually works now! Apparently my arguments for the while loop was screwed *bows in gratitude* Link to comment https://forums.phpfreaks.com/topic/113662-solved-problems-trying-to-generate-html-data-for-a-2-column-layout/#findComment-584280 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.