If else won't accept class - php

Im trying to make my language switcher change font weight depending on the language url but i cannot get it to accept the class
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langlight">Svenska</div>';
}
else
{
echo '<div class="langbold">Svenska</div>';
};
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langbold"><a href="http://xyz.com/">English</div>';
}
else
{
echo '<div class="langlight"><a href="xyz.com/">English</div>';
};

Remove semicolons, missing closing tags:
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langlight">Svenska</div>';
}
else
{
echo '<div class="langbold">Svenska</div>';
}
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langbold">English</div>';
}
else
{
echo '<div class="langlight">English</div>';
}

Related

Combo box 2 is not showing the values in output (php)

the cmbYL values are not showing in my output but the cmbCourse is working just fine, thank you in advance. This is the CmbYL code. I can't paste my whole table code because stack overflow is limiting me.
this is the output:
<?php
$course = "";
if (isset($_SESSION['ses_cmbCourse'])) {
if ($_SESSION['ses_cmbCourse']=='BSIT') {
echo "BS Information Technology";
}
elseif ($_SESSION['ses_cmbCourse']=='BSCS') {
echo "BS Computer Science";
}
elseif ($_SESSION['ses_cmbCourse']=='BSECE') {
echo "BS Electronics Engineering";
}
elseif ($_SESSION['ses_cmbCourse']=='BSID') {
echo "BS Interior Design";
}
elseif ($_SESSION['ses_cmbCourse']=='BSHRM') {
echo "BS Hotel and Restaurant Management";
}
else{
echo "BS Tourism Management";
}
}
?>
<?php
$year = "";
if (isset($_SESSION['ses_cmbYL'])) {
if ($_SESSION['ses_cmbYL']=='1st') {
echo "1st yr";
}
elseif ($_SESSION['ses_cmbYL']=='2nd') {
echo "2nd";
}
elseif ($_SESSION['ses_cmbYL']=='3rd') {
echo "BS Electronics Engineering";
}
elseif ($_SESSION['ses_cmbYL']=='4th') {
echo "4th";
}
else{
echo "5th yr";
}
}
?>

How may I write foreach for that sql query to show messages sender&receiver

I want to show messages as receiver on right and sender on left here's my query below:
<?php
$mesajsor=$db->prepare("SELECT * FROM mesaj where kullanici_gon=:send_id and kullanici_gel=:receiver_id
order by mesaj_zaman ASC");
$mesajsor->execute(array(
'send_id' => $_GET['kullanici_gon'],
'receiver_id' => $_GET['kullanici_gel']
));
$say=0;
while($mesajcek=$mesajsor->fetch(PDO::FETCH_ASSOC)) {
$say++;?>
How may I implement the foreach to that query?
foreach($messages as $message){
if($message->kullanici_gel == receiver_id){
<div class="right">$message</div>
}elseif($message->kullanici_gon == send_id){
<div class="left">$message</div>
}
}
You probably want to do this in two separate loops, like this:
echo '<div class="right">';
foreach($messages as $message){
if($message->kullanici_gel == $receiver_id) {
echo '<div class="message">$message</div>';
}
}
echo '</div><div class="left">';
foreach($messages as $message){
if($message->kullanici_gon == $send_id) {
echo '<div class="message">$message</div>';
}
}
echo '</div>';
To prevent writing code twice you could use functions. For instance:
function displayMessage($message)
{
echo '<div class="message">$message</div>';
}
echo '<div class="right">';
foreach($messages as $message){
if($message->kullanici_gel == $receiver_id) {
displayMessage($message);
}
}
echo '</div><div class="left">';
foreach($messages as $message){
if($message->kullanici_gon == $send_id) {
displayMessage($message);
}
}
echo '</div>';
Especially because displaying a message is probably more complicated than you let us believe.

If-else condition gives me errors in PHP

I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>

How can i shorten this PHP code? (if - elseif)

I have this simple if else function and i wonder how i could shorten this?
<?php
if( $count_bananas == '1' )
{ echo '<div class="stage-columns-1">'; }
elseif ($count_bananas == '2')
{ echo '<div class="stage-columns-2">'; }
elseif ($count_bananas == '3')
{ echo '<div class="stage-columns-3">'; }
elseif ($count_bananas == '4')
{ echo '<div class="stage-columns-4">'; }
?>
The code works but i wonder if there is a way to code this shorter and more elegant?
You really didn't see the pattern?
echo '<div class="stage-columns-' . $count_bananas . '">';
It can be done without condition checks. For the code snippet you are posted, if the values are same then -
echo '<div class="stage-columns-' . $count_bananas . '">';

PHP if - else - else statement

<html>
<input type='text' name='mobile phone' value='
<?php if (strpos($phone_number, '07') === 0) {
echo $phone_number;
} else {
echo $alt_phone;
}?>'
</html>
Works fine. I would like to combine the above with:
<?php if (!empty($alt_phone)) {
echo $alt_phone;
} else {
echo '07777777777';
}?>'`
I have tried ELSEIF with the new condition, and a completely separate <?php ?> section and both times I get a blank page, instead of a textbox with a telephone number in it.
I am trying to achieve this: If $phone_number is a mobile, enter this number, otherwise enter the alt_phone, unless $alt_phone is blank, then enter '07777777777'.
try
<?php
if (!empty($phone_number)) {
echo $phone_number;
}
elseif(!empty($alt_phone))
{
echo $alt_phone;
}
else{
echo '07777777777';
}
?>'`
This will do the trick
<?php
if (strpos($phone_number, '07') === 0) {
echo $phone_number;
}
else if (!empty($alt_phone)) {
echo $alt_phone;
}
else {
echo '07777777777';
}
?>

Categories