validation engine convert negative number to positive number - php

how can I enter only positive number and if I entered negative number it will convert automatically to positive number this the code That I have how can I edit it
<?php
if (isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"]) > 0) {
echo '<div class="cart-view-table-front" id="view-cart">';
echo '<h3>Your Shopping Cart</h3>';
echo '<form method="post" action="cart_update.php">';
echo '<table width="100%" cellpadding="6" cellspacing="0">';
echo '<tbody>';
$total = 0;
$b = 0;
foreach ($_SESSION["cart_products"] as $cart_itm) {
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$product_color = $cart_itm["product_color"];
$bg_color = ($b++ % 2 == 1) ? 'odd' : 'even'; //zebra stripe
echo '<tr class="' . $bg_color . '">';
echo '<td>Qty <input type="text" size="3" maxlength="3" name="product_qty[' . $product_code . ']" value="' . $product_qty . '" /></td>';
echo '<td>' . $product_name . '</td>';
echo '<td><input type="checkbox" name="remove_code[]" value="' . $product_code . '" /> Remove</td>';
echo '</tr>';
$subtotal = ($product_price * $product_qty);
$total = ($total + $subtotal);
}
echo '<td colspan="4">';
echo '<button type="submit">Update</button>Checkout';
echo '</td>';
echo '</tbody>';
echo '</table>';
$current_url = urlencode($url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
echo '<input type="hidden" name="return_url" value="' . $current_url . '" />';
echo '</form>';
echo '</div>';
}
?>

GrumpyCrouton > What is the variable that you are trying to convert to positive?
M.Alhaddad > #GrumpyCrouton product_qty
It's quite simple.
Just use abs() on the variable.
$product_qty = abs($cart_itm["product_qty"]);
Note: abs() works in (PHP 4, PHP 5, PHP 7)
"Converting a number to positive" is just getting it's absolute value.
Documentation:
PHP function.abs

Related

If statment to echo an image if var=EUR doesnt work

This code added currency switcher to a menu,
But unfortunately I don't have access to MySQL database so I can't add image "flag" to each currency, then execute them
So I tried to use if statement.
This is my code:
$currency_switcher_enable = houzez_option('currency_switcher_enable');
$is_multi_currency = houzez_option('multi_currency');
if( $currency_switcher_enable != 0 && $is_multi_currency != 1 ) {
if (class_exists('FCC_Rates')) {
$supported_currencies = houzez_get_list_of_supported_currencies();
if (0 < count($supported_currencies)) {
$current_currency = houzez_get_wpc_current_currency();
echo '<li class="btn-price-lang btn-price">';
echo '<form id="houzez-currency-switcher-form" method="post" action="#" class="open">';
echo '<button id="houzez-selected-currency" class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span>' . $current_currency . '</span> <i class="fa fa-sort"></i></button>';
echo '<ul id="houzez-currency-switcher-list" class="dropdown-menu" aria-labelledby="dropdown" style="display:none;">';
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
if (data-currency-code='EUR') {
echo '<img src="images/euro-flag.png"';
if (data-currency-code='TR') {
echo '<img src="images/turkish-flag.png"';
}
}
echo '</ul>';
echo '<input type="hidden" id="houzez-switch-to-currency" name="houzez_switch_to_currency" value="' . $current_currency . '" />';
echo '<input type="hidden" id="currency_switch_security" name="nonce" value="' . wp_create_nonce('houzez_currency_converter_nonce') . '"/>';
echo '</form>';
echo '</li>';
}
}
}
?>
But it doesn't work? What I'm doing wrong?
This is the original code:
$currency_switcher_enable = houzez_option('currency_switcher_enable');
$is_multi_currency = houzez_option('multi_currency');
if( $currency_switcher_enable != 0 && $is_multi_currency != 1 ) {
if (class_exists('FCC_Rates')) {
$supported_currencies = houzez_get_list_of_supported_currencies();
if (0 < count($supported_currencies)) {
$current_currency = houzez_get_wpc_current_currency();
echo '<li class="btn-price-lang btn-price">';
echo '<form id="houzez-currency-switcher-form" method="post" action="#" class="open">';
echo '<button id="houzez-selected-currency" class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span>' . $current_currency . '</span> <i class="fa fa-sort"></i></button>';
echo '<ul id="houzez-currency-switcher-list" class="dropdown-menu" aria-labelledby="dropdown" style="display:none;">';
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
}
echo '</ul>';
echo '<input type="hidden" id="houzez-switch-to-currency" name="houzez_switch_to_currency" value="' . $current_currency . '" />';
echo '<input type="hidden" id="currency_switch_security" name="nonce" value="' . wp_create_nonce('houzez_currency_converter_nonce') . '"/>';
echo '</form>';
echo '</li>';
}
}
}
?>
You have many fails here:
The first is as #kerbh0lz mentioned, the "=" and "==" is not the same purpose. here yours is wrong.
Second your second currency If is in the 1st Currency If so it wont ever pass by.
Third you are comparing a string without quote or a var without $ before data-currency-code so instead you should use $currency_code
Try this:
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
if ($currency_code =='EUR')
echo '<img src="images/euro-flag.png"';
elseif ($currency_code == 'TR')
echo '<img src="images/turkish-flag.png"';
}

PHP display images dynamically in grid pattern

I'm new to php and I'm working with a tutorial about showing images dynamically on the page, it works fine but it shows them vertically and I would like them to be horizontal. I created a page with code to do that but I can't seem to figure out where to insert the code to get the images to show.
Thanks for any help.
Vertical output looks like this
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added ASC LIMIT 6");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="1000px" border="0" cellspacing="0" cellpadding="6" align="center">
<tr>
<td width="1000px" align="center"><img style="border:#666 0px solid;" src="images/' . $id . '.jpg" width="50%" height="50%" alt="' . $product_name . '" width="77" height="102" border="1" /></td>
<td width="83%" valign="top">' . $product_name . '<br />
$' . $price . '<br /> $' . $details . '<br />
order</td>
</tr>
</table>';
}
mysql_close();
?>
Grid Output
$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15");
$i = 0;
// Establish the output variable
$dynamiclist = '<table width="1000px" border="1" cellspacing="2" cellpadding="10" align="center">';
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$price = $row["price"];
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamiclist .= '<tr><td>' . $product_name . '</br>' . $details . '</br>' . $price . '</td>';
} else {
$dynamiclist .= '<td>' . $product_name . '</td>';
}
$i++;
}
$dynamiclist .= '</tr></table>';
?>
I got it figured out, Thanks for the help.
$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15");
$i = 0;
// Establish the output variable
$dynamiclist = "";
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$price = $row["price"];
if ($i % 4 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamiclist .= '<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
' . $details . '<br /> $' . $price . '<br />
order</td>';
} else {
$dynamiclist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
' . $details . '<br /> $' . $price . '<br />
order</td>';
}
$i++;
}
$dynamiclist .= '</tr></table>';
?>

Echoing PHP API Data In 3 Columns

I am getting website design ideas from a website API.
Currently my setup displays these designs in a table with only one column, I have over 40 items that need to be displayed in the table, but id prefer to display it in 3 columns. I have tried using i=0 ++ things etc but couldnt work out where to put it.
Here is my code so far:
<?php
$output = json_decode($output);
curl_close($ch);
echo '<table border="1">';
foreach($output as $template) {
echo '<tr>';
echo '<td>' . $template->template_name . '</td>';
echo '</tr>' . '<tr>';
echo '<td><img src="' . $template->thumbnail_url. '">' . '</td>';
echo '<tr>';
echo '<td><form method="GET" action=' . $_SERVER['PHP_SELF'] . '>';
echo '<input type="hidden" name="template_id" value="' . $template->template_id . '">';
echo '<input type="url" name="original_url" placeholder="Existing Site URL">'.'<br />';
echo '<input type="email" name="email" placeholder="Your e-mail" required>'.'<br />';
echo '<button type="submit">Choose Site</button>';
echo '</form></td>';
echo '</tr>';
}
echo '</table>';
}
?>
The output includes all of the website templates.
I am putting it into rows.The first row contains the name of the template, the second the picture of the website template and the third a form which I am working on.
My question is how can I nest the loop so that it does 3 columns, each row with 3 different designs?
Many thanks in advance
The common idea may be as the following, for example:
<?php
$array = [0, 1, 2, 3, 4, 5, 6, 7, 8];
$i = 1;
echo "<table>";
foreach ($array as $arr) {
if ($i % 3 === 0) {
echo "<tr class='class_3'>";
} else if ($i % 2 === 0) {
echo "<tr class='class_2'>";
} else {
echo "<tr class='class_1'>";
}
echo '<td>'.($i++).'</td>';
echo '<td>'.($i++).'</td>';
echo '<td>'.($i++).'</td>';
echo "</tr>";
//$i++; for 123 456
}
echo '</table>';
?>
Update: a more specific example:
<?php
$assoc_array = json_decode('
[
{"template_id": "1","template_name":"A", "preview_url":"url_1"},
{"template_id": "2","template_name":"B", "preview_url":"url_2"},
{"template_id": "3","template_name":"C", "preview_url":"url_3"},
{"template_id": "4","template_name":"D", "preview_url":"url_4"},
{"template_id": "5","template_name":"E", "preview_url":"url_5"},
{"template_id": "6","template_name":"F", "preview_url":"url_6"},
{"template_id": "7","template_name":"G", "preview_url":"url_7"},
{"template_id": "8","template_name":"H", "preview_url":"url_8"},
{"template_id": "9","template_name":"I", "preview_url":"url_9"},
{"template_id": "10","template_name":"J", "preview_url":"url_10"}
]
', true);
echo '<table>';
for($i = 0; $i < sizeof($assoc_array); $i++) {
if ($i % 3 === 0) {
echo "<tr class='class_3'>";
} else if ($i % 2 === 0) {
echo "<tr class='class_2'>";
} else {
echo "<tr class='class_1'>";
}
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
if(isset($assoc_array[++$i]['template_name'])) {
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
}
if(isset($assoc_array[++$i]['template_name'])) {
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
You'll get:
A B C
D E F
G H I
J
Thanks everyone who contributed. My answer is now finished.
The final code was:
<?php
$output = json_decode($output);
curl_close($ch);
$i = 0;
echo '<table border="1" width="80%">';
echo '<tr>';
foreach($output as $template) {
echo '<td><img src="' . $template->thumbnail_url. '" height="300" width="460">' .'<br />';
echo '<form method="GET" action=' . $_SERVER['PHP_SELF'] . '>';
echo '<input type="hidden" name="template_id" value="' . $template->template_id . '">';
echo '<input type="url" name="original_url" placeholder="Existing Site URL">'.'<br />';
echo '<input type="email" name="email" placeholder="Your e-mail" required>'.'<br />';
echo '<button type="submit">Choose Site</button>';
echo '</form></td>';
$i++;
if($i == 3) { // three items in a row. Edit this to get more or less items on a row
echo '</tr>'.'<tr>';
$i = 0;
}
}
echo '</tr>';
echo '</table>';
}
?>

How to add a pagination in a html table using PHP?

I have PHP snippet that displayed a result in the table row. I'm trying to a pagination at the end of the table with (1 2 3 4 ...10). The minimum row will be displayed every page is 15 only.
But my code is not working. Please help me
$results_per_page = 10;
$current = get_query_var('paged') ? get_query_var('paged') : 1;
$start = ($current - 1) * $rows_per_page;
$end = $start + $rows_per_page;
$end = (sizeof($rows) < $end) ? sizeof($rows) : $end;
if (isset($_POST['list_position']) && $_POST['list_position'] != 'Select by Position'){
$list_position= $_POST['list_position'];
$result_position= $wpdb->get_results($wpdb->prepare("SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, message FROM tablename WHERE position= '" . $list_position . "' ORDER BY position ASC", OBJECT));
echo '<table>';
echo '<tr>';
$dir="";
$file="";
$optionId = 0;
echo '<th>Submit Time</th>';
echo '<th>Last Name</th>';
echo '<th>First Name</th>';
echo '<th>Middle Name</th>';
echo '<th>Mobile Number</th>';
echo '<th>Email</th>';
echo '<th>Position</th>';
echo '<th>Message</th>';
echo '<th>Resume</th>';
echo '<th>Processed?</th>';
foreach ($result_position as $record_s){
$optionId++;
//$optionId = $record_s['id'];
for ($i=$start;$i < $end ;++$i ){
echo '<tr>';
echo '<td id="submit_time">' . $record_s->submit_time . '</td>';
echo '<td id="last_name">' . $record_s->last_name . '</td>';
echo '<td id="first_name">' . $record_s->first_name . '</td>';
echo '<td id="middle_name">' . $record_s->middle_name . '</td>';
echo '<td id="mobile_number">' . $record_s->mobile_number . '</td>';
echo '<td id="email">' . $record_s->email . '</td>';
echo '<td id="position">' . $record_s->position . '</td>';
echo '<td id="message">' . $record_s->message . '</td>';
echo '<td id="resumeFile'.$id.'">Download Resume</td>';
echo '<td id="radioOption><label for="Yes">Yes</label>
<input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption" value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
<label for="No">No</label>
<input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption" value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';
echo '</tr>';
}
}
echo '</table>';
}
jQuery pagination one another option if you want to try? https://datatables.net/examples/basic_init/zero_configuration.html Here is datatables link you can easily implements in php.

Create rows and columns based on COUNT query

Simple question I believe for anyone with minimal php skills (which I don't have sufficient amounts of haha)
$numrows = $retour['nb'] / 4;
echo $numrows;
echo "<table><tr>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
}
echo "</tr></table>";
}
How would I go about presenting a table that will hold 4 results(4 columns) per row, based on the value of $numrows?
Thank you!
Output tr tags inside while loop:
$count = 0;
echo "<table>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
if ($count % 4 == 0)
echo '<tr>';
$count++;
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
if ($count % 4 == 0)
echo '</tr>';
}
if ($count % 4 != 0)
{
// need to add missing td-s here
echo '</tr>';
}
echo "</table>";
$numrows = floor($retour['nb'] / 4);
echo $numrows;
$i=0;
echo "<table>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
if($i==4)
{
echo "<tr>";
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
echo "</tr>";
$i=0;
}
$i++;
}
while ($i<4)
{
echo '<td></td>';
$i++;
}
echo "</table>";
}
//if numrows used for # of rows then use following
$count=0;
while ($callback = mysql_fetch_assoc($queryLocations2) && $count<=$numrows)
{
if($i==4)
echo "<tr>";
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
if($i==4)
{
echo "</tr>";
$i=0;
$count++;
}
$i++;
}
while ($i<4)
{
echo '<td></td>';
$i++;
}
echo "</table>";
}

Categories