Kindly help me with to insert password into database using SHA 256/512 with salted in codeigniter framework. Find below code which is not exactly as per my understanding in SHA 256/541.
Javascript Code
if(document.frm_login.password.value!="")
{
var hash = CryptoJS.HmacSHA256(document.frm_login.password.value, "");
$('#passwords').text('');
document.frm_login.password.value=mixUpValues(hash.toString(),document.frm_login.keyy.value,document.frm_login.keyy1.value);
}
User Model
public function findMd5Value($mixValue)
{
$password = substr($mixValue, 0, 12);
$password.= substr($mixValue, 22, 10);
$password.= substr($mixValue, 37);
return $password;
}
User Controller
$dataform=$request->getPost();
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
$randomPassword=implode($pass); //turn the array into a string
$dataform['password']=$randomPassword;
Related
I am building a pharmacy management system. I have a condition where I need to build a section
As shown in the picture, I need to store the data where the upper three should be same for all of the rows inputted below.
The form data is submitted as in the below picture.
But the data when looped and saved is not being saved as desired. Only the last row of the data is being inserted and I am also confused to store supplier, purchase date and note since these data should be repeated as many as the number of rows are added.
PurchaseController.php
public function storePurchase(PurchaseStoreRequest $request)
{
$purchase = new Purchase();
$count = count($request->name);
// return response()->json($request->all());
for ($i = 0; $i < $count; $i++) {
$purchase->supplier = $request->supplier;
$purchase->purchase_date = $request->purchase_date;
$purchase->description = $request->description;
$purchase->category = $request->category[$i];
$purchase->name = $request->name[$i];
$purchase->generic_name = $request->generic_name[$i];
$purchase->batch_number = $request->batch_number[$i];
$purchase->company = $request->company[$i];
$purchase->strength = $request->strength[$i];
$purchase->expiry_date = $request->expiry_date[$i];
$purchase->quantity = $request->quantity[$i];
$purchase->selling_price = $request->selling_price[$i];
$purchase->purchase_price = $request->purchase_price[$i];
$purchase->save();
}
return response()->json(['message' => 'Purchase Saved Successfully']);
}
Can someone help me to store these three fields in the database repeating the number of rows submitted ?
Currently only the last row is being inserted into the database.
This might be an another way to accomplish what you're looking for.
$sharedKeys = ['supplier', 'purchase_date', 'description'];
$sharedData = $request->only($sharedKeys);
$multiKeys = ['category', 'name', 'generic_name', 'batch_number', 'company', 'strength', 'expiry_date', 'quantity', 'selling_price', 'purchase_price'];
$multiData = $request->only($multiKeys);
for ($i = 0; $i < count($request->name); $i++) {
$individualData = array_combine($multiKeys, array_column($multiData, $i));
Purchase::create($sharedData + $individualData);
}
For every loop, you need to create a new instance, then It will create a new record on each loop :
public function storePurchase(PurchaseStoreRequest $request)
{
// $purchase = new Purchase();
$count = count($request->name);
// return response()->json($request->all());
for ($i = 0; $i < $count; $i++) {
$purchase = new Purchase(); // create new instance end with (); on each loop
$purchase->supplier = $request->supplier;
$purchase->purchase_date = $request->purchase_date;
$purchase->description = $request->description;
$purchase->category = $request->category[$i];
$purchase->name = $request->name[$i];
$purchase->generic_name = $request->generic_name[$i];
$purchase->batch_number = $request->batch_number[$i];
$purchase->company = $request->company[$i];
$purchase->strength = $request->strength[$i];
$purchase->expiry_date = $request->expiry_date[$i];
$purchase->quantity = $request->quantity[$i];
$purchase->selling_price = $request->selling_price[$i];
$purchase->purchase_price = $request->purchase_price[$i];
$purchase->save();
}
return response()->json(['message' => 'Purchase Saved Successfully']);
}
on my array push i want to concat the 2 data if they have the same date
$goal = Goal::where('employee_id',Auth::user()->employees->first()->id)
->with('accomplishments')->orderBy('date','asc')->get();
$next_week = $goal->whereBetween('date',[$add_start_date,$add_end_date]);
$last_week = $goal->whereBetween('date',[$sub_start_date,$sub_end_date]);
$goals = [];
$date = "";
for ($i=0; $i < count($next_week); $i++) {
if($next_week[$i]['date']==$date){
$goals[$i-1]['activity'] = $goals[$i-1]['activity'] .', '. $next_week[$i]['activity'];
continue;
}
array_push($goals,$next_week[$i]);
$date = $next_week[$i]['date'];
}
when using filtering on laravel collections, indexes are lost,
to re_index the result array, use 'values':
$next_week = $goal->whereBetween('date',[$add_start_date,$add_end_date])->values();
$last_week = $goal->whereBetween('date',[$sub_start_date,$sub_end_date])->values();
I am getting an array from database table and through a for each statement. Then check the difference between two values if there is any difference that is less than zero sets a value to 0 for that loop. Then do the same with the next loop.
$bom_id = App\Models\Bom::where('item_id', $item->order_item->id)->pluck('id')->first();
if ($bom_id > 0) {
$bomList = App\Models\Bom_list::where('bom_id', $bom_id)->get();
echo '<div class="row">';
foreach ($bomList as $bomlist) {
$availableQty = $item->order_item->qty;
$requiredQty = $bomlist->qty * $item->qty;
$dif = $availableQty - $requiredQty;
}
$check = '';
$arr = array($bomlist->$dif);
$light = in_array($check, $arr, true) ? 0 : 1;
} else {
$light = 0;
}
enter image description here
I'm trying to develop an algorithm to create a symfony template service.
I want to check if a template exists in a subset of paths, ordered.
Given an array of parameter like this (already ordered like I want):
$params = ['O', 'U', 'W', 'P']
How can I output this array?
$urls = [
'O/U/W/P/template',
'O/U/W/template',
'O/U/P/template',
'O/U/template',
'O/W/P/template',
'O/W/template',
'O/P/template',
'O/template',
'U/W/P/template',
'U/W/template',
'U/P/template',
'U/template',
'W/P/template',
'W/template',
'P/template',
'template'
];
I can perform for a little list of parameters (like everyone can do it I suppose) with a code like this :
private function getPaths($template, $params)
{
$urls = [];
$alreadyPerform = [];
$paramsCounter = count($params);
for ($i = 0; $i < $paramsCounter; $i++) {
for ($j = 0; $j < $paramsCounter; $j++) {
if ($i !== $j && !in_array($params[$j], $alreadyPerform, true)) {
$urls[] = sprintf(
'/%s/%s/%s.html.twig', $params[$i], $params[$j], $template
);
}
}
$alreadyPerform[] = $params[$i];
$urls[] = sprintf('/%s/%s.html.twig', $params[$i], $template);
}
$urls[] = sprintf('%s.html.twig', $template);
return $urls;
}
This function work like I wanted until today (max 3 parameters), but I want to add one parameters today, maybe more after.
Thank you very much for your help !
Cheers.
Using recursion, you can do the following:
/**
* #param array $elements
* #param array $extra
*
* #return Generator
*/
function gen(array $elements, array $extra = []): \Generator {
foreach ($elements as $i => $head) {
foreach (gen(array_slice($elements, $i + 1), $extra) as $tail) {
yield array_merge([$head], $tail);
}
}
yield $extra;
}
demo: https://3v4l.org/gJB8q
Or without recursion:
/**
* #param array $elements
*
* #return Generator
*/
function gen2(array $elements): \Generator {
for ($num = count($elements), $i = pow(2, $num) - 1; $i >= 1; $i -= 2) {
$r = [];
for ($j = 0; $j < $num; $j += 1) {
if ($i & (1 << ($num - $j - 1))) {
$r[] = $elements[$j];
}
}
yield $r;
}
}
demo: https://3v4l.org/grKXo
Consider using the following package:
https://github.com/drupol/phpermutations
Just a very basic example of what it can do:
$permutations = new \drupol\phpermutations\Generators\Permutations(['A', 'B', 'C'], 2);
foreach ($permutations->generator() as $permutation) {
echo implode('/', $permutation);
echo "\n";
}
A/B
B/A
A/C
C/A
B/C
C/B
I'm using doctrine2 with Zend Framework. When you build the entites need to set the prefix of the classes, for example (Model_User). Is it possible?. The command I use is
./doctrine orm:generate-entities --generate-annotations=1 ../../../application/models/
I'm new to Doctrine orm but I made this:
class TablePrefix{
protected $prefix = '';
public function __construct($prefix){
$this->prefix = '';
for($i = 0; $i < strlen($prefix); $i++){ // table prefix like in entity name
if( $prefix[$i] == '_' && $i-1 > 0 ){
$prefix[$i-1] = strtoupper($prefix[$i-1]);
}
else{
$this->prefix .= $prefix[$i];
}
}
$this->prefix[0] = strtoupper($this->prefix[0]);
}
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs){
$classMetadata = $eventArgs->getClassMetadata();
$tmp = substr($classMetadata->name, 0, strlen($this->prefix));
if($tmp == $this->prefix ){
$classMetadata->name = substr($classMetadata->name, strlen($this->prefix));
}
}
}
next when you create entity manager make this:
$tablePrefix = new TablePrefix('tbl_');
$evm = $entityManager->getEventManager();
$evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePrefix);
on top of file add:use \Doctrine\ORM\Event\LoadClassMetadataEventArgs;
when you run cmd php vendor/bin/doctrine orm:generate-entities models model's name will be ok!
add these lines in your application.ini file.
doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = false
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "ModuleName_Model_"