Display username according to number - php

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);

Related

The value still store in database even the number of room is max number

<?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.

Display current category name

Thanks in advance.
I am trying to display the name of the current category which I'm in. Nothing that I've been trying for 4 hours now has worked..
My code:
if($catList) {
$c = 0;
while($category = $catList->fetch(PDO::FETCH_ASSOC)){
$margintop = 38*$c;
if($category['id'] == $thiscategory) {
echo "<a href='".$link."&category=".$category['id']."'>
<div class='category_name'>".strip_tags($category['name']). "</div></a></div>";
} else {
echo "<a href='".$link."&category=".$category['id']."'>
<div class='category_name'>".strip_tags($category['name']). "</div></a></div>";
}
$c++;
}
} else {
echo "No categories to show.";
}
what I've tried to get the current category name, in a different area in the page:
if($category['id'] == $thiscategory) {
echo ".strip_tags($category[name])." ;
}
which gives a blank result.
You need:
Compare with $category (instead of $thiscategory), because you set in link &category=
You need to show some difference if value is selected
if($catList) {
$c = 0;
while($myCategory = $catList->fetch(PDO::FETCH_ASSOC)){
$margintop = 38*$c;
if($myCategory['id'] == $category) {
echo "<a href='".$link."&category=".$myCategory['id']."' class=\"active\">
<div class='category_name'><strong>".strip_tags($myCategory['name']). "</strong></div></a></div>";
} else {
echo "<a href='".$link."&category=".$myCategory['id']."'>
<div class='category_name'>".strip_tags($myCategory['name']). "</div></a></div>";
}
$c++;
}
} else {
echo "No categories to show.";
}
Hey you may try single_cat_title()
Source: https://developer.wordpress.org/reference/functions/single_cat_title/

How to display php script output in column wise based on the match in if statement

I have the following PHP code which will print the server names with different color codes likes Green-Ok,Yellow-warning,Red-critical,Orange-unknown.
foreach ($status->members_with_state as $server) {
if ($server[1] == 0) {
$state = "OK";
echo "Server: <font color=\"green\">$server[0]</font><br />";
} elseif ($server[1] == 2) {
$state = "WARNING";
echo "Server: <font color=\"yellow\">$server[0]</font><br />";
} elseif ($server[1] == 1) {
$state = "CRITICAL";
echo "Server: <font color=\"red\">$server[0]</font><br />";
} elseif ($server[1] == 3) {
$state = "UNKNOWN";
echo "Server: <font color=\"orange\">$server[0]</font><br />";
}
}
ISSUE:
All the servers are printed in one big list in a single column.
Could you please suggest required modifications in code to get the server names column wise like in attached screenshotenter image description here

Trouble with retrieving data from mysql with HTML select box

