Jump to content

Passing a category into a url/pagination problem


convy

Recommended Posts

Hi basically the category part of this script works but when i try the paginating part (next) it goes back to showing all of the categories so in other words i want it to stay on the same category when i click next. I tried passing the category through the url which ive kept in the code below and although the url does show the category im meant to be in, the code ignores it and shows the latest articles from all categories. I've been trying to figure this out for about 4 days and i dont really know where to even start since im completely new to php so thanks in advance for any help. Anyway heres the code:

<select name="sort_category">
    <option disabled selected>Sort by Category</option>
        <option value="General">General</option>
	<option value="Music">Music</option>
	<option value="Funny">Funny</option>
</select>
<input type="submit" />
</form>
<br>
<?php 
include('admin/conf.php');
include('admin/functions.php');

if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
  
  $startrow = 0;

} else {
  $startrow = (int)$_GET['startrow'];
}
if (isset($_REQUEST['sort_category']))
{
$select_category = $_REQUEST['sort_category']; 
$connection = mysql_connect($host, $user, $pass)
or die ('Unable to connect!');
mysql_select_db($db) or die ('Unable to select database!');
$sql = "SELECT id, title, content, photo, timestamp FROM approved 
WHERE category='$select_category' ORDER BY timestamp DESC LIMIT $startrow,2";
$result = mysql_query($sql) 
or die ("Error in query: $query. " . mysql_error());



if (mysql_num_rows($result) > 0)
{

while($row = mysql_fetch_object($result))
{
?>
<img src="http://trunham.info/pictures/
<?php echo $row->photo;?>" width="100" height="100" border="1" align="left" /> 
        <font size="+2" class="style1"><a style="text-decoration:none" class="articletext" href="article.php?id= 
<?php echo $row->id; ?>" ><?php echo $row->title; ?></a></font><br><br>
<font class="articletext2"><?php echo substr($row->content,0,100) ;?>...</font>
<?PHP

echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+2).'category='.($select_category).'">Next</a>';

$prev = $startrow - 2;


if ($prev >= 0)
    echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'">Previous</a>';
?>

 

I do have the closing curly brackets later on and after this i also have an else part which runs if no category is selected and this outputs the same results that im getting from the pagination but i took it out to see if it was the problem and the paginating still wasnt sticking to the category so i assume it isnt. Here it is anyway:

<?php	
}

else
{ echo 'Latest Articles:<br><br>';
$connection = mysql_connect($host, $user, $pass)
or die ('Unable to connect!');

mysql_select_db($db) or die ('Unable to select database!');
$query = "SELECT id, title, content, photo, timestamp FROM approved 
ORDER BY timestamp DESC LIMIT 0, 5";
$result = mysql_query($query) 
or die ("Error in query: $query. " . mysql_error());

if (mysql_num_rows($result) > 0)
{

while($row = mysql_fetch_object($result))
{
?>
<img src="http://trunham.info/pictures/
<?php echo $row->photo;?>" width="100" height="100" border="1" align="left" /> 
        <font size="+2" class="style1"><a style="text-decoration:none" class="articletext" href="article.php?id= 
<?php echo $row->id; ?>" ><?php echo $row->title; ?></a></font><br><br>
<font class="articletext2"><?php echo substr($row->content,0,100) ;?>...</font> 
<?php
}
}
}
mysql_close($connection);
?>

 

sorry if i haven't explained it very well. i dont really know what im doing

I made a few changes, try it and see if you can work with this.

 

<?php 
include('admin/conf.php');
include('admin/functions.php');

$select_category = mysql_real_escape_string($_GET['category']);
if (!isset($_GET['category'])) {
$select_category = "All";
}

if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
  
  $startrow = 0;

} else {
  $startrow = (int)$_GET['startrow'];
}

$connection = mysql_connect($host, $user, $pass)
or die ('Unable to connect!');
mysql_select_db($db) or die ('Unable to select database!');

if ($select_category == "All") {
$sql = "SELECT id, title, content, photo, timestamp FROM approved ORDER BY timestamp DESC LIMIT $startrow,2";
} else {
$sql = "SELECT id, title, content, photo, timestamp FROM approved 
WHERE category='$select_category' ORDER BY timestamp DESC LIMIT $startrow,2";
}


$result = mysql_query($sql) 
or die ("Error in query: $query. " . mysql_error());



if (mysql_num_rows($result) > 0) {

while($row = mysql_fetch_object($result))
{
?>
<img src="http://trunham.info/pictures/
<?php echo $row->photo;?>" width="100" height="100" border="1" align="left" />
        <font size="+2" class="style1"><a style="text-decoration:none" class="articletext" href="article.php?id=
<?php echo $row->id; ?>" ><?php echo $row->title; ?></a></font><br><br>
<font class="articletext2"><?php echo substr($row->content,0,100) ;?>...</font>
<?PHP
}
}
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+2).'category='.($select_category).'">Next</a>';

$prev = $startrow - 2;


if ($prev >= 0)
    echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'">Previous</a>';
?>
<form name="input" action="" method="get">
<select name="category">
       <option value="<?php echo $select_category; ?>"><?php echo $select_category; ?></option>
       <option value="All">All</option>
       <option value="General">General</option>        
       <option value="Music">Music</option>
       <option value="Funny">Funny</option>
   </select>
<input type="submit" value="Set" />/>
</form>

If wanted a different pagination I wrote 2 different ones up that control the mysql startrow and limit, the simpler one can do any amount per page while the more deluxe one I have set to 10 posts per page.

 

Can see them both working and the codes embed below them.

http://get.blogdns.com/paginate

 

and this is more deluxe

http://get.blogdns.com/dynaindex/paginate.php

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.