Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-27-2012, 01:11 AM   PM User | #1
TYJesus
New Coder

 
Join Date: Jan 2012
Location: Walla Walla
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
TYJesus is an unknown quantity at this point
help with getting certain content through url

I apologize for the simpleness of this for you. I am just looking for help with EVEN the names of commands to use to accomplish this:

In my nav bar, I would like subcategories for my product page like this:
thefamilyhunters.atwebpages.com/Products.php?page=motorBicycling
thefamilyhunters.atwebpages.com/Products.php?page=TYJBowstrings
thefamilyhunters.atwebpages.com/Products.php?page=airplanes

Right now I have this main product page working.
thefamilyhunters.atwebpages.com/Products.php
I am trying to get the site easier to change content by using one main product variable page. So I am working on getting subcategories to work with the php ?page=value.

Here is what I have been trying in the productvariable page:
PHP Code:
$productsTop $_GET['page']
                
                    
page "motorBicycle" () {
                        
'<iframe src="../htmlcs/motorBicycle.html"  
                        style="
                            position:absolute; 
                            margin-top:0px;
                            padding-top:0px;
                            left:0px; 
                            color:#fff; 
                            font-size:0.9em;
                            border-color:black;" 
                            width="1197" 
                            height="527">
                            
                            <p>Your browser does not support iframes.</p>
                        </iframe>'

                        }
                        
                    
page "TYJBows" {
                        
'<iframe src="../htmlcs/TYJBows.html"  
                        style="
                            position:absolute; 
                            margin-top:0px;
                            padding-top:0px;
                            left:0px; 
                            color:#fff; 
                            font-size:0.9em;
                            border-color:black;" 
                            width="1197" 
                            height="527">
                            
                            <p>Your browser does not support iframes.</p>
                        </iframe>'

                        } 
I am sure there are numerous ways to do this, and I would appreciate anyone patient enough to lead me in the right direction.

David

Last edited by TYJesus; 09-27-2012 at 05:25 PM..
TYJesus is offline   Reply With Quote
Old 09-27-2012, 04:49 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
What you appear to be doing is simply changing the iframe source based on a get. That is easiest done with a switch or with an array. I'll use an array.
PHP Code:
<?php
$aPages 
= array(
    
'motorBicycle' => '../htmlcs/motorBicycle.html',
    
'TYJBows' => '../htmlcs/TYJBows.html',
);

$sPage = isset($_GET['page']) ? $_GET['page'] : '';
if (isset(
$aPages[$sPage]))
{
?>
                        <iframe src="<?php echo $aPages[$sPage];?>"  
                        style="
                            position:absolute; 
                            margin-top:0px;
                            padding-top:0px;
                            left:0px; 
                            color:#fff; 
                            font-size:0.9em;
                            border-color:black;" 
                            width="1197" 
                            height="527">
                            
                            <p>Your browser does not support iframes.</p>
                        </iframe> 
<?php
}
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
TYJesus (09-27-2012)
Old 09-27-2012, 05:36 PM   PM User | #3
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,007
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
There is no reason to do this with frames, really. This is the time to start delving into the world of databases. The standard for most people at this stage seems to be MySql for database usage (as most hosts offer it and it is easy to find php code to tie into it).

Instead of hard-coding each section into a separate page and using a switch or an array to choose the correct page to jam into an iframe, what you will want to do is store the raw information (without the markup) in a database. Then your php page will have a script to read the $_GET variable, query the database for entries in that category, handle any errors (what if the category requested does not exist?), and format/present the results into HTML markup in the page.

So for the first step, you need to see what's in the $_GET variable. Always always ALWAYS sanitize input received from the user. I won't go into detail about why, other than to suggest that you read up on "MySql injection attacks" for a primer on why this is such a must.

So, you will retrieve the $_GET value for the product category, sanitize it (with a whitelist you can pull from your database, for one idea), and act on it.

Assuming the category requested is a real category, you proceed to the next step...

