SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I need Some Quick PHP Assistance

    Hello,

    My current code is -

    if ($similar['prodPrice'] == 0.00) {

    I am looking to list out 0.00 1.00 2.00 etc up to 5.00.

    Could someone share how I would need to write it out?

    Thanks in advance!

  2. #2
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well if I'm understanding your question, try this

    switch ($similar['prodPrice']) {
    case 0.00:
    do something;
    break;
    case 1.00:
    do something;
    break;
    case 2.00:
    do something;
    break;
    case 3.00:
    do something;
    break;
    case 4.00:
    do something;
    break;
    case 5.00:
    do something;
    break;
    }

  3. #3
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, I did not have any luck getting this to work in my system. Do you have any other methods you could mention?

    Thanks again,

  4. #4
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    "do something;" is just filler. You can remove that and put your own code in.

    Or, if prices are listed in your database, this might be more of a database question instead.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  5. #5
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, I attempted to modify to integrate to the surrounding code however it didn't seem to set right. Essentially, I am trying to tell the system that if 0.00 to 5.00 then specify "text here".

    I had success with the following, though only up to 1.00 using an if if:

    if ($similar['prodPrice'] == 0.00) {
    $box_content->assign("PRICE",$theTitle.'<span>- See store for pricing</span><br />');}
    if ($similar['prodPrice'] == 1.00) {
    $box_content->assign("PRICE",$theTitle.'<span>- See store for pricing</span><br />');

    Would you happen to know of another way to list up to 5.00?

    Thanks!

  6. #6
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    The switch statement will do that.

    http://php.net/manual/en/control-structures.switch.php

    If you want to use if-statements, I suggest if-else.

    Or, if you want to specify a range:

    Code:
    if($similar['prodPrice'] > 0.00 && $similar['prodPrice'] <= 5.00){
         $box_content->assign("PRICE",$theTitle.'<span>- See store for  pricing</span><br />');}
    }
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  7. #7
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent, the range example is a perfact solution. Thanks the help!

  8. #8
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On another note, using the below range solution:

    if($similar['prodPrice'] > 0.00 && $similar['prodPrice'] <= 5.00){
    $box_content->assign("PRICE",$theTitle.'<span>- See store for pricing</span><br />');


    Is there a simple modification that will only specify the exact values?

    I would still like to see for example 4.55 or 2.35, etc. I am just trying to exclude and rename the prodPrice if it is a match to the whole dollar amount being 1.00 2.00 3.00 4.00 5.00.

    I really like the range method as it is a major code saver, I really appreciate your help.

    Thanks,

  9. #9
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Yes. You can use any expression within the if-statement that fits the logic of what you need.

    $value==1.35

    $value==1.50 || $value==2.00

    $value>=1.50 && $value<=2.50

    ...and so on and so forth.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  10. #10
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My apologies for the redundancy, would you mind sharing a last quick example of how this will look.

    Thanks again,

  11. #11
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Like this?

    Code:
    if($similar['prodPrice'] == 4.55){
         $box_content->assign("PRICE",$theTitle.'<span>- See store  for  pricing</span><br />');
    }
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  12. #12
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is looking like I will need to give multiple ranges 0.01 - 0.99 and then specify the whole value 0.00 etc.

    I am sure I thinking too hard on writing this out. One of those frustrating code blocks.

  13. #13
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    $value >=0.00 && $value<=0.99
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  14. #14
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, your last post opened my eyes.

    The code below does the trick:

    if($row_product['prodPrice'] == 0.00 or $row_product['prodPrice'] == 1.00 or $row_product['prodPrice'] == 2.00 or $row_product['prodPrice'] == 3.00 or $row_product['prodPrice'] == 4.00 or $row_product['prodPrice'] == 5.00){
    $box_content->assign("PRICE",$theTitle.'<span>- See store for pricing</span><br />');

    Thank you for working with me while I sorted my issue out.

    - Mike

  15. #15
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    You should be using || (double pipe) for the OR operator.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  16. #16
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I will modify that. Just curious what is the significance between the use of the "||" or the "or" in this case?

  17. #17
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    AFAIK there's usually not much difference between using || or OR
    But there is a difference in "precedence". I usually avoid any potential problems by putting things in parentheses instead of thinking about precedence issues.

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,681
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by vessio View Post
    The code below does the trick:

    if($row_product['prodPrice'] == 0.00 or $row_product['prodPrice'] == 1.00 or $row_product['prodPrice'] == 2.00 or $row_product['prodPrice'] == 3.00 or $row_product['prodPrice'] == 4.00 or $row_product['prodPrice'] == 5.00){
    $box_content->assign("PRICE",$theTitle.'<span>- See store for pricing</span><br />');
    It my do the trick, but I don't think that it does it very well.

    The first thing to do is to remove a vast amount of duplication. Assign $row_product['prodPrice'] to a separate variable, so that the array is referenced only once.

    Code php:
    $price = $row_product['prodPrice'];
    if($price == 0.00 or $price == 1.00 or $price == 2.00 or $price == 3.00 or $price == 4.00 or $price == 5.00) {
         $box_content->assign("PRICE",$theTitle.'<span>- See store for  pricing</span><br />');
    }

    Now it's clear that we're checking if the price is 5 or less, and if the price is equal to the nearest dollar.


    Code php:
    $price = $row_product['prodPrice'];
    $nearestDollar = intval($price);
    if ($price >= 0 && $price <= 5 && $price == $nearestDollar) {
         $box_content->assign("PRICE",$theTitle.'<span>- See store for  pricing</span><br />');
    }

    That seems to be a clearer and easier to understand way of doing things.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •