A problem with seperating pages.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tnrh1
Forum Newbie
Posts: 14
Joined: Tue Nov 30, 2010 8:27 am

A problem with seperating pages.

Post by tnrh1 »

Hello,
I'm kind of new in this forum and I hope to become a static member :)

Anyway to my question:
I'm trying to seperate the results from the sql into pages with ID=x .
The problem is that i'm using a form to choose a category (of movies) and when I want to pass to the next page it asks me to choose again
cause the page reloaded .. so how do I solve it?
here is the code:

Code: Select all

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body dir="rtl">
<div id="wrapper">
<?php
require_once ("MyFormmScript.php");
require_once ("MyConnectionScript.php");
$selected=$_POST[movies];
if (isset($_GET["page"])) 
			$page  = $_GET["page"]; 
else 
			$page=1; 
$start_from = ($page-1) * 14; 
mysql_select_db("MyDatabase", $con);
		if ($selected=='הכל')
				$result = mysql_query("SELECT * FROM MyTable ORDER BY year DESC LIMIT $start_from, 14");
		else		
				$result = mysql_query("SELECT * FROM MyTable WHERE category='$selected' ORDER BY year DESC LIMIT $start_from, 5");

?>

<?php

if ($row = mysql_fetch_array($result)==0)
		echo "<br /><center>אנא בחר קטגוריה</center>";
while($row = mysql_fetch_array($result))
  {
	?>
	
	<hr/>	
	<div style="overflow : auto; height:140px;">
		<!--תמונה וקישור הסרט !-->
		<a href="<?php echo $row['mov_link'];?>" target="_new">
		<img src="<?php echo $row['pic_link'];?>" alt="<?php echo $row['tags'];?>" width="160" height="120" align="right" /></a>
		<div id="help"><font color="#000000">.</font></div>
		<!--שם הסרט וקישור !-->
		<div align="right" id="hesber4"><font size="3"><u><a href="<?php echo $row['mov_link']; ?>" alt="<?php echo $row['tags'];?>" target="_new" onMouseOver="javascript:this.style.color='#b02904'" 
		onMouseOut="javascript:this.style.color='#FFF'"><?php echo $row['name'];?></a></u></font><br /><br />
		<!--תקציר !-->
		<?php echo $row['summary'];?><br />
		<!--טריילר !-->
		<div align="left">לצפייה בטריילר <a href="<?php echo $row['trailer_link'];?>" target="_new">לחץ כאן</a>
		</div>
		</div>
		
		<!--פרטים נוספים על הסרט !-->
		<div id="hesber3" align="right">
		<u>קטגוריה:</u>&nbsp;<?php echo $row['category'];?><br />
		<u>שנת יציאה:</u>&nbsp;<?php echo $row['year'];?><br />
		<u>אורך:</u>&nbsp;<?php echo $row['length'];?><br />
		<u>שפה:</u>&nbsp;<?php echo $row['language'];?><br />
		<u>מדינה:</u>&nbsp;<?php echo $row['country'];?><br />
		<u>שחקנים:</u>&nbsp;<?php echo $row['actors'];?><br />
		<u>תגיות:</u>&nbsp;<?php echo $row['tags'];?>
		</div>
	</div>	
		
  <?php 
	}
?>
<hr/>	
</div>
<?php 
	if ($selected=='הכל')
				$result = mysql_query("SELECT COUNT(name) FROM MyTable");
	else			
				$result = mysql_query("SELECT COUNT(name) FROM MyTable WHERE category='$selected' "); 
	$row = mysql_fetch_array($result);
	$total_records = $row[0]; 
	$total_pages = ceil($total_records / 14); 
	echo "<center>";
	for ($i=1; $i<=$total_pages; $i++) 
		{ 
				echo "<a href='movies.php?page=".$i."'>".$i."</a> "; 
		}
	echo "</center>";
?>		
</body>
</html>
Since you guys dont speek hebrew I will explain few things:

Code: Select all

if ($selected=='הכל')
means if the result from the form is "all"
select * from xxx
else (means I choosed a category like comedy,action etc .. )
SELECT * FROM MyTable WHERE category ......

Here is a link to the page: http://www.sdarot2go.tv/movies.php
and choose the first option in the form "הכל" which means "all" cause you dont know hebrew.


Thanks in advanced.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: A problem with seperating pages.

Post by litebearer »

Passing data (read variables) from page to page can be done via $_GET (sends the data 'attached' to the url), $_POST (besides normal fields, you can have hidden fields) used with forms;OR by using sessions .
tnrh1
Forum Newbie
Posts: 14
Joined: Tue Nov 30, 2010 8:27 am

Re: A problem with seperating pages.

Post by tnrh1 »

Litebearer,
Thanks for replaying but I dont see how your post is connected to my problem.
And I know what is the role of the $_GET and $_POST ..

Thanks anyway
tnrh1
Forum Newbie
Posts: 14
Joined: Tue Nov 30, 2010 8:27 am

Re: A problem with seperating pages.

Post by tnrh1 »

Sorry of double posting but I really need your help guys.
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: A problem with seperating pages.

Post by jim.barrett »

You could pass a $_GET variable with the link to switch pages...

Code: Select all

echo "<a href='movies.php?page=".$i."&category=".$selected."'>".$i."</a> ";
and then something like

Code: Select all

if(isset($_GET['category'])) { $selected=$_GET['category']; } else { $selected=$_POST['movies']; }
Of course, you'll have to be certain that you will always have a value for either $_POST['movies'] or $_GET['category']
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: A problem with seperating pages.

Post by jim.barrett »

You may also want to indicate which category is selected in your drop-down list...something like this.

Code: Select all

<select>
  <option value="first" <?php if($selected=="first") { echo " selected"; }?>>first</option>
  <option value="second" <?php if($selected=="second") { echo " selected"; }?> selected>second</option>
  <option value="third" <?php if($selected=="third") { echo " selected"; }?>>third</option>
  <option value="fourth" <?php if($selected=="fourth") { echo " selected"; }?>>fourth</option>
</select>
tnrh1
Forum Newbie
Posts: 14
Joined: Tue Nov 30, 2010 8:27 am

Re: A problem with seperating pages.

Post by tnrh1 »

Thank you so much! It's working now.
But I have another problem,
After I select to show all the movies page 2 for example the url will look like that:
http://sdarot2go.tv/movies.php?page=2&c ... 7%9B%D7%9C
and then if I want to move to another category nothing happend.

You can try it in here: http://sdarot2go.tv/movies.php
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: A problem with seperating pages.

Post by jim.barrett »

Are you making an AJAX call when the 'Submit Query' button is clicked?
tnrh1
Forum Newbie
Posts: 14
Joined: Tue Nov 30, 2010 8:27 am

Re: A problem with seperating pages.

Post by tnrh1 »

No I dont, here is the files:

Code: Select all

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body dir="rtl">

<?php
require_once ("MyFormmScript.php");
require_once ("MyConnectionScript.php");
echo "<br /><div id='wrapper' border='1' STYLE='background-color:#000;border:groove;border-bottom:thick double;border-right:thick double;border-color:#191919;'>";
if(isset($_GET['category'])) 
		$selected=$_GET['category']; 
else 
		$selected=$_POST['movies']; 
		
if (isset($_GET["page"])) 
			$page  = $_GET["page"]; 
else 
			$page=1; 
			
$start_from = ($page-1) * 14; 
mysql_select_db("MyDatabase", $con);
		if ($selected=='הכל')
				$result = mysql_query("SELECT * FROM Movies ORDER BY year DESC LIMIT $start_from, 14");
		else		
				$result = mysql_query("SELECT * FROM Movies WHERE category='$selected' ORDER BY year DESC LIMIT $start_from, 5");

?>

<?php

if ($row = mysql_fetch_array($result)==0)
		echo "<center>אנא בחר קטגוריה</center>";
while($row = mysql_fetch_array($result))
  {
	?>
	
	<hr/>	
	<div id="movieHover" style="overflow:auto;height:140px;">
		<!--תמונה וקישור הסרט !-->
		<a href="<?php echo $row['mov_link'];?>" target="_new">
		<img src="<?php echo $row['pic_link'];?>" alt="<?php echo $row['tags'];?>" width="160" height="120" align="right" /></a>
		<div id="help"><font color="#000000">.</font></div>
		<!--שם הסרט וקישור !-->
		<div align="right" id="hesber4"><font size="3"><u><a href="<?php echo $row['mov_link']; ?>" alt="<?php echo $row['tags'];?>" target="_new" onMouseOver="javascript:this.style.color='#b02904'" 
		onMouseOut="javascript:this.style.color='#FFF'"><?php echo $row['name'];?></a></u></font><br /><br />
		<!--תקציר !-->
		<?php echo $row['summary'];?><br />
		<!--טריילר !-->
		<div align="left">לצפייה בטריילר <a href="<?php echo $row['trailer_link'];?>" target="_new">לחץ כאן</a>
		</div>
		</div>
		
		<!--פרטים נוספים על הסרט !-->
		<div id="hesber3" align="right">
		<u>קטגוריה:</u>&nbsp;<?php echo $row['category'];?><br />
		<u>שנת יציאה:</u>&nbsp;<?php echo $row['year'];?><br />
		<u>אורך:</u>&nbsp;<?php echo $row['length'];?><br />
		<u>שפה:</u>&nbsp;<?php echo $row['language'];?><br />
		<u>מדינה:</u>&nbsp;<?php echo $row['country'];?><br />
		<u>שחקנים:</u>&nbsp;<?php echo $row['actors'];?><br />
		<u>תגיות:</u>&nbsp;<?php echo $row['tags'];?>
		</div>
	</div>	
		
  <?php 
	}
?>

</div><br />
<?php 


	echo "<div border='1' STYLE='width:920px;height:20px;background-color:#000;border:groove;border-color:#191919;'>";
	
	if ($selected=='הכל')
				$result = mysql_query("SELECT COUNT(name) FROM Movies");
	else			
				$result = mysql_query("SELECT COUNT(name) FROM Movies WHERE category='$selected' "); 
	$row = mysql_fetch_array($result);
	$total_records = $row[0]; 
	$total_pages = ceil($total_records / 14); 
	echo "<center>";
	for ($i=1; $i<=$total_pages; $i++) 
		{ 
				echo "<a href='movies.php?page=".$i."&category=".$selected."'>".$i."</a> ";

		}
	echo "</center>";

?>
</div>	
</body>
</html>


and here is the MovieSelect.php:

Code: Select all

<html>
<body>

<div align="center" style="width:920px;height:25px;-moz-border-radius: 35px;border-radius: 35px;background-color:#340102;border:groove;border-color:#000;margin:0 auto 0 auto;"> 
		<form method="POST">
				<select name="movies">
						<option value="הכל">הכל</option>   //all
						<option value="מתח/פעולה">מתח/פעולה</option> // action
						<option value="קומדיה">קומדיה</option> // comedy
						<option value="אימה">אימה</option> //scary
						<option value="רומנטיקה">רומנטיקה</option> //romantic
						<option value="אנימציה">אנימציה</option> //animation
						<option value="ילדים">ילדים</option> //kids
						<option value="דרמה">דרמה</option> //dram etc ....
						<option value="מדע בדיוני">מדע בדיוני</option> 
						<option value="ישראלי">ישראלי</option>
						<option value="פשע">פשע</option>
						<option value="דוקומנטרי">דוקומנטרי</option>
				</select>
				<input type="submit" />
		</form>		
		
</div>

Post Reply