Per Page Results PHP - php

I currently pull through data from a soap feed using this PHP, is there anyway for me to have the results ordered from high to low using the data from $weekrent?
Any help would be great! Here is my PHP code:
<?php
$wsdl = "http://portal.letmc.com/PropertySearchService.asmx?WSDL";
$client = new SoapClient($wsdl, array ("trace"=>1, "exceptions"=>0));
$strClientID = "{0050-e58a-cd32-3771}";
$strBranchID = "{0004-e58a-cd32-399e}";
$strAreaID = $_GET['area'];
$nMaxResults = $_GET['perpage'];
$nRentMinimum = $_GET['minrent'];
$nRentMaximum = $_GET['maxrent'];
$nMaximumTenants = $_GET['numtennants'];
$parameters = array( "strClientID"=>$strClientID,
"strBranchID"=>$strBranchID,
"strAreaID"=>$strAreaID,
"nMaxResults"=>$nMaxResults,
"nRentMinimum"=>$nRentMinimum,
"nRentMaximum"=>$nRentMaximum,
"nMaximumTenants"=>$nMaximumTenants
);
$values = $client->SearchProperties($parameters);
if(!is_array($values->SearchPropertiesResult->PropertyInfo))
{
$values->SearchPropertiesResult->PropertyInfo = array($values->SearchPropertiesResult->PropertyInfo);
}
if($values != '')
{
foreach ($values->SearchPropertiesResult->PropertyInfo as $message)
{
$uglyid = $message->ID;
$id = $message->FriendlyID;
$mainphoto = $message->MainPhoto->PhotoUrl;
$furnished = $message->Furnished;
$addressline1 = $message->Address1;
$rooms = $message->MaxTenants;
$rent = $message->Rent;
$description = $message->Description;
$isletagreed = $message->IsLetAgreed;
$facilities = $message->Facilities->FacilityInfo;
$photos = $message->Photos->PhotoInfo;
$roomsinfo = $message->Rooms->RoomInfo;
$facilitiesstring = serialize($facilities);
$extractnumbers = ereg_replace("[^0-9]", "", $rent);
$monthrent = ($extractnumbers) / $rooms;
$monthrentrounded = number_format(($monthrent/100),2);
$weekrent = ($monthrentrounded) * 12 / 52;
$weekrentrounded = floor($weekrent * 100) / 100;
$roomsinfojson = json_encode($roomsinfo);
$facilitiesjson = json_encode($facilities);
$roomsinfodouble = (substr_count(strip_tags($roomsinfojson),"Double"));
$roomsinfosingle = (substr_count(strip_tags($roomsinfojson),"Single"));
$roomsinfobathroom = (substr_count(strip_tags($roomsinfojson),"Bathroom"));
$roomsinfoshower = (substr_count(strip_tags($roomsinfojson),"Shower"));
$facilitiesparking = (substr_count(strip_tags($facilitiesjson),"Parking"));
$facilitiesgarden = (substr_count(strip_tags($facilitiesjson),"Garden"));
$totalbathrooms = $roomsinfobathroom + $roomsinfoshower;
$totalimages = count($photos);
echo '
<div class="col-property-box col-property-box-1-3">
<div class="owl-property-box">';
$i=0; foreach ($photos as $data) { if($i==4) break; echo '<div class="property-grid-box-picture" style="background: url('. $data->PhotoUrl .') center center;"></div>'; $i++; };
echo '</div>
<div class="property-grid-box">
'. $addressline1 .'
<p class="property-grid-box-text">'. limit_words($description,19) .'...</p>
<div class="property-grid-box-price">
<div class="section group">
<div class="col col-property-box-1-2 property-grid-box-price-border-right">
<div class="property-grid-box-price-top">£'. $weekrentrounded.'pp</div> <div class="property-grid-box-price-under">Weekly</div>
</div>
<div class="col col-property-box-1-2">
<div class="property-grid-box-price-top">£'. $monthrentrounded .'pp</div> <div class="property-grid-box-price-under">Monthly</div>
</div>
</div>
</div>
<div class="property-grid-box-icon-box">
<div class="section group">
<div class="col col-1-3-border no-left-border">
<span class="property-grid-box-number-icon"><center><i class="fa fa-bed"></i></center><div class="property-grid-box-number-text">'. $rooms .'</div></span>
</div>
<div class="col col-1-3-border">
<span class="property-grid-box-number-icon"><center><i class="flaticon-shower5"></i></center><div class="property-grid-box-number-text">'. $totalbathrooms .'</div></span>
</div>
<div class="col col-1-3-border">
<span class="property-grid-box-number-icon"><center><i class="flaticon-beds12"></i></center><div class="property-grid-box-number-text">'. $totalimages .'</div></span>
</div>
</div>
</div>
</div>
</div>
';
}
}
function limit_words($string, $word_limit)
{
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
?>

you can use usort function .. so you get the sorted result
Read more on php

You can put your messages data to temporary array along with calculated weekrent and then sort desc by weekrent value with usort
if($values != '')
{
$arrayForSort = array();
foreach ($values->SearchPropertiesResult->PropertyInfo as $message) {
$rent = $message->Rent;
$rooms = $message->MaxTenants;
$extractnumbers = ereg_replace("[^0-9]", "", $rent);
$monthrent = ($extractnumbers) / $rooms;
$monthrentrounded = number_format(($monthrent/100),2);
$weekrent = ($monthrentrounded) * 12 / 52;
$arrayForSort[] = array('weekrent' => $weekrent, 'message' => $message);
}
usort($arrayForSort, function($a, $b) {
if ($a['weekrent'] == $b['weekrent']) {
return 0;
}
return ($a['weekrent'] > $b['weekrent']) ? -1 : 1;
});
foreach ($arrayForSort as $item)
{
$message = $item['message'];
// Your code here to proper process the message ...

Related

Split mysql results to 5 bootstrap grid

I am looking a way to show my mysql results to a 5 div with same numbers of rows.
Here what i did:
$query_1 = $mysqli->query("SELECT * FROM tables ORDER BY id ASC");
$all_cnt = array();
$i = 0;
while($rows = mysqli_fetch_assoc($query_1)) {
$all_cnt[$i] = $rows["name"];
$i++;
}
$all_cnt = array_chunk($all_cnt, 5);
$output = "";
foreach($all_cnt as $cnt) {
$output .= '
<div class="col-md-2 ">
<div class="row content">
<ul class="list-unstyled">
';
foreach ($cnt as $t) {
$output .= '<li>'.$t.'</li>';
}
$output .= '
</ul>
</div>
</div>
';
}
But i get not ordered results and not same number on each grid !
Any other solution please ?

evernote PHP SDK not working for free account

I have integrated the PHP SDK from Github: https://github.com/evernote/evernote-cloud-sdk-php
I have managed to connect a Premium account using Developer Token. It works a treat.
However, when I try to access more accounts, I get an error. Here is my code
<pre>
require_once '../libs/evernote/src/autoload.php';
require_once('../libs/mysql.class.php');
require_once('../libs/debug.php');
require_once('../libs/functions.php');
session_start();
//Siren is the search term
$gMysql = new MySQL_Class(array("host"=>"localhost","user"=>"horscote","password"=>"feO#6i77","dbname"=>"horscote"));
$gMysql->connect();
$siren = $_POST['id'];
//if logged user - saved as viewed
$sqlViewed = 'SELECT evernoteSync FROM company WHERE SIREN='.$siren;
$isSync = $gMysql->queryValue($sqlViewed);
$gMysql->close();
if($isSync==1)
{
$gMysql->connect();
//get all admin evernotes
$evernotes = $gMysql->queryList('SELECT * FROM evernote_users e inner join users u on u.id=e.userid WHERE evernote=1 ORDER BY e.ID');
$maintable = '';
foreach($evernotes as $evernote)
{
//var_dump($evernote);
$token = $evernote['token'];
$sandbox = false;
$china = false;
$client = new \Evernote\Client($token, $sandbox, null, null, $china);
$search = new \Evernote\Model\Search($siren);
$notebook = new \Evernote\Model\Notebook();
//$notebook->guid = $evernote['notebook_guid'];
//$scope = \Evernote\Client::SEARCH_SCOPE_ALL;
//$order = \Evernote\Client::SORT_ORDER_REVERSE | \Evernote\Client::SORT_ORDER_RECENTLY_CREATED;
//var_dump($notebook);
$maxResult = 5;
$results = $client->findNotesWithSearch($search, $notebook, $scope, $order, $maxResult);
//var_dump($results);
$table = '<div class="panel-group" id="accordionEvernote" role="tablist" aria-multiselectable="true" style="margin-top:10px">';
$i=0;
foreach ($results as $result) {
$noteGuid = $result->guid;
$note = $client->getNote($noteGuid);
$in = '';
if($i==0)
{
$in = ' in';
$plusMinus = '<i class="more-less fa fa-chevron-right"></i>';
}
else
{
$plusMinus = '<i class="more-less fa fa-chevron-right"></i>';
}
$advancedClient = new \Evernote\AdvancedClient($token, $sandbox);
$noteStore = $advancedClient->getNoteStore();
$userStore = $advancedClient->getUserStore();
$userInfo = $userStore->getPublicUserInfo($evernote['username']);
$completeNote = $noteStore->getNote($token,$noteGuid,TRUE,TRUE,TRUE,TRUE);
$resources = $completeNote->resources;
$sResources = '';
foreach($resources as $res)
{
$resource = $noteStore->getResource($token, $res->guid, true, true, true, true);
$fileContent = $res->data->body;
$fileType = $res->mime;
$fileName = $res->attributes->filename;
$resGuid = $res->guid; // example GUID
$resUrl = $userInfo->webApiUrlPrefix . 'res/' . $resGuid;
$attribute = $noteStore->getResourceAttributes($token, $res->guid);
$fileName = $attribute->fileName;
$sResources .= '
<div class="col-xs-10 col-sm-4 col-md-2">
<div class="thumbnail" style="text-align:center">
<i class="fa fa-file-text" aria-hidden="true" style="font-size:40px;color:#F05F40"></i>
<div class="caption">
<a tatget="_blank" href="'.$resUrl.'" style="font-size:10px">'.$fileName.'</a>
</div>
</div>
</div>';
}
$tags = $noteStore->getNoteTagNames($token,$noteGuid);
$created = date('d/m/Y',$completeNote->created/1000);
$updated = date('d/m/Y',$completeNote->updated/1000);
$hashTags = '';
foreach($tags as $tag)
{
$hashTags .='<span class="label label-primary" style="margin-right:5px"> <i class="fa fa-tag" aria-hidden="true"></i> '.$tag.'</span>';
}
$table .='<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading'.$note->guid.'">
<h4 class="panel-title" style="font-size:12px">
<a role="button" data-toggle="collapse" data-parent="#accordionEvernote" href="#collapse'.$note->guid.'" aria-expanded="true" aria-controls="collapse'.$note->guid.'">
'.$plusMinus.'
'.$note->title.' - Crée le '.$created.' par '.$evernote['firstName'].' - Modifié le '.$updated.'</i>
</a>
</h4>
</div>
<div id="collapse'.$note->guid.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading'.$note->guid.'">
<div class="panel-body">
<div id="ntD'.$note->guid.'">
<div style="margin-bottom:10px">'.$hashTags.'</div>
<div style="margin-bottom:10px" id="resources" class="row">'.$sResources.'</div>
'.$note->content.'
</div>
</div>
</div>
</div>';
$i++;
}
$maintable .=$table;
}
$gMysql->close();
if(isset($noteGuid) && !empty($noteGuid))
{
echo '<div style="text-align:justify">'.$maintable.'</div>';
}
else
{
$table = 'Aucune note trouvée pour cette société.';
echo $table;
}
}
else
{
$table = '<div style="text-align:center"><button class="btn btn-success btn-lg" onclick="sync('.$siren.')"> Syncroniser</button></div>';
echo $table;
}
</pre>
Any idea if all the accounts need to be premium? The premium account works but the others don't.

Wordpress: get_post_field() throws error like it's trying to redefine a function

I have a function called getNews().
I call getNews() like this:
<div class="wrapper" id="blog">
<div class="blogPosts">
<?PHP getNews($postPerPage, 0, $theme_link, $pageNum); ?>
</div>
</div>
The function works until it tries to get the article text from wordpress using get_post_field().
function getNews($numPosts, $offset, $theme_link, $pageNum) {
$offset = $numPosts * $pageNum;
$recent_args = array('numberposts' => $numPosts, 'offset' => $offset,);
$articles = wp_get_recent_posts($recent_args);
foreach($articles as $article){
$article_id = $article['ID'];
...
echo $article_text = get_post_field('post_content', $article_id);
...
}
}
When I run this, I get the error saying Fatal error: Cannot redeclare getNews() (previously declared in /homepages/30/d545089862/htdocs/VintageTroubleMusic/wp-content/plugins/enhanced-text-widget/enhanced-text-widget.php(57)
For some reason, it's running my loop twice.
Any advice?
EDIT
I'm including the full code here: This is written inside of an "Enhanced Text widget" inside of a page.
<?PHP
$theme_link = "/wp-content/themes/thestory-child/";
$pageNum = $_GET['pa'];
$postPerPage = 27;
$qry_numPosts = mysql_query("SELECT * FROM Ykcfdlqhposts WHERE post_status = 'publish' AND post_type = 'post'") or die(mysql_error());
$numPosts = mysql_num_rows($qry_numPosts);
$numPages = ceil($numPosts/$postPerPage) . " pages";
if(!$pageNum){
$pageNum = 0;
}
?>
<!-- START OF THE BODY HERE -->
<h3 class="widget-title" style="float:left;">Headlines</h3>
<div class="wrapper" id="paginate1">
<?PHP paginate($numPosts, $numPages); ?>
</div>
<div class="wrapper" id="blog">
<div class="blogPosts">
<?PHP getNews($postPerPage, 0, $theme_link, $pageNum); ?>
</div>
</div>
<div class="wrapper" id="paginate2">
<?PHP paginate($numPosts, $numPages); ?>
</div>
And inside of the functions.php page
function getNews($numPosts, $offset, $theme_link, $pageNum) {
$offset = $numPosts * $pageNum;
$recent_args = array('numberposts' => $numPosts, 'offset' => $offset,);
$articles = wp_get_recent_posts($recent_args);
foreach($articles as $article){
$article_id = $article['ID'];
$article_link = get_permalink($article_id);
$article_length = 350;
$article_background = get_the_post_thumbnail($article_id);
$article_title = get_post_meta( $article_id, 'short_title', true );
$article_text = "test";
$article_text = get_post_field('post_content', $article_id);
//$article_text = get_post_field('post_content', $article_id);
//$article_text = strip_tags($article_text);
//$article_text_length = strlen($article_text);
/*
if($article_text_length >= $article_length) {
$article_text_pos = strpos($article_text, " ", $article_length);
$article_text = substr($article_text, 0, $article_text_pos)."... ";
}
*/
?>
<div class="article">
<?PHP //Get the article image
if (!$article_background) {
$rand = rand(1, 5);
$img = $theme_link . "images/img" . $rand . ".jpg";
} else {
$img = explode('src="', $article_background);
$img = explode(".jpg", $img[1]);
$img = $img[0] . ".jpg";
}?>
<a href="<?= $article_link; ?>">
<div class="article__image" style="background-image: url('<?= $img; ?>');"></div>
<div class="article__title"><?= $article_title; ?></div>
</a>
<div class="article__details">
<div class="article-text">
<?= $article_text; ?>
</div>
READ MORE
</div>
</div>
<? }
}
function paginate($numPosts, $numPages) {
?>
<div class="page">
<span>Page:</span><?PHP
$i = 0;
while($i < $numPages){
$i++;
if($i == $pageNum){
echo '<span style="background-color:transparent; text-decoration:underline; color:white; cursor:default;">'.$i.'</span>';
} else {
echo "<a href='?pa=".$i."'><span>".$i."</span></a>";
}
}
?>
</div>
<?
}

php MySQL loop within nested divs

I have a nested div in which I want to fetch data from my database. I have explained what I want below:
<div class="row-fluid">
<div class="span6">
**First Entry Here**
</div>
<div class="span6">
**Second Entry Here**
</div>
</div>
<div class="row-fluid">
<div class="span6">
**Third Entry Here**
</div>
<div class="span6">
**FourthEntry Here**
</div>
</div>
and so on...
I am literally confused right now and the solution is not clicking in my mind. I have this code so far:
$sql = "SELECT * FROM `users`";
$result = mysqli_query($conn, $sql);
$ic = 0;
while ($row = mysqli_fetch_assoc($result)) {
if(($ic%2) == 0) {
$div1 = '<div class="row-fluid">';
$div2 = '</div>';
}else{
$div1 = '';
$div2 = '';
}
?>
<?=$div1;?>
<div class="span6">
<?=$row['userid'];?>
</div>
<?=$div2;?>
<?php
$ic = $ic + 1;
}
?>
I have tried two while loops but it was outputting around 5000 lines of code.
$ic = 0;
$temp = "";
while ($row = mysqli_fetch_assoc($result)) {
$temp .= "<div class='span6'>".$row['user_id']."</div>";
if ( $ic % 2 != 0 ){
echo "<div class='row-fluid'>".$temp."<div>";
$temp = "";
}
$ic++;
}
// in case the number of records are odd::
if ($temp != "" )
echo "<div class='row-fluid'>".$temp."<div>";
#Amr Magdy logic is correct, however the solution didn't work out for me. I have now managed to fix it and if anyone else has the same situation may refer to the code below:
<?php
$sql = "SELECT * FROM `users`";
$result = mysqli_query($conn, $sql);
$ic = 0;
$temp = "";
while ($row = mysqli_fetch_assoc($result)) {
$temp = '<div class="span6">'.$row['userid'].'</div>';
if(($ic%2) == 0) {
echo '<div class="row-fluid">'.$temp;
$temp = "";
}else{
echo $temp;
}
$ic = $ic + 1;
}
echo '</div>';
?>

Converting old pagination mysql_query php to PDO

I'm trying to switch over to using PDO. I have create a news section on my website.
My database connection is included in my header
I'm currently using this to create the pagination, but need help adding my post
to the $data = array("Hey","Hello","Good-Bye"); As of now Hey, Hello and Good-Bye are the only items working with the pagination.
Can someone hep me get this result: $data = array("add my post here")
Here is my code: My database connection is in the header section.
<?php
class Pagination{
var $data;
function Paginate($values,$per_page){
$total_values = count($values);
if(isset($_GET['page']))
{
$current_page = $_GET['page'];
}
else
{
$current_page = 1;
}
$counts = ceil($total_values / $per_page);
$param1 = ($current_page - 1) * $per_page;
$this->data = array_slice($values,$param1,$per_page);
for ($x=1; $x<= $counts; $x++)
{
$numbers[] = $x;
}
return $numbers;
}
function fetchResult(){
$resultsValues = $this->data;
return $resultsValues;
}
}
?>
I'm assuming that I need the <ul class="notices"> - </ul>(shown below) IN PLACE OF THE $data = array("Hey","Hello","Good-Bye"); DOWN BELOW
Not sure how to do this though?
<ul class="notices">
<?php
$query = $db->query("SELECT id, date, title, description FROM htp_news");
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
?>
<li data-id="id-<?=$row["id"] ?>">
<article class="box white-bg mb-25">
<div class="notices-title"><?=$row["title"] ?></div>
<div class="newsdate" style="margin: 10px 0 !important;"><?= date("F d, Y", strtotime($row["date"])); ?></div>
<div class="articletext"><style>.articletext img{width:100%; height:auto;}</style><?php $string = $row["description"];
$max = 300; // or 200, or whatever
if(strlen($string) > $max) {
// find the last space < $max:
$shorter = substr($string, 0, $max+1);
$string = substr($string, 0, strrpos($shorter, ' ')).'...';
}
echo $string;
?></div>
<div class="btn small green white-tx">Continue Reading</div>
</article>
</li>
<?php
}
?>
</ul>
<div class="grid_8">
<div class="paginationwrap" align="center">
<!--This is where the pagination numbers are being generated-->
<?php
$pag = new Pagination();
$data = array("Hey","Hello","Good-Bye");//This is where I'm getting confused..?
$numbers = $pag->Paginate($data,1);
$result = $pag->fetchResult();
foreach($result as $r)
{
echo '<div>'.$r.'</div>';
}
foreach($numbers as $num)
{
echo''.$num.'';
}
?>
Any help would be appreciated. Thank you.
Don't know why you need it, but to avoid duplication of code and javascript, you could do this like that, and then you have your data inside $inside_ul variable.
<?php
$query = $db->query("SELECT id, date, title, description FROM htp_news");
$inside_ul = '';
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
$inside_ul .= '
<li data-id="id-'.$row["id"].'">
<article class="box white-bg mb-25">
<div class="notices-title">'.$row["title"].'</div>
<div class="newsdate" style="margin: 10px 0 !important;">'.date("F d, Y", strtotime($row["date"])).'</div>
<div class="articletext"><style>.articletext img{width:100%; height:auto;}</style>
';
$string = $row["description"];
$max = 300; // or 200, or whatever
if(strlen($string) > $max) {
// find the last space < $max:
$shorter = substr($string, 0, $max+1);
$string = substr($string, 0, strrpos($shorter, ' ')).'...';
}
$inside_ul .= $string.'</div>
<div class="btn small green white-tx">
Continue Reading
</div>
</article>
</li>
';
}
?>
<ul class="notices"><?= $inside_ul; ?></ul>

Categories