Separate data from database into pages - php

This is my code:
$query="SELECT * from project where batch='$bach'" or die(mysql_error());
$var=mysql_query($query) or die(mysql_error());
if(mysql_num_rows($var)==0)
{
echo "No such batch exist.<br />";
}
while($arr=mysql_fetch_row($var))
{
echo $arr['batch'];
}
I am just displaying one field right now thats batch but there are 8 columns and many rows would be filled when user inputs the data.. This code will display the content on the same page making the page quite long. How can i split the data into various pages with html and php? A better example can be google which splits the search results into pages and with page numbers at the bottom.

Please chk this tutorial for php mysql pagination http://www.tutorialspoint.com/php/mysql_paging_php.htm .

I would personally recommend two things for you.
Create a router that will take a variable from url , like yoursite/list/11 , it will create a list , with eleventh set of your database values. Use mod_rewrite so you can have "pretty urls"
About PHP routing
You will need to create a function that will count your DB entries , and divide that number into page sets. Say One hundred entries equals ten pages. This will let you create that kind of menu that looks like 1,2,3....,9,10.
Another function which will retrieve the specific set of entries, and then pass it to whatever script runs your "list"
Since your question is not 'help me find error' but rather 'how to achieve this', I'm afraid I cannot offer more than basic logic of things.
Second thing i will recommend is avoiding mysql_ functions , because these are deprecated and will be removed from PHP.

Related

How Do I Make A New Page Every So Many Inputs In PHP and SQL?

I am trying to make it so users can enter text files and a short message near it and it will be visible for everyone. I am using PHP and SQL (MariaDB/MySQL for the database) and I want it so every like ten for example inputs it will make a new page. So for example if there is ten files up on the page then if someone puts another one in it will automatically make a page two and have the oldest file submitted be there. How can I do this. I have seen other sites use a GET method and have it be something like ?page=2 . How can I do this myself?
What you're looking for is "Pagination".
Pagination using MySQL LIMIT, OFFSET
Though that can look a bit scary at first. However, if you use a framework such as Laravel it can easily be done, see:
https://laravel.com/docs/8.x/pagination#introduction

Multiple result sites (pagenumber buttons)

I have a website were the user types in a city (f.e.: Washington), presses search and via ajax json it gets the entries from my database. I never know how many entries it got, so let's say this time I have 40 entries in my database where it says "Washington". Now a function appends all 40 entries on the site.
How do I get it to show only 4 entries and then add number buttons where the user can go to page "X" to see another 4 entries and so on?
second question:
And how do I tell the script to just add a specific amount of number buttons ( In this case just 10, since 10*4=40 already )?
I don't necessarily need a full code example, just a good explanation on how I can do this (without a plugin).
Here a simple drawing to clarify this:
Thank you very much in advance for any answer.
There is an excellent tutorial on tutsplus covering the kind of pagination as seen on your image. You would just need to modify the sql query in the script to match your requirements, point your ajax url to this pagination script, and ensure the results are returned as json. Hope this helps!
To setup the pagination you would need to use mysql LIMIT & OFFSET, also counting the total number of results and returning that in the JSON to set the correct number of pages in the pagination.
You would also need to pass the page number in the ajax call through a GET parameter so you can set the OFFSET correctly and return the correct page.
I tried some jQuery pagination plugins but they were a little buggy. So I chose pure PHP and made a really simple one myself, since the tutorials seemed a bit too overloaded to me.
I just pass pagenr via GET and in my script a set a specific amour of results to be displayed via Limit $start(=0 if pagenr=1), 10
If the pagenr is bigger then "1" it undergoes a for loop which adds up "10" to "$start" every time.
And with these values and COUNT(*) I do the pagination with for and if loops.

Pagination with XML and PHP?

