I am trying to return a specific iframe URL depending ont he input of a specific number of zip codes.
Example- zip code x returns url x
zip code y returns url y
I have a list of several zip codes per URL. The URL purpose is to redirect to a specific (3rd party) page based on the location input from the user.
Here is what I have so far:
<?php
$userzip = $_POST['ZipCode'];
echo $userzip;
$array = array(
'22942' => 'URL1',
'22701' => 'URL2');
// print_r($array);
foreach( $array as $key => $value ){
// echo "Output of Key=>Value pair:\r\n";
// echo $key . "->" . $value . "\r\n";
// echo "Testing $key...\r\n\r\n";
if(preg_match('/23456/',$key)){
echo "Service exists in: $value\r\n";
break;
} else {
echo "No Match for $key.\r\n\r\n";
}
}
?>
So, my first mistake is that only the zip code entered is returned for the moment. I can comment that out but left it in to show my thinking. Help?
If I have understood your request correctly then this code should work:
<?php
$userzip = $_POST['ZipCode'];
echo $userzip;
$array = array(
'22942' => 'URL1',
'22701' => 'URL2');
// print_r($array);
foreach( $array as $key => $value ){
// echo "Output of Key=>Value pair:\r\n";
// echo $key . "->" . $value . "\r\n";
// echo "Testing $key...\r\n\r\n";
if(strstr($key, $userzip)) {
echo "Service exists in: $value\r\n";
break;
} else {
echo "No Match for $key.\r\n\r\n";
}
}
?>
To actually handle the redirect then after echo "Service exists in: $value\r\n"; you can use one of these options:
header("Location: " . $value);
Or if you want to use the iFrame approach you have mentioned in the comment then:
echo '<iframe src="'.$value.'" border="0" frameborder="0" style="width:500px; height:700px;"></iframe>';
You should try to avoid loops as much as possible. You can easily check if a key/value exists in an array with isset:
$userzip = $_POST['ZipCode'];
echo $userzip;
if( isset($zipcodesA[$userzip]) ){
echo "Service exists in A: ".$array[$userzip]."\n";
} elseif( isset($zipcodesB[$userzip]) ){
echo "Service exists in B: ".$array[$userzip]."\n";
}elseif( isset($zipcodesC[$userzip]) ){
echo "Service exists in C: ".$array[$userzip]."\n";
} else {
echo "No Match for $userzip.\n\n";
}
In this case, you have no need to check every value in the array, just if one specific exists.
Related
I want to display a specific value from key value list..
here is my code:
if (isset($_POST) && count($_POST)>0 )
{
foreach($_POST as $paramName => $paramValue) {
echo "<br/>" . $paramName . " = " . $paramValue;
}
}
ouput
ORDERID = ORDS3700373
TXNAMOUNT = 200.00
CURRENCY = INR
TXNID = 32221284
BANKTXNID = 475815
STATUS = TXN_SUCCESS
RESPCODE = 01
RESPMSG = Txn Successful.
TXNDATE = 2017-01-10 18:13:25.0
GATEWAYNAME = WALLET
BANKNAME =
PAYMENTMODE = PPI
CHECKSUMHASH =
here I want to display only ORDERID and TXNID.. How do I get that value?
You can easily access post values by it's field name instead of looping through all post elements. Simply access that elements directly as below:
if(isset($_POST['ORDERID'])) {
echo 'ORDERID = '.$_POST['ORDERID'];
}
if(isset($_POST['TXNID'])) {
echo 'TXNID= '.$_POST['TXNID'];
}
Moving comments to an answer.
You do not need to loop post it is just a global array. You can access the values at any of the keys like any associative array because that is what it is. Likewise these value can be used like any other
if(isset($_POST['ORDERID'])){
$orderid = $_POST['ORDERID'];
}
if(isset($_POST['TXNID'])){
$txnid = $_POST['TXNID'];
}
// Should use htmlspecialchars() or htmlentities() here
// but didn't want to confuse OP. It is for security.
echo "ORDERID is: " . $orderid . " and TXNID is: " . $txnid;
A note for security never trust user input and sanitize all $_POST variables before echoing or persisting. There are far better article out on the internet than I can summarize here.
You can use if condition in the loop like this
if (isset($_POST) && count($_POST)>0 )
{
foreach($_POST as $paramName => $paramValue) {
if($paramName == 'ORDERID' || $paramName == 'TXNID')
echo "<br/>" . $paramName . " = " . $paramValue;
}
}
add an if like
if($paramName == "ORDERID" || $paramName == "TXNID") {
after foreach, remeber to close it after echo statement line
Don't overcomplicate a trivial task with a loop.
Just drop the loop and echo the two values directly:
// Assuming the two values are expected to come in pair:
if(isset($_POST['ORDERID']) && isset($_POST['TXNID'])) {
echo "<br/>ORDERID = " . $_POST['ORDERID'];
echo "<br/>TXNID = " . $_POST['TXNID'];
}
If you insist on having a loop, then you can go through the property names which you need
foreach(array('ORDERID', 'TXNID') as $paramName) {
if(isset($_POST[$paramName])) {
echo "<br/>" . $paramName . " = " . $_POST[$paramName];
}
}
I am try to create a condition where condition should check the presence of locality or zip code in Multidimensional array, if any one condition is satisfied, its should return true or else false
here is the code i tried with a array
$cars = array("locationabc","locationdef","locationdghi");
$locality = "locationabc";
if ( in_array($locality, $cars) ) {
echo "you can proceed service to your location";
}else {
echo "sorry we don't provide service to your location";
}
but how to do that in multidimensional array, i don''t know whether the below condition is right or wrong
$cars = array
(
array("locationabc",500001),
array("locationdef",500002),
array("locationdghi",500003)
);
$locality = "locationabc";
if ( array_key_exists($locality, $cars) ) {
echo "you can proceed service to your location";
}else {
echo "sorry we don't provide service to your location";
}
I assume that you have multidimensional array as per the example you shown. You can do it like this
$cars = array
(
array("locationabc",500001),
array("locationdef",500002),
array("locationdghi",500003)
);
$locality = "locationabc";
foreach($cars as $car){
if ( in_array($locality, $car) ) {
$msg = "you can proceed service to your location";
break;
}else {
$msg = "sorry we don't provide service to your location";
}
}
echo $msg;
You can use following example to check whether value exist or not-
$flag = 0;
foreach($cars as $c)
{
if(in_array($locality,$c))
{
$flag = 1;
break;
}
}
if($flag == 1)
echo 'value exist';
else
echo 'value does not exist';
Hello I have a simple script
This is my script, and try this script
<?php
$user_id = $_REQUEST['user_id'];
$pid = $_REQUEST['pid'];
$nopr = $_REQUEST['nopr'];
$tglpr = $_REQUEST['tglpr'];
$uraianpr = $_REQUEST['uraianpr'];
$jenispr = $_REQUEST['jenispr'];
$nilaipr = $_REQUEST['nilaipr'];
$costproject = $_REQUEST['costproject'];
$remarkpr = $_REQUEST['remarkpr'];
$tmpattachid = $_REQUEST['tmpattachid'];
if ($user_id==NULL)
{
$error "User Id Not Complete";
}
else if ($pid==NULL)
{
$error= "PID Not Complete";
}
echo $error;
?>
I want if $user_id or other variables is empty/null
i confused when other variable have empty/null data
i must typing code many if statement :(
example : 5 variables ($pid,$nopr,$tglpr,$jenispr,$costproject) is empty/null
this show error like this
PID Not Complete
NoPR Not Complete
Tgl Pr Not Complete
Jenis Pr Not Complete
Cost Project Not Complete
Help Me, Thank's.
I won't give you the full answer, as stated in comments, this is a basic basic concept you need to learn, but I will show you the template.
You want to use a FOREACH loop to check each value in the array. See http://php.net/manual/en/control-structures.foreach.php
And Set it out like this:
foreach($_REQUEST as $key=>$row){
if (is_null($row) || empty($row)){
$errorText .= "You have no data in your ".$key."<br>";
}
}
unset($key,$row);
Then you can output the $errorText value telling people which rows should be fixed. Easy.
Loop through the $_REQUEST and find the empty or null key.
CODE :
foreach ($_REQUEST as $key => $value) {
if(trim($value) ==='' || is_null($value))
{
echo $key . " is not complete" . "<br/>";
}
}
My foreach loop:
$empty = "You cannot decommission this truck. It is in use."
$msg = "";
foreach ($trucks_to_remove as $truck_id) {
$truck_id = trim($truck_id);
if (strlen($truck_id)) {
$msg .= decom_truck(
$db,
$depot_id,
$truck_id
);
}
else
{
echo $empty;
}
}
echo $msg;
If the condition is not met, it means the sql cursor that preceded this foreach loop did not return a result for this truck_id (meaning it is in use), I just want a simple message sent to the page to alert the user.
It does not have to be a pop-up or the like. Where would I put my write or print statement? I assume I'd use an else clause but when I add an else inside the foreach loop (see above), I get nothing printed to the page. I have also tried the following:
else if (strlen($truck_id)) == 0
{
echo $empty;
}
I am very new to php.
$arr1 = array();
if(!$arr1)
{
echo 'arr1 is emty<br/>';
$arr2 = array(1, 2);
if($arr2)
echo 'arr2 has data';
exit();
}
I have the following three possible urls..
www.mydomain.com/445/loggedin/?status=empty
www.mydomain.com/445/loggedin/?status=complete
www.mydomain.com/445/loggedin/
The www.mydomain.com/445 part is dynamically generated and is different each time so I can't do an exact match, how can i detect the following...
If $url contains loggedin but DOES NOT CONTAIN either /?status=empty OR /?status=complete
Everything I try fails as no matter what it will always detect the logged in part..
if(strpos($referrer, '?status=empty')) {
echo 'The status is empty';
}
elseif(strpos($referrer, '?status=complete')) {
echo 'The status is complete';
}
elseif(strpos($referrer, '/loggedin/')) {
echo 'The status is loggedin';
}
Slice up the URL into segments
$path = explode('/',$referrer);
$path = array_slice($path,1);
Then just use your logic on that array, the first URL you included would return this:
Array ( [0] => 445 [1] => loggedin [2] => ?status=empty )
You could do something like this:
$referrer = 'www.mydomain.com/445/loggedin/?status=empty';
// turn the referrer into an array, delimited by the /
$url = explode('/', $referrer);
// the statuses we check against as an array
$statuses = array('?status=complete', '?status=empty');
// If "loggedin" is found in the url, and count the array_intersect matches, if the matches = 0, none of the statuses you specified where found
if( in_array('loggedin', $url) && count(array_intersect($url, $statuses)) == 0 )
{
echo 'The user is logged in';
}
// if the complete status exists in the url
else if( in_array('?status=complete', $url) )
{
echo 'The status is complete';
}
// if the empty status exists in the url
else if( in_array('?status=empty', $url) )
{
echo 'The status is empty';
}
I would recommend looking at array_intersect, it is quite useful.
Hope it helps, not sure if this is the best way of doing it, but might spark your imagination.
Strpos is probably not what you want to use for this. You could do it with stristr:
if($test_str = stristr($referrer, '/loggedin/'))
{
if(stristr($test_str, '?status=empty'))
{
echo 'empty';
}
elseif (stristr($test_str, '?status=complete'))
{
echo 'complete';
} else {
echo 'logged in';
}
}
But it's probably easier/better to do it with regular expressions:
if(preg_match('/\/loggedin\/(\?status=(.+))?$/', $referrer, $match))
{
if(count($match)==2) echo "The status is ".$match[2];
else echo "The status is logged in";
}