Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A count problem in a loop 1

Status
Not open for further replies.

leeboycymru

Programmer
Jan 23, 2007
185
GB
I have a loop that only needs to bring out 3 hotels from the database which is set here:


$mPageSize = @$HTTP_POST_VARS["txtPageSize"];
if ((!isset($mPageSize))||intval($mPageSize)==0)
{
$mPageSize = @$HTTP_GET_VARS["pagesize"];
if ((!isset($mPageSize))||intval($mPageSize)==0)
$mPageSize = 3;
}

I'll post the loop below next, but whats happening is that it is displaying 3 in a row, but for some reason it goes the extra 1 which creates another but empty hotel, and so creating a new row beneath the 1st row which is pushing my footer down. Can anybody help:

<table width="100%" border="0" align="left" cellpadding="0" cellspacing="0" >
<?php
if(!$mRecordCount == 0) {
?>
<?php } ?>
<tr>
<td>&nbsp;</td>
</tr>
<?
if($records>0)
{
$cnt=$records+(3-$records%3);
$i=1;
for($i=1;$i<$cnt;$i++) { ?>
<tr>
<td><table border="0" align="left" cellpadding="0" cellspacing="0" width="98%" >
<?
if(($i%3)==0 || $i==1 ){ ?>
<tr>
<? for($j=1;$j<=3;$j++) {
$morevalue=true;
$rows=mysql_fetch_assoc($result) or $morevalue=false;
?>
<td width="<? if($j==1) { print("228"); } else { print("242"); } ?>" align="left" valign="top"><table width="217" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="middle"><table width="201" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="62" height="63" align="left" valign="top">
<? if($morevalue){
if(is_file("../admin1212/".$rows['foto1']))
{
imgresize("../admin1212/$rows[foto1]");
?> <a href="hotel.php?Id_Hot=<?=$rows['hotel_id']?>&amp;Id=<?=$hlist?>" class="bodySmall1"><img src="../admin1212<?=$rows['foto1']?>" alt="" height="80" width="80" border="0"/></a>
<? }} ?> </td>
<td width="7">&nbsp;</td>
<td width="132" align="left" valign="top"><span class="bahia">
</span><span class="bahia">
<? if($morevalue){ ?>
<?=$rows['hotel_name']?>
<? } ?>
</span><br>
<? if($morevalue){ ?>
<?php $int=strlen($rows['star_rating']);
if($int>0) { $k=0; while($k<$int) { ?>
<img src='images/star.gif' border='0' alt="" />
<?php $k++; } } ?>
<? } ?>
<br> <span class="maxico">
<? if($morevalue){ ?>
<?=$rows['country']?>
<? } ?>
</span><br>
<span class="bodySmall1">
<? if($morevalue){ ?>
<a href="hotel.php?Id_Hot=<?=$rows['hotel_id']?>&amp;Id=<?=$hlist?>" class="bodySmall1">More Info</a>
<? } ?>
</span></td>
</tr>
</table></td>
</tr>
if($morevalue){ ?>
<tr>
<td height="1" align="left" valign="top" bgcolor="#CCCCCC"></td>
</tr>
<? } ?>
</table></td>
</tr>
</table></td>
<? if(($j==1||$j==2) && $morevalue) { ?>
<td width="1" bgcolor="#CCCCCC"></td>
<? } ?>
<? } ?>
</tr>
<?
}
?>
</table></td>
</tr>
<? } ?>
<? } else { ?>
<? } ?>
</table>
 
Hi

Could you please enclose your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] tags ? And could you please indent your code ? And could you please remove unnecessary portions like code without effect
Code:
<?php
if(!$mRecordCount == 0) {
?>   
<?php } ?>
and programmatically useless HTML markup
Code:
<tr>
<td>&nbsp;</td>
</tr>
from your code before posting it ? This would help use a lot.

Feherke.
 
Ye sorry, I was going to after pasting it in, and I automatically clicked submit...

I was waiting for a telling off, but sorry I pressed submit when i didnt mean too

lee
 
i think this could be where your problem is
Code:
$cnt=$records+(3-$records%3);
as you may end up with a fraction.

consider using ceil() instead.

i can't help thinking that this it is better to deal with 0 based collections too. but if you don't then should you not be using <= in your comparisons in your loops instead?