Suppose the user selected "motorBicycling" and that your "motorBicycling" category has 7,000 products in it. You won't want to even try to display them all at once, so you will query your database with a "LIMIT" of, say, 20 results. Assuming your database of products has all information in a single table (which it probably shouldn't, but we're dealing with a simple case and this takes some of the confusion out of it) that might look something like this:

Code:
SELECT * FROM products WHERE category='motorBicycling' ORDER BY product_name ASC LIMIT 0, 20
This queries your "products" table to find all information about results whose category is listed as "motorBicycling." It arranges ("ORDER BY") the results in ascending ("ASC") alphabetical order by the product's name, and then cuts off the results from result #0 through result #19 ("LIMIT 0, 20" - remember since we start counting at zero, we end at 19 after getting 20 records). This will allow you to present "pages" of results. For example, if the above was the query for page 1, the query for page 2 would be:

Code:
SELECT * FROM products WHERE category='motorBicycling' ORDER BY product_name ASC LIMIT 20, 20
The first number after "LIMIT" is the record number to start with and the second number is the number of results to pull.

Now, assuming we have a result set of 20 items and their relevant product information, we set up a loop to print formatted HTML to the existing page (using this as "products.php"):

PHP Code:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Product Page Example</title>
<link type="image/x-icon" href="/favicon.ico" rel="shortcut icon" />
<link rel="stylesheet" href="/style.css" media="all" />
<link rel="stylesheet" href="/print.css" media="print" />
<style type="text/css">

</style>
<body>
<?php
#    Retrieve and sanitize the category...using "static" whitelisting here for simplicity of the example...
#    This isn't "santization" in the classic sense, but it does ensure only an approved string is used for the query...
$category $_GET['page'];
$valid_categories = array(
    
"motorBicycle",
    
"TYJBowstrings",
    
"airplanes"
);
$products = array(); #    Set this up for array_push to use next...
if(in_array($category,$valid_categories)){
    
#    If the category requested is valid, we connect to the DB and run the query...
    
$db_user "username_here";
    
$db_pw "password_here";
    
$db_name "db_name_here";
    
$conn mysql_connect("localhost",$db_user,$db_pw);
    if(
$conn){
        if(
mysql_select_db($db_name)){
            
$query="SELECT * FROM products WHERE category='$category' ORDER BY product_name ASC LIMIT 0, 20";    #    Note that in this line, if presenting a user with pages of results you will need to add logic to change the starting entry number from "0" to whatever is appropriate...
            
$result mysql_query($query);
            while(
$row mysql_fetch_assoc($result)){
                
array_push($products,$row);
            }
        }
        
mysql_close($conn);
    }
    if(
count($products)>0){
        
#    If we have one or more results, format and output the product information for each result... The example here uses a simple table, but the basic idea is the same regardless of your desired type of HTML markup...
        
print "<table><thead><tr><th>Product</th><th>Price</th><th>Quantity Available</th></tr></thead><tbody>";
        for(
$i=0;$i<count($products);$i++){
            print 
"<tr><td>".$products[$i]['product_name']."</td><td>".$products[$i]['unit_price']."</td><td>".$products[$i]['stock_quantity']."</td></tr>";
        }
        print 
"</tbody></table>";
    }
    else{
        
#    If there are no results, display a message to the user...
        
print "<p>Sorry, no results were found for your request...</p>";
    }
}
else{
    
#    If category was bogus, we let the user know...
    
print "<p>Invalid category, please try again...</p>";
}
?>
</body>
</html>
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

Last edited by Rowsdower!; 09-27-2012 at 05:40 PM..
Rowsdower! is offline   Reply With Quote
Users who have thanked Rowsdower! for this post:
TYJesus (09-27-2012)
Old 09-27-2012, 08:58 PM   PM User | #4
TYJesus
New Coder

 
Join Date: Jan 2012
Location: Walla Walla
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
TYJesus is an unknown quantity at this point
Okay. Thank You Both!!! This is awesome. I get to learn quite a bit!
TYJesus is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:11 PM.


Advertisement
Log in to turn off these ads.