Development Tools Using PHP with MySQL Filtering Input for Queries Preparing SQL Statements

PRICE value($)

I've had this issue from the first series with Randy, "Build a Simple PHP Application", that in my $products array, for the variable $price, if I don't actually enter a Dollar Sign ($) with "Quotes" around the value, I don't get a "Price" in my browser-- Just a big Number Value, that a user/visitor would have to guess its meaning. (In Randy's browser he's never had this issue & it hasn't come up.

I'm going to include the code which has my "solution" along with the original code commented out

    $products[103] = array(
        "name" => "Mike the Frog Shirt, Blue",
        "img" => "img/shirts/shirt-103.jpg",    
        // "price" => 20,    << ===== = ---- -- -- - -   -  - ?!!
        "price" => "$20",
        "paypal" => "7T8LK5WXT5Q9J",
        "sizes" => array("Small","Medium","Large","X-Large")
    );

it has become an issue because now the page is getting all this info from the database (utterly new stuff to me) & I lack even a shred of insight into resolving this.

Interesting question? i hope so. Please and Thank You!

B

Mkill Borodin
Mkill Borodin
Pro Student 3,442 Points

Hi,Byron! If you are still looking for an answer, attach a screenshot with your view code from shirt.php.,focused on a div with a "shirt-details" class;

2 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Byron.

I do not think your solution is ideal as writing the code the way you did will mean that "$20" will come out as a string, while with the price you would normally need to treat is as a number, let's say an integer. So I would try to sort it in a different way, but I haven't really understood what happens if you just do "price" => 20, on that line.

Could you please be more specific?

ty

Vittorio

Hey Vittorio, thank you for responding, and I'm sorry for the delay. I still need to figure out an answer for this.

The problem I'm having with setting the value as an integer, i.e. "price"=>20 is that it renders in the browser without the dollar-symbol ($ - as it does in the videos, Randy's shirt.php pages all have $-symbols when viewed in the browser) I can't find anywhere, in any of the other files or videos, where it's declared that, after the server has processed & delivered the page to the browser, that integer value should get a "$" with it.

This are the lines in my shirt.php file that display the price:

<div class="shirt-details">

    <h1><span class="price">$<?php echo $product["price"]; ?></span> <?php echo $product["name"]; ?></h1>

I add the dollar sign here, and I suspect somewhere in the css file the class "price" formats the integer into a standard dollar amount.

Mai Lon Ross, thank you for the post. You are right there. I had realized some time ago I was missing that (dollar sign) inside that span tag which solved my problem - but I failed to update this thread concerning the correction. So, thank you again for you effort, in case someone else has a similar problem - this way they won't be left hanging...