473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

links for next or previous in category - PHP

Looking for a little assistance in understanding what I am missing!

Using PHP/MySql to allow users to browse through items in a gallery. Clicking 'next' and 'previous' links works fine using the item ID number to sequentially see all items.

Trouble arises when category and ID variables are passed. I'm thoroughly confused on how to let the user click a 'next' link after they have entered a category page in order to see the next item in that category. The "elseif ($ID AND $cat)" statement is the offending code. Here's my script if anyone can make suggestions. Thanks in advance for any help!

<?php
// Check the database for correct authorization.
$host = "xx;
$user = "xx";
$password = "xx";
$db = "soaps";
mysql_connect ($host, $user, $password) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("soaps") or die ("Could not select database");
$all = mysql_query("SELECT * FROM soap_inventory") or die ("Query failed here");
$allpix = mysql_num_rows($all);
if (isset($_GET['ID'])) {
$ID = $_GET['ID'];
} else {
$ID = "";
}
if (isset($_GET['category'])) {
$cat = $_GET['category'];
} else {
$cat = "";
}
if ($ID AND !$cat) {
//show single soap if user clicks on a picture (THIS WORKS OK)
$sql = mysql_query("SELECT * FROM soap_inventory WHERE ID=$ID");
$row = mysql_fetch_array($sql);
echo "<img class='thumb' src='images/$row[ID].jpg' width='400px' alt='$row[Name]'><br />";
echo "<br /><strong>$row[Name]</strong><br /><br />";
echo "$&nbsp;$row[Price]<br /><br />";
echo "$row[PayPal_ADD_button]<br /><br />";
echo "$row[PayPal_VIEW_button]<br /><br />";
$IDpre = 0;
$IDnex = 0;//echo "$ID . $IDpre . $IDnex . $allpix";
if ($ID==1) {$IDpre = $allpix;$IDnex=$ID+1;}
elseif ($ID==$allpix) {$IDnex=1;$IDpre=$ID-1;}
else {$IDnex=$ID+1;$IDpre = $ID-1;}
//echo "<br />$ID . $IDpre . $IDnex . $allpix";
echo "<div class='clear'>&nbsp;</div>";
echo "<div class='catnav'>";
echo "<a href='soap_catalog.php?ID=$IDpre'><<< Previous</a>";
echo "<a href='soap_catalog.php'>Soap Catalog</a>";
echo "<a href='soap_catalog.php?ID=$IDnex'>Next >>></a>";
echo "</div>";
}
elseif ($ID AND $cat) {
//(TROUBLE SPOT - NEXT/PREVIOUS LINKS DO NOT WORK)
$sql = mysql_query("SELECT soap_inventory.ID, soap_inventory.Name, soap_inventory.Price FROM soap_inventory, soap_category, soap_cat_lookup WHERE soapid=soap_inventory.ID AND categoryid=soap_category.ID AND categoryid=$cat AND Active=1");
$IDcat = mysql_fetch_array($sql);
$IDrow = $IDcat;
$allpic = mysql_num_rows($sql);
echo "<img class='thumb' src='images/$IDrow[soapid].jpg' width='400px' alt='$IDrow[Name]'><br />";
echo "<br /><strong>$IDrow[Name]</strong><br /><br />";
echo "$&nbsp;$IDrow[Price]<br /><br />";

echo "<div class='clear'>&nbsp;</div>";
echo "<div class='catnav'>";
echo "<a href='soap_catalog.php?ID=$IDrow[0]&category=$cat'>First</a>";
echo "<a href='soap_catalog.php'>Soap Catalog</a>";
echo "<a href='soap_catalog.php?ID=($IDrow[0]+1)&category=$cat'>Next </a>";
echo "</div>";
}
elseif ($cat AND !$ID){
//show soaps in category if user clicks on a link in left column (THIS IS OK)
$sql = mysql_query("SELECT * FROM soap_inventory, soap_category, soap_cat_lookup WHERE soapid=soap_inventory.ID AND categoryid=soap_category.ID AND categoryid=$cat AND Active=1");
while ($cats = mysql_fetch_array($sql)) {
echo "<div class='thumb'><a href='soap_catalog.php?ID=$cats[soapid]&category=$cat'><img class='thumb' src='thumbs/$cats[soapid]-s.jpg' alt='$cats[Name]' width='150px'></a><div class='clear'>&nbsp;</div><h5>$cats[Name]</h5><br />$$cats[Price]<br /><br /></div>";
}
echo "<div class='clear'>&nbsp;</div><a href='soap_catalog.php'>Back to Soaps Catalog</a>";
}
else {
//show complete soap catalog listing (THIS IS OK)
// Select the table to query
$result = mysql_query("SELECT * FROM soap_inventory WHERE Active=1") or die ("Query failed here");
while ($res = mysql_fetch_array($result)) {
echo "<div class='thumb'><a href='soap_catalog.php?ID=$res[ID]'><img class='thumb' src='thumbs/$res[ID]-s.jpg' alt='$res[Name]' width='150px'></a><div class='clear'>&nbsp;</div><h5>$res[Name]</h5><br />$$res[Price]<br /><br /></div>";
}
echo "<div class='clear'>&nbsp;</div>";
}
?>
Jun 2 '06 #1
2 3371
Banfa
9,065 Expert Mod 8TB
$host = "<snipped>";
$user = "<snipped>";
$password = "<snipped>";
I do not know if these where you actual login details (and don't tell me) but you should never under any circumstances post your actual username and password for anything to a public forum. If these were your actual login details then you should change your password immediately.

What you want is not too hard to do, however you will need to re-write the page to change how $ID is used (and possibly rename it).

Currently $ID is the actual ID of the record that you wish to load, your problem is that if you are trying to select pictures by category then the IDs are not in sequence, i.e. you might want to scroll through IDs 1, 5, 6, 50 and 89.

To get round this you should redefine $ID as the index into the list of currently required pictures, be that all pictures or all pictures in a category. Then in your SELECT statements you can use the "LIMIT $id, 1" clause to select a specific picture from the list.

Then no matter what conditions you set on your selection criteria $ID will always be sequencetial, 0 to number of results - 1.

Your select staements would become

For the entire list

SELECT * FROM soap_inventory LIMIT $id, 1

for the categoried list

SELECT soap_inventory.ID, soap_inventory.Name, soap_inventory.Price FROM soap_inventory, soap_category, soap_cat_lookup WHERE soapid=soap_inventory.ID AND categoryid=soap_category.ID AND categoryid=$cat AND Active=1 LIMIT $id, 1

I do not think you need all the tables in this query I think it could be

change how $ID is used (and possibly rename it).

Currently $ID is the actual ID of the record that you wish to load, your problem is that if you are trying to select pictures by category then the IDs are not in sequence, i.e. you might want to scroll through IDs 1, 5, 6, 50 and 89.

To get round this you should redefine $ID as the index into the list of currently required pictures, be that all pictures or all pictures in a category. Then in your SELECT statements you can use the "LIMIT $id, 1" clause to select a specific picture from the list.

Then no matter what conditions you set on your selection criteria $ID will always be sequencetial, 0 to number of results - 1.

Your select staements would become

For the entire list

SELECT * FROM soap_inventory LIMIT $id, 1

for the categoried list

SELECT soap_inventory.ID, soap_inventory.Name, soap_inventory.Price FROM soap_inventory WHERE categoryid=$cat AND Active=1 LIMIT $id, 1

but I would need better visibility of your table structure to be sure of this
Jun 3 '06 #2
Banfa,
Thank you so much for your input. I did fix my problem using your suggestion of changing the ID variable name - that helped to cut the confusion as I now have distinct id numbers for each of my tables. I also used your "LIMIT $id, 1" and an ORDER BY clause and got my navigation to work. I did not realize that a variable could be used in the limit clause, so thanks again!!
Have a great day! :)
Jun 7 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Emile van Sebille | last post by:
QOTW (in the OS agnostic category): "There is a (very popular) Python package out there which exposes the win32 api. I'm not sure what it's called. (win32api? pythonwin? win32all?)" -- Francis...
4
by: frizzle | last post by:
Hi there, I've looked in many places for what i need, but i just can't seem to find it. I keep ending up in ad-managing systems, or link-trackers ... I have 1 mySQL table called 'categories':...
7
by: Patrick Olurotimi Ige | last post by:
I have a simple Stored Procedure with multiple select statements..doing select * from links for example. I created a DataTable and then fill the tables But the first dtTemplate DataTable doesn't...
24
by: Ian Rastall | last post by:
I do a site where I have a previous and next link at the bottom of every page. It looks like: <p><a href="foo01.html">Previous</a> | <a href="foo03.html">Next</a></p> Seeing as they're always...
0
by: Paul Boddie | last post by:
QOTW: "I still want to keep compile time type checking to make sure I don't make any mistakes." "Sounds like you want two wives, but I'm pretty sure polygamy gets a checkbox in the naughty...
3
by: John Nagle | last post by:
Are weak refs slower than strong refs? I've been considering making the "parent" links in BeautifulSoup into weak refs, so the trees will release immediately when they're no longer needed. In...
8
by: printline | last post by:
Hello I have a problem which i hope someone can help me with. I have a website where customers can login and their current and previous orders. What i need now is for the customers to look...
1
by: mister-Ed | last post by:
I am displaying subcategories in my datalist, and now I have a bizarre thing happen when I add a new subcategory record in my sql database, the new subcategory link does not click into the next...
3
osward
by: osward | last post by:
Hi, everyone, I had managed to make use of the date link from a simple calendar script to my query table. When I click on the date's link or Prev and Next Month link, The table first row will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.