473,756 Members | 9,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

links for next or previous in category - PHP

2 New Member
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("SE LECT * 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("SE LECT * FROM soap_inventory WHERE ID=$ID");
$row = mysql_fetch_arr ay($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;$ro w[Price]<br /><br />";
echo "$row[PayPal_ADD_butt on]<br /><br />";
echo "$row[PayPal_VIEW_but ton]<br /><br />";
$IDpre = 0;
$IDnex = 0;//echo "$ID . $IDpre . $IDnex . $allpix";
if ($ID==1) {$IDpre = $allpix;$IDnex= $ID+1;}
elseif ($ID==$allpix) {$IDnex=1;$IDpr e=$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_cata log.php?ID=$IDp re'><<< Previous</a>";
echo "<a href='soap_cata log.php'>Soap Catalog</a>";
echo "<a href='soap_cata log.php?ID=$IDn ex'>Next >>></a>";
echo "</div>";
}
elseif ($ID AND $cat) {
//(TROUBLE SPOT - NEXT/PREVIOUS LINKS DO NOT WORK)
$sql = mysql_query("SE LECT soap_inventory. ID, soap_inventory. Name, soap_inventory. Price FROM soap_inventory, soap_category, soap_cat_lookup WHERE soapid=soap_inv entory.ID AND categoryid=soap _category.ID AND categoryid=$cat AND Active=1");
$IDcat = mysql_fetch_arr ay($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>$IDr ow[Name]</strong><br /><br />";
echo "$&nbsp;$ID row[Price]<br /><br />";

echo "<div class='clear'>& nbsp;</div>";
echo "<div class='catnav'> ";
echo "<a href='soap_cata log.php?ID=$IDr ow[0]&category=$cat' >First</a>";
echo "<a href='soap_cata log.php'>Soap Catalog</a>";
echo "<a href='soap_cata log.php?ID=($ID row[0]+1)&category=$c at'>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("SE LECT * FROM soap_inventory, soap_category, soap_cat_lookup WHERE soapid=soap_inv entory.ID AND categoryid=soap _category.ID AND categoryid=$cat AND Active=1");
while ($cats = mysql_fetch_arr ay($sql)) {
echo "<div class='thumb'>< a href='soap_cata log.php?ID=$cat s[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_cata log.php'>Back to Soaps Catalog</a>";
}
else {
//show complete soap catalog listing (THIS IS OK)
// Select the table to query
$result = mysql_query("SE LECT * FROM soap_inventory WHERE Active=1") or die ("Query failed here");
while ($res = mysql_fetch_arr ay($result)) {
echo "<div class='thumb'>< a href='soap_cata log.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 3388
Banfa
9,065 Recognized Expert Moderator Expert
$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_inv entory.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
LizzyFin
2 New Member
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
2239
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 Avila QOTW (in the popular vote category): "So far, python has been the easiest language to learn I've ever come across. I tried learning perl, and it was a disaster.... Too convoluted. Python is a breath of fresh air. Also, the docs and...
4
1488
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': -> id (INT), category (VARCHAR) And 1 called 'links':
7
1700
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 give the error but the links does! I get the error on this LINE(when looping):- PageLinks.Text = PageLinks.Text & dtLinks.Rows(iLoop)("link_url")
24
2855
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 in sequential order, is there a way to automagically go to the previous file or the next file using JS? Ian --
0
1454
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 category on Santa's list" -- George Jempty (commenting on "Dear Open Source Santa," by Paul Browne) http://www.oreillynet.com/onjava/blog/2006/12/dear_open_source_santa.html#comment-384244 "But if you care to give it a closer look, you may understand...
3
3424
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 general, all links back towards the root of a tree should be weak refs; this breaks the loops that give reference counting trouble. John Nagle
8
5678
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 through their orders. I kind of already have a script for this: $recordcount = mssql_num_rows($result); $pagecount = $recordcount / $displayPerPage; $r = fmod($recordcount, $displayPerPage); if ($r > 0) {
1
1865
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 page: Here is the code surrounding my dynamic 'subcategory': <a href="prov-results1.aspx?county=< %#Server.UrlEncode(Request.QueryString("county")) & "&subcat=" & Server.UrlEncode((Eval("SubCategory").ToString())) & "&category=" &...
3
2051
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 that link's day and onwards. I have a problem with the page link part that it won't advance to the next page according but go back according to today's date. This is the calender script section // Display Calandar error_reporting('0'); ...
0
9482
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9292
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9901
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9728
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8733
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3827
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.