How to pass more arguments to that function? - php

I have this function:
function mobio_checkcode($servID, $code, $debug=0) {
$res_lines = file("http://www.mobio.bg/code/checkcode.php?servID=$servID&code=$code");
$ret = 0;
if($res_lines) {
if(strstr("PAYBG=OK", $res_lines[0])) {
$ret = 1;
}else{
if($debug)
echo $line."\n";
}
}else{
if($debug)
echo "Unable to connect to mobio.bg server.\n";
$ret = 0;
}
return $ret;
}
And here how i use it:
if(mobio_checkcode($servID, $code, 0) == 1) {
echo "Code is valid!!";
}
$code = $_REQUEST['code'];
$servID = 29;
$post = $_REQUEST['post'];
Here i have form! In this form u enter code and display is valid or no so i want to pass more $code like
$code1 = $_REQUEST['code1'];
$code2 = $_REQUEST['code2'];
$code3 = $_REQUEST['code3'];
I want to pass more variables to function how it will be done.. Please help me thank u <3

Just pass all of variables as array like
$array = ['servID'=>29,'code'=>XXXX];
function checkcode($array) {
//Do stuff
$array['servID']
$array['code']
}

function mobio_checkcode($servID, $code, $debug=0, $element1, $element2) {
$res_lines = file("http://www.mobio.bg/code/checkcode.php?servID=$servID&code=$code");
$ret = 0;
if($res_lines) {
if(strstr("PAYBG=OK", $res_lines[0])) {
$ret = 1;
}else{
if($debug)
echo $line."\n";
}
}else{
if($debug)
echo "Unable to connect to mobio.bg server.\n";
$ret = 0;
}
return $ret;
//you can pass extra elements so on.. otherwise create an array of elements and pass only array to function

Related

How to save multiple results in the array with for loop php

I'm working with a CSV file. when the user uploads the file. I parse the CSV then select the data from the array that I need. after that, I'm running a for loop to validate that data and saving the results in the array. but the problem is when I print the results array there's only result for 1 email and there are 4 emails. any suggestions?
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$result = validate_email($email);
$results['Email'] = $email;
if ($result) {
$results['Result'] = 'valid';
$valid_emails++;
} else {
$results['Result'] = 'invalid';
$invalid_emails++;
}
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
Use $results[] to add one or more elements :
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$result = validate_email($email);
$res['Email'] = $email;
if ($result) {
$res['Result'] = 'valid';
$valid_emails++;
} else {
$res['Result'] = 'invalid';
$invalid_emails++;
}
$results[] = $res;
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
You are overriding results every time in your loop, try this
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$rowResult=[];
$result = validate_email($email);
$rowResult['Email'] = $email;
if ($result) {
$rowResult['Result'] = 'valid';
$valid_emails++;
} else {
$rowResult['Result'] = 'invalid';
$invalid_emails++;
}
$results[]=$rowResult;
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
$results contains only the last email validation.
You should store multiple results, and not only the last ;-)
Something like :
$results[$i]['Email'] = $email;
if ($result) {
$results[$i]['Result'] = 'valid';
$valid_emails++;
} else {
$results[$i]['Result'] = 'invalid';
$invalid_emails++;
}
if ($result) {
$results[$i] = 'valid';
$valid_emails++;
} else {
$results[$i] = 'invalid';
$invalid_emails++;
}

How to use return value in PHP

Im sending a mail from one function to another like this
sendMail($email)
if($result == 1) {
return redirect('/')->with("msg", $response);
} else {
return redirect('/')->with("msg", $badResponse);
}
function sendMail($email) {
//...
if($mail->send){
$result = 1;
} else {
$result = 2;
}
echo $result;
}
How do i get the value of $result and use it after the function call?
You need to return the result and save it to a variable, rather than just echoing it:
$result = sendMail($email);
if($result == 1) {
return redirect('/')->with("msg", $response);
} else {
return redirect('/')->with("msg", $badResponse);
}
function sendMail($email) {
//...
if($mail->send){
$result = 1;
} else {
$result = 2;
}
return $result;
}

