Question

sorting results with sub categories

I have a category table and I have subcategories that are defined by parent_id which links to the main category id which is stored in cat_id

table category

cat_id PK int
parent_id int
category char

I want to add something to only show pets in cat X and in sub X

<?php
include 'config.php';
$domain = "http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php";
?>


<p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="action">
<label><b>Sort by:</b></label>

      <select name="sort">
        <option value="name">Name</option>
        <option value="date">Date</option>
        <option value="id">ID</option>
        <option value="owner">Owner</option>
        <option value="category">Category</option>
    </select>
then
    <select name="ascdesc">
        <option value="DESC">Descending</option>
        <option value="ASC">Ascending</option>
    </select>
and show
    <select name="max_results">
        <option value="5">5</option>
        <option value="10" selected="selected">10</option>
        <option value="20">20</option>
        <option value="50">50</option>
    </select>
    <input name="action" type="hidden" value="go">
    <input name="submit" type="submit" style="float:right;" value="Submit!">
</form>

<br />
<label for="pages"><b>Page:</b></label>
</p>

<?php

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


$max_results = 10;

$from = (($page * $max_results) - $max_results);

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pet"),0);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
$total_pages = intval($total_results / $max_results);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
if($page > 1)
{
    $prev = ($page - 1);
    echo "<a href=\"$domain?page=$prev\">&laquo; Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
      {
        echo "$i ";
        }else{
            echo "<a href=\"$domain?page=$i\">$i</a> ";
    }
}

if($page < $total_pages)
{
    $next = ($page + 1);
    echo "<a href=\"$domain?page=$next\">Next &raquo;</a>";
}

// alternating row colors
$color1 = "#f8f8f8";
$color2 = "#cccccc";
$row_count = 0;

if (isset($_POST['action']))
{
      echo "<h1>SORTING ENABLED</h1>";
    $sort = $_POST['sort'];
    $ascdesc = $_POST['ascdesc'];
    $max_results = $_POST['max_results'];
    $category = $_POST['category'];

$sql = "SELECT p.*, c.category FROM pet p, categories c WHERE p.category = c.category ORDER BY p." . $sort . " $ascdesc LIMIT $from, $max_results";

echo "<h2>" . $sql . "</h2>";
   $result = mysql_query($sql);
}
else
{
$sql = "SELECT p.*, c.category FROM pet p, categories c WHERE p.category = c.category ORDER BY p.id DESC LIMIT $from, $max_results";
$result = mysql_query($sql) or die("Bad Query: " . mysql_error());
}

