Php voucher system - update codes - php

I have this voucher system, that works very well. I ust want to change the code to extract vouchers code and value from database.
cart.php Voucher code is :
$cartTotal = 100;
if (isset($_POST['disCode'])) {
$arr = file('flatfile1.txt');
$disCode = strtoupper(trim($_POST['disCode']));
$found = false;
- List item
foreach($arr as $str)
{
// Skip empty or invalid lines (assuming your file flatfile1.txt has blank lines).
if (strpos($str, '|') === false)
continue;
list($code,$amount,$valz) = explode('|',$str);
if ($disCode === trim($code))
{
// Code found.
$cartTotal = round($cartTotal/$amount );
$found = true;
break;
}
}
if (!$found)
$mesaj_eroare= "sorry,voucher code: $disCode is not good";
else
$mesaj_aplicare_voucher = "succes message here.";
}
And flatfile1.txt has :
TEST20% | 1.20 | 20%
TEST10% | 1.1 | 10%
TEST05% | 1.05 | 5%
test20% is the voucher code, 1.20 is the value of code, and 20% is for a custom message to apear to the user.
I tried to change flatfile1.txt to flatfile1.php where i retrive values from database.
Database have this format
TEST20% | 1.20 | 20%
TEST10% | 1.1 | 10%
TEST05% | 1.05 | 5%
And values are recognised in flatfile1.php but when i tried to apply them into cart.php to the voucher system are not recognised.
Need a solution to replace flatfile1.txt to flatfile1.php for automatization of vouchers.

Related

Showing only the rows for the exact match (CSV with PHP) and having a message just once for a while loop in case of no match

I have a CSV file with three columns (Names, CAS number, and storage) as seen here CSV file. I have been trying to search it for either a specific reagent name or CAS number, and show only rows 1 and 2. It is working, but when searching for a name that is also part of other reagents' names, it shows all the matches instead of displaying only the specific result for the exact searched word.
E.g. While searching for "1H-Indazole", it is shown as:
CAS: 271-44-3 | Rotavap Cabinet, compartment AM-134
CAS: 40598-94-5 | Freezer - Small flasks, compartment AM-176
CAS: 53857-57-1 | Rotavap Cabinet, compartment AM-177
I would like to see only the first one "CAS: 271-44-3 | Rotavap Cabinet, compartment AM-134" for "1H-Indazole". For "$searchfor = "271-44-3";", it works like a charm!
$ch = fopen("QHeTem2023.csv", "r");
$header_row = fgetcsv($ch);
$searchfor = "1H-Indazole";
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*$/mi";
while($row = fgetcsv($ch)) {
if (preg_match($pattern, implode('', $row))) {
echo '\<div\> CAS: '.$row\[1\].' | '.$row\[2\].'\</div\>';
}
}
Additionally, if no match is found, I would like to echo "This reagent is not registered in our storage!". Multiple message instances appear if else {echo "message";} is placed within the while loop.
How could I address these issues?

Steam API decode URL json

I am trying to make a "echo" prices for different objects I have got in an array that contains weapons:
I have read lots about how to get prices through through Steammarket through Steamapi:
(none of these sources did fit my requirements)
Sources:
Get steam item prices
How get a response of multiple price items on the market
Get the price of an item on Steam Community Market with PHP and Regex
I found finally a code snippet that works flawless
FILE marketprices.php
<?php
$items = array("Exalted Manifold Paradox","Kinetic Gem","Mercurial's Call");
foreach($items as $item)
{
$json = json_decode(file_get_contents("http://steamcommunity.com/market/priceoverview/?appid=570&market_hash_name=".rawurlencode($item)), true);
if($json["success"] == true OR !empty($json))
{
echo $item."'s lowest price is ".$json["lowest_price"]."";
}
elseif($json["success"] == false OR empty($json))
{
echo "Could not get data for ".$item;
}
}
output>
Exalted Manifold Paradox's lowest price is $28.49Kinetic Gem's lowest price is $50.00Mercurial's Call's lowest price is $0.16
Source:http://gamebanana.com/tuts/11942
When I am trying to implent this snippet to my code I get error in my result:
I have created an array that contains different weapons:
<?
foreach($S_W as $item) // Steam weapon
{
echo $item;
}
?>
output>
AWP | Worm God (Factory New)
FAMAS | Cyanospatter (Field-Tested)
G3SG1 | Green Apple (Factory New)
G3SG1 | Polar Camo (Field-Tested)
Glock-18 | Death Rattle (Field-Tested)
M249 | Gator Mesh (Field-Tested)
MAC-10 | Heat (Field-Tested)
This is good so far..
I get error in the result
Here is my code below:
foreach($S_W as $item)
{
$json = json_decode(file_get_contents("http://steamcommunity.com/market/priceoverview/?appid=570&market_hash_name=".rawurlencode($item)), true);
if($json["success"] == true OR !empty($json))
{
echo $item."'s lowest price is ".$json["lowest_price"]."";
}
elseif($json["success"] == false OR empty($json))
{
echo "Could not get data for ".$item;
}
}
I receive following result:
I see a part of the error..
in the call in the end of the URL the code adds:</br>
Could someone help me with a solution?
Thank you in advance
Best regards
Daniel
Priceoverview 'API' endpoint is rate-limited by Steam, and was even further rate-limited by Valve very recently. You are getting 429 HTTP Status code, which stands for 'Too many requests', you are sending requests to their site way too often.

php multi dimensional array's

I am pulling data out of a table that has these fields:
zipcode,city,state,county
My question is that some zip codes are in multiple cities, and some cities are in multiple zip codes... so is there a way to have all the data put into an array, then group all of the zip codes in order, then build a table out of that, so that it puts them in order, but if the data is redundant, like the zip, city, state and county are already in the array, then it skips it?
so then I can print it to the browser using echo, into a table like this:
74801 | Shawnee, Oklahoma | Pottawatomie
74801 | Bethel, Oklahoma | Pottawatomie
74801 | Johnson, Oklahoma | Pottawatomie
74851 | McLoud, Oklahoma | Pottawatomie
74851 | Dale, Oklahoma | Pottawatomie
etc.
But in the case of these:
74015 | CATOOSA, Oklahoma | Rogers
74015 | COTOOSA, Oklahoma | Rogers
Because those are duplicates in those fields (not in the rest of the table), I need it to skip showing it twice.
I think it is something like this:
$zipArray = array();
$zipCode = $dbhhandle->zip;
$cityName = $dbhhandle->city;
$countyName = $dbhhandle->county;
if($zipArray[$zipCode][$cityName] != $countyName) {
$zipArray[$zipCode][$cityName] = $countyName;
}
but I'm not sure if that is the right way to do it.
Then once I have the array built, how do I build the table?
Your code looks pretty close, but it's missing the state. So change the elements of the array into associative arrays with state and county elements.
Also, you need to check whether there's an entry for the zip code at all, before checking whether it's a duplicate, otherwise you'll access an undefined index.
$zipArray = array();
$zipCode = $dbhhandle->zip;
$cityName = $dbhhandle->city;
$countyName = $dbhhandle->county;
$stateName = $dbhandle->state;
if(!isset($zipArray[$zipCode][$cityName]) || $zipArray[$zipCode][$cityName]['county'] != $countyName) {
$zipArray[$zipCode][$cityName] = array('county' => $countyName, 'state' => $stateName);
}
To display the table, you use nested loops.
foreach ($zipArray as $zip => $cities) {
foreach ($cities as $city => $cityData) {
echo "<tr><td>$zip</td><td>$city, {$cityData['state']}</td><td>{$cityData['county']}</td></tr>";
}
}

Render XSL file to HTML using PHP

