Solved

Can a html button have php attributes?

Posted on 2016-09-01
6
25 Views
Last Modified: 2016-09-01
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.
0
Comment
Question by:Black Sulfur
  • 3
  • 2
6 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 41779619
You can use something like this. The actual implementation depends on how you're iterating through data to build the html output.
<button
    class="add-item"
    data-item-id="2"
    data-item-name="<?php echo $item_name; ?>"
    data-item-price="<?php echo $item_price; ?>"
    data-item-weight="<?php echo $item_weight; ?>"
    data-item-url="http://mysite.com/products/bacon"
    data-item-description="<?php echo $item_description; ?>">
    Buy bacon
</button>

Open in new window

0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41779629
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

0
 

Author Closing Comment

by:Black Sulfur
ID: 41779631
Awesome, that worked! :)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Black Sulfur
ID: 41779632
Sorry, Julian. Only got your response after accepting Marco's. Why would you recommend HEREDOC ?
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41779663
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
2
 

Author Comment

by:Black Sulfur
ID: 41779709
Helpful, thank you!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question