SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Passing data

  1. #1
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Passing data

    Hi Guys,

    Seem to have got myself into a spot of bother in regards to organising my code to work and also be writen correctly. Here are the 3 things I am trying to do, with the 3rd issue being the problem.

    1. The system I have created allows for a user to view a list of items such as servers (servers.php).

    2. They are able to click on a server and by doing so are taken to a page (individualitem.php) displaying full details about this item. This page also displays related items the user may also want, (ram, drivers). Just like this: http://uk.insight.com/apps/productpr...2%26S%3D300149

    This is the bit I'm stuck on...

    3. They should then be able to click a related item and by doing so are taken to a page (individualitem.php) displaying full details about this item.

    I thought this bit would be quite straight forward as its the same as no 1 but my code is a mess and I'm not understanding what the problem is.

    I don't know if its the fact that I have too many select statements going on. Maybe I can put them all into one.
    servers.php
    PHP Code:
    $sql = ("SELECT * FROM product WHERE catid = 2 order by id asc") or die("SELECT Error: ".mysql_error());
    $result mysql_query($sql);
            
    for (;
    $row=mysql_fetch_array($result);) { 
    echo 
    '<a href="individualitem.php?id='$row['id'], '">'$row['title'], '</a>';
    echo 
    '<img src="images/'$row['photo'];
    echo 
    $row['shortdesc'];    
    echo 
    $row['salesprice'];
    echo 
    $row['productno']; 
    individualitem.php
    PHP Code:
    // check to be sure that the passed id is a number 
    if (is_numeric($_GET[id])) {  
    $result mysql_query("SELECT * FROM product WHERE id = '{$_GET[id]}'")or die("SELECT Error: ".mysql_error());
    $full_info mysql_fetch_array($result);

    printf("%s"$full_info[title]); // I display each table column like so

    }else {  
    echo 
    "Error, there is no such item.";
    }

    $result2 mysql_query("SELECT * FROM product p INNER JOIN relateditems r ON r.relateditemid = p.id WHERE r.itemid = '{$_GET[id]}'")or die("SELECT Error: ".mysql_error());    //select all from product and relateditems where relateditemid = productid and relateditemid is the id selected
     
    while ($row mysql_fetch_assoc($result2)) {
    echo 
    '<img src="images/'$row['photo'], '" border="1" width="50px" height="50px" />';
    echo 
    '<a href="individualitem.php?id='$row['id'], '">'$row['title'], '</a><br/>';
    echo 
    $row['productno'];
    echo 
    $row['salesprice'];


  2. #2
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You dont need to use while loop for selecting one result. Dont use * to select if you dont wanna use all fields of a sql table

    PHP Code:
    // check to be sure that the passed id is a number 
    if (is_numeric($_GET["id"])) {   
    $result mysql_query("SELECT title FROM product WHERE id = '".$_GET["id"]."'")or die("SELECT Error: ".mysql_error()); 
    $full_info mysql_fetch_array($result); 

    printf("%s"$full_info[0]); // I display each table column like so 

    }else {   
    echo 
    "Error, there is no such item."


    $result2 mysql_query("SELECT p.productno,p.salesprice,r.photo,p.id,p.title FROM product p INNER JOIN relateditems r ON  p.id = r.relateditemid WHERE r.itemid = '".$_GET["id"]."' LIMIT 1")or die("SELECT Error: ".mysql_error());    //select all from product and relateditems where relateditemid = productid and relateditemid is the id selected 
    $row mysql_fetch_array($result2);
    echo 
    '<img src="images/'$row[2], '" border="1" width="50px" height="50px" />'
    echo 
    '<a href="individualitem.php?id='$row[3], '">'$row[4], '</a><br/>'
    echo 
    $row[0]; 
    echo 
    $row[1]; 

  3. #3
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Thanks very much!

    Quote Originally Posted by coolR View Post
    Dont use * to select if you dont wanna use all fields of a sql table
    I must remember this as I kept forgeting.

    How come you changed '{.$_GET[id].}' to ".$_GET["id"]."'
    Had I just writen it incorrectly?

    Also why use $row[1] $row[2] instead of $row[photo] $row[title]?
    Last edited by am_dorky; Oct 21, 2008 at 11:13. Reason: had another question to add

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    '{.$_GET[id].}' to ".$_GET["id"]."' works same

    numeric array saves space show it works faster

  5. #5
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I now need an if statement to go with what I've got saying:

    if the passed id is a number display main item

    else if the passed id is a number and has items related to it display main item and related item

    else show error

    I tried using this below but am unsure what I'm doing wrong. At the moment my system is only displaying the main item no related items.

    PHP Code:
    if (is_numeric($_GET["id"])) {    
    $result mysql_query("SELECT * FROM product WHERE id = '{$_GET[id]}'")or die("SELECT Error: ".mysql_error());         
    $full_info mysql_fetch_array($result); 

    // list items

    }else if (is_numeric($_GET["id"]) == $r.itemid) {     
    $result2 mysql_query("SELECT p.productno,p.salesprice,p.photo,p.id,p.title FROM product p INNER JOIN relateditems r ON  p.id = r.relateditemid WHERE r.itemid = '".$_GET["id"]."' LIMIT 1")or die("SELECT Error: ".mysql_error());
    $row mysql_fetch_array($result2);

    // list items

    }else {
    echo 
    "Error, there is no such item."

    Anyone know where I'm going wrong?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use a left outer join.
    show the result. if the joined table columns aren't null, then show the related data too.

  7. #7
    SitePoint Enthusiast am_dorky's Avatar
    Join Date
    Sep 2008
    Location
    Bucks
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant, thanks. Got it working.

    Thanks to everyone that posted.


Tags for this Thread

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
  •