Limit pr/page problem

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

ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Limit pr/page problem

Post by ampersand »

I'm using this code. but when I click next I just ends up with a blank page. what am I doing wrong here. Please help me ..

Code: Select all

<?php
if ($cat <> "")
				{
						echo "<table width="100%" border="0" cellspacing="0" cellpadding="0">";
						echo "<tr valign="top">";
						echo "<td width="40%">Title</td><td width="35%">CD</td><td width="20%">Category</td><td width="5%">Status</td>";
						echo "</tr>";
						
					$color1 = "#F5F5F5";  
					$color2 = "#FFFFFF";  
					$row_count = 0;
					

					$result = mysql_query( "SELECT * FROM cd, sw, cat WHERE (sw.cat_id LIKE '$cat') AND sw.cat_id LIKE cat.cat_id AND sw.cd_id LIKE cd.cd_id LIMIT ".$start.", 15", $db_connection) or die("SELECT Error: ".mysql_error());
					$num_rows = mysql_num_rows($result); 

						while ($row = mysql_fetch_object ($result))
						{
						$row_color = ($row_count % 2) ? $color1 : $color2;
						
							echo	"<tr class="middle" valign="top">";
							echo	"<td bgcolor="$row_color"><a href="#" onClick="openWindow('sw_info.php?id=" . $row->sw_id . "','','scrollbars=yes,width=500,height=350')">" . $row->sw_title . "</a></td>";
							echo	"<td bgcolor="$row_color"><a href="#" onClick="openWindow('cd_info.php?id=" . $row->cd_id . "','','scrollbars=yes,width=500,height=500')">" . $row->cd_title . "</a></td>";
							echo	"<td bgcolor="$row_color">" . $row->cat_title . "</td>";
							echo	"<td bgcolor="$row_color"><a href="status?id=" . $row->cd_id . "" target="status"><img src="images\s" . $row->io . ".jpg " border="0"></a></td>";
							echo	"</tr>";

						$row_count++; 
						} 
				}
?>
This is the code that show the next/previous links

Code: Select all

<?php 					$result = mysql_query( "SELECT count(*) FROM cd, sw, cat WHERE (sw.cat_id LIKE '$cat') AND sw.cat_id LIKE cat.cat_id AND sw.cd_id LIKE cd.cd_id", $db_connection) or die("SELECT Error: ".mysql_error()); 
					$num_rows = array_shift(mysql_fetch_row($result)); 
					
							if($start > 0) 
							{
							echo "<a href="" . $PHP_SELF . "?start=" . ($start - 15) . "">Previous</a><BR>\n"; 
							}
							
							if($num_rows > ($start + 15)) 
							{
							echo "<a href="" . $PHP_SELF . "?start=" . ($start + 15) . "">Next</a><BR>\n"; 
							}

?>
Thanks in advance
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

blank means "nothing at all, even the browser's source view showed an empty page" ?
Have you checked the server's error-log (if possible)?
What happens if you put

Code: Select all

<?php
ini_set('display_errors', TRUE);
error_reporting(TRUE);
if ($cat <> "") 
...
into your script?
Did you read Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+ and found $cat, $start, ... to be valid?
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

Well. it's not blank, it shows like before with the borders around etc.., but with no data.
When clicking next I should get the next 15 results..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

have you tried the

Code: Select all

ini_set('display_errors', TRUE);
error_reporting(TRUE);
settings?
I'm quite sure you will see several warnings ;)
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

I've inserted the code, but I don't get any error messages :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

even if you press the "next" link?
curious, where does $cat come from?
not from

Code: Select all

echo "<a href="" . $PHP_SELF . "?start=" . ($start + 15) . "">Next</a><BR>\n";
as it is not included as parameter.
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

'$cat' is a variable from the search form on the previous page.
Here is the complete query page:

Code: Select all

<?php require_once('Connections/cds.php'); ?>

<?php
$db_connection = mysql_connect($hostname_cds , $username_cds , $password_cds) or die ("Could not connect to database");
mysql_select_db ($database_cds , $db_connection) or die ("Could not find database")
?>
<script language="JavaScript" type="text/JavaScript">

function openWindow(theURL,winName,features) 
{
  window.open(theURL,winName,features);
}

</script>
<?php

/* HEADER STUFF*/


echo "<title>Search Results</title>";
echo "<link href="styles/stylefile.css" rel="stylesheet" type="text/css">";
echo "<body bgcolor="#2B5796" leftmargin="0" topmargin="0">";

	echo "<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">";
	echo "<tr>";
	echo "<td align="center" valign="middle">";
/*--TABLE START--*/
	echo "<table border="0" cellpadding="0" cellspacing="0" class="table">";
	echo "<tr>";
	echo "<td class="top">Search Results</td>";
	echo "<td align="right" class="top"><a href="">New Search</a>><a href="crossroad.php?id=".$id."">Home</a>><a href="index.php">index</a></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan="2" valign="top" align="center">";
/*Next-Previous Settings*/
		if(!isset($start)) $start = 0; 
		$next=$start + 15;
		$prev=$start + 15;
		$end=15;

if ($s == "pw")
{
		if ($domain <> "")
		{	
						echo "<table width="100%" border="0" cellspacing="0" cellpadding="0">";
						echo "<tr valign="top">";
						echo "<td width="35%">Website Title</td><td width="35%">Username</td><td width="25%">Password</td><td width="5%"></td>";
						echo "</tr>";
						
					$color1 = "#F5F5F5";  
					$color2 = "#FFFFFF";  
					$row_count = 0;
								
					$query = "SELECT * FROM pw WHERE (title LIKE '%$domain%') LIMIT ".$start.", ".$end."";
					$result = mysql_query ($query, $db_connection) or die("SELECT Error [$query]:".mysql_error());
					$num_rows = mysql_num_rows($result); 
					
						while ($row = mysql_fetch_object ($result))
						{
						$row_color = ($row_count % 2) ? $color1 : $color2;
						
							echo	"<tr class="middle" valign="top">";
							echo	"<td bgcolor="$row_color"><a href="".$row->url."" target="_blank">" . $row->title . "</a></td>";
							echo	"<td bgcolor="$row_color">" . $row->user . "</a></td>";
							echo	"<td bgcolor="$row_color">" . $row->pass . "</td>";
							echo	"<td bgcolor="$row_color"></td>";
							echo	"</tr>";

						$row_count++; 
						} 

	
						echo "</td>";
						echo "</tr>";
						echo "<tr valign="bottom" align="center">";
						echo "<td align="center" colspan="4">";
						
						//second script added for navigation 
							$query = "SELECT count(*) FROM pw WHERE (pw.url LIKE '%$domain%')";
							$result = mysql_query($query , $db_connection) or die("SELECT Error [$query]:".mysql_error());
							$numrows = array_shift(mysql_fetch_row($result)); 
					
							if($start > 0) 
							{
							echo "| <a href="" . $PHP_SELF . "?start=" . $prev . "">Previous</a> |"; 
							}
							
							if($numrows > ($start + $end) ) 
							{
							echo "| <a href="" . $PHP_SELF . "?start=" . $next . "">Next</a> |"; 
							}
								
						echo "</td>";
						echo "</tr>";
						echo "</table>";
						echo "</td>";
					    echo "</tr>";
						echo "<tr>";
						echo "<td height="10" colspan="3" class="bottom">There are ".$num_rows." results in your query</td>";
					    echo "</tr>";
					 	echo "</table>";
		}	
}