before anyone asks; I've googled my 'question', I've also looks at the 'Questions that may already have your answer' and none of them work.
What I'm wanting to do is 'Pagination'. However, I don't want to use Databases as I've never had to and I'd rather not give up and go to them now as XML does everything I want it for.
The code I have is the following:
$files = glob('include/articles/*.xml');
foreach($files as $file){
$xml = new SimpleXMLElement($file, 0, true);
}
I've tried these ones already: XML pagination with PHP, PHP XML pagination and Pagination Filtered XML file and have achieved nothing. I have also tried a lot of Javascript 'pagination' scripts and still nothing.
So to sum it up: I have four articles (More to be added) and I want to show 2articles per a page. The following information will be 'pulled' from the xml file: ID, TITLE, CONTENT, PICTURE, AUTHOR, DATE by doing $xml->id and so on for the rest of them. Does anyone know of any way of doing this? as I've spent the past four hours (Its 4:04AM GMT) and have found nothing that works yet. (If I find anything that does work I'll make sure to update the question with the working code encase there is anyone else out there that needs help with this too.)
For a start define the order in which you want your articles to appear. I.e. which article goes on page 1, which one on page 2, etc. This is important, because that order will be the base for your pagination algorithm. Please note that glob() is not guaranteed to return results in any specific order, which means the order can change from one invocation of your script to another (notably when you add new articles) -- almost certainly not what you want.
Then the second step is to introduce another variable which is part of your URL that denotes the actual page (number) you're on. The URL query string would be a natural choice for putting this information, so your URL's look like: article.php?page=1. On the PHP side you can use the $_GET superglobal to retrieve the query string parameters.
Thirdly, use the new style URL's whenever you link to your article.php script. Additionally, validate the input --especially when you also want to display the current page based on this parameter (or you will end up with an injection vulnerability). This also means you want to have a default value (in case the value is invalid/wrong/ or not supplied at all for some reason).
Finally, filter your articles based on the two key pieces of information: the order of the articles w.r.t. the page number and the page number: i.e compute the actual articles that should appear on the current page.

multiple links per row in mysql

I have a MySQL database in which each row represents an episode of a podcast. I would like to include show notes for each episode and therefore need to be able to extract multiple links per row via PHP.
What would be the best data field to achieve this? I'm thinking that including the links via a linked table may be the only way to do this, but if anybody knows a simpler way I'd love to hear about it.
I would definitely recommend using a new table (podcast_link) because the number of links per podcast is flexible. Adding a text field to the podcast table wouldn't be very efficient due to the parsing of the links when you want to display them.
This will also allow you to e.g. count the number of links per podcast, so you can display "Show related links (4)" and you can add more fields to the links, so that you don't only display the links, but also a title for the link. Especially going forward you might want to add more information per link.
explode() Function :
You have to save your links for example with this structure :
http:\\example.com\podcast01.mp3,http:\\example.com\podcast02.mp3,http:\\example.com\podcast03.mp3 in the database rows.
then when you want to extract them you can easily use explode() function with this way :
$links= $row['links']; // your links in row
$links_array = explode("," , $links);// now you have an array that you can easily access to each block of it.
for example :
echo ($links_array[0]);// output : http:\\example.com\podcast01.mp3
good luck

sequentially show mysql data using php

I have a php code that fetches data from mysql. The data has (say) 15 rows. I want to display only 5 rows at a time to the user, with links to each of the set (3 in this case) such that when a user clicks on either of the links, the same page will show the corresponding results. Since, php code has the final result set, I don't want a solution that involves me to navigate to other pages and possibly re-calculate the next set of solutions (5~10 or 10~15). How can I do this? Thanks in advance.
If I am using javascript or ajax, how can I achieve this? I don't know javascript much.
You can try loading everything in your page and simulating the pagination thanks to javascript.
An example in jQuery here
It sounds like you want to send the Data as a complete set to the client but not let him display everything to the user. So use Javascript to just show you the pages 1*page_number to 5*page_number (with a for-loop).
By far, the easiest solution in this situation is a Dom-based Grid system. I recommend checking out Datatables. In essence, the Datatables code will take a fully-formatted html table and reformat it to include only the amount of rows you tell it to, with paging on the bottom as you've requested. In addition, you can turn on such features as filtering, toggle-able length, sorting, and selection. Once you get the hang of it, the additional code takes no more than a minute per table and the features are outstanding.
This is really straightforward. Use PHP to output ALL of the table, with all the rows. The only gotcha here is to include the full html, including <thead> and <tbody> Give the table an id such as "example"
Now, include the files that you download from the datatables site--datatables.js and Jquery.js. Instantiate the jquery like this:
$(document).ready(function() {
$('#example').dataTable();
} );
That's it. As you can see from the examples, it's a really cool tool. Good luck.

Categories