Jump to content

PHP Horizontal Looping Columns


dprichard

Recommended Posts

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

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>';

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

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>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.