if ($s == "cd")
{
		if (($title <> "")||($company <> ""))
		{
						echo "<table width="100%" border="0" cellspacing="0" cellpadding="0">";
						echo "<tr valign="top">";
						echo "<td width="40%">Title</td><td width="35%">CD</td><td width="20%">Category</td><td width="5%">Status</td>";
						echo "</tr>";
			
					$color1 = "#F5F5F5";  
					$color2 = "#FFFFFF";  
					$row_count = 0;
					
					$query = "SELECT * FROM cd, sw, cat WHERE (sw.sw_title LIKE '%$title%' AND sw.company LIKE '$company%') AND sw.cat_id LIKE cat.cat_id AND sw.cd_id LIKE cd.cd_id ORDER BY sw_title ".$order." LIMIT ".$start.", ".$end."";
					$result = mysql_query ($query, $db_connection) or die("No records found");
					$num_rows = mysql_num_rows($result); 
									
						while ($row = mysql_fetch_object ($result))
						{
						$row_color = ($row_count % 2) ? $color1 : $color2;
						
							echo	"<tr class="middle" valign="top">";
							echo	"<td bgcolor="$row_color"><a href="#" onClick="openWindow('sw_info.php?id=" . $row->sw_id . "','','scrollbars=yes,width=500,height=350')">" . $row->sw_title . "</a></td>";
							echo	"<td bgcolor="$row_color"><a href="#" onClick="openWindow('cd_info.php?id=" . $row->cd_id . "','','scrollbars=yes,width=500,height=500')">" . $row->cd_title . "</a></td>";
							echo	"<td bgcolor="$row_color">" . $row->cat_title . "</td>";
							echo	"<td><a href="status.php?id=" . $row->cd_id . "" onClick="OpenWindow"><img src="images\s" . $row->io . ".jpg " border="0"></a></td>";
							echo	"</tr>";
							
						$row_count++; 
						} 
					
						echo "</td>";
						echo "</tr>";
						echo "<tr valign="bottom" align="center">";
						echo "<td colspan="2">";
						
						//second script added for navigation 
							$query = "SELECT count(*) FROM cd, sw, cat WHERE (sw.sw_title LIKE '%$title%' AND sw.company LIKE '$company%') AND sw.cat_id LIKE cat.cat_id AND sw.cd_id LIKE cd.cd_id";
							$result = mysql_query($query , $db_connection) or die("SELECT Error [$query]:".mysql_error());
							$num_rows = array_shift(mysql_fetch_row($result)); 
					
							if($start > 0) 
							{
							echo "| <a href="" . $PHP_SELF . "?start=" . $prev . "">Previous</a> |"; 
							}
							
							if($num_rows > ($start + $end)) 
							{
							echo "| <a href="" . $PHP_SELF . "?start=" . $next . "">Next</a> |"; 
							}
								
						echo "</td>";
						echo "</tr>";
						echo "</table>";
						
						echo "</td>";
					    echo "</tr>";
						echo "<tr>";
						echo "<td height="10" colspan="3" class="bottom">There are ".$num_rows." results in your query</td>";
					    echo "</tr>";
					 	echo "</table>";
						exit;
		}

		if ($disc <> "")
		{
						echo "<table width="100%" border="0" cellspacing="0" cellpadding="0">";
						echo "<tr valign="top">";
						echo "<td width="650">CD</td><td width="50">Status</td>";
						echo "</tr>";
						
					$color1 = "#F5F5F5";  
					$color2 = "#FFFFFF";  
					$row_count = 0;
					
					$category_query = "SELECT * FROM cd WHERE (cd.cd_title LIKE '$disc%') ORDER BY cd_title ".$order." LIMIT ".$start.", ".$end."";
					$result = mysql_query ($category_query, $db_connection) or die("No records found");
					$num_rows = mysql_num_rows($result); 
									
						while ($row = mysql_fetch_object ($result))
						{
						$row_color = ($row_count % 2) ? $color1 : $color2;
						
							echo	"<tr class="middle" valign="top">";
							echo	"<td bgcolor="$row_color"><a href="#" onClick="openWindow('cd_info.php?id=" . $row->cd_id . "','','scrollbars=yes,width=500,height=500')">" . $row->cd_title . "</a></td>";
							echo	"<td><a href="status.php?id=" . $row->cd_id . "" onClick="OpenWindow"><img src="images\s" . $row->io . ".jpg " border="0"></a></td>";
							echo	"</tr>";

						$row_count++; 
						} 
					
						echo "</td>";
						echo "</tr>";
						echo "<tr valign="bottom" align="center">";
						echo "<td colspan="2">";
						
						//second script added for navigation 
							$query = "SELECT count(*) FROM cd WHERE (cd.cd_title LIKE '$disc%')";
							$result = mysql_query($query , $db_connection) or die("SELECT Error [$query]:".mysql_error());
							$num_rows = array_shift(mysql_fetch_row($result)); 
					
							if($start > 0) 
							{
							echo "| <a href="" . $PHP_SELF . "?start=" . $prev . "">Previous</a> |"; 
							}
							
							if($num_rows > ($start + $end)) 
							{
							echo "| <a href="" . $PHP_SELF . "?start=" . $next . "">Next</a> |"; 
							}
								
						echo "</td>";
						echo "</tr>";
						echo "</table>";

						echo "</td>";
					    echo "</tr>";
						echo "<tr>";
						echo "<td height="10" colspan="2" class="bottom">There are ".$num_rows." results in your query</td>";
					    echo "</tr>";
					 	echo "</table>";
		}


		if ($cat <> "")
					ini_set('display_errors', TRUE); 
					error_reporting(TRUE);
		{
						echo "<table width="100%" border="0" cellspacing="0" cellpadding="0">";
						echo "<tr valign="top">";
						echo "<td width="40%">Title</td><td width="35%">CD</td><td width="20%">Category</td><td width="5%">Status</td>";
						echo "</tr>";

					$color1 = "#F5F5F5";  
					$color2 = "#FFFFFF";  
					$row_count = 0;
					
					$query = "SELECT * FROM cd, sw, cat WHERE (sw.cat_id LIKE '$cat') AND sw.cat_id LIKE cat.cat_id AND sw.cd_id LIKE cd.cd_id ORDER BY sw_title ".$order." LIMIT ".$start.", ".$end.""; 
					$result = mysql_query($query , $db_connection) or die("SELECT Error [$query]:".mysql_error());
					$num_rows = mysql_num_rows($result); 
			
						while ($row = mysql_fetch_object ($result))
						{
						$row_color = ($row_count % 2) ? $color1 : $color2;
						
							echo	"<tr class="middle" valign="top">";
							echo	"<td bgcolor="$row_color"><a href="#" onClick="openWindow('sw_info.php?id=" . $row->sw_id . "','','scrollbars=yes,width=500,height=350')">" . $row->sw_title . "</a></td>";
							echo	"<td bgcolor="$row_color"><a href="#" onClick="openWindow('cd_info.php?id=" . $row->cd_id . "','','scrollbars=yes,width=500,height=500')">" . $row->cd_title . "</a></td>";
							echo	"<td bgcolor="$row_color">" . $row->cat_title . "</td>";
							echo	"<td bgcolor="$row_color"><a href="status.php?id=" . $row->cd_id . "" onClick="OpenWindow"><img src="images\s" . $row->io . ".jpg " border="0"></a></td>";
							echo	"</tr>";

						$row_count++; 
						} 
					
						echo "</td>";
						echo "</tr>";
						echo "<tr valign="bottom" align="center">";
						echo "<td colspan="2">";
						
						//second script added for navigation 
							$query = "SELECT count(*) FROM cd, sw, cat WHERE (sw.cat_id LIKE '$cat') AND sw.cat_id LIKE cat.cat_id AND sw.cd_id LIKE cd.cd_id";
							$result = mysql_query($query , $db_connection) or die("SELECT Error [$query]:".mysql_error());
							$num_rows = array_shift(mysql_fetch_row($result)); 
		
							if($start > 0) 
							{
							echo "| <a href="" . $PHP_SELF . "?start=" . $prev . "">Previous</a> |"; 
							}
							
							if($num_rows > ($start + $end)) 
							{
							echo "| <a href="" . $PHP_SELF . "?start=" . $next . "">Next</a> |"; 
							}
							
						echo "</td>";
						echo "</tr>";
						echo "<tr>";
						echo "<td colspan="4">";
						echo "</td>";
						echo "</tr>";
						echo "</table>";
						
						echo "</td>";
					    echo "</tr>";
						echo "<tr>";
						echo "<td height="10" colspan="3" class="bottom">There are ".$num_rows." results in your query</td>";
					    echo "</tr>";
					 	echo "</table>";
		}
}

	echo "</td>";
	echo "</tr>";
	echo "</table>";
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

still I don't see where $cat comes from.
echo "<a href=\"" . $PHP_SELF . "?start=" . ($start + 15) . "\">Next</a><BR>\n";
results in something like

Code: Select all

<a href="/dir/script.php?start=15">Next</a>
if you click it only $start == 15 (or $GET['start'] in recent versions with register_globals off) will be available. It's a new request. All variables of the first one are gone when the second request is handled.
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

Here is where the $disc , $cat etc comes from

Code: Select all

<?php require_once('Connections/cds.php'); ?>
<?php 
 $db_connection = mysql_connect($hostname_cds , $username_cds , $password_cds) or die ("Could not connect to database");
 mysql_select_db ($database_cds , $db_connection) or die ("Could not find database")
 ?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles/stylefile.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#2B5796" leftmargin="0" topmargin="0"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> 
<tr> 
  <td align="center" valign="middle"> 
<table border="0" cellpadding="0" cellspacing="0" class="table"> 
    <tr> 
      <td class="top">Search for specific application</td>
      <td align="right" class="top"><a href="crossroad.php?id=<?php echo "".$id."";?>">Home</a>><a href="index.php">Index</a></td>
    </tr> <tr> 
      <td colspan="2" align="center"> 

<?php

	if ($id == "pw")
	{
		echo "	<form name="form1" method="post" action="query_search.php?s=pw">";
		echo "              <table border="0" cellpadding="5" cellspacing="0" order="0" width="50%" class="border1">";
		echo "                <tr> ";
		echo "                  <td width="100" valign="top">Title:</td>";
		echo "                  <td height="10" valign="top"><input class="x" name="domain" type="text" id="title"></td>";
		echo "                </tr>";
		echo "                <tr>";
		echo "                  <td></td>";
		echo "                  <td></td>";
		echo "                </tr>";
		echo "                <tr>";
		echo "                  <td valign="top">Order by:</td>";
		echo "                  <td> <input type="radio" name="order" id="order" value="ASC" checked>Ascending ";
		echo "                    <input type="radio" name="order" id="order" value="DESC">Descending </td>";
		echo "                </tr>";
		echo "                <tr>"; 
		echo "                  <td colspan="2" valign="top"><hr width="75%"></td>";
		echo "                </tr>";
		echo "                <tr>";
		echo "                  <td colspan="2" align="center"><input name="submit" type="submit" src="images/submit.jpg" alt="Search" value="Search"></td>";
		echo "                </tr>";
		echo "              </table>";
		echo "      </form>";
	}
	

	else
	{
		echo "	<form name="form1" method="post" action="query_search.php?s=cd">";
		echo "              <table border="0" cellpadding="5" cellspacing="0" order="0" width="50%" class="border1">";
		echo "                <tr> ";
		echo "                  <td width="100" valign="top">Title:</td>";
		echo "                  <td height="10" valign="top"><input class="x" name="title" type="text" id="title"></td>";
		echo "                </tr>";
		echo "                <tr>";
		echo "                  <td width="100" valign="top">Company:</td>";
		echo "                  <td height="5" valign="top"><input class="x" name="company" type="text" id="company"></td>";
		echo "                </tr>";
		echo "                <tr> ";
		echo "                  <td height="3" colspan="2" valign="top"><hr width="75%"></td>";
		echo "                </tr>";
		echo "                <tr>";
		echo "                  <td valign="top">Disc:</td>";
		echo "                  <td height="2" valign="top"><input class="x" name="disc" type="text" id="disc"></td>";
		echo "                </tr>";
		echo "                <tr>";
		echo "                  <td valign="top">Category:</td>";
		echo "                  <td height="20" valign="top"><select class="x" name="cat" id="cat">";
		echo "                      <option selected>Choose..</option>";

			$category_query = "SELECT * FROM cat ORDER BY cat_title ASC";
			$query_result = mysql_query ($category_query, $db_connection);

			while ($row = mysql_fetch_object ($query_result))
			{
               print "<option value="" . $row->cat_id . "">" . $row->cat_title . "</option>";
			}

		echo "                    </select>";
		echo "					</td>";
		echo "                </tr>";
		echo "                <tr>";
		echo "                  <td valign="top">Order by:</td>";
		echo "                  <td> <input type="radio" name="order" id="order" value="ASC" checked>Ascending ";
		echo "                    <input type="radio" name="order" id="order" value="DESC">Descending </td>";
		echo "                </tr>";
		echo "                <tr>"; 
		echo "                  <td colspan="2" valign="top"><hr width="75%"></td>";
		echo "                </tr>";
		echo "                <tr>";
		echo "                  <td colspan="2" align="center"><input name="submit" type="submit" src="images/submit.jpg" alt="Search" value="Search"></td>";
		echo "                </tr>";
		echo "              </table>";
		echo "      </form>";
	}
