Solved

Can a html button have php attributes?

Posted on 2016-09-01
6
23 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 30

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 51

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now