Redbeanphp bad order - php

With this code, I parse post elements from table
$all = R::findAll('postdate'," ORDER BY id DESC LIMIT 15");
for($i = 1; $i <= count($all); $i++){
if(isset($all[$i])){
$date = date_create($all[$i]->date);
$post = R::findOne('post', 'date_id = ?', array($all[$i]->id));
$author = R::findOne('postauthor', 'id = ?', array($post->author_id));
$title = R::findOne('posttitle', 'id = ?', array($post->title_id));
echo '<div class=post>
<i class=post-date>'.date_format($date, 'd.m.Y G:i').' 📝 '.$author->author.'</i>
<h1>'.$title->title.'</h1><hr><br>'.
$post->content.
'</div>';
}
}
but in the end the last posts are not found and everything goes in ascending order. Help me please. I tried it through the date too

$all = R::getAll( 'SELECT * FROM postdate ORDER BY date DESC LIMIT 125' );
for($i = -1; $i <= count($all); $i++){
if(isset($all[$i])){
$date = date_create($all[$i]['date']);
$post = R::findOne('post', 'date_id = ?', array($all[$i]['id']));
$author = R::findOne('postauthor', 'id = ?', array($post->author_id));
$title = R::findOne('posttitle', 'id = ?', array($post->title_id));
echo '<div class=post>
<i class=post-date>'.date_format($date, 'd.m.Y G:i').' 📝 '.$author->author.'</i>
<h1>'.$title->title.'</h1><hr><br>'.
$post->content.
'</div>';
}
}

Related

php counter on mysql results

I got this simple foreach loop and can't figure out where is the problem with counter.
I get results like this. I am trying to make counter enlarged for one if it meets the conditions.
$building = 5;
$todaysdate = date("Y-m-d");
$tomorrows_date = date("Y-m-d", strtotime($todaysdate . "+1 days"));
$ends_date = "2018-04-30";
$counter = 0;
$query = "SELECT * FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC"; // results give me numbers from 30 to 110.
$querydone = $db->query($query);
while($row = $querydone->fetch_assoc()) {
$every_id[$row['sifra']] = $row;
}
$firstday = new DateTime($tomorrows_date);
$lastday = new DateTime($ends_date);
for($q = $firstday; $q <= $lastday; $q->modify('+1 day')){
$result_day = $q->format("Y-m-d");
$i = 0; // counter for every value from mysql
foreach ($every_id as $key => $value) {
$counter = ${$i++} = $value['sifra'];
if($building >= $i) {
$valuesResult = "('$result_day','$counter')" . "<br />";
} else {
break;
}
echo $valuesResult;
}
}
Where am I wrong?
$building = 5;
$todaysdate = date("Y-m-d");
$tomorrows_date = date("Y-m-d", strtotime($todaysdate . "+1 days"));
$ends_date = "2018-04-30";
$counter = 0;
$query = "SELECT * FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC LIMIT 80"; // results give me numbers from 30 to 110.
$querydone = $db->query($query);
while($row = $querydone->fetch_assoc()) {
$every_id = $row['sifra'];
}
$firstday = new DateTime($tomorrows_date);
$lastday = new DateTime($ends_date);
for($q = $firstday; $q <= $lastday; $q->modify('+1 day')){
$result_day = $q->format("Y-m-d");
$i = 0; // counter for every value from mysql
foreach ($every_id as $key => $value) {
$counter = $i++;
if($building >= $i) {
$valuesResult = "('$result_day','$counter')" . "<br />";
} else {
break;
}
echo $valuesResult;
}
}

HTML and PHP pagination not working correctly