$num_rows  =  mysql_num_rows($result);
echo "<br /><b>Results returned:</b> $num_rows\n";
echo "</div><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\">";
if ($num_rows > 0)
{
    while ($row = mysql_fetch_array($result))
      {
    $row_color = ($row_count % 2) ? $color1 : $color2;
    echo '<tr bgcolor="'. $row_color .'">
          <td>      
          <h1><a href="pet-detial.php?id='. $row['id'] .'" target="_blank">'. $row['title'] .'</a></h1><br />
          <h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
          <h2><b>Posted By:</b> <a href="profile.php?id='. $row['id'] .'" target="_blank">'.$row['username'].'</a></h2><br />

              <p>'.$row['description'].'</p>

              <b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row[date])) .' &middot; <b>Category:</b> '.$row['category'].' &middot;
 
          </td>
              </tr>'."\n";
          $row_count++;
    }

}
else
{
      echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>

That is my code for pets.php which displays them.

Would I do something like?

<label>Select Parent Category:</label>
    <select name="parent">
    <?php
    $query = "SELECT cat_id, category FROM categories WHERE parent_id=0";
    $result = mysql_query($query) or die(mysql_error());
   
    while ($row = mysql_fetch_object($result))
    {
      ?><option value="<?php echo $row->cat_id; ?>"><?php echo $row->category; ?></option>
      <?php
    }
    ?>
    </select><br />

Which will displays all of my main categories which is stored in cat_id as the ID # and category char as text.

Then do the same for maybe? subcategories?

Any suggestions, thanks.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-04-24 at 10:56:58ID22531198
Topics

PHP Scripting Language

,

PHP and Databases

,

PHP for Windows

Participating Experts
2
Points
500
Comments
27

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Uploading Images to a Category/SubCategory with PHP/M…
    Okay - I'm attempting to create an 'admin' section of my website so I can upload my images into a category or appropriate subcategory instead of having to manually add them/code them on the HTML pages.... I have three tables: icon_categories, icon_subcats, icon_images. the...
  2. working with sub-categories
    Does anyone have any idea how I would go about adding sub-categories? I have a client who wants a site like craigslist but with pets, I have tried google and other forums, but with no luck. My database is setup like: category: where the categories are stored (dogs, cats, bi...
  3. MYSQL extracting categories and subcategories
    I have one table that contains various main categories and sub categories and I'm having trouble extracting a list of the categories and subcategories that belong together. The main categories and sub categories are all referenced by a main_num and sub_num. MYSQL TABLE use...
  4. Category/Sub-Category Menu Tree
    Hey Everyone, I need help with a categories/subcategories menu tree for an online store I'm working on. I want to be able to list the menu in the left column of the online store with links to MAIN CATEGORIES as well as showing SUB-CATEGORIES (Infinite level) when a MAIN CATEG...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: rustycpPosted on 2007-04-24 at 14:11:06ID: 18969607

I'm confused about what you're asking for, can you consolidate the question some?

 

by: catonthecouchproductionsPosted on 2007-04-24 at 14:14:50ID: 18969630

I was in a rush typing it before a final sorry.

I want to be able to sort my results, like I have now, but add display on dogs or cats

And give the option for all the subcategories

Ex.

show only black labs in dogs.

 

by: SatelliteCreativePosted on 2007-04-25 at 03:38:32ID: 18972367

Hi,

Ok I'm struggling to get my head around this as I don't have a view of the DB and all your categories etc, but it looks to be me like you have the method of showing the categories and subcategories in dropdown boxes sorted. I suppose you want to leave your "Sort By:" fields as they are, but under them have a "Limit to:" heading with dropdowns for the categories and subcategories. Have you tried this?

It also looks like you have alreay added a WHERE clause to the SQL for: "p.category = c.cat_id"
and a variable to pick up a selected category: $category = $_POST['category'].
So after that i think all you'd need would be to change your SQL statement to look like:

$sql = "SELECT p.*, c.category FROM pet p, categories c WHERE p.category = $category ORDER BY p." . $sort . " $ascdesc LIMIT $from, $max_results";

And if you add a subcategory dropdown, change statment to:

$sql = "SELECT p.*, c.category FROM pet p, categories c WHERE p.category = $category AND p.subcategory = $subcategory ORDER BY p." . $sort . " $ascdesc LIMIT $from, $max_results";

I don't know, something like that! Oh and if you dont want to limit it I think that $category and $subcategory could be set to "%" and that would return all results...

Cheers! Ben

 

by: catonthecouchproductionsPosted on 2007-04-25 at 09:08:35ID: 18974861

Hey man, that sounds like what I want to do! I will try implementing that and hopefully it will work! Ill get back to ya.

I just need to add a drop down for the categories right? If I made that while it would feed from the categories table righT?

Here is my DB layout

-- --------------------------------------------------------

--
-- Table structure for table `categories`
--

CREATE TABLE `categories` (
  `cat_id` int(10) NOT NULL auto_increment,
  `category` varchar(50) NOT NULL default '',
  `parent_id` int(10) NOT NULL default '0',
  PRIMARY KEY  (`cat_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `critter`
--

CREATE TABLE `critter` (
  `id` int(11) NOT NULL auto_increment,
  `username` text NOT NULL,
  `password` varchar(32) NOT NULL default '',
  `name` text NOT NULL,
  `email` text NOT NULL,
  `address` text NOT NULL,
  `phone` text NOT NULL,
  `businfo` text NOT NULL,
  `website` text NOT NULL,
  `date` text NOT NULL,
  `ip` text NOT NULL,
  `actkey` varchar(40) NOT NULL default '',
  `activated` int(1) NOT NULL default '0',
  `zip` varchar(5) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `pet`
--

CREATE TABLE `pet` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(150) default NULL,
  `date` datetime default NULL,
  `name` varchar(255) NOT NULL default '',
  `price` varchar(10) NOT NULL default '',
  `description` text NOT NULL,
  `city` varchar(30) NOT NULL default '',
  `state` varchar(2) NOT NULL default '',
  `zip` varchar(5) NOT NULL default '',
  `username` varchar(255) NOT NULL default '',
  `category` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

 

by: catonthecouchproductionsPosted on 2007-04-25 at 09:21:20ID: 18974990

I could do this:

<select name="parent">
    <?php
    $query = "SELECT cat_id, category FROM categories WHERE parent_id=0";
    $result = mysql_query($query) or die(mysql_error());
   
    while ($row = mysql_fetch_object($result))
    {
      ?><option value="<?php echo $row->cat_id; ?>"><?php echo $row->category; ?></option>
      <?php
    }
    ?>
    </select><br />

But alter it to only show the main categories on not the sub right? then make another one for the sub?

 

by: catonthecouchproductionsPosted on 2007-04-25 at 09:33:56ID: 18975089

Here is my latest code with that while in there for the categories:

<?php
include 'config.php';
$domain = "http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php";
?>


<p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="action">

    <label>Sort by:</label>
    <select name="sort">
        <option value="name">Name</option>
        <option value="date">Date</option>
        <option value="id">ID</option>
        <option value="username">Username</option>
        <option value="category">Category</option>
    </select><br />
    <label>Filter Categories:</label>
    <select name="parent">
    <?php
    $query = "SELECT cat_id, category FROM categories WHERE parent_id=0";
    $result = mysql_query($query) or die(mysql_error());
   
    while ($row = mysql_fetch_object($result))
    {
      ?><option value="<?php echo $row->cat_id; ?>"><?php echo $row->category; ?></option>
      <?php
    }
    ?>
    </select><br />
    <label>DESC or ASC</label>
    <select name="ascdesc">
        <option value="DESC">Descending</option>
        <option value="ASC">Ascending</option>
    </select><br />
    <label>Display</label>
    <select name="max_results">
        <option value="5">5</option>
        <option value="10" selected="selected">10</option>
        <option value="20">20</option>
        <option value="50">50</option>
    </select><br />
   
   
    <input name="action" type="hidden" value="go">
    <input name="submit" type="submit" style="float:right;" value="Submit!">
</form>

<br />
<label for="pages"><b>Page:</b></label>
</p>

<?php

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


$max_results = 10;

$from = (($page * $max_results) - $max_results);

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pet"),0);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
$total_pages = intval($total_results / $max_results);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
if($page > 1)
{
    $prev = ($page - 1);
    echo "<a href=\"$domain?page=$prev\">&laquo; Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
      {
        echo "$i ";
        }else{
            echo "<a href=\"$domain?page=$i\">$i</a> ";
    }
}

if($page < $total_pages)
{
    $next = ($page + 1);
    echo "<a href=\"$domain?page=$next\">Next &raquo;</a>";
}

// alternating row colors
$color1 = "#f8f8f8";
$color2 = "#cccccc";
$row_count = 0;

if (isset($_POST['action']))
{
      echo "<h1>SORTING ENABLED</h1>";
    $sort = $_POST['sort'];
    $ascdesc = $_POST['ascdesc'];
    $max_results = $_POST['max_results'];
    $category = $_POST['category'];

$sql = "SELECT p.*, c.category FROM pet p, categories c WHERE p.category = $category ORDER BY p." . $sort . " $ascdesc LIMIT $from, $max_results";

print "first query - $sql</br>";

echo "<h2>" . $sql . "</h2>";
   $result = mysql_query($sql);
}
else
{
$sql = "SELECT p.*, c.category FROM pet p, categories c WHERE p.category = c.cat_id ORDER BY p.id DESC LIMIT $from, $max_results";

print "second query - $sql</br>";
    $result = mysql_query($sql) or die("Bad Query: " . mysql_error());
}

$num_rows  =  mysql_num_rows($result);
echo "<br /><b>Results returned:</b> $num_rows\n";
echo "</div><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\">";
if ($num_rows > 0)
{
    while ($row = mysql_fetch_array($result))
      {
    $row_color = ($row_count % 2) ? $color1 : $color2;
    echo '<tr bgcolor="'. $row_color .'">
          <td>      
          <h1><a href="pet-detial.php?id='. $row['id'] .'" target="_blank">'. $row['title'] .'</a></h1><br />
          <h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
          <h2><b>Posted By:</b> <a href="profile.php?id='. $row['id'] .'" target="_blank">'.$row['username'].'</a></h2><br />

              <p>'.$row['description'].'</p>

              <b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row[date])) .' &middot; <b>Category:</b> '.$row['category'].' &middot;
 
          </td>
              </tr>'."\n";
          $row_count++;
    }

}
else
{
      echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>


I get this error, when I use the form:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/g/l/a/glanceatx/html/clients/critterclassifieds/pets.php on line 133

$num_rows  =  mysql_num_rows($result);

Would i need to alter both queries, I am looking at my code now, and am confused.

 

by: catonthecouchproductionsPosted on 2007-04-25 at 09:37:11ID: 18975112

I would also have to add what you said:

I don't know, something like that! Oh and if you dont want to limit it I think that $category and $subcategory could be set to "%" and that would return all results...

Because when it loads we want all results correct?

 

by: SatelliteCreativePosted on 2007-04-25 at 09:56:17ID: 18975289

Re that error, I think:

$category = $_POST['category'];

should be:

$category = $_POST['parent'];

when its in the 'sorting enabled' section.

I have to leave work now, and going out for a meal, so Good luck! If you are still having problems I'll check back either late tonight or first thing in the morning.

By the way, that wildcard % thing might not be a problem actually becasue when the first page is showed you use 'second query' which doesnt have any category selected does it?
Its only when people refine their search that they would have to select a category. Although if you always want them to be able to show all then we will have to use a wildcard.

Cheers!

 

by: catonthecouchproductionsPosted on 2007-04-25 at 10:02:57ID: 18975348

I updated that change with parent, but it wont start selecing the categories, it sorts but not by the category type.

Is there a way to only show category X and then also sort it by that, i think to save time I just want to have show only cat X.

Thats fine. If you did check tomorrow or tonight, that would be a big help thank you!

That is correct, the second query is the default one, that has no sorting options.

 

by: SatelliteCreativePosted on 2007-04-26 at 01:50:49ID: 18979950

Hi, how you getting on? I was just about to take a look again when I see that the page has changed and you've added some INNER JOINS etc. And any selection now only returns one result.

So whats the latest? I think the easiest way for me to help is if I set the page up here with your DB, will take 5 mins. If you still need help can you paste the whole of the pets.php. I'll use the DB structure you pasted above, so if thats changed could you paste the new structure too. I can just fill it with random stuff.

 

by: catonthecouchproductionsPosted on 2007-04-26 at 07:12:12ID: 18981586

Alrighty, thanks for you help man! I appreciate it alot! You're a big help here is code for pets.hpp

<?php
include 'config.php';
$domain = "http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php";
?>


<p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="action">

    <label>Sort by:</label>
    <select name="sort">
        <option value="name">Name</option>
        <option value="date">Date</option>
        <option value="id">ID</option>
        <option value="username">Username</option>
        <option value="category">Category</option>
    </select><br />
    <label>Show only:</label>
    <select name="parent">
    <?php
    $query = "SELECT cat_id, category FROM categories WHERE parent_id=0";
    $result = mysql_query($query) or die(mysql_error());
   
    while ($row = mysql_fetch_object($result))
    {
      ?><option value="<?php echo $row->cat_id; ?>"><?php echo $row->category; ?></option>
      <?php
    }
    ?>
    </select><br />
    <label>DESC or ASC</label>
    <select name="ascdesc">
        <option value="DESC">Descending</option>
        <option value="ASC">Ascending</option>
    </select><br />
    <label>Display</label>
    <select name="max_results">
        <option value="5">5</option>
        <option value="10" selected="selected">10</option>
        <option value="20">20</option>
        <option value="50">50</option>
    </select><br />
   
   
    <input name="action" type="hidden" value="go">
    <input name="submit" type="submit" style="float:right;" value="Submit!">
</form>

<br />
<label for="pages"><b>Page:</b></label>
</p>

<?php

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


$max_results = 10;

$from = (($page * $max_results) - $max_results);

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pet"),0);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
$total_pages = intval($total_results / $max_results);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
if($page > 1)
{
    $prev = ($page - 1);
    echo "<a href=\"$domain?page=$prev\">&laquo; Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
      {
        echo "$i ";
        }else{
            echo "<a href=\"$domain?page=$i\">$i</a> ";
    }
}

if($page < $total_pages)
{
    $next = ($page + 1);
    echo "<a href=\"$domain?page=$next\">Next &raquo;</a>";
}

// alternating row colors
$color1 = "#f8f8f8";
$color2 = "#cccccc";
$row_count = 0;

if (isset($_POST['action']))
{
      echo "<h1>SORTING ENABLED</h1>";
    $sort = $_POST['sort'];
    $ascdesc = $_POST['ascdesc'];
    $max_results = $_POST['max_results'];
      $category = $_POST['parent'];

$sql = "SELECT p.*, c.category, U.ID as CritterID FROM pet p INNER JOIN categories c ON p.category = c.cat_id INNER JOIN critter U on p.username = U.username
ORDER BY p." . $sort . " DESC LIMIT $from, $max_results";

print "first query - $sql</br>";

echo "<h2>" . $sql . "</h2>";
   $result = mysql_query($sql);
}
else
{
$sql = "SELECT p.*, c.category, U.ID as CritterID FROM pet p INNER JOIN categories c ON p.category = c.cat_id INNER JOIN critter U on p.username = U.username
ORDER BY p.id DESC LIMIT $from, $max_results";
print "second query - $sql</br>";
    $result = mysql_query($sql) or die("Bad Query: " . mysql_error());
}

$num_rows  =  mysql_num_rows($result);
echo "<br /><b>Results returned:</b> $num_rows\n";
echo "</div><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\">";
if ($num_rows > 0)
{
    while ($row = mysql_fetch_array($result))
      {
    $row_color = ($row_count % 2) ? $color1 : $color2;
    echo '<tr bgcolor="'. $row_color .'">
          <td>      
          <h1><a href="pet-detial.php?id='. $row['id'] .'" target="_blank">'. $row['title'] .'</a></h1><br />
          <h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
          <h2><b>Posted By:</b> <a href="profile.php?id='. $row['CritterID'] .'" target="_blank">'.$row['username'].'</a></h2><br />

              <p>'.$row['description'].'</p>

              <b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row[date])) .' &middot; <b>Category:</b> '.$row['category'].' &middot;
 
          </td>
              </tr>'."\n";
          $row_count++;
    }

}
else
{
      echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>

If there is anything else you need I will be at my computer, the good ole gmail notifier I love it. I will be headed back for home, but I should be back on around 2 I will answer any questions you have immeadiately.

Ryan

 

by: SatelliteCreativePosted on 2007-04-26 at 08:54:35ID: 18982591

OK! can you give this a go? I have set it up so that you can 'show' all, or one of your categories.
I've also added/fixed the Ascending / Descending option.
There are loads of little tweaks that can be made but i'm not sure exactly what you want, and presumably you are going to move this all into a tarted up website once you've finished testing it.
I had to guess at the values if config.php, but dont think that will effect you when testing. And of course theres only so much of my companies time I can use to look at this :)

<?php
include 'config.php';
$domain = "http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php";
?>


<p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="action">

    <label>Sort by:</label>
    <select name="sort">
        <option value="name">Name</option>
        <option value="date">Date</option>
        <option value="id">ID</option>
        <option value="username">Username</option>
        <option value="category">Category</option>
    </select><br />
    <label>Show:</label>
    <select name="parent">
            <option value="'%'">All</option>
    <?php
    $query = "SELECT cat_id, category FROM categories WHERE parent_id=0";
    $result = mysql_query($query) or die(mysql_error());
   
    while ($row = mysql_fetch_object($result))
    {
      ?><option value="<?php echo $row->cat_id; ?>"><?php echo $row->category; ?></option>
      <?php
    }
    ?>
    </select><br />
    <label>DESC or ASC</label>
    <select name="ascdesc">
        <option value="DESC">Descending</option>
        <option value="ASC">Ascending</option>
    </select><br />
    <label>Display</label>
    <select name="max_results">
        <option value="5">5</option>
        <option value="10" selected="selected">10</option>
        <option value="20">20</option>
        <option value="50">50</option>
    </select><br />
   
   
    <input name="action" type="hidden" value="go">
    <input name="submit" type="submit" style="float:right;" value="Submit!">
</form>

<br />
<label for="pages"><b>Page:</b></label>
</p>

<?php

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


$max_results = 10;

$from = (($page * $max_results) - $max_results);

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pet"),0);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
$total_pages = intval($total_results / $max_results);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
if($page > 1)
{
    $prev = ($page - 1);
    echo "<a href=\"$domain?page=$prev\">&laquo; Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
      {
        echo "$i ";
        }else{
            echo "<a href=\"$domain?page=$i\">$i</a> ";
    }
}

if($page < $total_pages)
{
    $next = ($page + 1);
    echo "<a href=\"$domain?page=$next\">Next &raquo;</a>";
}

// alternating row colors
$color1 = "#f8f8f8";
$color2 = "#cccccc";
$row_count = 0;

if (isset($_POST['action']))
{
    echo "<h1>SORTING ENABLED</h1>";
    $sort = $_POST['sort'];
    $ascdesc = $_POST['ascdesc'];
    $max_results = $_POST['max_results'];
    $category = $_POST['parent'];

$sql = "SELECT p.*, c.category, U.ID as CritterID FROM pet p INNER JOIN categories c ON p.category = c.cat_id INNER JOIN critter U on p.username = U.username
WHERE c.cat_id LIKE ".$category." ORDER BY p." . $sort . " " . $ascdesc . " LIMIT $from, $max_results";

print "first query - $sql</br>";

echo "<h2>" . $sql . "</h2>";
   $result = mysql_query($sql);
}
else
{
$sql = "SELECT p.*, c.category, U.ID as CritterID FROM pet p INNER JOIN categories c ON p.category = c.cat_id INNER JOIN critter U on p.username = U.username
ORDER BY p.id DESC LIMIT $from, $max_results";
print "second query - $sql</br>";
    $result = mysql_query($sql) or die("Bad Query: " . mysql_error());
}

$num_rows  =  mysql_num_rows($result);
echo "<br /><b>Results returned:</b> $num_rows\n";
echo "</div><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\">";
if ($num_rows > 0)
{
    while ($row = mysql_fetch_array($result))
      {
    $row_color = ($row_count % 2) ? $color1 : $color2;
    echo '<tr bgcolor="'. $row_color .'">
          <td>      
          <h1><a href="pet-detial.php?id='. $row['id'] .'" target="_blank">'. $row['title'] .'</a></h1><br />
          <h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
          <h2><b>Posted By:</b> <a href="profile.php?id='. $row['CritterID'] .'" target="_blank">'.$row['username'].'</a></h2><br />

              <p>'.$row['description'].'</p>

              <b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row['date'])) .' &middot; <b>Category:</b> '.$row['category'].' &middot;
 
          </td>
              </tr>'."\n";
          $row_count++;
    }

}
else
{
      echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>

 

by: catonthecouchproductionsPosted on 2007-04-26 at 14:28:33ID: 18985325

Hey man, first off, thank you very much, this really helps me out, i am learning PHP more and more everyday, thank you!

Thank you again!

I just uploaded it and it still shows only one result, when I go click submit I get:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/g/l/a/glanceatx/html/clients/critterclassifieds/pets.php on line 135

$num_rows  =  mysql_num_rows($result);


Any suggestions?

Ryan

 

by: SatelliteCreativePosted on 2007-04-27 at 01:18:02ID: 18987296

Hi, Can you post the contents of config.php here please? I can't see why you'd only get one result, unless you only had 1 entry in the DB :)
Also I presume that you have pets of different categories in your DB? Here's the test DB that I used, maybe you can try the code with it:

-- Dumping data for table `categories`
INSERT INTO `categories` VALUES (1, 'Dogs', 0);
INSERT INTO `categories` VALUES (2, 'Cats', 0);
INSERT INTO `categories` VALUES (3, 'Birds', 0);

-- Dumping data for table `critter`
INSERT INTO `critter` VALUES (1, 'ben', 'ben', 'ben hutchings', 'hutch@myaddres.com', '123 street', '01234 5678910', 'Business Info', 'Website Address', 'The Date', 'The IP', 'Act Key', 0, 'ABC 1');

-- Dumping data for table `pet`
INSERT INTO `pet` VALUES (1, 'Mrs Sushi', '2007-04-03 16:10:03', 'Sushi', '£15', 'A highly intelligent animal', '', '', '', 'ben', '2');
INSERT INTO `pet` VALUES (2, 'Mrs Noodles', '2007-04-30 16:17:25', 'Noodles', '£20', 'A foolish cat', '', '', '', 'ben', '2');
INSERT INTO `pet` VALUES (3, 'Master Tibbles', '2007-02-05 16:22:07', 'Tibbles', '£35', 'A woofit if ever there was one', '', '', '', 'ben', '1');
INSERT INTO `pet` VALUES (4, 'S. Parrot', '2006-10-25 16:24:22', 'Sparrot', '£45', 'A psychopathic Parrot', '', '', '', 'ben', '3');

I had to guess at the field values, and think that it was done ok. I only used one 'critter' (presumably thats what you call humans?)

 

by: SatelliteCreativePosted on 2007-04-27 at 01:38:07ID: 18987359

Ah... ok the problem is this:

To allow the sorting of ALL I was using this in my SELECT query:
...WHERE c.cat_id LIKE '%'...

However - it seems that your install of PHP has has 'magic_quotes_gpc = On'. This means that whenever a slash, single or double quote appears in GET or POST or COOKIE data, it is automatically back-slashed to make it safe to insert into a database. This means that where I have set the value of 'parent' to '%' in the dropdown box (for the ALL condition), when it's pulled out and placed in the SELECT query it ends up looking like this:

WHERE c.cat_id LIKE \'%\'

Note the unwanted backslashes?

One way to fix this is to 'stripslashes' from the $category varable like this (line 117):

replace: $category = $_POST['parent'];
with: $category = stripslashes($_POST['parent']);

Another way (preferable if possible in my opinion) is to turn magic-quotes OFF in your php.ini file.
You can find it in the DATA HANDLING section of the ini file, and can set it to:

magic_quotes_gpc = Off

But... even after that, it still looks like your DB only has one record :)

 

by: catonthecouchproductionsPosted on 2007-04-27 at 04:33:50ID: 18987921

Hey man, thank you for your help, I never really knew what magic quotes did, but a simple tip to fix would be to add:

stripslashes?

I added that on the category POST and I still get only 1 results, but NO error, which is a good thing, haha.

Oh yeah, I like your method of turning magic quotes off, but with go daddy I dont think you can access/edit php.ini

Here is my latest code:

<?php
include 'config.php';
$domain = "http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php";
?>


<p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="action">

    <label>Sort by:</label>
    <select name="sort">
        <option value="name">Name</option>
        <option value="date">Date</option>
        <option value="id">ID</option>
        <option value="username">Username</option>
        <option value="category">Category</option>
    </select><br />
    <label>Show:</label>
    <select name="parent">
            <option value="'%'">All</option>
    <?php
    $query = "SELECT cat_id, category FROM categories WHERE parent_id=0";
    $result = mysql_query($query) or die(mysql_error());
   
    while ($row = mysql_fetch_object($result))
    {
      ?><option value="<?php echo $row->cat_id; ?>"><?php echo $row->category; ?></option>
      <?php
    }
    ?>
    </select><br />
    <label>DESC or ASC</label>
    <select name="ascdesc">
        <option value="DESC">Descending</option>
        <option value="ASC">Ascending</option>
    </select><br />
    <label>Display</label>
    <select name="max_results">
        <option value="5">5</option>
        <option value="10" selected="selected">10</option>
        <option value="20">20</option>
        <option value="50">50</option>
    </select><br />
   
   
    <input name="action" type="hidden" value="go">
    <input name="submit" type="submit" style="float:right;" value="Submit!">
</form>

<br />
<label for="pages"><b>Page:</b></label>
</p>

<?php

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


$max_results = 10;

$from = (($page * $max_results) - $max_results);

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pet"),0);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
$total_pages = intval($total_results / $max_results);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
if($page > 1)
{
    $prev = ($page - 1);
    echo "<a href=\"$domain?page=$prev\">&laquo; Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
      {
        echo "$i ";
        }else{
            echo "<a href=\"$domain?page=$i\">$i</a> ";
    }
}

if($page < $total_pages)
{
    $next = ($page + 1);
    echo "<a href=\"$domain?page=$next\">Next &raquo;</a>";
}

// alternating row colors
$color1 = "#f8f8f8";
$color2 = "#cccccc";
$row_count = 0;

if (isset($_POST['action']))
{
    echo "<h1>SORTING ENABLED</h1>";
    $sort = $_POST['sort'];
    $ascdesc = $_POST['ascdesc'];
    $max_results = $_POST['max_results'];
    $category = stripslashes($_POST['parent']);

$sql = "SELECT p.*, c.category, u.id as CritterID FROM pet p INNER JOIN categories c ON p.category = c.cat_id INNER JOIN critter u on p.username = u.username
WHERE c.cat_id LIKE ".$category." ORDER BY p." . $sort . " " . $ascdesc . " LIMIT $from, $max_results";

print "first query - $sql</br>";

echo "<h2>" . $sql . "</h2>";
   $result = mysql_query($sql);
}
else
{
$sql = "SELECT p.*, c.category, u.id as CritterID FROM pet p INNER JOIN categories c ON p.category = c.cat_id INNER JOIN critter u on p.username = u.username
ORDER BY p.id DESC LIMIT $from, $max_results";
print "second query - $sql</br>";
    $result = mysql_query($sql) or die("Bad Query: " . mysql_error());
}

$num_rows  =  mysql_num_rows($result);
echo "<br /><b>Results returned:</b> $num_rows\n";
echo "</div><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\">";
if ($num_rows > 0)
{
    while ($row = mysql_fetch_array($result))
      {
    $row_color = ($row_count % 2) ? $color1 : $color2;
    echo '<tr bgcolor="'. $row_color .'">
          <td>      
          <h1><a href="pet-detial.php?id='. $row['id'] .'" target="_blank">'. $row['title'] .'</a></h1><br />
          <h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
          <h2><b>Posted By:</b> <a href="profile.php?id='. $row['CritterID'] .'" target="_blank">'.$row['username'].'</a></h2><br />

              <p>'.$row['description'].'</p>

              <b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row['date'])) .' &middot; <b>Category:</b> '.$row['category'].' &middot;
 
          </td>
              </tr>'."\n";
          $row_count++;
    }

}
else
{
      echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>

 

by: SatelliteCreativePosted on 2007-04-27 at 04:47:26ID: 18987991

Hi, I'm a bit confused. I guess you missed my comments about stripslashes.
And are you saying it all works now or you still have a problem. Did you try the data from my DB?
Not a great deal of time today so sorry if you get rushed - to the point - replies.

 

by: catonthecouchproductionsPosted on 2007-04-27 at 04:53:01ID: 18988017

Thats fine! I wont get rushed, thanks for your help

I got no errors with and without submiting form

I just still have only one result being displayed:

http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php

And there are three in the database

All three rows have all information, mainly including the important CATEGORY ID.

So right as we stand we have:

Only displaying one of the three rows.
No errors but not sorting.

Thanks for your help man!

Ryan

 

by: SatelliteCreativePosted on 2007-04-27 at 05:06:01ID: 18988105

Ok can you paste the data from your DB. I want to see what cat numbers are being used. Thanks.

Alternatively you can use my data (from post at 4.27.2007 at 08:18AM GMT) and tell me what happens.

 

by: catonthecouchproductionsPosted on 2007-04-27 at 05:11:29ID: 18988139

I didnt even see that post! Let me try that I am right here at my computer, so I have gmail notifier so I get the emails right away.

Let me try that out!

Back within minutes.

 

by: catonthecouchproductionsPosted on 2007-04-27 at 05:16:49ID: 18988183

Hi, Can you post the contents of config.php here please? I can't see why you'd only get one result, unless you only had 1 entry in the DB :)
Also I presume that you have pets of different categories in your DB? Here's the test DB that I used, maybe you can try the code with it:
------------------

that is right, I do have pets in different categories of my DB.

I am going to empty it and start new.

I will be back with results.

And I will dump lastest SQL and PHP

 

by: catonthecouchproductionsPosted on 2007-04-27 at 05:21:49ID: 18988239

I started from scratch. Later I need to edit my add.php to work in my sub cats so I might need help with that, I can make a new post and give you points! haha

http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php

I have three entires in the DB

http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php

One dog, one cat, one bird, for testing puporses.

Here is my SQL DUMP:



-- --------------------------------------------------------

--
-- Table structure for table `categories`
--

CREATE TABLE `categories` (
  `cat_id` int(10) NOT NULL auto_increment,
  `category` varchar(50) NOT NULL default '',
  `parent_id` int(10) NOT NULL default '0',
  `subcategory` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`cat_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `categories`
--

INSERT INTO `categories` VALUES (1, 'Dogs', 0, '');
INSERT INTO `categories` VALUES (2, 'Cats', 0, '');
INSERT INTO `categories` VALUES (3, 'Birds', 0, '');

-- --------------------------------------------------------

--
-- Table structure for table `critter`
--

CREATE TABLE `critter` (
  `id` int(11) NOT NULL auto_increment,
  `username` text NOT NULL,
  `password` varchar(32) NOT NULL default '',
  `name` text NOT NULL,
  `email` text NOT NULL,
  `address` text NOT NULL,
  `phone` text NOT NULL,
  `businfo` text NOT NULL,
  `website` text NOT NULL,
  `date` text NOT NULL,
  `ip` text NOT NULL,
  `actkey` varchar(40) NOT NULL default '',
  `activated` int(1) NOT NULL default '0',
  `zip` varchar(5) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `critter`
--

INSERT INTO `critter` VALUES (1, 'glanceatx', 'df745a6ecc79adb85f58b55dc66bdd3b', 'Ryan Coughlin', 'glanceatx@gmail.com', '3 Captain Sawyer Lane', '2075905241', 'College Student', 'http://www.rksdesignstudios.com', '19, 04 07', '216.93.157.21', 'a9d124839e56e89cec7af9fd8f9e0072fcce1239', 1, '');
INSERT INTO `critter` VALUES (2, 'ryangatto', '098f6bcd4621d373cade4e832627b4f6', 'Ryan Gatto', 'ryangatto@catonthecouchproductions.com', '117 meadow', '617-347-3313', 'test', 'http://www.catonthecouchproductions.com', '19, 04 07', '71.169.162.9', 'a1f53f7db2f20cb3ebfd7af4e01a678720d58b2d', 1, '');

-- --------------------------------------------------------

--
-- Table structure for table `pet`
--

CREATE TABLE `pet` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(150) default NULL,
  `date` datetime default NULL,
  `name` varchar(255) NOT NULL default '',
  `price` varchar(10) NOT NULL default '',
  `description` text NOT NULL,
  `city` varchar(30) NOT NULL default '',
  `state` varchar(2) NOT NULL default '',
  `zip` varchar(5) NOT NULL default '',
  `username` varchar(255) NOT NULL default '',
  `category` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `pet`
--

INSERT INTO `pet` VALUES (1, 'Pet for sale', '2007-04-27 05:18:03', 'Fluffy', '', 'This is a nice Cat', '04046', 'ME', '04046', 'glanceatx', '');
INSERT INTO `pet` VALUES (2, 'Tough guy for sale', '2007-04-27 05:18:45', 'Tough Guy', '', 'Bad Dog but sweet at the same time', '02262', 'MA', '02262', 'glanceatx', '');
INSERT INTO `pet` VALUES (3, 'New bird', '2007-04-27 05:19:29', 'Tucan Sam', '', 'Nice calm yellow parrot', '05401', 'VT', '05401', 'glanceatx', '');



PHP FOR PETS.php

<?php
include 'config.php';
$domain = "http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php";
?>


<p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="action">

    <label>Sort by:</label>
    <select name="sort">
        <option value="name">Name</option>
        <option value="date">Date</option>
        <option value="id">ID</option>
        <option value="username">Username</option>
        <option value="category">Category</option>
    </select><br />
    <label>Show:</label>
    <select name="parent">
            <option value="'%'">All</option>
    <?php
    $query = "SELECT cat_id, category FROM categories WHERE parent_id=0";
    $result = mysql_query($query) or die(mysql_error());
   
    while ($row = mysql_fetch_object($result))
    {
      ?><option value="<?php echo $row->cat_id; ?>"><?php echo $row->category; ?></option>
      <?php
    }
    ?>
    </select><br />
    <label>DESC or ASC</label>
    <select name="ascdesc">
        <option value="DESC">Descending</option>
        <option value="ASC">Ascending</option>
    </select><br />
    <label>Display</label>
    <select name="max_results">
        <option value="5">5</option>
        <option value="10" selected="selected">10</option>
        <option value="20">20</option>
        <option value="50">50</option>
    </select><br />
   
   
    <input name="action" type="hidden" value="go">
    <input name="submit" type="submit" style="float:right;" value="Submit!">
</form>

<br />
<label for="pages"><b>Page:</b></label>
</p>

<?php

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


$max_results = 10;

$from = (($page * $max_results) - $max_results);

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pet"),0);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
$total_pages = intval($total_results / $max_results);

if ($total_pages%$perpage)
{
        // has remainder so add one page
            $total_pages++;
}
if($page > 1)
{
    $prev = ($page - 1);
    echo "<a href=\"$domain?page=$prev\">&laquo; Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
      {
        echo "$i ";
        }else{
            echo "<a href=\"$domain?page=$i\">$i</a> ";
    }
}

if($page < $total_pages)
{
    $next = ($page + 1);
    echo "<a href=\"$domain?page=$next\">Next &raquo;</a>";
}

// alternating row colors
$color1 = "#f8f8f8";
$color2 = "#cccccc";
$row_count = 0;

if (isset($_POST['action']))
{
    echo "<h1>SORTING ENABLED</h1>";
    $sort = $_POST['sort'];
    $ascdesc = $_POST['ascdesc'];
    $max_results = $_POST['max_results'];
    $category = stripslashes($_POST['parent']);

$sql = "SELECT p.*, c.category, u.id as CritterID FROM pet p INNER JOIN categories c ON p.category = c.cat_id INNER JOIN critter u on p.username = u.username
WHERE c.cat_id LIKE ".$category." ORDER BY p." . $sort . " " . $ascdesc . " LIMIT $from, $max_results";

print "first query - $sql</br>";

echo "<h2>" . $sql . "</h2>";
   $result = mysql_query($sql);
}
else
{
$sql = "SELECT p.*, c.category, u.id as CritterID FROM pet p INNER JOIN categories c ON p.category = c.cat_id INNER JOIN critter u on p.username = u.username
ORDER BY p.id DESC LIMIT $from, $max_results";
print "second query - $sql</br>";
    $result = mysql_query($sql) or die("Bad Query: " . mysql_error());
}

$num_rows  =  mysql_num_rows($result);
echo "<br /><b>Results returned:</b> $num_rows\n";
echo "</div><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\">";
if ($num_rows > 0)
{
    while ($row = mysql_fetch_array($result))
      {
    $row_color = ($row_count % 2) ? $color1 : $color2;
    echo '<tr bgcolor="'. $row_color .'">
          <td>      
          <h1><a href="pet-detial.php?id='. $row['id'] .'" target="_blank">'. $row['title'] .'</a></h1><br />
          <h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
          <h2><b>Posted By:</b> <a href="profile.php?id='. $row['CritterID'] .'" target="_blank">'.$row['username'].'</a></h2><br />

              <p>'.$row['description'].'</p>

              <b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row['date'])) .' &middot; <b>Category:</b> '.$row['category'].' &middot;
 
          </td>
              </tr>'."\n";
          $row_count++;
    }

}
else
{
      echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>

 

by: SatelliteCreativePosted on 2007-04-27 at 05:59:06ID: 18988559

Goodness... that vexed me for 1o minutes! I used your code and your DB and could NOT get any results out of it. So I had a good look at every table and entry and realised that you forgot to add the category types to the pets!

Ah well at least it was something small. If you open your pets DB and enter the 'category' your code should work.

Uh... can I have the efternoon off boss? ;)

 

by: catonthecouchproductionsPosted on 2007-04-27 at 06:02:55ID: 18988579

Small change haha. One more last thing! Where do I enter category? Is it in my pets.php

Thank you!

Ryan

 

by: catonthecouchproductionsPosted on 2007-04-27 at 06:04:24ID: 18988591

Nevermind I see! But that means that, the add.php page isnt working, I will post another question for that, I am not sure why that isnt working.

Ryan

 

by: SatelliteCreativePosted on 2007-04-27 at 06:11:08ID: 18988629

Ok... i'll see you over on the other side then:)
I can try and take a look at the add.php, but need to really spend the afternoon confusing myself with an actionscript problem... pff i hate Flash.

 

by: catonthecouchproductionsPosted on 2007-04-27 at 06:13:07ID: 18988641

Haha, I dont blame you! i am going to try to sort it out here, but will probably end up posting its stumping me.
Good luck with that!

Ryan

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...