e.g.
Code:
for($i=1;$i<[red]=[/red]$cnt;$i++) { ?>

lastly - i'd strongly recommend that you get rid of the tables and use block level elements, floats and css to make your page design. it's more flexible and will allow a neater degradation for smaller browser windows.

if that does not work for you, consider creating a dummy array to debug with and then just going back to first principles. to create a limited number of columns you typically need an inner and an outer loop. the inner loop needs to test for when you run out of data before the end of the row and then insert dummy table cells where necessary

something like this might help clarify
Code:
<?

//dummy arrray
for ($i=0; $i<20; $i++){
	$array[] = $i+1;
}

$maxCols = 3;

$output = "<table>\r\n";

$outerloop = 0;
while ($outerloop < count ($array) ){
	$output .= "\t<tr>\r\n";
	for ($innerloop=0; $innerloop<$maxCols; $innerloop++){
		if ($outerloop >= count($array)) {
			$output .= "\t\t<td>&nbsp;</td>\r\n";
		} else {
			$output .= "\t\t<td>".$array[$outerloop]."</td>\r\n";
		}
		$outerloop++;
	}
	$output .= "\t</tr>\r\n";
}
$output .= "</table>\r\n";
echo $output;
?>

and as a counterpoint for a css based design
Code:
<?

//dummy arrray
for ($i=0; $i<20; $i++){
	$array[] = $i+1;
}

$maxCols = 3;
$loop=0;
//output some style info.  better in the <head> tags
echo <<<HTML
<style type="text/css">
.wrapper {width:500px;}
.col {width:150px; float:left;}
</style>

HTML;

echo "<div class=\"wrapper\">\r\n";	//create the holding div
while ($loop<count($array)){
	echo <<<HTML
	<div class="col">
		<span class="content">{$array[$loop]}</span>
	</div>

HTML;
$loop++;
}

echo "</div>"
?>
 
I agree, there is just simply to much there to tell where your loop might be going wrong, however, this:

$cnt=$records+(3-$records%3);

looks strange to me.

where does $records come from, why are you doing that werid operation on it.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I think its to do with the chunk of code below, so just to recap whats basically happening is that yes its counting 3 to a row, and moving to the next, but when there is only 3, it for some reason creates 1 more empty value that creates a new row on the second line.

Code:
<? 
if($records>0)
{ 
$cnt=$records+(3-$records%3);
$i=1;
for($i=1;$i<$cnt;$i++) { ?>
<tr> 
<td><table border="0" align="left" cellpadding="0" cellspacing="0" width="98%" >
<? 
if(($i%3)==0 || $i==1 ){ ?>
<tr> 
<? for($j=1;$j<=3;$j++) { 
$morevalue=true;
$rows=mysql_fetch_assoc($result) or $morevalue=false;
?>
[code]
 
Its a website I have inherited and trying to do something with. I'll try and post as much other relevant code as I can
 
I'm sorry guys, there is just way to much here for me to pull it appart.

In honesty, I took it from a completely table based site to a css and div site, and these blocks of code are the last standing parts of the old site.

I wish you could see the whole page, maybe you could work the flow out that way.

I'll see if i can pull those bits of code out
 
Lee

as everyone has said, it is difficult to see what is going on with your code. two reasons why

there is a critical variable called $records that you have not explained to us. I am assuming it derives from a count(*) type query and therefore holds the total number of records available

the tables are not properly validating and are so deeply nested that the site must take ages to load.

i have had a bash at rewriting your code with a much simpler table structure. again - i'd recommend ditching it and going for css: i've shown you the core structure for this above. it's nothing more than an overall wrapper div and an inner div for each column, plus a couple of lines of css.

in rewriting the code I have dumped all the nested tables so the code will output a single table with three columns and "n" rows. I have not been able to test it without dummy data.

the code is commented, somewhat.

I have used the heredoc syntax to guarantee that the resultant HTML is nicely indented and easy for you to debug from the source code.

code follows
Code:
<table width="100%" border="0" align="left" cellpadding="0" cellspacing="0" >
<?php
if(!$mRecordCount == 0) {
?>   
<?php } ?>
<tr> 
<td>&nbsp;</td>
</tr>
<? 
if ($records > 0) {
	//create the inner table
	echo <<<HTML
		<table border="0" align="left" cellpadding="0" cellspacing="0" width="98%" >

HTML;
		//create the outer loop for the rows
		$i = 0; //start the outerloop counter
		while ($i < $records)  {

			//create the new row
			echo <<<HTML
			<tr>

HTML;
			//create the interior colum loop
			for ($iLoop=0; $iLoop<3; $iLoop++){
				if ($i < $records){		//retrieve some data
					
					$row = mysql_fetch_assoc($result);
				} else {
					//this will create the dummy columns if we need them
					$row['star_rating'] = 
					$row['foto1'] =
					$row['hotel_id'] =
					$row['hotel_name'] = 
					$row['country'] =
					"&nbsp;";
				}
				
				//all variables set up
				//now build the column content
					
				//handle the production of stars
				$int=strlen($row['star_rating']); 
				$stars = '';
				for ($s=0; $s<$stars; $s++){
					$stars .= "<img src=\"images/star.gif\" border=\"0\" alt=\"\" />";
				}
				//end stars
				
				//construct the inner loop
				echo <<<HTML
			<td width="{$width[$iLoop]}">

HTML;
				if(is_file("../admin1212/".$rows['foto1'])){
					//imgresize("../admin1212/".$rows['foto1']); //what is this doing!  
					echo <<<HTML
					<a 	href="hotel.php?Id_Hot={$row['hotel_id']}&Id=$hlist" class="bodySmall1">
						<img src="../admin1212/{$row['foto1']}" alt="" height="80px" width="80px" border="0px"/>
					</a>
					
HTML;
				} //end of the isfile If
				
				//textual content
				echo <<<HTML
					<span class="bahia">
						{$row['hotel_name']}
					</span>
					<br/>
					$stars
					<br/>
					<span class="maxico"> <!-- country data -->
						{$row['country']}
					</span>
					<br/>
					<span class="bodysmall1">
						<a href="hotel.php?Id_Hot={$row['hotel_id']}&Id={$hlist}" class="bodySmall1">
							More Info
						</a>
					</span>
			</td>

HTML;
			$i++;
			}	//end of the inner FOR loop
			//close the row
			echo <<<HTML
			</tr>

HTML;
		} //the end of the outer while loop.
		//close out the table
		echo "</table>\r\n";	
} //end of the general if
?>
 
Thank you jpadie, that is awsome...

I will give that a go now, and feedback.

thanks again

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top