2
$sql = "SELECT * FROM item";
$stmt = $conn->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$data['result_2'] .= '
    <div class="col-sm-4 col-md-4">
        <div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
            <div class="content-boxes-text">
                <form action="php/additem.php" method="post" class="form-inline pull-right">
                    <h4>'.$row['itemName'].'</h4><input type="hidden" name="itemName" value="'.$row['itemName'].'">
                    <img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
                    <p>'.$row['description'].'</p><input type="hidden" name="description" value="'.$row['description'].'">
                    <div class="form-group">
                    <label class="sr-only" for="exampleInputAmount">Qty</label>
                    <div class="input-group">
                    <input type="number" name="qty" class="form-control" id="exampleInputAmount" placeholder="How Many?">
                    </div>
                    </div>
                    <button type="submit" class="btn btn-primary">Add</button>
                </form>
            </div>
            <!-- //.content-boxes-text -->
        </div>
        <!-- //.content-boxes -->
    </div>
';
}

I want to be able to add an if statement in this result_2 string. This is for displaying a product, and i would like to display price depending on users session value? eg.

if ($_SESSION['customer_x'] == a) {
    display price a
}
else if ($_SESSION['customer_x'] == b) {
    display price b
}

Is this the correct method to be able to add an if statement to a JSON query?

1
  • If you want to display different prices based on the value of $__SESSION['customer_x'], then you can just assign the price to a variable (say $display_price) in your above if-else statement. And use the variable in your long string of HTML. But where does price a and price b come from? $row ? Commented Apr 29, 2015 at 4:18

3 Answers 3

2

After Starting your while loop, put a if else there,

$price ="";
if ($__SESSION['customer_x'] == a) {
   $price='display price a';
}
else if ($_SESSION['customer_x'] == b) {
  $price='display price b';
}

and now echo this price where ever you want to in your html

this is more neet and less messy way

Sign up to request clarification or add additional context in comments.

4 Comments

I initially thought this would work too but the code breaks when i add this :/
No it wont i tested it, put the code just after starting your while, After that start your html content
Actually this worked after i set the A and B as a string eg, 'A' 'B' Thanks
Gud, happy to help you
1

You can use a ternary if operator to have conditional statements inside strings, example

$bool = false;
echo "The value of \$bool is " . ($bool == true ? "TRUE" : "FALSE");

Example in use

Comments

1

To add that you can keep doing the same but:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ // the next of the content
    $priceOfSession = ($__SESSION['customer_x'] == a) ? 'pricea' : 'priceb';
      $data['result_2'] .= '
        <div class="col-sm-4 col-md-4"> 
             '.$priceOfSession.' 
                </div>
             </div>
             <button type="submit" class="btn btn-primary">Add</button>
          </form>
        </div>
        <!-- //.content-boxes-text -->
    </div>
</div>

'; }

So if you want to evaluate only two conditions if otherwise the if you want, simply add it there. Do as you add the $ row but before defining value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.