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

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

Post by volka »

ah, shi.., forgot it's the same page... :oops:
ok, something more explanatory.
If your script stops somewhere and you can't figure why and where try to trace to where the script is running.
e.g.

Code: Select all

<?php ...
echo 'checking $cat, value is: ', $cat, '<br />'; // debug, remove me
if ($cat <> "")
            { ...
?>
esp. since you have some if-clauses without an else-block. If none of your conditions are fulfilled, what happens?

Although echo/printf-debugging is effective for small/medium projects it's ugly and can lead to unexpected behaviours (if you forget to remove all of the statements for example).
When your projects become more complex you should consider installing a real debugger (see: http://php.net/debugger)

Another way is to use assertions (http://www.php.net/manual/en/function.assert.php)
(btw: looking into your code I found you've placed error_reporting() and ini_set() at the wrong place)
ok, let's take (a part of) the basic structure of your script

Code: Select all

<?php
error_reporting(TRUE);
ini_set('display_errors', TRUE);
require_once('Connections/cds.php');
?>

<?php
$db_connection = mysql_connect($hostname_cds , $username_cds , $password_cds) or die ("Could not connect to database");

...

if ($s == "pw")
{
	...
}

if ($s == "cd")
{
	if (($title <> "")||($company <> ""))
	{
	
	}
	
	if ($disc <> "")
	{
	
	}
	
	if ($cat <> "")
	{
	
	}
}
?>
and now think about possible assertions. (only this part. It does not reflect all of your script, but it's only an example.
If there's something else in the script there have to be other assertions. But I'm sure you'll get the point ;) )
At the top-level your script takes different actions on the value of $s, valid values are pw and cd. You might write

Code: Select all

<?php ...
if ($s == "pw")
{ ... }
elseif($s == "cd")
{ ... }
else
{ echo 'malformed request'; }
... ?>
but since this is about assertions...
If the script shall do anything there must be a variable $s and its value must be either pw or cd

Code: Select all

assert('isset($s) &amp;&amp; ($s=="pw" || $s=="cd")');
now if it is =="cd" there must be one of the following variables defined: $title, $company, $disc, $cat. Otherwise nothing happens. Again this could be written as

Code: Select all

<?php ...
if (($title <> "")||($company <> ""))
{ ...	}
elseif ($disc <> "")
{ ...	}
elseif ($cat <> "")
{ ...	}
else
	echo 'malformed request for s=cd';
... ?>
or as assertion

Code: Select all

assert('!empty($title) || !empty($company) || !empty($disc) || !empty($cat)')
The "whole" script

Code: Select all

<?php
error_reporting(TRUE);
ini_set('display_errors', TRUE);
assert_options (ASSERT_ACTIVE, 1);
assert_options (ASSERT_WARNING, 1);
require_once('Connections/cds.php');
?>

<?php
$db_connection = mysql_connect($hostname_cds , $username_cds , $password_cds) or die ("Could not connect to database");

...

assert('isset($s) && ($s=="pw" || $s=="cd")');

if ($s == "pw")
{
	...
}
elseif ($s == "cd")
{
	assert('!empty($title) || !empty($company) || !empty($disc) || !empty($cat)');
	if (($title <> "")||($company <> ""))
	{ ...	}
	elseif ($disc <> "")
	{ ...	}
	elseif ($cat <> "")
	{ ...	}
	else
		echo 'malformed request for s=cd';
}
else
	'echo malformed request, parameter s is missing';
?>
hope this helps a bit

p.s.: no code-snippet is tested ;)
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

Thanks! :) :) I finally got it to work

Code: Select all

<?php
		elseif ($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;
					
					$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 - $end). '&cat=' .$cat. '&s='.$s.'">Previous</a> |';
							}
							
							if($num_rows > ($start + $end)) 
							{
							echo '| <a href="'. $PHP_SELF .'?start=' .($start + $end). '&cat=' .$cat. '&s='.$s.'">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 | ".$start."</td>";
					    echo "</tr>";
					 	echo "</table>";
		}
?>
I'm having 3 variables in my next/previous links

Code: Select all

<?php
							if($start > 0) 
							{
							echo '| <a href="', $PHP_SELF, '?start='.($prev - $end). '&cat=' .$cat. '&s='.$s.'">Previous</a> |';
							}
							
							if($num_rows > ($start + $end)) 
							{
							echo '| <a href="'. $PHP_SELF .'?start=' .($start + $end). '&cat=' .$cat. '&s='.$s.'">Next</a> |';
							}
?>
$s (for either cd query or pw query)
$cat (for category (in this snippet only))
$start for the next/prev stuff..

Thanks for all your help Volka. :) :) :)
Post Reply