Hi,

Is it possible to code something like this http://www.tom-muck.com/extensions/help/HorizontalVerticalLooperHelp/
?

I'm not quite sure how i should code it in my Do-while loop which i want my recordset to display for every 3 columns another row. So it would be something like

1 2 3
4 5 6
7 8 9
10 11 12

      <table width="137" border="0" cellspacing="1" cellpadding="1">
        <tr>
		<?php do{ ?>
          <td width="91"><img name="<?php echo $row_Recordset2['itemname']; ?>" src="upldimg/<?php echo $row_Recordset2['thumb_filename']; ?>" width="80" height="100" alt=""></td>
          <td width="39"><p class="itemstyle"><?php echo $row_Recordset2['itemname']; ?></p>
          <p class="pricestyle">$<?php echo $row_Recordset2['price']; ?></p></td>
		  <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
		  <?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
        </tr>
      </table>

    Nobody knows how? :bemused:

      Here's a little something....

      <?php
      
      function vertHorizLoop($array, $cols=3, $horiz=true)
      {
      	// Define some defaults
      	$count = count($array);
      	if(!$horiz)
      	{
      		$sect = array();
      		for($i=0; $i<$cols; $i++)
      			$sect[$i] = array();
      
      	$max = ceil($count/$cols);
      	$k=0;
      
      	for($i=0; $i<$count; $i++)
      	{
      		if($i%$max==0 && $i!=0)
      			$k++;
      
      		$sect[$k][] = $array[$i];
      	}
      }
      
      if($horiz)
      {
      	for($i=0; $i<count($array); $i++)
      	{
      		if($i%$cols==0)
      			echo '<br>';
      		echo $array[$i].' ';
      	}
      }
      
      else
      {
      	for($i=0; $i<$max; $i++)
      	{
      		foreach($sect as $key=>$arr)
      		{
      			echo $arr[$i].' ';
      		}
      		echo '<br>';
      	}
      }
      
      }
      
      // Numbers for the 3x4 block:
      $block = array('1','2','3','4','5','6','7','8','9','10','11','12');
      
      // call our function:
      vertHorizLoop($block, 3, true); // Horizontal
      echo '<hr>';
      vertHorizLoop($block, 3, false); // Vertical
      
      ?>

      Gives this output:

      1 2 3
      4 5 6
      7 8 9
      10 11 12 
      -------------------
      1 5 9
      2 6 10
      3 7 11
      4 8 12 

        thanks... i see how i can work wif the code u provided. 🙂

          6 days later

          I'm having problems if i used <table>, as I'm unable to make tables display side by side. The tables just goes down the next row.

          I basically have an item with 3 rows and if it prints the next table it usually bring it down.

          Example:
          1
          2
          3
          4
          5
          6

          But i wanted something like
          Example:

          1 2 3
          4 5 6

          <?php do { ?>
          <table width="126" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><img name="" src="" width="120" height="100" alt="" /></td>
            </tr>
            <tr>
              <td align="center"><?php echo $row_Recordset1['itemname']; ?>
             </td>
            </tr>
            <tr>
              <td><div align="center">$<?php echo $row_Recordset1['price']; ?></div></td>
            </tr>
          
          </table>
          <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
          

            Your php becomes:

            <?php 
            $i=0;
            echo '<table width="126" border="0" cellspacing="0" cellpadding="0">
              <tr>';
            do { ?>
                <td align="center"><?php echo $row_Recordset1['itemname']; ?>
               </td>
                <td><div align="center">$<?php echo $row_Recordset1['price']; ?></div></td>
            <?php 
                if($i%3==0)
                    echo '</tr>
                    <tr>';
                $i++;
            } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
              </tr>
            </table> 

            Right now you have it hard coded to make a new row.... you need to make it dynamic (which I've done) so that it counts where it is.... understand?

              I tried the codes and only 1 product displays on the 1st row and
              the rest of the products displayed on the 2nd row.

              I wanted 3 products on each row to be displayed. I was able to do coding if item and pricing was using <td> instead of <tr> to be displayed.

              Something like this:

              <?php if($totalRows_Recordset2 == 0){
              			echo "<table width=\"520\" height=\"40\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"tablewarning\"><tr><td align=\"center\">There are no product available yet.<br></td></tr></table>";
              		}else
              		{ 
              			?>      
              <table width="137" border="0" cellspacing="1" cellpadding="1"> <?php $num=0; do{ if($num%3==0) { echo "<tr>"; } ?> <td width="91"><img name="<?php echo $row_Recordset2['itemname']; ?>" src="upldimg/<?php echo $row_Recordset2['thumb_filename']; ?>" width="80" height="100" alt=""></td> <td width="39"><p class="itemstyle"><?php echo $row_Recordset2['itemname']; ?></p> <p class="pricestyle">$<?php echo $row_Recordset2['price']; ?></p></td> <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php $num++; ?></td> <?php if($num%3==0) { echo "</tr>"; } } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?> </table> <?php }?>

              Codes get tricky if price and list on <tr>. I'm unable to solve it.

                ok i made an image to illustrate better what i meant by <td> and <tr>.

                Basically the price and name of product being displayed in either <td> of the same row or another row using <td>.

                Link to the image
                http://i6.photobucket.com/albums/y209/ezarc/misc/color.gif

                I'm unable to code if the price was displayed below the image.

                  When someone gives you code, you can't just destroy it without regard for what you're destroying. WHere do you increment the $num varaible? You don't.

                  Why do you have 2 checks that don't need to be there? No reason for it.

                  As for the price being on a new row, just use a <br> tag..... same effect....

                  Look at my original code, then look at how to incorporate it into your code properly.

                  You don't need to step out of PHP anytime you run into HTML. it's okay to echo it.

                    I didnt change ur codes and i tried it. It didnt display wad i wanted.

                    It was like displaying in this manner:

                    1
                    2 3 4

                    The codes i used with the variable $num works and its for a different page. But its juz that the price is beside the image instead of under it. As to why i provided the codes, it was just to show an example.

                    Please look at my image Link aboved. It will illustrate what i'm trying to do. There are two diagrams in it. I'm unable to do the one bottom.

                    To sum things up:

                    I'm doing this other page working out another set of codes to display price underneath my product image.

                      I didnt change ur codes and i tried it. It didnt display wad i wanted.
                      It was like displaying in this manner:

                      1
                      2 3 4

                      Try bpat1434's code in post #3 of this thread.

                        I tried the codes in the 6th post. 3th post was kinda of tough to understand and besides i require the use of <table> <tr> <td>. damn these are all complicating.

                          Ok... sorry bpat1434 if i troubled u. Because i'm very newbie i dont dare to try ur call functions on the 3rd post. I didnt a simpler version of it and its quite messy.

                          But here's my codes: I figured i add another table inside the<td></td> and modified wad i was able to archieve for the price at the side.

                          Here's the codes i did:

                                    <table width="126" border="0" cellspacing="0" cellpadding="0">
                                     	<?php
                          			$num=0;
                          			do{
                          			if($num%3==0)
                          			{
                          				echo "<tr>";
                          			}
                          		    ?>
                                        <td>
                                        <table width="126" border="0" cellspacing="0" cellpadding="0">
                                          <tr>
                                            <td rowspan="2"><img src="../upldimg/<?php if(($row_Recordset1['thumb_filename']) == NULL)
                          				  {
                          				  	echo "noimage.jpg";
                          				  }else
                          				  {
                          				  	echo $row_Recordset1['thumb_filename'];
                          				  } ?>" width="100" height="150" alt=""></td>
                                            <td height="62"><?php echo $row_Recordset1['itemname']; ?></td>
                                          </tr>
                                          <tr>
                                            <td>$<?php echo $row_Recordset1['price']; ?><?php $num++; ?></td>
                                          </tr>
                                        </table>
                                       </td>
                                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                      <?php 
                          		 if($num%3==0)
                          		 {
                          		  echo "</tr>";
                          		 }
                          			} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
                                    </table>
                          
                          

                            Modifying the code from post #3 wasn't difficult to make it a table layout... so here it is:

                            <?php
                            
                            function vertHorizLoop($array, $cols=3, $horiz=true, $table=true)
                            {
                            	$output = '';
                                // Define some defaults
                                $count = count($array);
                            	if($table)
                            		$output .= '
                            <table>
                            	<tr>';
                                if(!$horiz)
                                {
                                    $sect = array();
                                    for($i=0; $i<$cols; $i++)
                                        $sect[$i] = array();
                            
                                $max = ceil($count/$cols);
                                $k=0;
                            
                                for($i=0; $i<$count; $i++)
                                {
                                    if($i%$max==0 && $i!=0)
                                        $k++;
                            
                                    $sect[$k][] = $array[$i];
                                }
                            }
                            
                            if($horiz)
                            {
                                for($i=0; $i<count($array); $i++)
                                {
                                    if($i%$cols==0)
                            		{
                            			if($table)
                            				$output .= '
                            </tr>
                            <tr>';
                            			else
                            				$output .= '<br>';
                            		}
                            
                            		if($table)
                            			$output .= '
                            	<td>'.$array[$i].'</td>';
                            		else
                            			$output .= $array[$i].' ';
                                }
                            }
                            
                            else
                            {
                                for($i=0; $i<$max; $i++)
                                {
                                    foreach($sect as $key=>$arr)
                                    {
                            			if($table)
                            				$output .= '
                            	<td>'.$arr[$i].'</td>';
                            			else
                            				$output .= $arr[$i].' ';
                                    }
                            		if($table)
                            			$output .= '
                            </tr>
                            <tr>';
                            		else
                            			$output .= '<br>';
                                }
                            }
                            
                            if($table)
                            	$output .= '
                            </tr>
                            </table>';
                            	echo $output;
                            
                            } 
                            
                            // Numbers for the 3x4 block:
                            $block = array('1','2','3','4','5','6','7','8','9','10','11','12');
                            
                            // call our function:
                            echo '<h3>Horizontal [<i>Table</i>]:</h3>';
                            vertHorizLoop($block, 3, true); // Horizontal
                            echo '<hr>
                            <h3>Vertical [<i>Table</i>]:</h3>';
                            vertHorizLoop($block, 3, false); // Vertical
                            echo '<hr>
                            <h3>Horizontal [<i>HTML</i>]:</h3>';
                            vertHorizLoop($block, 3, true, false); // Horizontal
                            echo '<hr>
                            <h3>Vertical [<i>HTML</i>]:</h3>';
                            vertHorizLoop($block, 3, false, false); // Vertical
                            
                            ?>

                            That outputs:

                            Horizontal [Table]:
                            1 	2 	3
                            4 	5 	6
                            7 	8 	9
                            10 	11 	12
                            Vertical [Table]:
                            1 	5 	9
                            2 	6 	10
                            3 	7 	11
                            4 	8 	12
                            Horizontal [code=html]:
                            
                            1 2 3
                            4 5 6
                            7 8 9
                            10 11 12
                            Vertical [code=html]:
                            1 5 9
                            2 6 10
                            3 7 11
                            4 8 12

                            KEYWORDS:
                            Vertical / Horizontal Loop
                            Horizontal / Vertical Loop
                            Vertical Column
                            Vertical table listing

                              Write a Reply...