Okay so I am developing a search engine for a non-profit group using MYSQL and PHP. The data appears fine from all the other fields. But when I try to get data from a drop down select box the data doesn't appear.
I know my code is pron to SQL injections however there isn't any important data kept on the server, it just info about treatments and research studies.
I would like to do it with a function. I was also wondering if there is anyway to retrieve data from multiple boxes. Like I have a keyword search and a drop down combo box both of them are set. How do I retrieve data from both columns in the database using the function I have? Thanks
Here is my code if you guys could help me that would be great thanks
The code for the html box
Broad Research Topic <select name="Treatment1">
<option value="Other">
Other
</option>
<option value="Treatment">
Treatment
</option>
<option value="PracticalCure">
Practical Cure
</option>
The php code to check if it is set
if (isset($_GET['Treatment1']) && in_array($_GET(['Treatment1']),
array('Treatment', 'PracticalCure', 'Other')) {
$state = $_GET['Treatment1'];
Investigator6($state); }
}
The Function Investigator6
function Investigator6($state)
{
$state = trim($state);
$state = preg_replace('/\s+/', ' ', $state);
//seperate multiple keywords into array space delimited
$keywords = explode(" ", $state);
//Clean empty arrays so they don't get every row as result
$keywords = array_diff($keywords, array(
""
));
//Set the MySQL query
if ($state == NULL or $state == '%') {
} else {
for ($i = 0; $i < count($keywords); $i++) {
$query = ("SELECT * FROM Studies1 WHERE BroadResearchTopic LIKE
' %$keywords[$i]%'");
}
//Store the results in a variable or die if query fails
$result = mysql_query($query) or die(mysql_error());
}
if ($state == NULL or $state == '%') {
} else {
//Count the rows retrived
$count = mysql_num_rows($result);
echo $count;
}
//If search variable is null do nothing, else print it.
if ($state == NULL) {
} else {
echo "You searched for <b><FONT COLOR=\"blue\">";
foreach ($keywords as $value) {
print "$value ";
}
echo "</font></b>";
}
echo "<p> </p><br />";
echo "</center>";
//If users doesn't enter anything into search box tell them to.
if ($state == NULL) {
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.
b</font></b><br /></center>";
} elseif ($state == '%') {
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.
</font></b><br /></center>";
//If no results are returned print it
} elseif ($count <= 0) {
echo "<center><b><FONT COLOR=\"red\">Your query returned no results from the
database.</font></b><br /></center>";
//ELSE print the data in a table
} else {
//Table header
echo "<center>";
echo "</center>";
//Colors for alternation of row color on results table
$color1 = "#d5d5d5";
$color2 = "#e5e5e5";
//While there are rows, print it.
while ($row = mysql_fetch_array($result)) {
//Row color alternates for each row
$row_color = ($row_count % 2) ? $color1 : $color2;
//table background color = row_color variable
echo "<td style = \"padding: 10px\">" . $row['BroadResearchTopic'] . "</td>";
$row_count++;
if ($state == NULL or $state == '%') {
} else {
//clear memory
mysql_free_result($result);
}
}

Having Trouble with an odd/even script

I'm trying to make a script that outputs user names based on weather they are assigned an odd or even value. I think I've managed to get the odd ones working but the even ones wont output. Here is what it looks like. The 'commentid' is the value which determines if they are to be assigned to odd or even.
<?php
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query2 = "SELECT * FROM comments";
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_assoc($result2))
if ( $row2['commentid'] % 2 )
{
echo $row2['name'];
echo "<br />";
}
elseif (!$row2['commentid'] % 2)
{
echo $row2['name'];
}
else
{
echo "";
}
?>
I think it has something to do with either the $row2['name'] or (!$row2['commentid'] % 2) or maybe I need to assign rows to strings or something but I can't figure out what I'm doing wrong.
EDIT I really should have made it more clear exactly what I am trying to do with this script because while your answers make sense they dont solve my problem. On the same page as this script I have another script running. This script calls a value from a different table in the same database. It's running on a timed cron event that resets daily so the value is different every day. It looks like this and is working as it should.
<?
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
?>
What I am trying to do is only call up the names associated with either the odd or even value based on the number the script above dictates. So if this script chooses Result 1, I only want to display the user names who would fall under Result 1.
It is sufficient to do a:
if ($row2['commentid'] % 2)
{
// odd
}
else
{
// even
}
If you want to know whats wrong with your logic, you should learn something about operator precedence in PHP. According to the chart, ! has higher precendence than %. The expression !$row2['commentid'] % 2 is thus evaluated as follows:
when $row2['commentid'] is 0
!0 = true
true % 2 = 1
when $row2['commentid'] is 1
!1 = false
false % 2 = 0
when $row2['commentid'] is 2
!2 = false
false % 2 = 0
Hope this helps
if ( $row2['commentid'] % 2 ==0 )
{
echo $row2['name'];
echo "<br />";
}
else
{
echo $row2['name'];
}
if ( $row2['commentid'] % 2 )
{
echo $row2['name'];
echo "<br />";
}
elseif (!$row2['commentid'] % 2)
{
echo $row2['name'];
}
else
{
echo "";
}
replace this code with
if ( $row2['commentid'] % 2 )
{
echo $row2['name'];
echo "<br />";
}
else
{
echo $row2['name'];
}
since there is only two posibility either it commentid divisible by 2 or not divisible by 2
There is a problem. If you delete a comment, you will get 2 adiacent rows with/without <br>. You should use a an iterator variable, like this:
$i=1;
while ($row2 = mysql_fetch_assoc($result2)){
echo $row2['name'];
if($i % 2 == 0){
echo "<br />";
}
$i++;
}

Categories