I have a bit of code that pulls all the results from a database and displays the ones that are relevant to the users search. I have some more code that counts the amount of items and generates a certain amount of pages based on how many items are relevant to the users search. The problem is as follows. If I do a search all, my code displays everything in the database on 11 pages. If I search for car, it will still display 11 pages but only 2 results that have the word car in the title. The problem is that these results display on the eighth page and all other pages are blank. During the search all the two results with car in the title displayed on the eighth page as well. The search all is based on the order the items are in, in the database. Here is my current code:
$pagesQuery = mysql_query("SELECT count(id) FROM(`posts`)");
$pageNum = ceil(mysql_result($pagesQuery, 0)/5);
$start = (($page-1)*5);
$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5");
while ($row = mysql_fetch_array($currentname)) {
//recieve relevant data.
$title = $row[0];
$desc = $row[13];
$ID = $row[6];
$views = $row[3];
$user = $row[7];
//fetch the last id from accounts table.
$fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
$lastrow1 = mysql_fetch_row($fetchlast1);
$lastid1 = $lastrow1[6];
//acquire the username of postee.
for ($i1=1; $i1 <= $lastid1; $i1++) {
$currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user");
while ($row1 = mysql_fetch_array($currentname1)) {
$username1 = $row1[0];
}
}
//Format Title, description and view count.
$title2 = rtrim($title);
$donetitle = str_replace(" ", "-", $title2);
$url = "articles/".$ID."/".$donetitle."";
$donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title;
$donedesc = '';
if(strlen($desc) > 150) {
$donedesc = explode( "\n", wordwrap( $desc, 150));
$donedesc1 = $donedesc[0] . '...';
}else{
$donedesc1 = $desc;
}
$finviews = number_format($views, 0, '.', ',');
//Give relevant results
if(stripos($title, $terms) !== false || stripos($desc, $terms) !== false || stripos($username1, $terms) !== false){
if($row[10] == null){
$SRC = "img/tempsmall.jpg";
}else{
$SRC ="generateThumbnailSmall.php?id=$ID";
}
echo "<div id = \"feature\">
<img src=\"$SRC\" alt = \"article thumbnail\" />
</div>
<div id = \"feature2\">
$donetitle
<p id=\"resultuser\" >$username1</p>
<p id=\"resultp\">$donedesc1</p>
<img src=\"img/icons/flag.png\"/><b id=\"resultview\">$finviews views</b>
</div>
<div id = \"border\"></div>";
}
}
$totalPages = $pageNum;
$currentPage = $page;
$numPagesToShow = 10;
if($currentPage > $totalPages) {
$currentPage = $totalPages;
}
if($numPagesToShow >= $totalPages) {
$numMaxPageLeft = 1;
$numMaxPageRight = $totalPages;
} else {
$pagesToShow = ceil($numPagesToShow/2);
$numMaxPageLeft = $currentPage - $pagesToShow;
$numMaxPageRight = $currentPage + $pagesToShow;
if($numMaxPageLeft <= 0) {
$numMaxPageRight = $numMaxPageRight - $numMaxPageLeft +1;
$numMaxPageLeft = 1;
} elseif($numMaxPageRight >= $totalPages) {
$numMaxPageLeft -= ($numMaxPageRight - $totalPages);
$numMaxPageRight = $totalPages;
}
}
for ($i=$numMaxPageLeft; $i<=$numMaxPageRight; $i++) {
echo "<a id =\"pagenationlink\" href=\"searchresults.php?search=".$terms."&page=".$i."\">".$i."</a>";
}
How can I only display one page with the two results on it instead of 11 pages with the two relevant results on the eighth page? Thanks
Please update your code as below.
But try to use mysqli_() as mysql() are depricted
$cond = "";
if(!empty($_POST["search"]))
{
$cond = " write your search condition " ;
}
$start = (($page-1)*5);
$query = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM posts where $cond LIMIT $start, 5");
$TotalDataQuery = mysql_query("SELECT FOUND_ROWS() tot;");
$rsVal = mysql_fetch_array($pagesQuery);
$pagesQuery = $rsVal['tot'];
$pageNum = ceil($pagesQuery/5);
while ($row = mysql_fetch_array($query)) {
//continue your code
}

HTML and PHP generate pagination links

I have some PHP and html code that loads in results from my database. It shows five results per page. Let's pretend I have 1000 pages. The links for all those pages would go off the screen. Google had this problem but they fixed it by only displaying the current link as well as 5 links back and 5 links forward. I want to do something like this. I don't want to display 100 links to the various pages. Pretend the user is on page 100. I want to display the links for page 100 as well as link 95 to 105. How can I do this? Here is my code so far:
$page = $_GET["page"];
$pagesQuery = mysql_query("SELECT count(id) FROM(`posts`)");
$pageNum = ceil(mysql_result($pagesQuery, 0)/5);
$start = (($page-1)*5);
$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5");
while ($row = mysql_fetch_array($currentname)) {
//recieve relevant data.
$title = $row[0];
$desc = $row[13];
$ID = $row[6];
$views = $row[3];
$user = $row[7];
//fetch the last id from accounts table.
$fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
$lastrow1 = mysql_fetch_row($fetchlast1);
$lastid1 = $lastrow1[6];
//acquire the username of postee.
for ($i1=1; $i1 <= $lastid1; $i1++) {
$currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user");
while ($row1 = mysql_fetch_array($currentname1)) {
$username1 = $row1[0];
}
}
//Format Title, description and view count.
$title2 = rtrim($title);
$donetitle = str_replace(" ", "-", $title2);
$url = "articles/".$ID."/".$donetitle."";
$donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title;
$donedesc = '';
if(strlen($desc) > 150) {
$donedesc = explode( "\n", wordwrap( $desc, 150));
$donedesc1 = $donedesc[0] . '...';
}else{
$donedesc1 = $desc;
}
$finviews = number_format($views, 0, '.', ',');
//Give relevant results
if(stripos($title, $terms) !== false || stripos($desc, $terms) !== false || stripos($username1, $terms) !== false){
if($row[10] == null){
$SRC = "img/tempsmall.jpg";
}else{
$SRC ="generateThumbnailSmall.php?id=$ID";
}
echo "<div id = \"feature\">
<img src=\"$SRC\" alt = \"article thumbnail\" />
</div>
<div id = \"feature2\">
$donetitle
<p id=\"resultuser\" >$username1</p>
<p id=\"resultp\">$donedesc1</p>
<img src=\"img/icons/flag.png\"/><b id=\"resultview\">$finviews views</b>
</div>
<div id = \"border\"></div>";
}
}
for ($j=1; $j < $pageNum; $j++) {
echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>";
}
What you want is to change this:
for ($j=1; $j < $pageNum; $j++) {
echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>";
}
Here you list all links $j < $pageNum and you want to list just 10: $j <= 10 starting $j = $currentPage or $j = $currentPage - 5 if $currentPage is > 5

