SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display one field in page from database

    Hi,

    Hopefully someone can help me while i still have hair left to pull out...

    Here's what i'd like to do;

    i have a simple database - "brother" which consists of two columns, BroNo and SPrice. There's 22 entries, BroNo contains the part number, and SPrice the selling price.

    i would like to write a page where the prices shown for each model are taken directly from the "brother" database and displayed, according to the model.

    For instance, where i would normally write this for a static page;

    <table><tr>
    <td>FAXT104U1</td>
    <td>54.00</td>
    </tr>
    </table>

    i would really rather write php, so it'd be something like this;

    <table><tr>
    <td>FAXT104U1</td>
    <td>*magical php code*</td>
    </tr>
    </table>

    ..where the *magical php code* bit refers to the "brother" database and pulls the correct (and current) price out and displays it, as in this case, 54.00.

    If anyone can help with what should be a fairly straightforward bit of coding i'd be extremely grateful..as would my diminishing hair.

    And lastly, please please don't point me at yet another tutorial. i've gone through so many and i'm still just as far from what i want as i was when i started.

    Many thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Coventry, UK
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming you're using mySQL, here's the basics...

    Don't forget to edit the user, pass and db_name.

    PHP Code:
    <?
    $db 
    mysql_connect("localhost","user","pass");
    mysql_select_db("db_name",$db);

    $query "SELECT * FROM brother";
    $result mysql_query($query);

    echo 
    "<table>\n";

    while (
    $row mysql_fetch_array($result)) {
         echo 
    "<tr>\n";
         echo 
    "<td>".$row[BroNo]."</td>\n";
         echo 
    "<td>".$row[SPrice]."<\td>\n";
         echo 
    "</tr>\n";
    }

    echo 
    "</table>\n";

    ?>
    Adam

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Adam, but it's not quite what i'm looking for. What it is, is i have an existing page full of products, and i want to be able to put that bit of *magical code* in where the price appears for each product.

    This is so i can update the database with the new prices, without having to change any html code at all.

    So, the html table (still being designed) will contain a <td> </td> that will display the price determined within the existing database in that table definition.

    Does that make sense?

    And rather than each <td> containing all the php code (such as the above), it will only contain the necessary code that checks the part number and produces the price, and all the username, database, and password etc.. will appear once at the start of the html code (like javascript does).

    In case it's still not clear, this is something i had in mind;

    <html>
    <head>
    <title>Price List</title>
    </head>

    <body>

    <?
    All the appropriate php database code; username, password, database etc..
    ?>


    <table>
    <tr>
    <td>PART NUMBER</td><td>DESCRIPTION</td><td>PRICE</td>
    </tr>
    <tr>
    <td>PRODUCT IMAGE</td><td>ORDER CODE</td>
    </tr>

    </body>
    </html>

    So, where PRICE is, i want that <td> to display the price in the database for that particular product.

    Hopefully that makes sense, and is actually possible using php. If it isn't, then it's back to changing the same price on different pages over and over again...which is what i'm trying to avoid.

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Adam's code is basically correct, only you need one particular row from the table:
    PHP Code:
    $query "SELECT * FROM brother WHERE BroNo='$BroNo'"
    Saul

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, php_daemon.

    I'm sure it is right, and i'm just not seeing it, but i still don't understand how i'm supposed to put it in the html code so that it shows how i'd like it to show?

    At the moment it shows all part numbers and all prices in one long list, and if i put your line within a <td></td> it still just comes back with errors. i just don't know where to put it!

    All i'm looking for is a tag type thing of php that will request the price of the product and place it in the <td> that i'd like it to appear in.

    While i really do appreciate all your help, i'm getting increasingly fed up with this, so much so i'm getting to the stage where i think it'll be easier just to manually put the prices everytime they change.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Perhaps post what you've got there, it'll be easier to suggest something.
    Saul

  7. #7
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do something like this:
    PHP Code:
    <?php
    $db 
    mysql_connect("localhost","user","pass");
    mysql_select_db("db_name",$db);
    $query "SELECT BroNo, SPrice FROM brother";
    $result mysql_query($query);
    $prices = array();
    while (
    $row mysql_fetch_array($result)) { 
      
    $prices[$row['BroNo']] = $row['SPrice'];
    }
    That will store all the prices in a an array for you, then in the rest of your table, you could do:

    HTML Code:
    <table><tr>
    <td>FAXT104U1</td>
    <td><?php echo $prices['FAXT104U1']; ?></td>
    </tr>
    </table>
    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. Nos, yes that's more how i saw it, a tag within the <td></td> fields that looks for the part number and shows the price. Only trouble is, is that it's not actually showing the price But if it did, that would be perfect and just what i'm looking for.

    Daemon, apart from changing the username, password and db name, what has been posted here is pretty much what i've tried, as the page that this code (hopefully) will sit and work on is still being designed (and the design depends on if i can get the code working).

    But what nos has attempted is just the sort of thing i was hoping for. And if we could get it to actually work..all the better!

  9. #9
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // debug settings
    ini_set('display_errors'1);
    error_reporting(E_ALL);

    $db mysql_connect("localhost","user","pass"); 
    mysql_select_db("db_name",$db); 
    $query "SELECT BroNo, SPrice FROM brother"
    $result mysql_query($query) or die(mysql_error());
    $prices = array(); 
    while (
    $row mysql_fetch_array($result)) { 
      
    $prices[$row['BroNo']] = $row['SPrice']; 
    }

    var_dump($prices['foo']);
    var_dump($prices['bar']); 
    obviously, foo and bar are valid entrys you must supply

    this should work, or you should get errors.

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm, that doesn't seem to work either, clamcrusher. i get "Parse error: parse error, unexpected '<' in C:\xampplite\htdocs\brother\brother.php on line 28" when i try that.

    i've tried various combinations to see if i can get it working, but no joy at all.

    i'll post what i'm using here and see if that will help figure out why it's not doing it, hopefully there's a glaring error that i'm missing;

    PHP Code:
    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body>
    <?php 
    // debug settings 
    ini_set('display_errors'1); 
    error_reporting(E_ALL); 

    $db mysql_connect("localhost","root",""); 
    mysql_select_db("brother",$db); 
    $query "SELECT BroNo, SPrice FROM brofaxsql"
    $result mysql_query($query) or die(mysql_error()); 
    $prices = array(); 
    while (
    $row mysql_fetch_array($result)) { 
      
    $prices[$row['BroNo']] = $row['SPrice']; 


    var_dump($prices['FAXT104U1']); 
    var_dump($prices['FAX1030EU1']); 



    <
    table><tr>
    <
    td>FAXT104U1</td>
    <
    td><?php echo $prices['FAXT104U1']; ?></td>
    </tr>
    </table>
    </body>
    </html>
    *'brofaxsql' is the name of the table in the database 'brother' btw

  11. #11
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to close the php ff, like so:

    PHP Code:
    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body>
    <?php
    // debug settings
    ini_set('display_errors'1);
    error_reporting(E_ALL);

    $db mysql_connect("localhost","root","");
    mysql_select_db("brother",$db);
    $query "SELECT BroNo, SPrice FROM brofaxsql";
    $result mysql_query($query) or die(mysql_error());
    $prices = array();
    while (
    $row mysql_fetch_array($result)) {
      
    $prices[$row['BroNo']] = $row['SPrice'];
    }

    var_dump($prices['FAXT104U1']);
    var_dump($prices['FAX1030EU1']);
    ?>


    <table><tr>
    <td>FAXT104U1</td>
    <td><?php echo $prices['FAXT104U1']; ?></td>
    </tr>
    </table>
    </body>
    </html>
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  12. #12
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a thought after looking at you code, for it to work and loop through each entry in the DB u need to do this:

    PHP Code:
    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body>
    <?php
    // debug settings
    ini_set('display_errors'1);
    error_reporting(E_ALL);

    $db mysql_connect("localhost","root","");
    mysql_select_db("brother",$db);
    $query "SELECT BroNo, SPrice FROM brofaxsql";
    $result mysql_query($query) or die(mysql_error());
    $prices = array();
    while (
    $row mysql_fetch_array($result)) {
      
    $prices[$row['BroNo']] = $row['SPrice'];
      
      
    ?>
      <table><tr>
      <td>FAXT104U1</td>
      <td><?php echo $prices['FAXT104U1']; ?></td>
      </tr>
      </table>
      <?php

    }

    var_dump($prices['FAXT104U1']);
    var_dump($prices['FAX1030EU1']);
    ?>

    </body>
    </html>
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  13. #13
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic!! It works!!

    Many, many thanks to you all, that is exactly what i needed. I am now an extremely happy bunny...Yay!

    And webnoob, your first code (where you said it needed closing) works fine, and if i use your second one it says there's a parse error on line 33. So, if it (the first code) works, i guess i don't really need those extra bits on the end, no?

    And lastly, one more thing, now that it works so brilliantly...if i wanted to add more fields in future, how would i modify the code to include them, say a 'Description' and 'image' field..? (i've had a go at adding one, but i just kept breaking it...)

    Thanks again, all. Superb job.

  14. #14
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well if the 1st works forget the second
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"


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
  •