SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    knowledge gap: product index page

    Hi guys
    Just a quick question that perhaps you could refer me to a tutorial or a book.

    I have site with products... I've been able to create a product template page that queries the product image, description, price etc...

    But now I want to create an index page that a) queries the db for all the products in there and b) when they click the product link, it gets taken to the product page...

    I'm just wondering how that linking relationship can be established?

    For example, I know that the page name will be something like product.php?=id=223 or something like that but I don't know how to create that.

    Thanks

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In its simplest form:
    PHP Code:
    <?php
    /* set up db connection first, then: */
    $q mysql_query("select * from table");
    $r mysql_query($q) or die();
    while(
    $row mysql_fetch_assoc($r)){
        print 
    "<a href='product.php?id=$row[id]'>$row[description]</a><br/>";
    }
    ?>

  3. #3
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would you "echo" the ID and description?

    Thanks by the way!

  4. #4
    SitePoint Zealot erikwebb's Avatar
    Join Date
    Dec 2003
    Location
    Atlanta, GA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mudshark
    In its simplest form:
    PHP Code:
        <?php
        
    /* set up db connection first, then: */
        
    $q mysql_query("select * from table");
        
    $r mysql_query($q) or die();
        while(
    $row mysql_fetch_assoc($r)){
            print 
    "<a href='product.php?id=$row[id]'>$row[description]</a><br/>";
        }
        
    ?>
    You reference each column in the database table given the array key like mudshark wrote. $row[id] represents each ID, while $row[description] represents each description.

    PHP Code:
    print $row[id];    // prints ID
      
    print $row[description];    // print description 
    Also, for future knowledge, echo and print are virtually identical. print() has some more features like the fact that it returns a result, unlike echo(). See full explanation here: http://www.faqts.com/knowledge_base/...l/aid/1/fid/40.

    HTH.

  5. #5
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys... This isn't working out for me all that well... For some reason, the page blanks out at this script when I view the source... Is there a general name for this type of script so that I can look it up and see how it can be set up??

    For your curiosity, here's the script I have so far...

    Code:
    <?php 
    
    // set server access variables 
        $host = "xx"; 
        $user = "xx"; 
        $pass = "xx"; 
        $db = "xx"; 
    
    // open connection 
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
    
    // select database 
    mysql_select_db($db) or die ("Unable to select database!"); 
    
    // create query 
    $query = "SELECT id, name FROM product"; 
    
    // execute query 
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    
    /* set up db connection first, then: */
        $r = mysql_query($q) or die();
        while($row = mysql_fetch_assoc($r)){
            print "<a href='product.php?id=$row[id]'>$row[description]</a><br/>";
        }
    
    // free result set memory 
    mysql_free_result($result); 
    
    // close connection 
    mysql_close($connection); 
    
    ?>
    Thanks!

  6. #6
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    <?php 

    // set server access variables 
        
    $host "xx"
        
    $user "xx"
        
    $pass "xx"
        
    $db "xx"

    // open connection 
    $connection mysql_connect($host$user$pass) or die ("Unable to connect!"); 

    // select database 
    mysql_select_db($db) or die ("Unable to select database!"); 

    // create query 
    $query "SELECT id, name FROM product"

    // execute query 
    $result mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    print 
    "<ul>";
    while(
    $row mysql_fetch_assoc($result)){
            print 
    "<li><a href='product.php?id=$row[id]'>$row[name]</a></li>";
    }
    print 
    "</ul>";
    ?>
    You were having two queries ($query and $q). I've also made the output an unorderded list, so you can see where you put the html tags.

  7. #7
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a bunch mudshark... I just realized that too... should have paid more attention...

    I'll play with the HTML part.. that should be the easy portion!

    i appreciate your time

  8. #8
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there
    As a follow up to this post

    Code:
             echo "<table cellpadding=1 border=1 width=100%>"; 
    		echo "<tr>";
    		echo "<td width=25><a href='product.php?id=$row[id]'>$row[id]</a></td>";
    		echo "<td width=125><a href='product.php?id=$row[id]'>$row[name]</a></td>";
    		echo "<td width=400>$row[shortdesc]</td>";
    		echo "<td width=125><img src=images/$row[pix] alt=$row[pixalt]></td>";
    		echo "</tr>";  
    echo "</table>";
    Say i wanted to display records 3 per row instead of 1 per row, how would this code be modified?

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •