Drop Down Year Showing Last Year Twice - php

I am working on creating a drop down button that lets one select the year from a list of options in order to sort through a data table. The code (reproduced below) keeps repeating the last year available, I can't seem to figure out why it is doing this. I am using PHP to pull from a MySql database.
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Select Year</button>
<div id="myDropdown" class="dropdown-content">
<?php
$year = array();
while($subject = mysqli_fetch_assoc($subject_set)) {
$i=0;
$exists = 0;
$n = count($year);
while($i < $n){
if ($subject['year'] == $year[$i]){
$exists = 1;
}
$i++;
}
if($exists == 0){
$year[$n] = $subject['year'];
echo " {$subject['year']} ";
}
}
$subject_set = find_all_subjects();
?>
</div>
</div>

Related

Create Divs for each group of values

I need to group all rows with matching roomcode. When I also group resort, it acts like it disables the grouping of roomcode and only gives me the results of the first row with a roomcode.
How can I group the resort column and still group matching roomcode columns? If I ungroup my resort code the query works correctly, but then I can't create separate divs for differing resorts.
The user is searching a date range, hence having multiple rows with the same roomcode, which fetches my points. This is why I am getting the sum of those points. My current way of grouping each resort for output HTML purposes is breaking that. I don't necessarily have to group the resorts, I just need each room at each resort created within its own DIV set. I have a basic example below.
Ideal HTML output
<div id="results">
<div id="resort resortcode">
<div id="roomresults">
<div id="room1"></div>
<div id="room2"></div>
</div>
</div>
<div id="resort2 resortcode2">
<div id="roomresults2">
<div id="room3"></div>
<div id="room4"></div>
</div>
</div>
</div>
This PHP code will group each set of results by resorts, but jacks up my price.
<?php
include("dbh.php");
mysqli_select_db($con, "checkavail") or die ("no database");
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$occupants = $_POST['occupants'];
$sqlCommand=
"SELECT
MIN(staydate) AS checkin,
MAX(staydate) AS lastnight,
MIN(available) AS available,
ra.resort AS resortcode,
ri.resort,
ri.room,
ri.roomcode,
ri.view,
SUM(ra.points*'20') AS price,
ri.sqfoot,
ri.description,
ri.bedding,
ri.amenities,
ri.sleeps,
ri.sleep_details,
ri.layout_img AS layoutimg,
ri.room_img AS roomimg,
ri.roomimg_thumb
FROM resort_availability AS ra
LEFT JOIN room_info AS ri ON (ra.room = ri.roomcode)
WHERE staydate >= '$checkin' AND staydate = '$checkout'
AND sleeps >= '$occupants'
GROUP BY ri.roomcode
ORDER BY ri.resort, points
";
$result=mysqli_query($con, $sqlCommand) or die(mysqli_error($con));
$data = array();
while($row = $result->fetch_assoc())
{
$itemName = $row["resort"];
$resortCode = $row["resortcode"];
if ( !array_key_exists($itemName, $data)) {
$data[$itemName] = array ();
}
$data[$itemName][] = $row;
}
foreach ($data as $itemName => $rows) {
echo '<div class="resort ' ,$resortCode , '">';
echo '<div class="resort_header"><h1>', $itemName, '</h1></div>';
foreach ($rows as $row){
if ($row['available'] > 0) {
$roomresult = '<div class="room_result roomavailable">';
$available = '<button class="available">BOOK NOW</button>';
$filtavail = 'available';
}
else {
$roomresult = '<div class="room_result roomsoldout" style="">';
$available = '<button class="soldout">SOLD OUT</button>';
$filtavail = 'soldout';
}
echo $roomresult;
echo '<div class="room_thumb"></div>
<div class="room_info"><h4>'
,$row['room'], '-' ,$row['view'] ,
'</h4></div>
<div class="price"><center><h3>','' ,$row['price'],'</h3>' , $available ,'</center></div></div>';
}
echo '</div>';
}
?>
This PHP code gives me accurate results but does not allow me to Create a div with results based on each resort IE, I don't know how to create a Header with the resort name, separating each set of rooms by resort as it is in the first example.
<?php
include("dbh.php");
mysqli_select_db($con, "checkavail") or die ("no database");
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$occupants = $_POST['occupants'];
$sqlCommand=
"SELECT
MIN(staydate) AS checkin,
MAX(staydate) AS lastnight,
MIN(available) AS available,
ra.resort AS resortcode,
ri.resort AS resort,
ri.room,
ri.roomcode AS roomcode,
ri.view,
SUM(ra.points) AS points,
ri.sqfoot,
ri.description,
ri.bedding,
ri.amenities,
ri.sleeps,
ri.sleep_details,
ri.layout_img AS layoutimg,
ri.room_img AS roomimg,
ri.roomimg_thumb
FROM resort_availability AS ra
LEFT JOIN room_info AS ri ON (ra.room = ri.roomcode)
WHERE staydate >= '$checkin' AND staydate < '$checkout'
AND sleeps >= '$occupants'
GROUP BY ri.resort, roomcode
ORDER BY ri.resort, points
";
$result=mysqli_query($con, $sqlCommand) or die(mysqli_error($con));
while($row = $result->fetch_assoc()) {
$pricecalc = ($row['points'])*20;
$price = number_format($pricecalc, 2);
//RESULTS BOX
if($row['available'] < "1"){
echo '<div id="'.$row['roomcode'].'-'.$row['resortcode'].'"','class="soldout roomresults room'.$row['resortcode'].'">';
}
if($row['available'] > "0"){
echo '<div id="'.$row['roomcode'].'-'.$row['resortcode'].'"','class="available roomresults room'.$row['resortcode'].'">';
}
//IMAGE BOX
echo '<div class="thumbnail"><img height="90" src="',$row['roomimg_thumb'],'">';
echo '</div>';
//ROOM DETAILS
echo '<div class="roomdetails">';
echo '<h4>'.$row['room'].' - '.$row['view'].'</h4>';
echo '<p>Sleeps: '.$row['sleep_details'].'</p>';
echo '<p class="clickfordetails">Click Image For Room Details</p>';
echo '</div>';
//PRICING BOX AND SUBMIT BUTTON
echo '<div class="price">';
echo '<h3>$',$price,'</h3>';
if ($row['available'] < "1"){
echo '<button type="button" class="soldoutbtn">SOLD OUT</button>';
}
if ($row['available'] > "0"){
echo '<button type="button" class="booknowbtn">BOOK NOW</button>';
}
echo '</div,$row>';
echo '</div></div>';
};
?>
Yes group by will give you the 1st record of the same group.
In my understanding , you want to show all the data but place all the records with the same roomcode together.
In that case, please change
GROUP BY ri.roomcode
ORDER BY ri.resort, points
to
ORDER BY ri.roomcode, ri.resort, points

