Jump to content

Repeat region in a table.


mike760534211

Recommended Posts

I am totally lost here.  I am trying to get a repeat region into a table.  I am get the table to repeat correctly in a single column but I want it to repeat from the database in the following form:

 

item 1

item 2

[/td]

item 3

item 4

 

But when I insert the repeat code it repeats like this:

item 1

item 1

item 2

item 2

item 3

item 3

item 4

item 4

[td]

 

I have included the source code also below:

        <table width="100%" border="1" cellspacing="0" cellpadding="0">

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <tr>

    <?php do { ?>

      <td><img src="images/<?php echo $row_Recordset1['image']; ?>" align="left" /><?php echo $row_Recordset1['name']; ?><br /><?php echo $row_Recordset1['sizes']; ?><br /><div align="center"><a href="full_desc.php?id=<?php echo $row_Recordset1['number']; ?>" target="_blank"><img src="details_btn.png" border="0" /></a></div></td>

      <td><img src="images/<?php echo $row_Recordset1['image']; ?>" align="left" /><?php echo $row_Recordset1['name']; ?><br /><?php echo $row_Recordset1['sizes']; ?><br /><div align="center"><a href="full_desc.php?id=<?php echo $row_Recordset1['number']; ?>" target="_blank"><img src="details_btn.png" border="0" /></a></div></td>

  </tr>

  <tr>

    <td> </td>

    <td> </td>

  </tr>

  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

        </table>

 

I have tried everything I can think of.  I am still a little new to inserting databases and repeat regions into php files and any help would be greatly appreciated.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/150911-repeat-region-in-a-table/
Share on other sites

Hi

 

Could do with more of the code.

 

I would set it up to loop through your returned records and have a record counter. Mod the counter by 2 (which gets you the remainder which will be 0 or 1) and depending on that put out "<tr><td>your variable data</td>" or "<td>your variable data</td><tr>". You will have to tidy it up at the end of the table is there is not an even number of entries.

 

All the best

 

Keith

not sure what you mean but i have attached the full source code for the page.

<?php require_once('../Connections/store.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_store, $store);
$query_Recordset1 = "SELECT * FROM feed ORDER BY `int` ASC";
$Recordset1 = mysql_query($query_Recordset1, $store) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

<style type="text/css">
<!--
body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
background-image: url('../floral_bg.jpg');
background-repeat: repeat-x;
margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
padding: 0;
text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */
color: #fff8c5;
background-attachment: scroll;
background-color: #2c261b;
}
.oneColFixCtr #container {
width: 760px;  /* using 20px less than a full 800px width allows for browser chrome and avoids a horizontal scroll bar */
background: #FFFFFF url('../leather_bg.jpg');
margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
border: 1px solid #000000;
text-align: left; /* this overrides the text-align: center on the body element. */
}
.oneColFixCtr #mainContent {
padding: 0 0px; /* remember that padding is the space inside the div box and margin is the space outside the div box */
}
.oneColFixCtr #footer {
width: 760px;  /* using 20px less than a full 800px width allows for browser chrome and avoids a horizontal scroll bar */
background: #2F291B;
margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
border: 1px solid #000000;
text-align: left; /* this overrides the text-align: center on the body element. */
}

a:link {
color: #FFF8C5;
text-decoration: none;
}
a:visited {
color: #FFCC66;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: none;
}
p {
font-size:14px;
}
-->
</style>

<script src="../AC_ActiveX.js" type="text/javascript"></script>
<script src="../AC_RunActiveContent.js" type="text/javascript"></script>
<style type="text/css">
<!--

.TITLE {font-family: arial; color: black; font-size: 14pt; text-align: center;}
.PED8C {font-family: arial; color: black; font-size: 8pt; text-align: center;}
.PED8L {font-family: arial; color: black; font-size: 8pt; text-align: left;}

I       {
font-style:     italic;
}
.style1 {
color: #FEF8BA;
font-size: x-small;
}

-->
</style>

<script type="text/javascript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>

