Member Avatar for OldDeveloper01

Hello.

I have been following a tutorial, but want to adapt it.

Currently the code below echoes out all of the any occasion cards into a list. I would like them to be echoed into a 2 by 3 formation. 2 down, 3 along.

Struggling to get it to do this, any help appreciated!

<?php 
	
		 
		require ("scripts/connect.php");
		 
		$dynamicList = "";
		$sql = mysql_query("SELECT * FROM products WHERE category = 'anyoccasion'");
		$productcount = mysql_num_rows($sql); // count the output amount
		if ($productcount > 0) {
			while($row = mysql_fetch_array($sql)){ 
		             $id = $row["id"];
					 $productname = $row["productname"];
					 $price = $row["price"];
					 $dateadded = strftime("%b %d, %Y", strtotime($row["dateadded"]));
					 $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
		        <tr>
		          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventoryimages/' . $id . '.jpg" alt="' . $productname . '" width="77" height="102" border="1" /></a></td>
		          <td width="83%" valign="top">' . $productname . '<br />
		            £' . $price . '<br />
		            <a href="product.php?id=' . $id . '">View Product Details</a></td>
		        
		        <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventoryimages/' . $id . '.jpg" alt="' . $productname . '" width="77" height="102" border="1" /></a></td>
		          <td width="83%" valign="top">' . $productname . '<br />
		            £' . $price . '<br />
		            <a href="product.php?id=' . $id . '">View Product Details</a></td>

		        </tr>
		      </table>';
		    }
		} else {
			$dynamicList = "We have no Birthday Cards listed in our store yet";
		}
		mysql_close();
		
		?>
		
	<table >
	<tr>
	<td><?php echo $dynamicList; ?></td>
	</tr>
	</table>

Thanks Brants91

I have changed code between your if(productcount>0)

if ($productcount > 0) {

		 	$colindex=1;
		 	$totcols=2;
		 	$rowclosed=false;
  		    //$totrows=ceil($count/$totcols);
  		    
			$dynamicList .= "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"6\">";		 
			
			while($row = mysql_fetch_array($sql)){ 
		             $id = $row["id"];
					 $productname = $row["productname"];
					 $price = $row["price"];
					 $dateadded = strftime("%b %d, %Y", strtotime($row["dateadded"]));
				
				if($colindex==1)
				{
				 
			        $dynamicList .= "<tr>";
			        $rowclosed=false;
		        }
		        $dynamicList .= '<td width="9%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventoryimages/' . $id . '.jpg" alt="' . $productname . '" width="77" height="102" border="1" /></a></td>
		          <td width="41%" valign="top">' . $productname . '<br />
		            £' . $price . '<br />
		            <a href="product.php?id=' . $id . '">View Product Details</a></td>';
		        
		        $tmp='<td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventoryimages/' . $id . '.jpg" alt="' . $productname . '" width="77" height="102" border="1" /></a></td>
		          <td width="83%" valign="top">' . $productname . '<br />
		            £' . $price . '<br />
		            <a href="product.php?id=' . $id . '">View Product Details</a></td>';

				if($colindex==$totcols)
				{

		        	$dynamicList .= " </tr>";
		        	$colindex=1;
		        	$rowclosed=true;
				}
				else
				{
					$colindex++;
				}		     
		    }
			if(!$rowclosed)
			{
	        	$dynamicList .= " </tr>";		    
	        }
		     $dynamicList .= "</table>";
		} else {
			$dynamicList = "We have no Birthday Cards listed in our store yet";
		}
<?php 
	
		 
		require ("scripts/connect.php");
		 
		$dynamicList = "";
		$sql = mysql_query("SELECT * FROM products WHERE category = 'anyoccasion'");
		$productcount = mysql_num_rows($sql); // count the output amount
		if ($productcount > 0) 
		{
			$cnt = 0;
			$perRowProduct = 3;
			
			$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">';
			while($row = mysql_fetch_array($sql))
			{ 
				$id = $row["id"];
				$productname = $row["productname"];
				$price = $row["price"];
				$dateadded = strftime("%b %d, %Y", strtotime($row["dateadded"]));
				
				if( $cnt==0 || $cnt % $perRowProduct==0 )
				{
					$dynamicList .= '<tr>';
				}
				$dynamicList .= '<td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventoryimages/' . $id . '.jpg" alt="' . $productname . '" width="77" height="102" border="1" /></a></td>
				<td width="83%" valign="top">' . $productname . '<br />
				£' . $price . '<br />
				<a href="product.php?id=' . $id . '">View Product Details</a></td>
				
				<td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventoryimages/' . $id . '.jpg" alt="' . $productname . '" width="77" height="102" border="1" /></a></td>
				<td width="83%" valign="top">' . $productname . '<br />
				£' . $price . '<br />
				<a href="product.php?id=' . $id . '">View Product Details</a></td>';
				
				if( ($cnt+1) % $perRowProduct==0 || ($cnt+1)==$productcount )
				{
					$dynamicList .= '</tr>';
				}
				$cnt++;	    
			}
			$dynamicList .= '</table>';
			
		} else {
			$dynamicList = "We have no Birthday Cards listed in our store yet";
		}
		mysql_close();
		
		?>
		
	<table >
	<tr>
	<td><?php echo $dynamicList; ?></td>
	</tr>
	</table>

$perRowProduct is total column per row.

Member Avatar for OldDeveloper01

Thanks for that, however i did adjust it a little as the variables were being echoed twice!

This works fine.

<?php 
	
			require ("scripts/connect.php");
		
		$per_page = 5;
		
		$start = $_GET['start'];
		
		$productcount = mysql_num_rows(mysql_query("SELECT * FROM products WHERE category = 'easter'"));
		
		$max_pages = $productcount / $per_page;
		
		if (!$start)
			$start = 0;
		
		$dynamicList = "";
		$get = mysql_query("SELECT * FROM products WHERE category = 'easter' LIMIT $start, $per_page");
		if ($productcount > 0) {
		
		$cnt= 0;
		$perRowProduct =3;
		
		while ($row = mysql_fetch_assoc($get))
		{ 
		             $id = $row["id"];
					 $productname = $row["productname"];
					 $price = $row["price"];
					 
					 
						if( $cnt==0 || $cnt % $perRowProduct==0 )
				{
					$dynamicList .= '<tr>';
				}
				
				$dynamicList .= '<td width="40px" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventoryimages/' . $id . '.jpg" alt="' . $productname . '" width="77" height="130" border="1" /></a></td>
				<td width="160px" valign="top">' . $productname . '<br />
				£' . $price . '<br />
				<a href="product.php?id=' . $id . '">View Product Details</a></td>';
 
		 
				if( ($cnt+1) % $perRowProduct==0 || ($cnt+1)==$productcount )
				{
					$dynamicList .= '</tr>';
				}
				$cnt++;	    
			}
			$dynamicList .= '</table>';	
		      
		  }
		   else {
			$dynamicList = "We have no Birthday Cards listed in our store yet";
		}

		    
		    $prev = $start - $per_page;
		    $next = $start + $per_page;
		  	
			if (!($start<=0))
			echo "<a href='easter.php?start=$prev'>Prev</a>";
		    	
			if (!($start>=$productcount-$per_page))
			echo "<a href='easter.php?start=$next'>Next</a>";
			
		?>
	
	<table >
	<tr>
	<td><?php echo $dynamicList; ?></td>
	</tr>
	</table>	
	</div>

I also added pagination! Works like a dream. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.