Get two results while looping trough associative array in PHP

I want to get two results at a time when using while looping trough a associative array in PHP.
I need to echo two results per row, something like this:
<?
while($row = mysqli_fetch_assoc($result)) {
$client_name = $row['client_name'];
$review = $row['review'];
echo('
<div class="row">
<div>
<p>'.$client_name.'</p>
<p >'.$review.'</p>
</div>
<div>
<p>'.$client_name.'</p>
<p >'.$review.'</p>
</div>
</div>
');
}
}
?>
Right now it's giving me the same result twice rather than the next one.
You can use a counter to control the output of the outside div so that you get two inside div output for each outside one:
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
$client_name = $row['client_name'];
$review = $row['review'];
if ($i % 2 == 0) echo '<div class="row">';
echo '<div><p>'.$client_name.'</p><p>'.$review.'</p></div>';
if ($i % 2 == 1) echo '</div>';
$i++;
}

How to arrange values alphabetically in php

I am working on job portal project, and want to arrange values alphabetically according to their starting alphabet section, I want companies starting with A to be under the A column header, and companies starting with B to be under the B header..."
or if i have no companies with the value of B then B be skipped.
<span>a</span>
<?php
$sql = "select * from companies";
$result = mysqli_query($con, $sql);
while($data = mysqli_fetch_array($result)) {
$company = $data['company_name'];
?>
<div class="list-col"> <?php echo $company; ?>(0) </div>
<?php
}
?>
</div>
</li>
<!-- Sample of how column in HTML -->
<li class="loop-entry">
<div class="col">
<span>C</span>
<div class="list-col">
Company Name (0)
Company Name (1)
Company Name (2)
Company Name (3)
Company Name (4)
</div>
</div>
</li>
i wants to arrange according to this image
Okay here it is. I used functions to capture what you wanted. I will detail more later to explain things that you need to understand.
I completed an example on PHP Sandbox with an array of values to display the code as outputted in text in HTML
<?php
$sql = "select * from companies ORDER BY company_name";
$result = mysqli_query($con, $sql);
$letter = '';
$count = 0;
while($data = mysqli_fetch_array($result)) {
$company = $data['company_name'];
createCompanyList($company, $letter,$count);
}
/**
creates Company Listing
*/
function creatCompanyList($company, &$letter, &$count){
if($letter !== $company[0]) {
if($count >0) {
endCompanyList();
$count = 0;
}
$letter = $company[0];
startCompanyList($letter, $count);
}
//only adds the company if the letter and company matches
//and permits only the first 5 companies
if($letter == $company[0] && $count <= 4) addCompanyList($company);
if($count==4) endCompanyList();
$count++;
}
//creates opening divs for a Company List based upon letter
function startCompanyList($letter, &$count){
?>
<li class="loop-entry">
<div class="col"> <span><?=$letter?></span>
<div class="list-col">
<?php
}
//closes divs for the Company List
function endCompanyList(){
?>
</div>
</div>
</li>
<?php
}
//inserts company into to a column
function addCompanyList($company){
?>
<?=$company?>
<?php
}
?>

PHP mysql jquery pagination modification

I have a problem with my pagination, I need your help please.
I have 53 records but with this pagination, I can get 50 records only, I can't see the 3 others
I want to change this code to get all records please, and if I want to make the pagination easy to navigate for big records. Like this (for exp):
<< 1 2 3 4 5 ...... 184 185 >>
Thanks
<?php
include("db.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
//validate page number is really numaric
if(!is_numeric($page_number)){die('Invalid page number!');}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$results = mysqli_query($db,"SELECT id,name,message FROM paginate ORDER BY id ASC LIMIT $position, $item_per_page");
//output results from database
while($row = mysqli_fetch_array($results))
{
echo' <ul class="page_result">
<li class="page_result_img"><img src="images/pic1.jpg" class="img-responsive" alt=""/></li>
<li class="page_result_desc" id="item_'.$row["id"].'">
<h3>'.$row["id"].'.'.$row["name"].'</h3>
<p>'.$row["message"].'</p>
</li>
<p class="no">'.$row["id"].'<br><span>projet</span></p>
<div class="clearfix"> </div>
</ul>';
}
?>
projet.php
<?php
include("db.php");
$results = mysqli_query($db,"SELECT COUNT(*) FROM paginate");
$get_total_rows = mysqli_fetch_array($results); //total records
//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page);
//create pagination
if($pages > 1)
{
$pagination = '';
$pagination .= '<ul class="paginate">';
for($i = 1; $i<$pages; $i++)
{
$pagination .= '<li>'.$i.'</li>';
}
$pagination .= '</ul>';
}
?>
.
.
.
.
<div class="approach" id="app">
<div class="container">
<div class="gallery-head text-center">
<h3>Nos projets</h3>
<p>Trouvez ici tout les projets</p>
<span> </span>
</div>
<ul id="results"></div>
<?php echo $pagination; ?>
</div>
</div>
For correct LIMIT in SQL query must be value of $page_number for the first page equal to 0. But your pagination starts from 1. Then you never get $position = 0 and SQL query skips first $item_per_page records.
Try modify $position calculation:
$position = (($page_number - 1) * $item_per_page);
Second problem is in projet.php. For 53 records (and $item_per_page = 10) is value of $pages = 6. But for cycle stops on 5. It should be:
for($i = 1; $i <= $pages; $i++)
I like to work this way .. So try this code hope it help
$results = mysqli_query($db,"SELECT * FROM paginate");
$get_total_rows = mysqli_num_rows($results); //total records
$item_per_page = 10;
//break total records into pages
$pages = ceil($get_total_rows/$item_per_page);
//create pagination
if($pages > 1)
{
?>
<ul class="paginate">
<?php
for($i = 1; $i<$pages; $i++)
{
if($pages <= 5){
?>
<li><?php echo $i; ?></li>
<?php
}elseif($pages >= $pages - 5){
?>
<li><?php echo $i; ?></li>
<?php
}
}
?>
</ul>
<?php
}

Paginating MySql Results

I have a table that pulls tickets from a MYSQL database and displays them to the user. This part works wonderfully! But, once the user gets 5-10 tickets, the page becomes long and ugly. I am wanting to paginate the responses automatically to show maybe 5 tickets per page and show the rest on following pages.
I am using Bootstrap and have the following for the display of the pagination. "Although I fear this is only pseudo code" I added id's to each main element.
<ul class="pager" id="pagerUL">
<li class="previous" id="previousUL">
← Previous
</li>
<li class="next" id="nextUL">
Next →
</li>
</ul>
Essentially the only code used to pull the database information on the page is:
<?php if(count($tickets) > 0) : ?>
<?php foreach ($tickets as $ticket): ?>
//table content
<?php endforeach ?>
<?php else : ?>
<tr>
<td colspan="7">No Tickets Created.</td>
</tr>
<?php endif; ?>
I thought we could add something to the <?php if(count($tickets) > 0) : ?> But honestly, I am not sure as I am not a expert at php and never even attempted or had the need to build pagination up until now. Any help, guidance, thoughts would be appreciated.
Pass the page number thru the URL and then grab it via php with $_GET.
$page = $_GET['page'];
$per_page = 10; // Define it as you please
$start = $page*$per_page;
$query = "SELECT * FROM table LIMIT ".$start.", ".$per_page;
$result = mysql_query($query);
$count = mysql_num_rows($result);
$i = 0;
for($i=0;$i<$count;$i++)
{
//echo out what you want
echo "";
}
$total_pages = ceil($count/$per_page);
for($i=0;$i<$total_pages;$i++)
{
if($page != $i)
{
echo "<a href='/page.php?page=".$i."'>".$i."</a>";
} else
{
echo $i;
}
}

Categories