Jump to content

Wierd issue with FOR loops and <div>s


FlashNinja

Recommended Posts

So I'm producing a number of results in a for loop. There results are displayed on the site and should stay on the 'page'.

 

E.G.

 

<div = 'body'>

 

'For loop'

 

"Results"

 

'End of For loop'

 

</div>

 

The problem I'm having here is that when more than 2 or 3 results are produced from this For loop the results actually start moving off of the page.

 

E.G.

 

< div id = 'body>

 

'For loop'

 

'Result'

 

'Result'

 

</div>

 

'Result'

 

'Result'

 

'End of For loop'

 

Could someone take a look at this code and figure out why this is happening, please. I'm stumped.

 

<?php

include 'connect.php';

session_start();

if(empty($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}

$start_from = ($page -1) * 10;
$end = $page * 10;

if (empty($_GET['con']))
{
$cat = $_GET['cat'];
$cat = stripslashes($cat);
$cat = trim($cat);
$cat = mysql_real_escape_string($cat);

$games = mysql_query("SELECT * FROM games WHERE catagory = '$cat' ORDER BY id DESC LIMIT $start_from, $end") or die(mysql_error());
$numRows = mysql_num_rows($games);
}
else
{
$con = $_GET['con'];
$con = stripslashes($con);
$con = trim($con);
$con = mysql_real_escape_string($con);

$games = mysql_query("SELECT * FROM games WHERE console = '$con' ORDER BY id DESC LIMIT $start_from, $end") or die(mysql_error());
$numRows = mysql_num_rows($games);
}

if(empty($_GET['con']) || (empty($_GET['cat'])))
{

}
else
{
    $con = $_GET['con'];
$con = stripslashes($con);
$con = trim($con);
$con = mysql_real_escape_string($con);

$cat = $_GET['cat'];
$cat = stripslashes($cat);
$cat = trim($cat);
$cat = mysql_real_escape_string($cat);

$games = mysql_query("SELECT * FROM games WHERE console ='$con' AND catagory = '$cat' ORDER BY id DESC LIMIT $start_from, $end") or die(mysql_error());
$numRows = mysql_num_rows($games);
}

?>

<html>

<link href="game.css" type="text/css" rel="stylesheet"/>

<div id ='body1'>

<div id ='headm'>

<a style = 'text-decoration:none' href ='index.php'>Home</a>
|
<a style = 'text-decoration:none' href ='about.php'> About</a>
|
<a style = 'text-decoration:none' href ='support.php'> Support</a>

</div>

<div id = 'banner'>

<div id = 'logo'><a  style="text-decoration:none" href='index.php'>

<h1>Game-on</h1>

</a></div>

<div id='basket'>

You have <?php

if(empty($_SESSION['id']))
   {
   echo "0";
   }
   else
   {
   $uid = $_SESSION['id'];
   $sql = "SELECT * FROM basket WHERE session = '$uid'";
   $basket = mysql_query($sql) or die (mysql_error());
   $numrows = mysql_num_rows($basket);
   echo $numrows;
   }

?> items in your basket

</div>

<div id='bbutton'>

<ul><li><a href='basket.php'>Basket</a></li><ul>

</div>

<div id ='search'>

	<form id="search" class = 'form' action="search.php?page=" enctype="multipart/form-data" method="post">
	<input type="text" id="search" name = "search" tabindex="1"/>
	<input type ='submit' class = 'submit' name ='Search' value ='Search' />
    </form>

</div>

</div>

<div id ='body2'>

<div id ='bmenu'>

Filter:
<br>
<?php if(empty($_GET['con']))
               {
		   echo 'Console: ';
		   }
		   else
		   {
		   echo 'Console: '.$_GET['con'];
		   }
?>
<br>
<?php if(empty($_GET['cat']))
		   {
		   echo 'Category: ';
		   }
		   else
		   {
		   echo 'Category: '.$_GET['cat'];
		   }
?>			   
<br>
<br>

<h2>Consoles:</h2>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=Xbox 360&cat=<?php if(isset($_GET['cat'])){ echo $_GET['cat'];}?>'>Xbox 360</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=PS3&cat=<?php if(isset($_GET['cat'])){ echo $_GET['cat'];}?>'>PS3</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=Nintendo Wii&cat=<?php if(isset($_GET['cat'])){ echo $_GET['cat'];}?>'>Nintendo Wii</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=Nintendo 3DS&cat=<?php if(isset($_GET['cat'])){ echo $_GET['cat'];}?>'>Nintendo 3DS</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=PS Vita&cat=<?php if(isset($_GET['cat'])){ echo $_GET['cat'];}?>'>PS Vita</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=Nintendo DS&cat=<?php if(isset($_GET['cat'])){ echo $_GET['cat'];}?>'>Nintendo DS</a>
<br>
<br>
<br>
<h2>Categories:</h2>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Action'>Action</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Adventure'>Adventure</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Fighting'>Fighting</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Horror'>Horror</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Puzzle'>Puzzle</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Racing'>Racing</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Role Playing'>Role Playing</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Shooter'>Shooter</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Sport'>Sport</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=<?php if(isset($_GET['con'])){ echo $_GET['con'];}?>&cat=Strategy'>Strategy</a>
<br>

</div>

<div id ='gctext'>

<h1> Games </h1>

</div>



<?php
for($count = 1; $count <= $numRows; $count++)
	{
	$game = mysql_fetch_array($games);
	$img = $game['img'];
	$name = $game['name'];
	$id = $game['id'];
	$stock = $game['stock'];

	if( $stock == 0)
	{
	$instock = "Out of stock";
	}
	else 
	{
	$instock = "In stock";
	}

	?>

<div id = "gametable">

<div id = 'block'>

<a href='game_page.php?game=<?php echo $id; ?>'><? echo $name; ?></a>
<br>
<br>
<div id = 'price'>
Only ?<?php echo $game['price']; ?>
</div>
<div id = 'stock'>
	<?php echo $instock; ?>
</div>
<div id = 'img' >
<a href='game_page.php?game=<? echo $id; ?>'><img src = '<?php echo $img; ?>'></a>
</div>
<div id = 'basketb'>
<ul><li><a href = 'basket_check.php?id=<? echo $id;?>'>Add to basket</a></li></ul>
</div>
<br>
<br>
</div>
<br>
</div>
<?php		}

?>

<br>

<table>

<?php

if(empty($_GET['cat'])){
$con = $_GET['con'];
$con = stripslashes($con);
$con = trim($con);
$con = mysql_real_escape_string($con);

$pgsql = "SELECT COUNT(name) FROM games WHERE console = '$con'";
$pgresult = mysql_query($pgsql);
$pgrow = mysql_fetch_row($pgresult);
$totalrec = $pgrow[0];
$totalpg = ceil($totalrec / 10);
   
for( $i=1; $i<=$totalpg; $i++)
{
echo "<a href ='member_list.php?page=".$i."'>".$i."</a>";
}
}
else
{
$cat = $_GET['cat'];
$cat = stripslashes($cat);
$cat = trim($cat);
$cat = mysql_real_escape_string($cat);
$pgsql = "SELECT COUNT(name) FROM games WHERE catagory = '$cat'";
$pgresult = mysql_query($pgsql);
$pgrow = mysql_fetch_row($pgresult);
$totalrec = $pgrow[0];
$totalpg = ceil($totalrec / 10);
   
for( $i=1; $i<=$totalpg; $i++)
{
echo "<a href ='game_cat.php?page=".$i."'>".$i."</a>";
}
}

?>

</table>

</div>

</div>

</div>

<footer>

Game-On is a student project and not an actual e-commerce website. Please do not submit any payment details to the website.

</footer>

</html>

 

P.S. Sorry for the crude 'diagrams' of my issue. Hopefully you understand my issue.  :P

Link to comment
https://forums.phpfreaks.com/topic/266027-wierd-issue-with-for-loops-and-s/
Share on other sites

I've tidied it up - getting rid of unused tags and such - but the problem still persists.

 

Here's the new code:

 

<?php

include 'connect.php';

session_start();

$games = mysql_query("SELECT * FROM games ORDER BY id DESC LIMIT 5") or die(mysql_error());
$numRows = mysql_num_rows($games);

?>
<html>

<head>
<title>
Game-On
</title>
<link href="game.css" type="text/css" rel="stylesheet">
</head>


<div id ='body1'>

<div id ='headm'>

<a style = 'text-decoration:none' href ='index.php'>Home</a>
|
<a style = 'text-decoration:none' href ='about.php'> About</a>
|
<a style = 'text-decoration:none' href ='support.php'> Support</a>

</div>

<div id = 'banner'>

<div id = 'logo'><a  style="text-decoration:none" href='index.php'>

<h1>Game-on</h1>

</a></div>

<div id='basket'>

You have <?php

if(empty($_SESSION['id']))
   {
   echo "0";
   }
   else
   {
   $uid = $_SESSION['id'];
   $sql = "SELECT * FROM basket WHERE session = '$uid'";
   $basket = mysql_query($sql) or die (mysql_error());
   $numrows = mysql_num_rows($basket);
   echo $numrows;
   }

?> items in your basket 

</div>

<div id='bbutton'>

<ul><li><a href='basket.php'>Basket</a></li></ul>

</div>

	<form id="search" class = 'form' action="search.php?page=" enctype="multipart/form-data" method="post">
	<input type="text" id="search" name = "search" tabindex="1"/>
	<input type ='submit' class = 'submit' name ='Search' value ='Search' />
    </form>

</div>

<div id ='body2'>

<div id ='bmenu'>

<h2>Consoles:</h2>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=Xbox 360&cat='>Xbox 360</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=PS3&cat='>PS3</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=Nintendo Wii&cat='>Nintendo Wii</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=Nintendo 3DS&cat='>Nintendo 3DS</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=PS Vita&cat='>PS Vita</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=Nintendo DS&cat='>Nintendo DS</a>
<br>
<br>
<br>
<h2>Categories:</h2>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Action'>Action</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Adventure'>Adventure</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Fighting'>Fighting</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Horror'>Horror</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Puzzle'>Puzzle</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Racing'>Racing</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Role Playing'>Role Playing</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Shooter'>Shooter</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Sport'>Sport</a>
<br>
<a style = 'text-decoration:none' href='game_cat.php?page=&con=&cat=Strategy'>Strategy</a>
<br>

</div>

<div id ='gtext'>

<h1>Welcome to Game-On.</h1>

</div>

<div id = 'ngames'>

Recently Added Games:
<br>
<br>

</div>

<div id = 'gametableg'>

<?php
for($count = 1; $count <= $numRows; $count++)
	{
	$game = mysql_fetch_array($games);
	$img = $game['img'];
	$name = $game['name'];
	$id = $game['id'];
	$stock = $game['stock'];

	if( $stock == 0)
	{
	$instock = "Out of stock";
	}
	else 
	{
	$instock = "In stock";
	}

	echo "<div class = 'block'>";
	echo "<a href=game_page.php?game=" . $id . ">" . $name . "</a>";
	echo "<br>";
	echo "<br>";
	echo "<div class = 'price'>";
	echo "Only ?".$game['price'];
	echo "</div>";
	echo "<div class = 'stock'>";
	echo $instock;
	echo "</div>";
	echo "<div class = 'img'>";
	echo "<a href=game_page.php?game=" . $id . "><img src = '$img'></a>";
	echo "</div>";
	echo "<div class = 'basketb'>";
	echo "<ul><li><a href = 'basket_check.php?id=". $id ."'>Add to basket</a></li></ul>";
	echo "</div>";
	echo "<br>";
	echo "<br>";
	echo "</div>";
	echo "<br>";



	}

	echo "</div>";
	echo "</div>";

echo "</div>";
?>

<footer>

Game-On is a student project and not an actual e-commerce website. Please do not submit any payment details to the website.

</footer>

</html>

 

I'm going to continue to try and solve this, but hopefully someone here has a clue what's wrong here.

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.