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.
Main Topics
Browse All TopicsI 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.rksdesignstudi
?>
<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</optio
<option value="category">Category<
</select>
then
<select name="ascdesc">
<option value="DESC">Descending</o
<option value="ASC">Ascending</opt
</select>
and show
<select name="max_results">
<option value="5">5</option>
<option value="10" selected="selected">10</op
<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></
</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("
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\
}
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
echo "$i ";
}else{
echo "<a href=\"$domain?page=$i\">$
}
}
if($page < $total_pages)
{
$next = ($page + 1);
echo "<a href=\"$domain?page=$next\
}
// 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='.
<h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
<h2><b>Posted By:</b> <a href="profile.php?id='. $row['id'] .'" target="_blank">'.$row['us
<p>'.$row['description'].'
<b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row[date])) .' · <b>Category:</b> '.$row['category'].' ·
</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.
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.
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.
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.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
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.
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
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;
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?
Here is my latest code with that while in there for the categories:
<?php
include 'config.php';
$domain = "http://www.rksdesignstudi
?>
<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 value="category">Category<
</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</o
<option value="ASC">Ascending</opt
</select><br />
<label>Display</label>
<select name="max_results">
<option value="5">5</option>
<option value="10" selected="selected">10</op
<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></
</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("
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\
}
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
echo "$i ";
}else{
echo "<a href=\"$domain?page=$i\">$
}
}
if($page < $total_pages)
{
$next = ($page + 1);
echo "<a href=\"$domain?page=$next\
}
// 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='.
<h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
<h2><b>Posted By:</b> <a href="profile.php?id='. $row['id'] .'" target="_blank">'.$row['us
<p>'.$row['description'].'
<b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row[date])) .' · <b>Category:</b> '.$row['category'].' ·
</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/glance
$num_rows = mysql_num_rows($result);
Would i need to alter both queries, I am looking at my code now, and am confused.
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!
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.
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.
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.rksdesignstudi
?>
<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 value="category">Category<
</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</o
<option value="ASC">Ascending</opt
</select><br />
<label>Display</label>
<select name="max_results">
<option value="5">5</option>
<option value="10" selected="selected">10</op
<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></
</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("
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\
}
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
echo "$i ";
}else{
echo "<a href=\"$domain?page=$i\">$
}
}
if($page < $total_pages)
{
$next = ($page + 1);
echo "<a href=\"$domain?page=$next\
}
// 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='.
<h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
<h2><b>Posted By:</b> <a href="profile.php?id='. $row['CritterID'] .'" target="_blank">'.$row['us
<p>'.$row['description'].'
<b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row[date])) .' · <b>Category:</b> '.$row['category'].' ·
</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
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.rksdesignstudi
?>
<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 value="category">Category<
</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</o
<option value="ASC">Ascending</opt
</select><br />
<label>Display</label>
<select name="max_results">
<option value="5">5</option>
<option value="10" selected="selected">10</op
<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></
</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("
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\
}
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
echo "$i ";
}else{
echo "<a href=\"$domain?page=$i\">$
}
}
if($page < $total_pages)
{
$next = ($page + 1);
echo "<a href=\"$domain?page=$next\
}
// 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='.
<h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
<h2><b>Posted By:</b> <a href="profile.php?id='. $row['CritterID'] .'" target="_blank">'.$row['us
<p>'.$row['description'].'
<b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row['date'])) .' · <b>Category:</b> '.$row['category'].' ·
</td>
</tr>'."\n";
$row_count++;
}
}
else
{
echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>
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/glance
$num_rows = mysql_num_rows($result);
Any suggestions?
Ryan
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?)
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['paren
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 :)
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.rksdesignstudi
?>
<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 value="category">Category<
</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</o
<option value="ASC">Ascending</opt
</select><br />
<label>Display</label>
<select name="max_results">
<option value="5">5</option>
<option value="10" selected="selected">10</op
<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></
</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("
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\
}
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
echo "$i ";
}else{
echo "<a href=\"$domain?page=$i\">$
}
}
if($page < $total_pages)
{
$next = ($page + 1);
echo "<a href=\"$domain?page=$next\
}
// 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['paren
$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='.
<h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
<h2><b>Posted By:</b> <a href="profile.php?id='. $row['CritterID'] .'" target="_blank">'.$row['us
<p>'.$row['description'].'
<b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row['date'])) .' · <b>Category:</b> '.$row['category'].' ·
</td>
</tr>'."\n";
$row_count++;
}
}
else
{
echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>
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.rksdesignstudio
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
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
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.rksdesignstudio
I have three entires in the DB
http://www.rksdesignstudio
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', 'df745a6ecc79adb85f58b55dc
INSERT INTO `critter` VALUES (2, 'ryangatto', '098f6bcd4621d373cade4e832
-- --------------------------
--
-- 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.rksdesignstudi
?>
<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 value="category">Category<
</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</o
<option value="ASC">Ascending</opt
</select><br />
<label>Display</label>
<select name="max_results">
<option value="5">5</option>
<option value="10" selected="selected">10</op
<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></
</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("
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\
}
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
echo "$i ";
}else{
echo "<a href=\"$domain?page=$i\">$
}
}
if($page < $total_pages)
{
$next = ($page + 1);
echo "<a href=\"$domain?page=$next\
}
// 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['paren
$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='.
<h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
<h2><b>Posted By:</b> <a href="profile.php?id='. $row['CritterID'] .'" target="_blank">'.$row['us
<p>'.$row['description'].'
<b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row['date'])) .' · <b>Category:</b> '.$row['category'].' ·
</td>
</tr>'."\n";
$row_count++;
}
}
else
{
echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>
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? ;)
Business Accounts
Answer for Membership
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?