Hi there in my database I have 3 columns, is_contract, is_permenant and is_temporary. Within these columns there is either a Y or N value.
I am using these columns to echo onto the page what kind of work someone is looking for, my problem is that the user can be looking for more than one type of work, I am currently running 3 if statements to determine what to echo to the page, however I am struggling to add a comma if more than one of the statemnts returns as true, below is my code so far,
<?php
if($rslt['is_contract'] == 'Y') {
echo "Contract ";
}
if($rslt['is_permanent'] == 'Y') {
echo "Permanent ";
}
if($rslt['is_temporary'] == 'Y') {
echo "Temporary";
}
?>
<?php
$out=array();
if($rslt['is_contract'] == 'Y') $out[]="Contract";
if($rslt['is_permanent'] == 'Y') $out[]="Permanent";
if($rslt['is_temporary'] == 'Y') $out[]="Temporary";
echo implode(", ",$out);
?>
Either you can use like this in simple way
if($rslt['is_contract'] == 'Y') {
echo "Contract ";
}
if($rslt['is_permanent'] == 'Y') {
if($rslt['is_contract'] == 'Y') {
echo ", ";
}
echo "Permanent ";
if($rslt['is_temporary'] == 'Y') {
echo ", ";
}
}
if($rslt['is_temporary'] == 'Y') {
if($rslt['is_contract'] == 'Y' && $rslt['is_permanent'] != 'Y') {
echo ", ";
}
echo "Temporary";
}
Related
I have database table with users. There is one column called fkhouseid which can be (1, 2, 3, 4, 5) it connects user to the house which leads to another table with house names.
I am trying to print each user in separate column for each house. Here is first query to get users:
$numbers = '12345';
$unityResult = getUnityEmail();
Then get number of houses:
$houseResult = getHouses();
$count = mysqli_num_rows($houseResult);
There are 5 houses in total. Then I am trying to print names for each house:
for ($x=0;$x<$count;$x++){
echo "<div id='memberList$x' style='display:inline-block;vertical-align:top'>";
echo "<fieldset class=fieldname style='display:inline'>";
if( $numbers[$x] == 1){ echo "<legend class=name_legend>u</legend>"; }
else if( $numbers[$x] == 2){ echo "<legend class=name_legend>n</legend>"; }
else if( $numbers[$x] == 3){ echo "<legend class=name_legend>i</legend>"; }
else if( $numbers[$x] == 4){ echo "<legend class=name_legend>t</legend>"; }
else{ echo "<legend class=name_legend>y</legend>"; }
echo "<ul class='list'>";
while ($row_unity = mysqli_fetch_array($unityResult)) {
if ($row_unity['fkhouseid'] == $numbers[$x]) {
format_member_list_filters( $row_unity);
}
}
echo "</ul>";
echo "</fieldset>";
echo "<br>";
echo "</div>";
mysqli_data_seek($houseResult, 0);
}
I am to get houses(each letter for each house) but I am not able to get names according their houses:
How I can make it display names for other columns too?
The problem was easy to solve.
mysqli_data_seek($houseResult, 0);
Changed to
mysqli_data_seek($unityResult, 0);
<?php
$sql1 = "SELECT rmType,dateCi, SUM(rmNum) as total FROM reserve GROUP BY rmType,dateCi";
$result = mysqli_query($conn,$sql1);
while ($row1 = mysqli_fetch_assoc($result)) {
if ($row1['dateCi'] == $_SESSION['checkIn']){
if ($row1['rmType'] == 'sBed'){
if ($row1['total'] >= 10) {
echo "<script>alert('Single Bed Room is Full Occupied.')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else {
echo "";
}
}
if ($row1['rmType'] == 'tSBed'){
if ($row1['total'] >= 10) {
echo "<script>alert('Two Single Bed Room is Full Occupied'></script>";
echo "<script>window.open('index.php','_self')</script>";
}
else {
echo "";
}
}
if ($row1['rmType'] == 'dBed'){
if ($row1['total'] >= 5) {
echo "<script>alert('Double Bed Room is Full Occupied')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else {
echo "";
}
}
if ($row1['rmType'] == 'fBed'){
if ($row1['total'] >= 12) {
echo "<script>alert('Four-Bed Domitary Room is Full Occupied'></script>";
echo "<script>window.open('index.php','_self')</script>";
}
else {
echo "";
}
}
}
if ($row1['dateCi'] == $_SESSION['checkOut']){
if ($row1['rmType'] == 'sBed'){
if ($row1['total'] >= 10) {
echo "<script>alert('Single Bed Room is Full Occupied.')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else {
echo "";
}
}
if ($row1['rmType'] == 'tSBed'){
if ($row1['total'] >= 10) {
echo "<script>alert('Two Single Bed Room is Full Occupied'></script>";
echo "<script>window.open('index.php','_self')</script>";
}
else {
echo "";
}
}
if ($row1['rmType'] == 'dBed'){
if ($row1['total'] >= 5) {
echo "<script>alert('Double Bed Room is Full Occupied'></script>";
echo "<script>window.open('index.php','_self')</script>";
}
else {
echo "";
}
}
if ($row1['rmType'] == 'fBed'){
if ($row1['total'] >= 12) {
echo "<script>alert('Four-Bed Domitary Room is Full Occupied'></script>";
echo "<script>window.open('index.php','_self')</script>";
}
else {
echo "";
}
}
}
}
?>
<?php
$query = "INSERT INTO `reserve` (`userId`,`userName`,`userEmail`,`rmType`,`rmNum`,`guest`,`dateCi`,`dateCo`,`cost`) VALUES('".$userRow['userId']."','".$userRow['userName']."','".$userRow['userEmail']."','".$roomtype."','".$roomNum."','".$guest."','".$checkIn."','".$checkOut."','".$pay."')";
$res1 = mysqli_query($conn,$query);
if($res1) {
$update = "UPDATE users SET costPay='$pay1' WHERE userId=".$_SESSION['user'];
$run_up = mysqli_query($conn,$update);
if($run_up){
echo "<script>alert('This process reservation has successful.')</script>";
echo "<script>window.open('index.php','_self')</script>";
}}
else
{
echo "Error: " . $res . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
From the coding shown is explain about checking the room number, if it reach the max then it will echo the script that room full and open new window to homepage.
if it do not reach the max then it will store those value into table.
but the problem i met now is even the room is full, it still store those value into the table.
May someone help me please?
You are (wrongly) assuming that echo outputs the contents straight away in all cases. It doesn't. Instead it builds up a buffer to output when:
the buffer reaches a certain size, or
when the script stops executing, or
when the buffer is flushed to force an output
As you build the output and don't have any controls around the insert statement, it will always hit that insert statement, which is what it is currently doing.
Fortunately there's a few ways around this:
put an exit; after each block of echo'd script (that will terminate the script and cause the output to happen)
set a variable after the echo parts and check for that before inserting (or not inserting)
Change the control flow so instead of if ... if ... if ... you make use of if...elseif...else, where the else houses the insert statement which will then only run if none of the other conditions matched
There are other ways of doing the same action, but the up-shot is, the script will try not to output anything until it has completed, and part of that includes the insert.
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 . '">';
The following validates only one text box (txt1). If I have a series of text boxes which lies in side a table (which is dynamically populated) how do I assign that entire values of the text boxes to one single session?
Is it possible if at all?
Or else can you suggest me a way out?
<?php
if(isset($_POST['sendtwo']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['loged'] = $_POST['txt1'];}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{if(isset($_SESSION['loged'])) echo $_SESSION['loged']; echo "<table border='1'><tr><td>form 2 is here!</rd></tr>";
?>
//try this
<?php
if(isset($_POST['sendtwo']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!';} else {$_SESSION['loged'] = $_POST['txt1'];}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{
if(isset($_SESSION['loged'])) {
echo $_SESSION['loged'];
echo " border='1'>form 2 is here!";
}
}
?>
Something like this? I wasn't sure what you meant:
<?php
if(isset($_POST['sendtwo']))
{
if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;}
else{
foreach($_POST as $Key => $Posted)
$_SESSION[$Key] = $Posted;
}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{if(isset($_SESSION['loged'])) echo $_SESSION['loged']; echo "<table border='1'><tr><td>form 2 is here!</rd></tr>";
?>
Please have a quick look at the code below:
<?php
$pagexfoot = $_GET[page_id];
?>
<?php
if ($pagexfoot == '5' OR !isset($_GET['page_id'])) {
echo 'Hello';
} else {
echo 'Bye';
}
?>
So, if the user is on index.php?page_id=5 then it will echo "Hello" and it will echo "Bye" anywhere else. Now, how do I echo "Hello" on page index.php?page_id=5 and index.php and echo "Bye" on all other pages? Who can solve this puzzle...
<?php
if(isset($_GET['page_id']) && $_GET['page_id'] != 5)
{
echo 'Bye';
}
else
{
echo 'Hello';
}
?>
<?php
if (!isset($_GET['page_id']) || $_GET['page_id'] == 5) {
echo 'Hello';
} else {
echo 'Bye';
}
We are using the || operator to check if either it isn't set or the value is 5, if so tell "Hello" and else, Bye.
$pagexfoot = $_GET[page_id];
if($pagexfoot != '5' || isset($_GET['page_id'])) { echo 'Bye'; } elseif($pagexfoot == '5') { echo 'Hello'; }
You code loks fine to me by try the above code :)