SQL Results Pagination - sql

I'm having a bit of trouble paginating my sql query results using PHP. I have tried a few different solutions but have had no success so far. Would someone be able to help me please? Below is my current code I use to display all results however I'd like them splitting into pages of 50.
<?
//query
$data = mysql_query("SELECT * FROM names");
//counts_result
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "ERROR";
}
else {
echo "<div class='search_result_output'>
SEARCH SUCCESS
</div><hr>";
}
?>
<?
//display results
while($result = mysql_fetch_array( $data ))
{
echo "<div class='panel panel-default'>";
echo "<div class='panel-heading'>";
echo "<h4 class='panel-title'>";
echo $result['name'];
echo "</h4>";
echo "</div>";
echo "<div class='game_actions' style='float:right; margin-top:-40px; margin-right:10px;'>";
echo "<a data-toggle='collapse' class='btn btn-custom' data-parent='#accordion' href='#".$result['id']."' title='More Info on ".$result['name']."'><i class='fa fa-plus'></i></a>";
echo "</div>";
echo "<div id='".$result['id']."' class='panel-collapse collapse'>";
echo "<div class='panel-body'>";
echo $result['name'];;
echo "</div>";
echo "</div>";
echo "</div>";
}
}
?>

You have this:
$data = mysql_query("SELECT * FROM names");
instead use this:
$page = (isset($_GET["page"])) ? $_GET["page"] : "0";
$data = mysql_query("SELECT * FROM names limit ".$page.", 50");
Display your links accordingly; add the page value to them. Your paging depends on the get parameter named "page".
EDIT:
If you want to display a link pointing to the 5th page, for instance, then:
echo '5';
4 * 50 = page index * page size. Page index is 4, as on the first page you have no offset. It is difficult to add any more help, since I do not know what kind of paging you need. However, this edit should help you to start solving your problem.

try something like this query and then just just change parameter i.e (3 and 8)
Select * from
(select Id,Name ,ROW_NUMBER() over (order by Id) as RowNum from Person.dbo.pers
) tbl
Where tbl.RowNum between 3 and 8
it gets 3 to 8 rows

Related

Pagination with PDO prepare statement

I am using David Carr's pagination class successfully for a whole-table call (i.e. where I'm calling all the rows in the table without filtering). After including my config and the class, I accomplish it this way:
$pages = new Paginator('3','p');
$stmt = $dbh->query('SELECT count(id) FROM sermons');
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->query('SELECT * FROM sermons ORDER BY date_preached DESC '.$pages->get_limit()); ?>
<div class="paging"><p><?php echo $total; ?> sermons found</p> <?php echo $pages->page_links();?></div>
<?php
foreach($results as $row)
Etc.
That works great, but now I'm trying to implement it for the result of user filtering via forms, and because I can't use an unnamed placeholder in the query, I'm trying to do it with a prepare (which I'm guessing is more secure anyway):
$pages = new Paginator('2','p');
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$stmt = $dbh->prepare("SELECT count(id) FROM sermons WHERE series = ?");
$stmt->execute(array($series));
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ? ".$pages->get_limit());
$results->bindParam(1, $series, PDO::PARAM_STR);
$results->execute(array($series));
// the call
echo $total; ?> sermons found</p> <?php echo $pages->page_links();
When I first get to the results page from the form, it looks like everything is working properly. The call correctly identifies the number of sermons found for the search result, and displays the first two (or however many I set in the $pages statement). But when I click to another page result, no sermons display, the count is gone, and my !isset(found_rows) echo returns a zero as well.
I was originally thinking the issue was that at first I was trying to do this without binding. That is questionable, since without the pagination I can display the page without binding. In any case, I've been trying that with both bindValue and bindParam, but with no luck.
Am I mishandling something in the prepare statement? Or what?
Note: I have the results page displaying fine without the pagination with this:
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ?");
$results->execute(array($series));
// Etc.

Yii2: How to show SQL results?

I want to show the result of this SQL query execution in a view:
$results = Yii::$app->getDb()->createCommand('SELECT * FROM colors')->queryAll();
$people = print_r($results);
<?= $people ?>
But it always shows: 1
The results should be a table with 3 columns and 10 rows. So I don't know why it shows 1. I tried with different tables but always shows the same.
depend of your need you
QueryALL return All rows of the query result.
Each array element is an array representing a row of data
so you could simple iterate over you $result
foreach($result as $key => $row){
echo $row['column1'] . '<br />' ;
echo $row['column2'] . '<br />' ;
....
}
otherwise you can use the queru for build a sqlDataProvider and the manage the models show with widget (eg: gridView)
wrong usage of print_r. see this link. did you mean:
$people = print_r($results, true);
or just use var_dump to avoid confusion.
$results already contains your rows.
This way you'll see this array $results.
$results = Yii::$app->getDb()->createCommand('SELECT * FROM colors')->queryAll();
echo "<pre>";
print_r($results);
exit;

get each post number of views count pdo

i am new in pdo. want to echo number of times post viewed. each time a user view it will add +1 autometically. i created a table views that type int length 16. my code is below
<?php
try {
$stmt = $db->query('SELECT postID, postTitle, postDesc, postDate, views FROM blog_posts ORDER BY postID ASC');
while($row = $stmt->fetch()){
echo '<div>';
echo '<h1>'.$row['postTitle'].'</h1>';
echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
echo '<p>'.$row['postDesc'].'</p>';
echo $row['views']; echo "times";
echo '<p>Read More</p>';
$stmt = $db->prepare('UPDATE blog_posts SET views = views+1 WHERE postID = :postID') ;
echo '</div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
You just prepare update statement not execute so execute the prepare statement .
$stmt = $db->prepare('UPDATE blog_posts SET views = views+1 WHERE postID = :postID') ;
$stmt->execute(array(':postID'=>$row['postID']));

How to set function in HTML while outputing HTML with PHP

Sorry for little weird tittle, english is not my native language, and its little hard for me to express myself about this, so I hope i'll get clearer in explanation :)
I have this piece of code, to fetch all rows from MYSQL db, and when it fetch it out, i want to do some action on everyone of that row. (I want to display different PDF files stored in DB).
Take a look
include_once 'dbcon.php';
$query = mysql_query(" SELECT email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='' value= 'button' > <br />";
}
How to increment title of button name, on every row, so I can use it later to access it via POST method to display PDF?
I want something like this...
1st row : name='button1'
2nd row : name ='button2' etc etc..
When im outputing HTML with PHP it only allow me to read some variables, not some functions stored in quotation marks.
Please help guys! Thanks
You can end the string and concatenate it with an expression. In this case, I use $i++ which works like a counter. It returns the value of $i and then increments $i for the next iteration:
$i = 1;
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button" . $i++ . "' value= 'button' > <br />";
}
But 'row 1' doesn't always have to be the same row. Due to rows being added and removed you may get a different result, so 'button1' isn't a very good name. It would be better if you could use the name or id of the row. So if the person table has a numeric id as well, you could use that:
$query = mysql_query(" SELECT id, email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button{$row['id']}' value= 'button' > <br />";
}
As you can see here, I didn't concat, but instead embedded the array variable in the string. This works similar to normal variables, except you have to put braces around the variable. Of course, you could still concatenate the value as in the first example.
We use [] to create an array of the buttons
I'm not in a live environment so I can't test it.
Accessing via $_POST:
$size = sizeof($_POST['button']);
for($i = 0; $i < $size; $i++){echo $_POST['button'][$i];}
// Code
include_once 'dbcon.php';
$query = mysql_query(" SELECT email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button[]' value= 'button' > <br />";
}
$query = mysql_query(" SELECT id, email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . " ";
}
Yes, i've tried to increment with for loop, but no luck.. This is doing a job very well :)
Good idea. Thanks guys very much!

Webform : how to count and live display results

Update (that one works)
<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load($nid);
if ($node->type == 'webform') {
$count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d', $nid));
$atelier_1 = "sources" ;
$sql = "SELECT count(*) FROM {webform_submitted_data} WHERE data LIKE \"".$atelier_1."\" ;";
$count_atel_1 = db_result(db_query($sql));
}
}
echo $sql;
echo $count_atel_1;
?>
This webform has been submitted <?php print $count ?> times.
We'd like to use a webform so that our students should register on some workshops.
The webform works great. Now we'd like to display live the number of students that are already register in on of the workshops so that the other should know if there remains some possibilities of registration (each workshop can only accept 20 students at the same time)
I'm trying with that that doesn't work ($atelier_1 = "sources" ;) is the name of one workshop :
<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load($nid);
if ($node->type == 'webform') {
$count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d', $nid));
$atelier_1 = "sources" ;
$count_atel_1 = db_result(db_query('SELECT count(*) FROM {webform_submitted_data} WHERE data LIKE %d', $atelier_1');
}
}
echo $count_atel_1;
?>
Any help or suggestion welcome
The only way you can show that live, is by creating an ajax in drupal, or with drupal behaviours, or with javascript.