SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Including Data in a MySQL Column

    Is it possible to include data from one column in a MySQL table in another column in the same table? Actually, let me elaborate. I have one column called "price" that will have prices. I have another column, a text column, that will have html and a product description. In with this description will be the price. I was hoping there was some way to do a sort of "MySQL include" so it would pull the price from the "price" column, thereby not having to change the price in 2 places.

  2. #2
    With More ! for your $ maxor's Avatar
    Join Date
    Feb 2004
    Location
    Scottsdale, Arizona
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you shouldn't be mixing and matching table information, at least not in the database.

    If you have a products table, and each product has a description and a price, you can just make sure to print out the price twice when you print out each record.

    Something like this
    PHP Code:
    while( $result msql_fetch_array($mysql_query)){

               echo 
    'Description : '.$result['description'].$result['price'];
               echo 
    'Price : '.$result['price'];

    Does that make sense?

  3. #3
    SitePoint Enthusiast bind727's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Alberta
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another idea would be to have a marker like [PRICE] in the description that would be replaced by the price column

    PHP Code:
       $description str_replace("[PRICE]"$row['price'], $row['description']); 
    This might be preferable as it is more flexible on where the price is in the description.
    +---------------------------+
    Joel Pittet | Web Developer
    www.JoelPittet.com
    +---------------------------+

  4. #4
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by maxor
    Well you shouldn't be mixing and matching table information, at least not in the database.
    If you have a products table, and each product has a description and a price, you can just make sure to print out the price twice when you print out each record.
    Something like this
    PHP Code:
    while( $result msql_fetch_array($mysql_query)){

               echo 
    'Description : '.$result['description'].$result['price'];
               echo 
    'Price : '.$result['price'];

    Does that make sense?
    A section of html will be pulled out of a database column and inserted onto pages. Somewhere right in the middle of that html would be the price. If the HTML was being generated as part of a PHP script your solution would work, but in this case, like I said, the HTML code is coming right out of the database.

  5. #5
    With More ! for your $ maxor's Avatar
    Join Date
    Feb 2004
    Location
    Scottsdale, Arizona
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If that's the case bind727's approach is probably your best bet.

  6. #6
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bind727
    Another idea would be to have a marker like [PRICE] in the description that would be replaced by the price column
    I didn't know you could do something like that. I'll give it a try and report back.


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
  •