Unable to edit while using trim in function

I am designing an application and in my modal came across a strange error where i am unable to edit the entry
Below is the modal where its showing the error lies :
<?php
include_once dirname(dirname(dirname(__FILE__))) . "/const.php";
include_once PHP_PATH . "/config1.php";
include_once CONFIG_PATH.'/modal/routemgmt/route_mgmt.php';
function sanitize($input) {
if (is_array($input))
return array_map('sanitize', $input);
else
return htmlspecialchars(trim($input));
}
// Sanitize all the incoming data
$sanitized = array_map('sanitize', $_POST);
$reason = $sanitized['reason'];
if($reason == "insert"){
$staffs = [];
$stops = [];
$name = $sanitized['rname'];
$code = $sanitized['rcode'];
$desc = $sanitized['rdesc'];
$vnum = $sanitized['vnum'];
$stf = $_POST['staff'];
$st = isset($_POST['stops'])? $_POST['stops']: [];
$st = [];
// foreach($staffs as $staff){
// $stf[] = array_map('sanitize', $staff);
// }
// if(isset($stops)){
// foreach($stops as $stop){
// $st[] = array_map('sanitize', $stop);
// }
// }
$val = insertRoute($conn,$name, $code, $desc, $vnum, $stf, $stops);
echo $val;
}
if($reason == "view"){
$id = $sanitized['id'];
$val = [];
$val = viewRoute($conn,$id);
echo json_encode($val);
}
if($reason == "edit"){
$stf = [];
$stp = [];
$id = $sanitized['pkid'];
$name = $sanitized['rname'];
$code = $sanitized['rcode'];
$desc = $sanitized['rdesc'];
$vnum = $sanitized['vnum'];
$estaffs = $_POST['estaff'];
$estops = $_POST['estops'];
$edel = $_POST['del'];
foreach($estaffs as $val){
$stf[] = array_map('sanitize', $val);
}
foreach($estops as $val){
$stp[] = array_map('sanitize', $val);
}
$cnt = 0;$n_stp = [];
for($i = 0; $i<sizeof($stp); $i++){
if($stp[$i]['stat'] != "Exist"){
$n_stp[$cnt] = $stp[$i];
$cnt++;
}
}
$val = editValues($conn,$id, $name, $code, $desc, $vnum, $stf, $n_stp, $edel);
echo $val;
}
if($reason == "delRoute"){
$id = $sanitized['id'];
$val = delRoute($conn,$id);
echo $val;
}
I tried changing the function to :
function sanitize($input) {
return htmlspecialchars(trim($input));
}
But then it started giving me the below error
<b>Warning</b>: trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\gurukul\demo2\controller\routemgmt\route_mgmt.php</b> on line <b>7</b><br />
As far as I can understand its passing an array instead of string in trim.
Can someone please guide me how can I resolve this ? Tried few debugging steps but didnt get succeded
In this function you are returning array in your if statement when it should just be a string all the time.
Replace:
function sanitize($input) {
if (is_array($input))
return array_map('sanitize', $input);
else
return htmlspecialchars(trim($input));
}
With:
function sanitize($input) {
if (is_array($input))
return sanitize($input);
else
return htmlspecialchars(trim($input));
}
You need to return value as string not array. Don't mess with the $_POST one.
Replace all:
array_map('sanitize', $val);
With:
sanitize($val);

How to count Array based on statement?