<body class="oneColFixCtr" onload="MM_preloadImages('../images/nav_over_02.gif','../images/nav_over_04.gif','../images/nav_over_06.gif','../images/nav_over_08.gif','../images/nav_over_10.gif','../images/nav_over_12.gif')" text="#FFFFFF">
<center>
<table id="Table_01" width="762" height="239" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="130" colspan="11"><img src="../images/DE_images_01.jpg" width="760" height="130" alt="" /></td>
      </tr>
      <tr>
        <td width="88" height="30"><a href="../index.html"><img src="../images/DE_images_02.gif" alt="" width="88" height="30" border="0" id="Image1" onmouseover="MM_swapImage('Image1','','../images/nav_over_02.gif',1)" onmouseout="MM_swapImgRestore()" /></a></td>
        <td width="28"><img src="../images/DE_images_03.gif" width="28" height="30" alt="" /></td>
        <td width="161"><a href="../story.html"><img src="../images/DE_images_04.gif" alt="" width="161" height="30" border="0" id="Image2" onmouseover="MM_swapImage('Image2','','../images/nav_over_04.gif',1)" onmouseout="MM_swapImgRestore()" /></a></td>
        <td width="28"><img src="../images/DE_images_05.gif" width="28" height="30" alt="" /></td>
        <td width="103"><a href="../barrelhorseforsale_home.html"><img src="../images/DE_images_06.gif" alt="" width="103" height="30" border="0" id="Image3" onmouseover="MM_swapImage('Image3','','../images/nav_over_06.gif',1)" onmouseout="MM_swapImgRestore()" /></a></td>
        <td width="31"><img src="../images/DE_images_07.gif" width="31" height="30" alt="" /></td>
        <td width="94"><a href="../gallery.html"><img src="../images/DE_images_08.gif" alt="" name="Image4" width="94" height="30" border="0" id="Image4" onmouseover="MM_swapImage('Image4','','../images/nav_over_08.gif',1)" onmouseout="MM_swapImgRestore()" /></a></td>
        <td width="30"><img src="../images/DE_images_09.gif" width="30" height="30" alt="" /></td>
        <td width="52"><a href="../faq.html"><img src="../images/DE_images_10.gif" alt="" name="Image5" width="52" height="30" border="0" id="Image5" onmouseover="MM_swapImage('Image5','','../images/nav_over_10.gif',1)" onmouseout="MM_swapImgRestore()" /></a></td>
        <td width="29"><img src="../images/DE_images_11.gif" width="29" height="30" alt="" /></td>
        <td width="118"><a href="../contact.html"><img src="../images/DE_images_12.gif" alt="" name="Image6" width="116" height="30" border="0" id="Image6" onmouseover="MM_swapImage('Image6','','../images/nav_over_12.gif',1)" onmouseout="MM_swapImgRestore()" /></a></td>
      </tr>
      <tr>
        <td colspan="11" align="center" valign="top">
        <!--Store Content Start-->
        <table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <?php do { ?>
      <td><img src="images/<?php echo $row_Recordset1['image']; ?>" align="left" /><?php echo $row_Recordset1['name']; ?><br /><?php echo $row_Recordset1['sizes']; ?><br /><div align="center"><a href="full_desc.php?id=<?php echo $row_Recordset1['number']; ?>" target="_blank"><img src="details_btn.png" border="0" /></a></div></td>
      <td><img src="images/<?php echo $row_Recordset1['image']; ?>" align="left" /><?php echo $row_Recordset1['name']; ?><br /><?php echo $row_Recordset1['sizes']; ?><br /><div align="center"><a href="full_desc.php?id=<?php echo $row_Recordset1['number']; ?>" target="_blank"><img src="details_btn.png" border="0" /></a></div></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
        </table>

        
        <!--Store COntent End-->
        <table width="100%" border="0" cellpadding="5">
          <tr>
        <td width="79%" height="26" align="left" bgcolor="#2F291B"><p class="style1">
	<a href="../index.html">HOME</a> | <a href="../story.html">FALLON'S STORY</a> | <a href="../forsale_home.html">FOR SALE</a> | GALLERY| <a href="../faq.html">FAQ</a> | CONTACT</p>        </td>
        <td width="12%" align="right" bgcolor="#2F291B"><span class="style1">site design by</span></td>
        <td width="9%" height="26" align="right" bgcolor="#2F291B"><span class="style1"><a href="http://www.letglow.com" target="_blank"><img src="../images/glow_logo.gif" width="54" height="36" border="0" /></a></span></td>
      </tr>
</table>
</td>
      </tr>
</table>
</center>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Hi

 

Quick play with your original source:-

 


<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
	<td> </td>
	<td> </td>
</tr>
<tr>
<?php 
$recCnt = 0;
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1))
{ 
	if (($recCnt % 2) == 0)
	{?>
	<tr><td><img src="images/<?php echo $row_Recordset1['image']; ?>" align="left" /><?php echo $row_Recordset1['name']; ?><br /><?php echo $row_Recordset1['sizes']; ?><br /><div align="center"><a href="full_desc.php?id=<?php echo $row_Recordset1['number']; ?>" target="_blank"><img src="details_btn.png" border="0" /></a></div></td>
	<?php 
	}
	else
	{
	?>
	<td><img src="images/<?php echo $row_Recordset1['image']; ?>" align="left" /><?php echo $row_Recordset1['name']; ?><br /><?php echo $row_Recordset1['sizes']; ?><br /><div align="center"><a href="full_desc.php?id=<?php echo $row_Recordset1['number']; ?>" target="_blank"><img src="details_btn.png" border="0" /></a></div></td>
</tr>
<tr><td> </td><td> </td></tr>
	<?php
	}
	$recCnt++;
} 
if (($recCnt % 2) == 1)
{
	echo "<td> </td></tr><tr><td> </td><td> </td></tr>";
}
?>
</table>

untested!!

 

the theory here is you have a counter $i

 

if $i is zero, you are in the first column, so you echo "<TR>" tag to start the row.  it will then go to the next result and make a second column of it.  then it will see that the counter is now 1 so it will echo "</TR>" to end the row and reset the counter

 

<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td> </td>
    <td> </td>
  </tr>

    <?php 
    	$i=0; //counter
    	while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { 
    	if($i==0) 
    	{
    		//this is the first of two columns
    		echo "<TR>";		
    	}
    ?>

      <td>
      	<img src="images/<?php echo $row_Recordset1['image']; ?>" align="left" />
      	<?php echo $row_Recordset1['name']; ?>
      	<br />
      	<?php echo $row_Recordset1['sizes']; ?>
      	<br />
      	<div align="center">
      		<a href="full_desc.php?id=<?php echo $row_Recordset1['number']; ?>" target="_blank"><img src="details_btn.png" border="0" /></a>
      	</div>
      </td>
  
  	<?php 
  		if($i==1) {
  			//this is the second of two columns
  			echo "</TR>";
                        $i=0; //reset the counter
  			}
               else { $i++; } //else increment the counter
  		} 
              

  	?>
</table>

Hi

 

That is because both mine and lonewolf217 solutions have the while at the start of the loop (so it will grab the next row before doing a loop), but your code has a line $row_Recordset1 = mysql_fetch_assoc($Recordset1); which is unnecessary with the while at the start of the loop (it gets the first row, then the loop gets the 2nd row as it starts processing).

 

Also you do not apear to use the result of $totalRows_Recordset1 = mysql_num_rows($Recordset1);.

 

All the best

 

Keith

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.