Jump to content

Pagination/Array loop question.


dmacman

Recommended Posts

Hi all,

 

I am trying to use a pagination loop, and I have it is close to doing what I need with one exception. I needed to set the values of the array items to start at 1 instead of 0 (zero) for some loops I am doing on the next form. So I manually assigning them and here is where I am causing the issues.

 

So when I build my table, I get back the correctly listed items in order, but I am stuck trying to get the ID's ($ID) correctly matched to the products.

 

What I am currently getting is the IDs are assigned row by row (left to right, top to bottom) and I need them assigned columns (top to bottom, left to right).

 

Currently I have..

 

 

1-2-3

4-5-6

7-8-9

 

And I need...

 

1-4-7

2-5-8

3-6-9

 

Here is the code I am using ...

 

//connect to MySQL
        include('conn.php');// Select the DB    
        $db_selected=mysql_select_db("db");
        
        if(!$db_selected)
            {
              die(mysql_error());
            }
        
        $name = array();    
        $sql = "SELECT * FROM table WHERE SiteEnabled='1' AND Path='1' ORDER BY DateAdded DESC";

// We do the query, and find out how many items we'll need to list.
$result = mysql_query($sql);
// Define whats left and right for columns
$num = mysql_num_rows($result);
$left = ceil($num/3); // Define the left column for reference and the loop below       
//    Set up the array to hold the items
$items = array(); // Get all rows into array
while($row = mysql_fetch_array($result))
{
  $items[] = $row;
// $x++;
}
//now start the table (HTML)
$ID = 1; //set the start ID for the Products loop

echo '<table>';
for($i=0; $i<$left;$i++)
{
    $left_item = $items[$i];
     $mid_item = $items[$i+$left];
     $right_item = $items[$i+$left+$left];
    
     echo '<tr>';
    
    //------------------------1st column start----------------------------------//
    if(!empty($left_item['Name']))
    {
         echo '<td width="300 align=left style="padding-left:10px" nowrap="nowrap">';
        echo '<input name="Products['.$ID.']" id="dy_rating_chk_'.$ID.'" type="checkbox" tabindex="' . $tab . '" value="'.$left_item['SKU'].'"';
        if(isset($_SESSION['products'][$ID])) //check and see if the box is checked IE they came back in the browser
         {
          echo ' checked>';
         } else {
         echo ' >';
        }
        echo htmlstripline($left_item['Name']);
        echo '</td>';
    } else {
            echo '<td> </td>';
            }
            $ID++;
        //------------------------1st column end----------------------------------//
        
        //------------------------2nd column start----------------------------------//
    if(!empty($mid_item['Name']))
    {
         echo '<td width="300 align=left style="padding-left:10px" nowrap="nowrap">';
        echo '<input name="Products['.$ID.']" id="dy_rating_chk_'.$ID.'" type="checkbox" tabindex="' . $tab . '" value="'.$mid_item['SKU'].'"';
        if(isset($_SESSION['products'][$ID])) //check and see if the box is checked IE they came back in the browser
         {
          echo ' checked>';
         } else {
         echo ' >';
        }
        echo htmlstripline($mid_item['Name']);
        echo '</td>';
    } else {
            echo '<td> </td>';
            }
            $ID++;
        //------------------------2nd column end----------------------------------//
    
    //------------------------3rd column start----------------------------------//
    if(!empty($right_item['Name']))
    {
         echo '<td width="300 align=left style="padding-left:10px" nowrap="nowrap">';
        echo '<input name="Products['.$ID.']" id="dy_rating_chk_'.$ID.'" type="checkbox" tabindex="' . $tab . '" value="'.$right_item['SKU'].'"';
        if(isset($_SESSION['products'][$ID])) //check and see if the box is checked IE they came back in the browser
         {
          echo ' checked>';
         } else {
         echo ' >';
        }
        echo htmlstripline($right_item['Name']);
        echo '</td>';
    } else {
            echo '<td> </td>';
            }
            $ID++;
        //------------------------3rd column end----------------------------------//
    echo '</tr>';
}
echo '</table>';
//-------------------------------- End dynamic loop --------------------------------// 

 

As always, I appreciate the help,

 

regards,

 

Don

Link to comment
https://forums.phpfreaks.com/topic/103594-paginationarray-loop-question/
Share on other sites

This should give you a start:

 

<?php

$num_columns = 3;

$array = range(0, 100);

echo '
<table>';

for ($i = 0; $i <= count($array); $i += $num_columns) {
$j = 1;

echo '
	<tr>';
while ($j <= $num_columns) {
	if (array_key_exists($i + $j, $array)) {
		echo '
		<td>' . $array[$i + $j] . "</td>";

	} else {
		echo '
		<td> </td>';
	}

	$j++;

}

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.