I want to mail the details of a web shop order and therefore need to use a foreach loop to construct my message. I thought the message below would work, but it doesn't. How can I make this work?
'<table>
<tr>
<td class="column-product"><strong>Productomschrijving</strong></td>
<td class="column-aantal"><strong>Aantal</strong></td>
<td class="column-prijs"><strong>Prijs</strong></td>
</tr>'.foreach($shopper_cart as $item) {
$value = $item['aantal'] * $item['price'];
$sum += $value;.'
<tr>
<td>'.$item["product_id"].'</td>
<td>'.$item["aantal"].'</td>
<td>€'.$item["aantal"] * $item["price"].'</td>
</tr>
</table>'.};
You can't just put a foreach into the middle of a string like that. That's fundamentally broken syntax. It just doesn't work like that.
The best solution for you is to declare the loop as a separate function, like so:
function cartItemTableRows($shopper_cart) {
$output = '';
foreach($shopper_cart as $item) {
$value = $item['aantal'] * $item['price'];
$sum += $value;
$output .='<tr>
<td>'.$item["product_id"].'</td>
<td>'.$item["aantal"].'</td>
<td>€'.$item["aantal"] * $item["price"].'</td>
</tr>';
}
return $output;
}
Now you can put a call to your new function into the existing code where you're building the string:
$tableHTML = '<table>
<tr>
<td class="column-product"><strong>Productomschrijving</strong></td>
<td class="column-aantal"><strong>Aantal</strong></td>
<td class="column-prijs"><strong>Prijs</strong></td>
</tr>'.cartItemTableRows($shopper_cart).'
</table>';
You can only use . to join strings together. You'd need to do something along the lines of this:
$order_details_table = '<table>
<tr>
<td class="column-product"><strong>Productomschrijving</strong></td>
<td class="column-aantal"><strong>Aantal</strong></td>
<td class="column-prijs"><strong>Prijs</strong></td>
</tr>';
// append a row to the table string for each order item
foreach($shopper_cart as $item) {
$value = $item['aantal'] * $item['price'];
$sum += $value;
$order_details_table .= '
<tr>
<td>'.$item["product_id"].'</td>
<td>'.$item["aantal"].'</td>
<td>€'.$item["aantal"] * $item["price"].'</td>
</tr>';
}
$order_details_table .= '</table>';
Then you can use $order_details_table where you want this table to appear.
The .= works a bit like ., but it appends a string to a variable already containing a string.
You can't embed a foreach loop in a string like that. You need to break out of the string, then run your foreach().
$emailBody = '<table>
<tr>
<td class="column-product"><strong>Productomschrijving</strong></td>
<td class="column-aantal"><strong>Aantal</strong></td>
<td class="column-prijs"><strong>Prijs</strong></td>
</tr>';
foreach($shopper_cart as $item) {
$emailBody .= '<tr>
<td>'.$item["product_id"].'</td>
<td>'.$item["aantal"].'</td>
<td>€'.$item["aantal"] * $item["price"].'</td>
</tr>';
}
$emailBody .= '</table>';
here:
$value = $item['aantal'] * $item['price'];
you should be Changed like this:
$value = $item["aantal"] * $item["price"];
because your Single quotation marks as delimiters
As #Spudley says you can't concatenate a foreach like that. You need to separate it from the string. You may also want to initialize the $sum var before adding to it.
The code should look something like this:
$table = '<table>
<tr>
<td class="column-product"><strong>Productomschrijving</strong></td>
<td class="column-aantal"><strong>Aantal</strong></td>
<td class="column-prijs"><strong>Prijs</strong></td>
</tr>';
$sum = 0;
foreach($shopper_cart as $item) {
$value = $item['aantal'] * $item['price'];
$sum += $value;
$table .= '<tr>
<td>'.$item["product_id"].'</td>
<td>'.$item["aantal"].'</td>
<td>€'.$item["aantal"] * $item["price"].'</td>
</tr>';
}
$table .= '</table>';
Related
I am trying to implement PAYTM payment integration method in my ASP.NET MVC project. I am applying paytm payment integration in my websites but I get a result like
Result after payment code run error showing
I have one controller:
public class PaytmController : Controller
{
// GET: Paytm
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult CreatePayment(RequestData requestData)
{
String merchantKey = Key.merchantKey;
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("MID", Key.merchantId);
parameters.Add("CHANNEL_ID", "WEB");
parameters.Add("INDUSTRY_TYPE_ID", "Retail");
parameters.Add("WEBSITE", "WEBSTAGING");
parameters.Add("EMAIL", requestData.email);
parameters.Add("MOBILE_NO", requestData.mobileNumber);
parameters.Add("CUST_ID", "1");
parameters.Add("ORDER_ID", "ORDER8");
parameters.Add("TXN_AMOUNT", requestData.amount);
parameters.Add("CALLBACK_URL", "http://localhost:60308/Paytm/paytmResponse"); //This parameter is not mandatory. Use this to pass the callback url dynamically.
string checksum = paytm.CheckSum.generateCheckSum(merchantKey, parameters);
string paytmURL = "https://securegw-stage.paytm.in/theia/processTransaction?orderid=" + parameters.FirstOrDefault(x => x.Key == "ORDER_ID").Value;
string outputHTML = "<html>";
outputHTML += "<head>";
outputHTML += "<title>Merchant Check Out Page</title>";
outputHTML += "</head>";
outputHTML += "<body>";
outputHTML += "<center><h1>Please do not refresh this page...</h1></center>";
outputHTML += "<form method='post' action='" + paytmURL + "' name='f1'>";
outputHTML += "<table border='1'>";
outputHTML += "<tbody>";
foreach (string key in parameters.Keys)
{
outputHTML += "<input type='hidden' name='" + key + "' value='" + parameters[key] + "'>";
}
outputHTML += "<input type='hidden' name='CHECKSUMHASH' value='" + checksum + "'>";
outputHTML += "</tbody>";
outputHTML += "</table>";
outputHTML += "<script type='text/javascript'>";
outputHTML += "document.f1.submit();";
outputHTML += "</script>";
outputHTML += "</form>";
outputHTML += "</body>";
outputHTML += "</html>";
ViewBag.htmlData = outputHTML;
return View("PaymentPage");
}
[HttpPost]
public ActionResult paytmResponse(PaytmResponse response)
{
return View("paytmResponse" , response);
}
}
The index view looks like this:
#model MyHoroscope.ViewModels.RequestData
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div align="center">
#using (Html.BeginForm("CreatePayment", "Paytm", FormMethod.Post))
{
<table>
<tr>
<td>
Mobile Number :
</td>
<td>
<input type="text" name="mobileNumber" value="" />
</td>
</tr>
<tr>
<td>
Email :
</td>
<td>
<input type="text" name="email" value="" />
</td>
</tr>
<tr>
<td>
Amount :
</td>
<td>
<input type="text" name="amount" value="" />
</td>
</tr>
</table>
<pre>
<input type="submit" value="Save" />
</pre>
}
</div>
and PaymentPage view is like this:
#{
Layout = null;
}
#Html.Raw(ViewBag.htmlData)
and I also created a paymentResponse view:
#model MyHoroscope.ViewModels.PaytmResponse
#{
ViewBag.Title = "paytmResponse";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>paytmResponse</h2>
<div align="center">
<table border="1" width="50%">
<tr>
<td>#Html.DisplayNameFor(m => m.MID)</td>
<td>#Html.DisplayFor(m => m.MID)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.ORDER_ID)</td>
<td>#Html.DisplayFor(m => m.ORDER_ID)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.TXN_AMOUNT)</td>
<td>#Html.DisplayFor(m => m.TXN_AMOUNT)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.CURRENCY)</td>
<td>#Html.DisplayFor(m => m.CURRENCY)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.TXNID)</td>
<td>#Html.DisplayFor(m => m.TXNID)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.BANKTXNID)</td>
<td>#Html.DisplayFor(m => m.BANKTXNID)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.STATUS)</td>
<td>#Html.DisplayFor(m => m.STATUS)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.RESPCODE)</td>
<td>#Html.DisplayFor(m => m.RESPCODE)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.RESPMSG)</td>
<td>#Html.DisplayFor(m => m.RESPMSG)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.TXNDATE)</td>
<td>#Html.DisplayFor(m => m.TXNDATE)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.GATEWAYNAME)</td>
<td>#Html.DisplayFor(m => m.GATEWAYNAME)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.BANKNAME)</td>
<td>#Html.DisplayFor(m => m.BANKNAME)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.PAYMENTMODE)</td>
<td>#Html.DisplayFor(m => m.PAYMENTMODE)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(m => m.CHECKSUMHASH)</td>
<td>#Html.DisplayFor(m => m.CHECKSUMHASH)</td>
</tr>
</table>
</div>
Why is my code not working?
I have a table than shown one record of my model. for example list of category.
i want to create manually row number for this table.
<table class="table table-striped table-bordered table-hover table-full-width dataTable">
#foreach (var item in Model)
{
<tr>
<td>
**I want to Show Row Number Here**
</td>
<td>
#Html.ActionLink(item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.Id }, null)
</td>
</tr>
}
</table>
Define a counter rowNo and write
#{ int rowNo = 0; }
#foreach (var item in Model) {
<tr style="background: #classtr;" >
<td>
#(rowNo += 1)
</td>
Define a counter rowNo and write
<table class="table table-striped table-bordered table-hover table-full-width dataTable">
#{int rowNo;}
#foreach (var item in Model)
{
<tr>
<td>
#rowNo++;
</td>
<td>
#Html.ActionLink(item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.Id }, null)
</td>
</tr>
}
</table>
None of the above worked for me - I did this:
#{int RowNo = 0;}
#foreach (var item in Model) {
<tr>
<td>#{RowNo++;} #RowNo</td>
You may use the Select overload:
<table class="table table-striped table-bordered table-hover table-full-width dataTable">
#foreach (var item in Model.Select((item,index)=>new{item,index})
{
<tr>
<td>
#item.index
</td>
<td>
#Html.ActionLink(item.item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.item.Id }, null)
</td>
</tr>
}
</table>
I've found the next option for my ASP.Net MVC project. All above options didn't work for my project.
#foreach (var item in Model.Select((x, i) => new { Data = x, Index = i + 1 }))
{
<tr>
<td>
#item.Index
</td>
<td>
#Html.DisplayFor(modelItem => item.Data.ReviewerName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Data.ReviewerComments)
</td>
<td>
#Html.DisplayFor(modelItem => item.Data.ReviewerRating)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Data.Id })
</td>
</tr>
}
I think this code will solve the problem
let rows = document.querySelectorAll('tr');
rows.forEach((row) => {
let z = document.createElement("td");
z.textContent = `(row #${row.rowIndex})`;
row.appendChild(z);
});
<table>
<thead>
<tr><th>Item</th> <th>Price</th></tr>
</thead>
<tbody>
<tr><td>Bananas</td> <td>$2</td></tr>
<tr><td>Oranges</td> <td>$8</td></tr>
<tr><td>Top Sirloin</td> <td>$20</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td> <td>$30</td></tr>
</tfoot>
</table>
Easiest approach is to use CSS to have the row numbers in your tables in the index of you MVC View.
.css-serial {
counter-reset: serial-number;
/* Set the serial number counter to 0 */
}
.css-serial td:first-child:before {
counter-increment: serial-number+1;
/* Increment the serial number counter */
content: counter(serial-number);
/* Display the counter */
}
table{
font-family: Arial;
border:solid;
border-width:1px;
border-color:#dedede;
border-radius:5px;
}
th{
background-color:#efefef;
}
td{
padding:3px;
margin:3px;
}
<table class="css-serial">
<tr>
<th>Row #</th>
<! –– Row Number goes here automatically with CSS style ––>
<th>Name</th>
</tr>
<tr>
<td></td>
<td>John</td>
</tr>
<tr>
<td></td>
<td>Richard</td>
</tr>
<tr>
<td></td>
<td>Joe</td>
</tr>
<tr>
<td></td>
<td>Rogan</td>
</tr>
<tr>
<td></td>
<td>Lebron</td>
</tr>
<tr>
<td></td>
<td>Marcus</td>
</tr>
<tr>
<td></td>
<td>Jane</td>
</tr>
<tr>
<td></td>
<td>Maria</td>
</tr>
</table>
Well, easily I have a model:
#model IEnumerable<CompanyWebSite.Category>
And it's quite straightforward to iterating through its items and create a One-item-per-row table to show them.
And the problem is that I want to create a table like this:
<table>
<tr>
<td></td>
<td></td>
</tr>
//
// ...
//
<tr>
<td colspan="2"> ... LastItem ...</td>
</tr>
</table>
Seems simple, but I can't do that...!
#{
ViewBag.Title = "Products";
var last = Model.Last();
int i = 1;
}
<table id="tblCats">
#foreach(var item in Model)
{
if (i == 1){
<tr>
#(i = 2)
}
#if (!item.Equals(last))
{
<td>
<h4>#item.CategoryName</h4>
<label>#item.Description</label>
</td>
if (i == 2)
{
</tr>
i = 1;
}
else
i = 2;
}
else
{
<td colspan="2">
</td>
</tr>
}
}
Because of the first opening <tr>, Razor gets confused and doesn't see the closing }s and else statements... What can I do...?!
You could use HtmlHelper.Raw method to help razor
Ok, I have index.html with a form as follows:
<form action="process.php" method="post">
<table>
<tr>
<td><input name="Field[]" type="checkbox" value="Accounting" />Accounting</td>
<td><input name="Field[]" type="checkbox" value="Finance" />Finance</td>
<td><input name="Field[]" type="checkbox" value="Marketing" />Marketing</td>
</tr>
</table>
</form>
And I have process.php as follows:
<table>
<tr>
<th>Field(s):</th>
<td>
<?php
if(isset($_POST['Field']))
{
for($i = 0; $i < count($_POST['Field']); $i++)
{ echo $_POST['Field'][$i] . ' '; }
}
?>
</td>
</tr>
</table>
Yet for some reason, I only get the first letter of the last checkbox that was checked printed out. Help please!
Try this one in process.php to get the values from $_POST['Field']
<table>
<tr>
<th>Field(s):</th>
<td>
<?php
if(isset($_POST['Field']))
{
foreach ($_POST['Field'] as $value) {
echo $value;
}
}
?>
</td>
</tr>
</table>
I am trying to use the jQuery's progressbar method on the value in my table. I've been able to traverse the table and add the proper div's for the required progressbar's selector. The progress bar does not display the val variable correctly.
var i = 0;
var val = 0;
var id = "";
$("document").ready(function() {
$('#progress tr').find('td').each(function() {
//$(this).append("<div></div>");
if ($(this).html() >= 0)
{
//alert($(this).html());
val = $(this).html();
id = "p_"+i;
$(this).html('<div id="'+id+'"></div>');
$('#'+id).progressbar({
"value": val
});
i++;
//$('#'+id).attr('aria-valuenow',val);
alert(val);
}
});
});
$(function() {
$("#progressbar").progressbar( "option", "value", 37 );
});
<table cellspacing="0" cellpadding="0" border="0" id="progress">
<caption>Class Performance</caption>
<tbody>
<tr>
<th>Student Name</th>
<th>Grade 1</th>
<th>Grade 2</th>
<th>Grade 3</th>
<th>Grade 4</th>
<th>Grade 5</th>
<th>Grade 6</th>
</tr>
<tr>
<td>Wayne, Bruce</td>
<td>100</td>
<td>100</td>
<td>67</td>
<td>14</td>
<td>6</td>
<td>0</td>
</tr>
<tr>
<td>Dent, Harvey</td>
<td>100</td>
<td>100</td>
<td>33</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
val = $(this).html();
will retrieve all the 'html'.. i.e along with the tags of the elements within the TD....
If you want to get the integer value alone, try saving it in a hidden field inside the td and accessing it when you want to get the value....