I use validate php function to filter badword. And my problem is this script can't count badword in a statement that I've input. How to count badword in a statement..?
For example: You're badword1 and they badword2.
It's supposed to be 2 badword in that sentence.
PHP
function validasi($string,$banned_words) {
foreach($banned_words as $banned_word) {
if(stristr($string,$banned_word)){
return false;
}
}
return true;
}
$banned_words = array('badword1','badword2','badword3','badword4','badword5','badword6','badword7');
$teks = $_POST['teks'];
if (!validasi($teks,$banned_words)) {
echo count(!validasi($teks,$banned_words));
echo 'blocked!';
}else{
echo 'Text valid';
}
HTML
<form action="validasi.php" method="POST">
<input type="text" name="teks">
<input type="submit" value="Validasi">
</form>
Output
1 !blocked.
Expected Result
2 !blocked
Your script also don't count extra the words if they are using the same word multiple times in a string. Below here is a script i should use.
function getBadWords(){
$db_con = new PDO('dsn', 'use', 'pass'); // You PDO connection
$query = $db_con->prepare("SELECT * FROm tb_bannedwords");
$query->execute();
$return = array();
while($row=$query->fetch(PDO::FETCH_OBJ)) {
$data = $return[] = $row->banned_words;
}
return $return;
}
function validate($string){
$banned_words = getBadWords();
$count = 0;
foreach($banned_words as $banned_word){
$wordCount = substr_count($string, $banned_word);
if($wordCount > 0){
$count = $count + $wordCount;
}
}
return $count;
}
$teks = 'You\'re badword1 and they badword2 with badword1';
if(validate($teks) > 0){
echo validate($teks) . ' blocked!';
}else{
echo 'Text valid';
}
do this
function validasi($string,$banned_words) {
$badWordCount = 0;
foreach($banned_words as $banned_word) {
if(stristr($string,$banned_word)){
$badWordCount++;
}
}
return $badWordCount;
}
$banned_words = array('badword1','badword2','badword3','badword4','badword5','badword6','badword7');
$teks = $_POST['teks'];
$badWordsCount2 = validasi($teks,$banned_words);
if ($badWordsCount2 != 0) {
echo $badWordsCount2;
echo 'blocked!';
}else{
echo 'Text valid';
}

Why does this PHP function cycle endlessly?

function nonrecgen($min, $max, $amount) {
for($i=0;$i<$amount;$i++) {
$NrArray[$i] = rand($min,$max);
echo $NrArray[$i];
do {
for($j=0;$j<=$i;$j++) {
if ($NrArray[$j] == $NrArray[$i]) {
$NrArray[$i] = rand($min,$max); }
}
$Reccuring = false;
if ($i > 0) {
for($k=0;$k<=$i;$k++) {
if ($NrArray[$k] == $NrArray[$i]) {
$Reccuring = true; }
}
}
}
while ($Reccuring = true);
}
Return $NrArray;
}
$Test = nonrecgen(0,1,2);
print_r($Test);
I wanted to look into how to generate an array of nonreccuring numbers and while this is certainly not the most efficient way I believe, I can't seem to figure out why it loops endlessly on the first iteration. I tried logical analysis over and over, but there has to be something I'm missing.
do {
...
} while ($Reccuring = true);
Because your while statement sets $Reccuring to true, instead of evaluating it.
Try:
do {
...
} while ($Reccuring === true);
Other than the = to == you were also resetting the $Recurring in the wrong place:
<?
function nonrecgen($min, $max, $amount)
{
for($i=0;$i<$amount;$i++)
{
$NrArray[$i] = rand($min,$max);
do
{
for($j=0;$j<=$i;$j++)
{
if ($NrArray[$j] == $NrArray[$i])
{
$NrArray[$i] = rand($min,$max);
}
}
if ($i > 0)
{
for($k=0;$k<=$i;$k++)
{
if ($NrArray[$k] == $NrArray[$i])
{
$Reccuring = true;
}
}
}
$Reccuring = false;
}
while ($Reccuring == true);
}
return $NrArray;
}
$Test = nonrecgen(0,2,5);
echo "<pre>";
print_r($Test);
?>
You're currently assigning a value rather than checking (which will always be true).
Change it to: while ($Reccuring == true);

Categories