How to implement final steps in pagination project?

I am doing my first pagination implementation to my site. I feel like I am very close and just confusing my logic or mistaking the placement or actual value of some variables.
Here is the code:
$pgsize_wave = 40;
$pg_wave = (is_numeric($_GET["p"]) ? $_GET["p"] : 1);
$start = ($pg_wave-1)*$pgsize_wave;
$waves = mysql_query("SELECT * FROM `CysticAirwaves` LIMIT $start, $pgsize_wave");
$waves_total = mysql_query("SELECT COUNT(1) FROM `CysticAirwaves`");
$waves_total = mysql_fetch_row($waves_total);
$waves_total = $waves_total[0];
$max_pages = $waves_total / $pgsize_wave;
$max_pages = ceil($waves_total/$pgsize_waves);
?>
<div id="all_page_turn">
<ul>
<?php if($waves_total > 40 && $pg_wave >= 40) { ?>
<li class="PreviousPageBlog round_10px">
Previous Page
</li>
<?php } ?>
<?php if($waves_total > 40 && $pg_wave < ($waves_total-40)) { ?>
<li class="NextPageBlog round_10px">
Next Page
</li>
<?php } ?>
</ul>
</div>
To clarify any confusion, an airwave is a post from a user and I want to limit 40 per page. If it helps here is the query that pulls an airwave on the same page:
$query = "SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = `ToUserID` AND `status` = 'active' ORDER BY `date` DESC, `time` DESC";
$request = mysql_query($query,$connection);
$counter = 0;
while($result = mysql_fetch_array($request)) {
$replies_q = "SELECT COUNT(`id`) FROM `CysticAirwaves_replies` WHERE `AirwaveID` = '" . $result['id'] . "' && `status` = 'active'";
$request2 = mysql_query($replies_q,$connection);
$replies = mysql_fetch_array($request2);
$replies_num = $replies['COUNT(`id`)'];
$counter++;
$waver = new User($result['FromUserID']);
Thanks so much in advance.
This is pseudo code but hopefully explains the logic.
$total = SELECT COUNT(*) FROM `waves`
$currentPage = 1 // changes through page parameter in URL
$wavesPerPage = 40
$totalPages = ceil($total / $wavesPerPage)
$offset = ($wavesPerPage * ($currentPage - 1)) + 1
$waves = mysql_query("SELECT * FROM `waves` LIMIT $offset, $wavesPerPage");
while ($row = mysql_fetch_assoc($waves)) {
echo $row['wave']
…
}
To output the pagination links:
if ($totalPages > 1) {
if ($currentPage > 1) {
printf('Previous', $currentPage - 1);
}
for ($i = 1; $i <= $totalPages; $i++) {
$class = ($i == $currentPage) ? 'selected' : null;
printf('Page %1$u', $i, $class);
}
if ($currentPage < $totalPages) {
printf('Next', $currentPage + 1);
}
}

Help building a simple SQL query in Zend

My SQL query is based on data provided.
For example :
// table
$table = new Model_MyTable_DbTable();
// data
$columns = array( 'column1', 'column2', 'column3' );
// query
$select = $table->select();
$select->where('deleted = ?', '0' );
for( $i = 0; $i < count( $columns ); $i++ ) {
if( $i == 0 ) {
$select->where( $columns[$i] . ' > ?', '0' );
} else {
$select->orWhere( $columns[$i] . ' > ?', '0' );
}
}
$select->where('disabled = ?', '0' );
// print query
echo $select->assemble();die();
Above code result into following query:
SELECT `mytable`.* FROM `mytable` WHERE
(deleted = '0') AND
(column1 > '0') OR (column2 > '0') OR (column3 > '0') AND
(disabled = '0')
But I want something like this:
SELECT `mytable`.* FROM `mytable` WHERE
(deleted = '0') AND
(column1 > '0' OR column2 > '0' OR column3 > '0') AND
(disabled = '0')
How it is possible ?
Thanks
I think this should do the job:
// table
$table = new Model_MyTable_DbTable();
// data
$columns = array('column1', 'column2', 'column3');
// query
$select = $table->select();
$select->where('deleted = ?', '0');
$myOrParts = array();
for ($i = 0; $i < count($columns); $i++) {
$myOrParts []= $table->getAdapter()->quoteInto($columns[$i] . ' > ?', '0');
}
$myWhere = implode(' OR ', $myOrParts);
$select->where(new Zend_Db_Expr($myWhere));
$select->where('disabled = ?', '0');
// print query
echo $select->assemble();
die();

Categories