I need to read a excel (xls) file and render it to a HTML page.
I'm developing in PHP, using Symfony 2.6.5 and I want to have access to the excel cel data, in this way I can generate HTML in a Symfony Controller(or better in a a Template).
I'm using ExcelBundle and I've read the documentation, but I found only ways to set information and not to get.
Which is the best we to read the content of the excel?
PS: the excel is a price list in a tabular form like the follow:
+––––+––––––+–––––––––––––+–––––––+
| id | name | description | price |
|____|______|_____________|_______|
+––––+––––––+–––––––––––––+–––––––+
| 1 | foo | foo desc | 12€ |
+––––+––––––+–––––––––––––+–––––––+
| 2 | bar | abr desc | 65€ |
+––––+––––––+–––––––––––––+–––––––+
|... |... | ... | ... |
In according with the code source, simply pass the filename in the createPHPExcelObject method.
As Example:
public function xlsAction()
{
$filenames = "your-file-name";
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($filenames);
foreach ($phpExcelObject ->getWorksheetIterator() as $worksheet) {
echo 'Worksheet - ' , $worksheet->getTitle();
foreach ($worksheet->getRowIterator() as $row) {
echo ' Row number - ' , $row->getRowIndex();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
echo ' Cell - ' , $cell->getCoordinate() , ' - ' , $cell->getCalculatedValue();
}
}
}
}
}
Simply pass the array to your view for rendering in the template.
Hope this help

getting child id's from database dynamically?

I try to get the child id's of products dynamically.Below is my table structure.
parent|child
---------------------
44 | 35,6,47,5,50
---------------------
47 | 8,9
---------------------
50 | 12, 15
am going to pass only one parent id and get the child ids, and if anyone of child ids having again child, then i have to fetch that record also.example 44->35,6,47,5,50 in this 47 and 50 is having child ids, so my final output should be like this 44-> 35,6,47,8,9,5,50,12,15.
I tried below this,
$sql=mysql_fetch_assoc(mysql_query("select * from chain_product where parent='44'"));
$parent=$sql['parent'];
$child=$sql['child'];
$ex=explode(",",$child);
$count=sizeof($ex);
for($i=0;$i<$count;$i++)
{
$list=add_child($ex[$i],$child);
$check=explode(",",$list);
$chck_count=sizeof($check);
if($chck_count>$count)
{
$exit=add_child($ex[$i],$list);
print_r($exit);
}
}
function add_child($main,$ch)
{
$find=mysql_query("select * from chain_product where parent='$main'");
$res=mysql_fetch_assoc($find);
if($res)
{
$replace=$main.",".$res['child'];
$alter=str_replace($main,$replace,$ch);
echo $alter;
}
}
but i get the result like this,
35,6,47,8,9,5,5035,6,47,5,50,12,15
but i need output should be like this..
35,6,47,8,9,5,50,12,15.
can anyone help me to do this..
Your database structure isnt optimal for this, this would be better:
id | parent
1 | 0
2 | 1
3 | 1
4 | 2
5 | 2
This way you can do something recursive:
function getChilds($parent=0, $depth=0){
// Select the items for the given $parent
$query = $conn->mysqli_query("SELECT id WHERE parent=".$parent); // mysqli is better, but mysql will do fine
// get the items by the parent giving as input:
while($fetch = $query->fetch_assoc() ){
echo str_repeat('-', $depth) . " ".$fetch['id'];
getChilds($fetch['id'], $depth+1); // Use id of this line to find its childs
echo "<br />";
}
}
getChilds(0); // And start it. The 0 is optional, I personaly prefer -1. Whatever rows your boat
This is called a tree structure and should give something like this:
1
- 2
- - 4
- - 5
- 3
In this example I use an echo for display purposes, you can return the values via an array, same principle
To answer a bit better, your current structure could support a similar method, but because you use strings, it will be allow slower and alot less flexible. You can see the difference in the code you are using, and the amount I just used. If you would remove the echo's and only return arrays, it will be even smaller :)

Categories