?>

		</td>
	 </tr>
     <tr> 
		<td colspan="3" class="bottom">&nbsp;</td>
	 </tr>
	</table>

	</td>
 </tr>
</table>

<?php
mysql_close($db_connection);

      
?>
But what should I do to make the variables stay when clicking next so I can get the rest of the results ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might add them to the link, e.g. for cat

Code: Select all

echo '<a href="', $PHP_SELF, '?start=', ($start + 15), '&cat=', $cat, '">Next</a><br />';
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

I tried what you said but I don't get the next results. Is there any other errors that you can see in my code ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hm, not that quickly ;)
try some additional debug code

Code: Select all

<?php
if(!isset($cat))
	die('cat not set');
elseif(!isset($start))
	 die('start not set');
else
	 'starting search'; // so there's something printed anyway

if ($cat <> "")
of course the isset()-tests have to take place before you use/set $cat or $start (there was somthing like if(!$start) $start=0; in your code)

insisting in $cat or $start may not be set can be wrong but I can't spot another error that will produce NO output at all.
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

I inserted the debug code before all my if's and the $start at the top.

It prints "start not set" when I choose a category at the search page and clicks submit.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php phpinfo(); ?>
what does this tell you about your php-version and esp. about register_globals?
(really take a look, even if you're sure about the result ;) )
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

This is my info:
PHP Version: 4.1.1

register_globals - Local: On
register_globals - Master: On

System: Windows NT 5.1 build 2600
Server: API Apache
Post Reply