I have a series of if codes that need to be put together but the code acts weird and chooses the last if code.
if($boardid='1'){ $altboard='13' and $title='Archives' ;}
elseif($boardid='2'){ $altboard='14' and $title='Archives' ;}
so this code turns out to work, but it cant be repeated to finish the rest of the codes
the code that doesn't work is:
if($boardid=='1'){ $altboard='13' and $title='Archives' ;}
if($boardid='2'){ $altboard='14' and $title='Archives' ;}
if($boardid='3'){ $altboard='15' and $title='Archives' ;}
if($boardid='6'){ $altboard='16' and $title='Archives' ;}
if($boardid='7'){ $altboard='17' and $title='Archives' ;}
and there is more if codes to add, but i didn't add them, as it only uses whatever turns out to be the last of the lines regardless of boards, even though it clearly recognizes the boards. so what gives and how do i make a series of if codes work together? I have another file with the exact same thing and it reacts the same. If then elseif for the rest of the code doesn't work and neither does alternating if and else if.
putting two equal signs Works fine with The above mentioned code but not with the second mentioned code which is this
if($wrong=='0'){$prize='1000';}
if($wrong=='1'){$prize='700';}
if($wrong=='2'){$prize='500';}
if($wrong=='4'){$prize='200';}
if($wrong=='5'){$prize='100';}
Unlike the last one where it keeps only the last code this one takes only the first one. so the prize always turns out to be 1000. any help here
You’re using = rather than == in your if conditions, which, supposing the value you assign is truthy, will always be true. Change your conditions to use == for comparison.
That said, you might want to consider using an associative array:
$data = array(
'1' => array('altboard' => '13', 'title' => 'Archives'),
'2' => array('altboard' => '14', 'title' => 'Archives'),
// ...
);
if(array_key_exists($data, $boardid)) {
$altboard = $data[$boardid]['altboard'];
$title = $data[$boardid]['title'];
}
Or, if the title is always “Archives”, pull that out:
$altboard_map = array('1' => '13', '2' => '14', /* ... */);
if(array_key_exists($data, $boardid)) {
$title = "Archives"; // consider moving out of the 'if' if applicable
$altboard = $altboard_map[$boardid]['altboard'];
}
should be like
if($boardid=='1'){ $altboard='13'; $title='Archives';}
if($boardid=='2'){ $altboard='14'; $title='Archives';}
if($boardid=='3'){ $altboard='15'; $title='Archives';}
if($boardid=='6'){ $altboard='16'; $title='Archives';}
if($boardid=='7'){ $altboard='17'; $title='Archives';}
Use switch case :
$title='Archives';
switch ($boardid) {
case 1:
$altboard='13';
break;
case 2:
$altboard='14';
break;
case 3:
$altboard='15';
break;
case 6:
$altboard='16';
break;
case 7:
$altboard='17';
break;
}
Related
I need to display site visitor's continent. I am using geoip functionality to pull 2 letter code, which I can get to work very easily. However, just displaying 'AS' for Asia to the site visitor is clearly undesirable. So I'm trying to convert that. This is what I am trying to do. . .
<?php
$continent = geoip_continent_code_by_name($_SERVER['REMOTE_ADDR']);
$africa = array('AF');
$antarctica = array('AN');
$asia = array('AS');
$europe = array('EU');
$northamerica = array('NA');
$oceania = array('OC');
$southamerica = array('SA');
if ($africa) {
echo 'Africa';
}
elseif ($antarctica) {
echo 'Antarctica';
}
elseif ($asia) {
echo 'Asia';
}
elseif ($europe) {
echo 'Europe';
}
elseif ($northamerica) {
echo 'North America';
}
elseif ($oceania) {
echo 'Australia and Oceania';
}
elseif ($southamerica) {
echo 'South America';
}
else {
echo '';
}
?>
It just displays 'Africa' no matter where the site visitor, so it's getting stuck there. I'm reading though PHP tutorials but can't figure out what I'm doing wrong. My if, else and elsifs look good as far as I can tell.
Well, you're kind of going about it wrong.
You're getting the code correctly with your first line, but then you are creating a bunch of different arrays, each containing one of the possible codes. Then you are checking for the existence of each array, and since you just created them, they do exist, so your IF block will stop with the first check and output Africa.
What you want to do is create one array that contains all the codes as keys, with the output name as values, and then just use the code you got from $_SERVER to return the matching value from that array. (I got the list of codes from the manual)
$continent = geoip_continent_code_by_name($_SERVER['REMOTE_ADDR']);
$continents = [
'AF' => 'Africa',
'AN' => 'Antarctica',
'AS' => 'Asia',
'EU' => 'Europe',
'NA' => 'North America',
'OC' => 'Oceania',
'SA' => 'South America',
];
echo $continents[$continent];
It's worth a little explanation for WHY your IF block functions the way it does. PHP is loosely typed, meaning that you don't have to explicitly set the type of variables. So when PHP encounters a variable it has to guess about how to use it. So when you say if($africa), php tries to make sense of $africa in the context of a boolean question, and since it DOES exist and is not 0 or false, it sees it as TRUE and executes the first block.
I cannot seem to get this working. I am trying to modify a very old script (of our late technician and close friend) which we use to be used for searching servers with which OS being used. This consists (as far as I can tell) out PHP and SMARTY.
I already tried escaping the content with slashes and using backticks. But it simply does not work. I really have no idea where to look. Below are the original two pieces of code (I couldn't find more parts for it, or I have overlooked).
Piece of PHP code from the 1st PHP-file:
$values['osname'] = array( '-' => 'no choice',
'5' => 'CentOS 5.x',
'6' => 'CentOS 6.x',
'7' => 'CentOS 7.x',
);
Piece of code from the 2nd PHP-file:
$osname = intval(Common::GPvar('osname'));
$_SESSION['form']['serverselect']['osname'] = $osname;
if ($osname != '-') { $where .= " AND dsh.sumup LIKE '%OS: CentOS ".$osname."%'"; }
This is being used in a search form, so when I select "CentOS 6.x" it will display all servers which have the text OS: CentOS 6.10 in it.
Now what I am trying to achieve is to make the following to work:
$values['osname'] = array( '-' => 'no choice',
'CentOS 5' => 'CentOS 5.x',
'CentOS 6' => 'CentOS 6.x',
'CentOS 7' => 'CentOS 7.x',
'Virtuozzo 7' => 'Virtuozzo 7.x',
);
I tried to escape the content, as I mentioned above, however that didn't work. So I am guessing the coding in the 2nd PHP-file also needs some adjusting. So I tried removing certain stuff, like "CentOS", "%" and several other things. But it does not work. The result is that, on a search, I am getting all servers being displayed (no matter what OS is on them).
I guess I did correctly on escaping the variables in the array, but the 2nd piece of coding is not compatible for some reason with the requested search input?
Anyone has an idea what I am doing wrong here?
You can remove intval(...) because intval will turn strings into integers
https://php.net/manual/en/function.intval.php
$osname = Common::GPvar('osname');
The whole code
$osname = Common::GPvar('osname');
$_SESSION['form']['serverselect']['osname'] = $osname;
if ($osname != '-') { $where .= " AND dsh.sumup LIKE '".$osname."%'"; }
I have a random content script that has worked perfectly but now seems to have a glitch.
It's the "Spotlight On:" story on the upper lefthand corner at http://fiction.deslea.com/index2.php and the code is as follows:
$storyspotlights = array("bluevial", "biophilia", "real", "edgeofreality",
"limitsofperception", "markofcain", "spokenfor", "closer",
"feildelm", "purgatory", "elemental");
$randomstoryID = array_rand($storyspotlights);
$randomstory = $storyspotlights[$randomstoryID];
switch ($randomstory) {
case ($randomstory == 'closer'):
$storyspotlightheader = "<div class='storyspotlightheader'>Closer</div>";
$storyspotlighttext = "snip";
//some stories snipped
case ($randomstory == 'bluevial'):
$storyspotlightheader = "<div class='storyspotlightheader'>The Blue
Vial</div>";
$storyspotlighttext = "snip";
break;
//more stories snipped
}
print($storyspotlightheader);
print($storyspotlighttext);
My problem is - all the stories from Blue Vial to Spoken For appear when you refresh the page, in random order (although Blue Vial seems to stick a fair bit). These were the stories in the script originally.
Since then I have added the last four to the array and the content generation switch case fragment, but these last four stories never, ever appear in the randomiser. I've literally sat and refreshed for hours. I've confirmed over and over that the updated script is on the server, and even deleted and re-uploaded it.
I did try unset and also $storyspotlights = array() at the beginning of the script at various stages of troubleshooting, but to no avail. I also tried moving the new stories to the start of the array - no change there either.
What am I missing?
It's surprising this works at all. That's not how you use switch..case.
switch (<value to compare>) {
case <value to compare against>:
...
}
That means you write this:
switch ($randomstory) {
case 'closer':
...
}
With what you've written it's actually executing like:
if ($randomstory == ($randomstory == 'closer')) ...
Also make sure you have not actually forgotten some break statements, which would make the code fall through to the next case and indeed make certain cases "more sticky" than others.
Also, I'd simplify the whole thing to this:
$stories = array(
array('header' => '...', 'text' => '...'),
array('header' => '...', 'text' => '...'),
...
);
$story = $stories[array_rand($stories)];
echo $story['header'];
echo $story['text'];
I am facing a really strange problem which i am debugging from past 2 hours but unable to find the solution. Before explaining the problem, let me show the code
My Controller Function is
$this->load->library('datatables');
$actionLinkBar = $this->load->view("content/updates/dt_files/action_bar", array(), TRUE);
$this->datatables
->select("id, name, status")
->where('id', $this->session_data['user_id'])
->from("t_user")
->add_column("action", $actionLinkBar, 'id, name, status');
echo $this->datatables->generate();
And the code in my action_bar view is
<?php
$status_rec = '$3';
var_dump($status_rec); // STRANGE OUTPUT - string(2) "1"
?>
<div class="action_bar" data-update-id="<?php echo '$1'; ?>">
<?php if ($status_rec == '1') { ?> // HENCE COMPARISON ALWAYS FAILS
<span>Present</span>
<?php }else { ?>
<span>Absent</span>
<?php } ?>
</div>
Now explaining the problem.. I am using Datatables with Codeigniter. I have a view template action_bar which will be displayed in one of the columns of datable in front end. The view has if/else condition based on value of status field from DB. If status feild value = 1 = Present. Else it is Absent. But though the $status_rec has value as '1' it still fails in comparison. Strange thing is on var_dumping $status_rec, i found that though it has proper value, the length is weird(2) though its single int. I even tried trimming etc but still no effect. Maybe that's why the comparison is failing. Your help is really needed :/
P.S - The DB feild that holds this value is int with length 1
I also had a similar problem solved it a bit by changing the library:
Datatables.php with if condition
changes in the 194, 196 and 440 strings
How to use:
$if = array('0' => array('if_condition'=>'$3', 'if_condition_eqv'=>'1', 'if_true'=>'<span>Present</span></div>', 'if_false'=>'<span>Absent</span></div>'));
$this->datatables->add_column("action", '<div class="action_bar" data-update-id="$1">', 'id, name, status');
BUT you have to change: ".=" On "=" (strings 497 and 504)
Should work.
I would really appreciate if someone will correct code
P.S. Sorry for my english
I am creating a 3D Secure PHP Project. I am having a rather bizzare issue in that the "MD" code is going missing when re-submitting the Array of data
My code is as follows :
$paRes = $_REQUEST['PaRes'];
$md = $_REQUEST['MD'];
require "payment_method_3d.php";
x_load('cart','crypt','order','payment','tests');
/*
* For Debugging Purposes
* Only.
echo "The Value Of PaRes is : ";
echo $paRes;
*/
$soapClient = new SoapClient("https://www.secpay.com/java-bin/services/SECCardService?wsdl");
$params = array (
'mid' => '',
'vpn_pswd' => '',
'trans_id' => 'TRAN0095', // Transaction ID MUST match what was sent in payment_cc_new file
'md' => $md,
'paRes' => $paRes,
'options' => ''
);
It seems that the $_REQUEST['MD'] string seems to go missing AFTER the soap call. Although I am having difficulty print this out to the screen. The strange thing is the $paRes variable works without issue.
Any ideas why this would be the case?
Check your case. PHP array keys are case sensitive. From this little bit of code it looks as if the request variable may be 'md' instead of 'MD'.
Try $md = $_REQUEST['md'];
PHP array statements are case sensitive, so this should work:....
$md = $_REQUEST['md'];
Thanks for your responses guys.
What was happening was the include page was sitting in front of the request methods and causing issues loading the REQUEST methods to the page.