Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on 

Can a html button have php attributes?

I am playing around with integrating a shopping cart into my own website and the way you create a product or buy button is like this:

<button
    class="add-item"
    data-item-id="2"
    data-item-name="Bacon"
    data-item-price="3.00"
    data-item-weight="20"
    data-item-url="http://mysite.com/products/bacon"
    data-item-description="Some fresh bacon">
    Buy bacon
</button>

Open in new window


This could get tedious if for every item you had to now do this. Can you make these dynamic with PHP? I would more than likely have all products in the database but for simplicity lets say I have:

<?php 
$id = 2;
$item_name = "cheese";
$item_price = 3.00;
$item_weight = 20;
$item_description = "Chedder Cheese";
?>

Open in new window


How could I put that into the button code? I have tried various ways but none of them work so I am obviously doing something wrong.
PHP

Avatar of undefined
Last Comment
Crazy Horse
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
I would use HEREDOC though

<?php
$id = 2;
$item_name = "cheese";
$item_price = 3.00;
$item_weight = 20;
$item_description = "Chedder Cheese";

echo <<< BUTTON
<button
    class="add-item"
    data-item-id="{$id}"
    data-item-name="{$item_name}"
    data-item-price="{$item_price}"
    data-item-weight="{$item_weight}"
    data-item-url="http://mysite.com/products/bacon"
    data-item-description="{$item_description}">
    Buy bacon
</button>
BUTTON;

Open in new window

Avatar of Crazy Horse
Crazy Horse
Flag of South Africa image

ASKER

Awesome, that worked! :)
Avatar of Crazy Horse
Crazy Horse
Flag of South Africa image

ASKER

Sorry, Julian. Only got your response after accepting Marco's. Why would you recommend HEREDOC ?
Why would you recommend HEREDOC ?

PHP provides for three type of string output
Single quotes - everything inside the quotes is taken literally - there is no substitution of variable names. Single quotes must be escaped (\') double quotes can be used as is
Example
$name = "Julian";
$mystring = 'My name is "{$name}" and my neighbour\'s name is Fred';

Open in new window

Will output
My name is "{$name}" and my neighbour's name is Fred

Open in new window

Note the escape '\' is not in the output

Double Quotes - any variables in the string will be replaced with their values, single quotes can be used as is double quotes must be escaped (\")
Example
$name = "Julian";
$mystring = "My name is \"{$name}\" and my neighbour's name is Fred";

Open in new window

Will output
My name is "Julian" and my neighbour's name is Fred

Open in new window


HEREDOC is the best of both worlds
You can include both single and double quotes in the string without escaping them and you can include variable names. For situations (like yours) where you want to output a block of text with double quote (around the class names) and variables in the string it makes for a much neater ordering of your code.
Example
$name = "Julian";
$mystring = <<< ARBITARYTAG
My name is "{$name}" and my neighbour's name is Fred
ARBITARYTAG;

Open in new window


You can read more about it here
Avatar of Crazy Horse
Crazy Horse
Flag of South Africa image

ASKER

Helpful, thank you!
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo