I'm building php mysql app for job interviews, and I want to have for admin filer with bootstrap panels which are sorted by date in the same color column.
$db = connectPDO();
$query = ("SELECT razgovori.*, djelatnik.ime, djelatnik.prezime
FROM razgovori INNER JOIN djelatnik
ON djelatnik.id = razgovori.insert_by
ORDER BY datum_raz ASC");
$stmt = $db->prepare($query);
$stmt->execute();
$count = $stmt->rowCount();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$date = strtotime($row['datum_raz']);
$twoDaysAgoStart = strtotime('-2 day 00:00:00');
$yesterdayEnd = strtotime('yesterday 23:59:59');
$todayStart = strtotime('today 00:00:00');
$todayEnd = strtotime('today 23:59:59');
$tomorrowStart = strtotime('tomorrow 00:00:00');
$twoDaysAheadEnd = strtotime('+2 day 23:59:59');
<div class="container">
<div class="row">
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)): ?>
<?php $date = strtotime($row['datum_raz']); ?>
<?php if ($twoDaysAgoStart < $date && $yesterdayEnd > $date): ?>
<!--Panel info if past time Job interviews-->
<div class="col-md-4 pull-left">
<div class="panel panel-info">
<div class="panel-heading"><h3 class="panel-title"><?php echo
datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
<div class="panel-body">
<div class="col-md-9">
<h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php
echo $row['rime'].' '.$row['rprezime']; ?></a></h5>
<?php echo $row['kontakt_br']; ?>
</div>
<div class="col-md-3">
<span class="label label-default pull-right"></h5><?php echo
$row['ime'].' '.$row['prezime']; ?></span>
</div>
</div>
</div>
</div>
<!--//Panel-->
</tr>
<tr>
<!--Panel primary if Job interview is today-->
<?php elseif ($todayStart <= $date && $todayEnd >= $date): ?>
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading"><h3 class="panel-title"><?php echo
datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
<div class="panel-body">
<div class="col-md-9">
<h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php echo
$row['rime'].' '.$row['rprezime']; ?></a></h5>
<?php echo $row['kontakt_br']; ?>
</div>
<div class="col-md-3">
<span class="label label-default pull-right"></h5><?php echo
$row['ime'].' '.$row['prezime']; ?></span>
</div>
</div>
</div>
</div>
</tr>
<tr>
<!--Panel warning if Job interview is tomorrow-->
<?php elseif ($tomorrowStart <= $date && $twoDaysAheadEnd >= $date): ?>
<div class="col-md-4 pull-right">
<div class="panel panel-warning">
<div class="panel-heading"><h3 class="panel-title"><?php echo
datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
<div class="panel-body">
<div class="col-md-9">
<h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php
echo $row['rime'].' '.$row['rprezime']; ?></a></h5>
<?php echo $row['kontakt_br']; ?>
</div>
<div class="col-md-3">
<span class="label label-default pull-right"></h5><?php echo
$row['ime'].' '.$row['prezime']; ?></span>
</div>
</div>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div><!--/row-->
</div><!--/container-->
I want to have sorted like thiswith Main Panal or without
I get this:enter image description here
Because stackoverflow red message constantly asking me for more details, i can't put my all php code
You can do this in your MySQL query.
Example:
SELECT * FROM t1
ORDER BY key_part1, key_part2;
SELECT * FROM t1
WHERE key_part1 = constant
ORDER BY key_part2;
SELECT * FROM t1
ORDER BY key_part1 DESC, key_part2 DESC;
SELECT * FROM t1
WHERE key_part1 = 1
ORDER BY key_part1 DESC, key_part2 DESC;
SELECT * FROM t1
WHERE key_part1 > constant
ORDER BY key_part1 ASC;
SELECT * FROM t1
WHERE key_part1 < constant
ORDER BY key_part1 DESC;
SELECT * FROM t1
WHERE key_part1 = constant1 AND key_part2 > constant2
ORDER BY key_part2;
More information can be found here: https://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html or https://www.w3schools.com/sql/sql_orderby.asp
If you want to do this inside PHP you can use sort:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}
?>
The above example will output:
fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] =
orange
More information about PHP sort():
http://php.net/manual/en/function.sort.php
Related
I want to limit my articles to 3 per page ... the page has appeared but the article is still on one page .
require_once 'koneksi.php';
$result = mysqli_query($koneksi, "SELECT * FROM tbl_artikel");
$aktif = 'artikel';
$halaman = 3;
$page = isset($_GET["halaman"]) ? (int)$_GET["halaman"] : 1;
$mulai = ($page>1) ? ($page * $halaman) - $halaman : 0;
$total = mysqli_num_rows($result);
$pages = ceil($total/$halaman);
$query = "SELECT * FROM tbl_artikel LIMIT $mulai, $halaman";
$result2 = mysqli_query($koneksi, $query);
$no =$mulai+1;
?>
and here is the code for article
<?php while($artikel = mysqli_fetch_assoc($result)) : ?>
<div class="artikel">
<div class="info">
<div class="post mb-2">
<span class="tgl"><?= date('d M y', strtotime($artikel['tanggal'])) ?></span>
<span class="judul"><?= $artikel['judul'] ?></span>
</div>
</div>
<div class="detail">
<div class="clearfix" style="text-align: justify;">
<img src="images/artikel/<?= $artikel['foto'] ?>" alt="" width="200px" class="float-left mr-2">
<?= substr($artikel['isi'], 0, 150) . '...' ?>
</div>
I have the following code which displays a theatre show feed on my website, coming from an external ticketing company.
<!-- PULL IN SHOW FEED -->
<div class="container">
<div class="col-md-12 whats-on-feed">
<!-- PULL IN SHOW FEED -->
<div class="container">
<div class="col-md-12 whats-on-feed">
<?php
$xml = simplexml_load_file('https://tickets.leicesterymca.co.uk/feed/shows');
if(isset($wp_query->query_vars['month'])) {
$month = $wp_query->query_vars['month'];
$fromDate = date("Y"). '-'. $month;
$shows = $xml->xpath("//*[contains(ActiveFrom,'$fromDate')]");
}else if(isset($wp_query->query_vars['genre'])) {
$genre = str_replace("-", " ", $wp_query->query_vars['genre']);
if($genre != 'all'){
$shows = $xml->xpath("//*[Genres/Genre/#Name='$genre']");
}else{
$shows = $xml;
}
}else{
$shows = $xml;
}
?>
<div class="block-grid-xs-1 block-grid-sm-2 block-grid-md-3 block-grid-lg-4 show-list">
<?php
$i = 1;
foreach ($shows as $Show) {
$activeFrom=date_create($Show->ActiveFrom);
$activeTo=date_create($Show->ActiveTo);
if(strlen($Show->ShowId) > 0){
?>
<div>
<div class="box">
<div class="caption">
<h3><?php echo $Show->Name ?></h3>
<p><?php echo $activeFrom->format('D d M Y'); ?></p>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<a href='/show?showid=<?php echo $Show->ShowId ?>' class='btn btn-success btn-sm btn-clear-white'>More Info</a>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<a href='/buy?showid=<?php echo $Show->ShowId ?>' class='btn btn-success btn-sm btn-clear-white'>Buy Tickets</a>
</div>
</div>
</div>
<div class='image-holder' style='background-image: url("<?php echo $Show->SmallImageUrl ?>");' /></div>
</div>
</div>
<?php $i++; }} ?>
</div>
</div>
This is coming from an XML feed that the ticketing company outputs from their system.
Currently they are displaying in order of show ID, I think this is the default, but I need them to display in order of date - The data node in the xml feed for date is: ActiveFrom.
I have seen this solution:
<xsl:sort select="ActiveFrom" order="ascending" />
But I don't really know if A: it's the correct approach, or B: Where I would add it in relation to my code? Can anyone help?
You can try this code
$xml = 'https://tickets.leicesterymca.co.uk/feed/shows';
$data = simplexml_load_file($xml);
$encode = json_encode($data);
$decode = json_decode($encode, true);
$show = $decode['Show'];
//function to sort by array value
function date_compare($a, $b) {
$t1 = strtotime($a['ActiveFrom']);
$t2 = strtotime($b['ActiveFrom']);
return $t1 - $t2;
}
usort($show, 'date_compare');
//test active from value
foreach ($show as $time) {
echo $time['ActiveFrom'].'<br>';
}
//echo '<pre>'.print_r($show, true).'</pre>';
Edited
<?php
$xml = 'https://tickets.leicesterymca.co.uk/feed/shows';
$data = simplexml_load_file($xml);
$encode = json_encode($data);
$decode = json_decode($encode);
$results = $decode->Show;
//function to sort by object (not array)
function date_compare($a, $b) {
$t1 = strtotime($a->ActiveFrom);
$t2 = strtotime($b->ActiveFrom);
return $t2 - $t1; // reverse it to check it works or not $t2 - t1
}
usort($results , 'date_compare');
if(isset($wp_query->query_vars['month'])) {
$month = $wp_query->query_vars['month'];
$fromDate = date("Y"). '-'. $month;
$shows = $xml->xpath("//*[contains(ActiveFrom,'$fromDate')]");
}
else if(isset($wp_query->query_vars['genre'])) {
$genre = str_replace("-", " ", $wp_query->query_vars['genre']);
if($genre != 'all'){
$shows = $xml->xpath("//*[Genres/Genre/#Name='$genre']");
}else{
$shows = $results;
}
}
else {
$shows = $results;
}
?>
<!--next-->
<?php
$i = 1;
foreach ($results as $Show) {
$activeFrom = date_create($Show->ActiveFrom);
$activeTo = date_create($Show->ActiveTo);
if(strlen($Show->ShowId) > 0){
?>
<div>
<div class="box">
<div class="caption">
<h3><?php echo $Show->Name ?></h3>
<p><?php echo $activeFrom->format('D d M Y'); ?></p>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<a href='/show?showid=<?php echo $Show->ShowId ?>' class='btn btn-success btn-sm btn-clear-white'>More Info</a>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<a href='/buy?showid=<?php echo $Show->ShowId ?>' class='btn btn-success btn-sm btn-clear-white'>Buy Tickets</a>
</div>
</div>
</div>
<div class='image-holder' style='background-image: url("<?php echo $Show->SmallImageUrl ?>");' /></div>
</div>
</div>
<?php $i++; }} ?>
</div>
</div>
see screenshot development
as seen in the picture, where is the red circle should the number of user rankig appear. example: 1ts, 2th, 3rd...
I have tried everything but not me.
My table is users
ID Name username password Wins Loses
I leave my php code file profile.php
<?
require_once('values.inc.php');
require_once('func.inc.php');
if(!($_GET[user])) exit('Error.');
open_conn();
$res=mysql_query("SELECT ID, Name, username, Thumbnail, LinkText, LinkUrl, country FROM users WHERE Status='1' ORDER BY RAND() LIMIT 2");
while($row=mysql_fetch_assoc($res)) {
$username[] = $row[username];
$ID[] = $row[ID];
$Thumbnail[] = $row[Thumbnail];
$Name[] = $row[Name];
$LinkText[] = $row[LinkText];
$Model=mysql_query("SELECT ID, Name, username, Wins, Loses, Draws, Thumbnail, LinkText, LinkUrl, country, (Wins) / (Wins+Loses) AS WinPercent, (Wins+Loses+Draws) AS Matches, DATE_FORMAT(DateTime, '%d %M %Y') AS Date FROM users WHERE username='$_GET[user]' LIMIT 1");
$Model=mysql_fetch_assoc($Model);
$WinPercent=#round(($Model['Wins'] / ($Model['Wins']+$Model['Loses'])) * 100, 2);
$LosePercent=#round(($Model['Loses'] / ($Model['Wins']+$Model['Loses'])) * 100, 2);
if($Model['LinkText'] != '' && $Model['LinkUrl'] != '') {$Url="<br><br>$Model[LinkText]";}
if($row['LinkText'] != '' && $row['LinkUrl'] != '') {$Url[]="<br><br>$row[LinkText]<br><br>";}else{$Url[]='';}
}
close_conn();
?>
<?php if (isset($Model['username'])) { ?>
<div class="content-holder">
<div class="profile-holder">
<div class="profile-image">
<img src="images/uploads/<?php echo ucwords($Model['Thumbnail'])?>" class="photo">
</div>
<div id="photo-upload-holder">
<label for="pic" id="photo-upload-label">
<h1 class="txtShadow"><?php echo ucwords($Model['Name'])?></h1>
</label>
</div>
<!--<p class=\"userName\">#dada</p>-->
</div>
<div class="dashboard-Details overFlow">
<div class="leftColumn pull">
<div class="displayBlock"><br>
<i class="heart-icon"></i>
<span><?php echo ucwords($Model['Wins'])?></span>
</div><br>
<div class="displayBlock">
<i class="star-icon"></i>
<span><?php echo ($WinPercent)?>%</span>
</div>
</div>
<div class="rightColumn push">
<h2><?php echo ucwords($Model['username'])?></h2>
<?php if($Model['country']):?>
<h4> <?php echo $countrys; ?> <?php echo ucwords($Model['country'])?></h4>
<?php endif?>
<?php if($Model['LinkText']):?>
<h4> Instagram: #<?php echo ucwords($Model['LinkText'])?></h4>
<?php endif?>
</div>
<?php } else { ?>
<?php echo $profile14; ?>
use quotes here
$username[] = $row['username'];
$ID[] = $row['ID'];
$Thumbnail[] = $row['Thumbnail'];
$Name[] = $row['Name'];
$LinkText[] = $row['LinkText'];
I have a table with fields like id, date, heading, news. I want to display the fields date, heading and news in an Owl Carousel slider. Can anyone suggest how to pick two rows at a time from an SQL table to display two entries at a time in the Owl Carousel slider. I have used an SQL query like this:
<?php
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date LIMIT 0, 1";
$result3 = mysql_query($sql3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)) {
?>
<div class="item ">
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><p>Mar<br>09</p></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><p>Mar<br>09</p></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
</div>
<?php
}
?>
Can anyone suggest how to do this?
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date LIMIT 0, 1";
Don't you change the LIMIT 0, 2
Which means select 2 starting with the value at index 0?
Once you've done that, take out the duplicate code inside the loop
Try this pls.
<?php
$sql3 = "SELECT date, heading, news FROM news ORDER BY news.date"; // No limit
$result3= mysql_query($sql3) or die(mysql_error());
$counter = 0;
while($row3 = mysql_fetch_array($result3, MYSQL_ASSOC))
{
if($counter % 2 == 0){
echo '<div class="item ">'.PHP_EOL;
}
?>
<div class="l_blk">
<div class="news_container">
<div class="col-md-1 date_b"><?php $row3['date'] ?></div>
<div class="col-md-11 cont_b">
<p>
<span class="news_t">"<?php echo $row3['heading']; ?>"</span><br>
<?php echo $row3['news']; ?>
</p>
</div>
</div>
</div>
<?php
if($counter % 2 == 1){
echo '</div>'.PHP_EOL;
}
$counter++;
}
if($counter % 2 == 1){
echo '</div>'.PHP_EOL;
}
?>
got stuck in extracting the correct month from timestamp...Here's my Code
$sql = "SELECT id,notice,description,DAY( `posted_date` ) AS DAY, MONTH(`posted_date` ) AS MONTH, YEAR( `posted_date`) AS YEAR, enable FROM n_notice";
$result=mysqli_query($con,$sql);
while ($run = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$notice = $run['notice'];
$description = $run['description'];
$day =$run['DAY'];
//$month=$run['MONTH'];
$month=date("M",$run['MONTH']);
$year=$run['YEAR'];
?>
<div class="media">
<div class="media-left media-middle">
<div id="date">
<div class="mon"><?php echo $month;?></div>
<div class="day"><?php echo $day;?></div>
<div class="year"><?php echo $year;?></div>
</div>
</div>
<div class="media-body">
<h5 class="media-heading"><?php echo $notice; ?></h5>
<?php echo $description; ?>
</div>
</div>
<?php
}
// Free result set
mysqli_free_result($result);
?>
</div>
DB timestamp column
How to get the correct month in "M" format? All the Months are shown as "Jan"!!!
$month=date("M",$run['MONTH']);
Try to change "M" to "F".
F - A full textual representation of a month (January through December)