Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Echoing the value of MySQL field data

    I have a MySQL table called "products":
    +------+-------+-------+-------+
    | id | sizes | sizem | sizel |
    +------+-------+-------+-------+
    | 1 | 2 | 0 | 1 |
    | 2 | 3 | 1 | 0 |
    +------+-------+-------+-------+
    What I am wanting to know is if I can make PHP print a value from it, but only if it is not a zero.. something like:

    $sizeL = (table(products)id1.sizel);
    if (table(products)id1.sizel == "0") {echo "";} else {echo "$sizeL";}

    Yeah I know the above code is far from beeing valid, but is the best I could come up with. heh
    Last edited by Democrazy; 09-12-2011 at 12:45 PM.

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,312
    Thanks
    4
    Thanked 206 Times in 203 Posts
    Let SQL do the work and only select values from the column you are interested in if they have a value that is not zero. Then all you have to do with PHP is loop through the results and echo/print them.

    Code:
    SELECT `id` FROM `products` WHERE `sizel` != 0
    Dave .... HostMonster for all of your hosting needs

  • #3
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Sure, Dave, but what I am going to do is have:

    Size S: 1
    Size M: 2
    Size L: 3

    ... am I correct in assuming that using the code you said, would produce this for a size M if it were 0?

    Size S: 1
    Size M:
    Size L: 3

    If so, thats why I want to echo the product size also, so if a product size does equal 0, then there is not going to be a product size with no number next to it, as can be seen above with size M.

    Last edited by Democrazy; 09-12-2011 at 01:04 PM.

  • #4
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Ah, I have left that out in my original post:

    $sizeL = (table(products)id1.sizel);
    if (table(products)id1.sizel == "0") {echo "";} else {echo "Size L: $sizeL";}

  • #5
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Oh God, I have totally botched this post!
    I will start over.


    I have a MySQL table called "products":
    +------+-------+-------+-------+
    | id | sizes | sizem | sizel |
    +------+-------+-------+-------+
    | 1 | 3 | 2 | 2 |
    +------+-------+-------+-------+
    What I want to do is produce the following on a webpage:

    Size S: 3
    Size M: 2
    Size L: 2

    Using Size M for an example, to achieve this, I am thinking of code that would go along the lines of something like this:
    $sizeM = (table(products)id1.sizem);
    if ($SizeM == "0") {echo "";} else {echo "Size M: $sizeM";}


    Just for example that Size M (sizem) in my MySQL database equaled 0, then the webpage would render like this:

    Size S: 3
    Size L: 2

    How would I write that in PHP?
    Last edited by Democrazy; 09-12-2011 at 01:18 PM.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Closing this since it's answered in your other thread.


  •  

    Posting Permissions

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