dprichard Posted October 22, 2007 Share Posted October 22, 2007 Okay, I am trying to do this without Dreamweaver so I can learn the how and why it works of PHP. I have an extension that will do this in dreamweaver, but I want to figure this out instead of using the extension. I have a table and want the results of my query to go 3 columns over, then down to the next row. I am new to handcoding php so any assistance here would be greatly appreciated. Here is my current code. I am just looking to be pointed in the right direction. Not looking for free code. Thanks for any help!!!!!! <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <?php do { ?><td width="200" class="locationbox"><strong><?php echo $row_location['lname']; ?></strong><br> <?php echo $row_location['ladd1']; ?> <?php echo $row_location['ladd2']; ?><br> <?php echo $row_location['lcity']; ?>, <?php echo $row_location['lstate']; ?> <?php echo $row_location['lzip']; ?><br> <?php echo $row_location['lphone']; ?><br> <a href="http://maps.google.com/maps?q=<?php echo $row_location['ladd1']; ?>,+<?php echo $row_location['lcity']; ?>,+<?php echo $row_location['lstate']; ?>+<?php echo $row_location['lzip']; ?>,+USA&sa=X&oi=map&ct=title" target="_blank">View on Map</a></td><?php } while ($row_location = mysql_fetch_array($location)); ?> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/74306-php-horizontal-looping-columns/ Share on other sites More sharing options...
only one Posted October 22, 2007 Share Posted October 22, 2007 I find it difficult too read you code, try making it a bit more tidy. Here's an example of what you are looking for: <?php while($vairable = array_query()) { $i++ if($i == 3) { echo "<tr>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/74306-php-horizontal-looping-columns/#findComment-375417 Share on other sites More sharing options...
only one Posted October 22, 2007 Share Posted October 22, 2007 Oops, i made a mistake in the code above <?php while($vairable = array_query()) { $i++ if($i == 3) { echo "<tr>"; $i = 0; } } ?> Link to comment https://forums.phpfreaks.com/topic/74306-php-horizontal-looping-columns/#findComment-375423 Share on other sites More sharing options...
sasa Posted October 22, 2007 Share Posted October 22, 2007 another solution //query database $result = mysql_query($sql); //echo table header $data_per_row = 3; echo '<table>'; while($row = mysql_featch_row($result)) { //start new row echo '<tr>'; //echo 1st data echo "<td>",$row['field_name'],'</td>'; for($i = 1; $i < $data_per_row; $i++) { if($row = mysql_featch_row($result)) echo "<td>",$row['field_name'],'</td>'; else echo "<td>",' ','</td>'; } echo '</tr>'; } echo '</table>'; Link to comment https://forums.phpfreaks.com/topic/74306-php-horizontal-looping-columns/#findComment-375434 Share on other sites More sharing options...
GingerRobot Posted October 22, 2007 Share Posted October 22, 2007 Sasa, i think that will miss out some rows of data. You wont get the first result from your database, or the 5th etc. You use the mysql_fecth_array() function twice in the loop, causing some results to be missed. Personally, i'd use the example from the FAQ here: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Link to comment https://forums.phpfreaks.com/topic/74306-php-horizontal-looping-columns/#findComment-375437 Share on other sites More sharing options...
sasa Posted October 22, 2007 Share Posted October 22, 2007 OK i made typo in fetch cirrect cide is //query database $result = mysql_query($sql); //echo table header $data_per_row = 3; echo '<table border="3">'; while($row = mysql_fetch_row($result)) { //start new row echo '<tr>'; //echo 1st data echo "<td>",$row[0],'</td>'; for($i = 1; $i < $data_per_row; $i++) { if($row = mysql_fetch_row($result)) echo "<td>",$row[0],'</td>'; else echo "<td>",' ','</td>'; } echo '</tr>'; } echo '</table>'; ?> Link to comment https://forums.phpfreaks.com/topic/74306-php-horizontal-looping-columns/#findComment-375601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.