Jump to content

Troubles with array


i73
Go to solution Solved by .josh,

Recommended Posts

I have never been the strongest with Arrays, I think its because I am all self taught. But I have a quick question. I have a ton of items in a table that I want to arrange by different variables and display accordingly to what the user chooses. Lets start with the basic, newest first to oldest. I know I use auto increment to find out what is the newest and oldest. Now how would I display that with a limit of 15 or 20 items? 

To me I don't understand how the "next" page would know that you were just at items 1-20 then the next page at 21-31 Also if I have missing vales in the auto increment 'lets say a person deletes their item' will this affect the display, If value 10 was missing will it only show 1-9-11-20 'so only 19 items on page one?  

 

Thanks for y our help! And sorry for being so retarded here...

Link to comment
Share on other sites

  • Solution

I have never been the strongest with Arrays, I think its because I am all self taught. But I have a quick question.

So you mention arrays, but it sounds like you're really talking about databases and selecting data from it, so I'm going to go ahead and assume you're just using the wrong word.

 

I have a ton of items in a table that I want to arrange by different variables and display accordingly to what the user chooses. Lets start with the basic, newest first to oldest. I know I use auto increment to find out what is the newest and oldest. Now how would I display that with a limit of 15 or 20 items?

Auto Increment is usually only used for automatically generating a unique id for a new row, commonly known as the "id" which is used as the primary key or index column. While you could maybe use this to sort "oldest" to "newest", normally you would sort by some other column, such as a date/timestamp column. The query for that would look something like this:

 

// sort ascending
SELECT * FROM tablename ORDER BY column ASC

// sort descending
SELECT * FROM tablename ORDER BY column DESC
Read up on basic database handling. That tutorial demonstrates basic database interaction, and the example script includes the code principle for allowing users to sort tabulated data on a page. Caution: this is an old tutorial. While the principle remains true, the actual code used is out of date. For example, don't use mysql_xxx syntax, as it's deprecated. So, read the tut, learn the principle, apply it to modern code syntax.

 

Now how would I display that with a limit of 15 or 20 items?

The query would then look something like this:

 

// sort ascending, maximum of 15 results returned
SELECT * FROM tablename ORDER BY columnname ASC LIMIT 15
LIMIT n tells the database to only return n rows from what was selected.

 

To me I don't understand how the "next" page would know that you were just at items 1-20 then the next page at 21-31

This is what is called pagination. The principle is the result of using an offset with LIMIT [offset],[result limit]. For example, let's say you have 20 rows, and you want to put 5 rows per page, for the first page you'd do this:

 

// sort ascending, maximum of 15 results returned
SELECT * FROM tablename ORDER BY columnname ASC LIMIT 0,5
So by default, when you use LIMIT n, you are telling the db to return a maximum of n results. But if you have 2 numbers separated by a comma, the first one specifies an offset, and the 2nd one specifies the limit. So in the example, we have 20 rows total, and we want 5 per page. So for the first page, the offset is 0, because internally, the row list starts at 0, not 1. Then we tell the db to only give us 5 results from how many we select (which we selected all results, since we don't have any conditions to filter the data or anything). For page 2, we'd use LIMIT 1,5. For page 3, we'd use LIMIT 3,5 and so on.

 

The database determines which rows are offset markers by how many results are returned from the query divided by the limit you specify. So if you do a select query that yields 20 results, and you put a limit of 5 on it, well 20 / 5 = 4, so there are 4 offset markers (0-3). So offset 0 tells the database to give you rows 1-5, Offset 1 tells the database to give you rows 6-10, #2 : 11-15 and #3 : 16-20. If there are say, only 18 rows, then the last offset would only have 3 rows.

 

Also if I have missing vales in the auto increment 'lets say a person deletes their item' will this affect the display, If value 10 was missing will it only show 1-9-11-20 'so only 19 items on page one?

It will not affect how many results are shown per page, except for whatever "remainder" is left over from the result set, on the last page. For example, if you have 20 rows and select all 20, and use pagination to show 5 rows per page, you will have 4 pages with 5 results each. If you delete the 2nd row on page 1 and then refresh the page, what will happen is the query will select 5 rows per page the same as before. So when you refresh, it will still show 5 results on page 1, but what you had as the first row on page #2 will now be bumped up to the last row on page #1, and then on your last page, there will only be 4 rows.

Link to comment
Share on other sites

So you mention arrays, but it sounds like you're really talking about databases and selecting data from it, so I'm going to go ahead and assume you're just using the wrong word.

 

 

Auto Increment is usually only used for automatically generating a unique id for a new row, commonly known as the "id" which is used as the primary key or index column. While you could maybe use this to sort "oldest" to "newest", normally you would sort by some other column, such as a date/timestamp column. The query for that would look something like this:

 

// sort ascending
SELECT * FROM tablename ORDER BY column ASC

// sort descending
SELECT * FROM tablename ORDER BY column DESC
Read up on basic database handling. That tutorial demonstrates basic database interaction, and the example script includes the code principle for allowing users to sort tabulated data on a page. Caution: this is an old tutorial. While the principle remains true, the actual code used is out of date. For example, don't use mysql_xxx syntax, as it's deprecated. So, read the tut, learn the principle, apply it to modern code syntax.

 

 

The query would then look something like this:

 

// sort ascending, maximum of 15 results returned
SELECT * FROM tablename ORDER BY columnname ASC LIMIT 15
LIMIT n tells the database to only return n rows from what was selected.

 

 

This is what is called pagination. The principle is the result of using an offset with LIMIT [offset],[result limit]. For example, let's say you have 20 rows, and you want to put 5 rows per page, for the first page you'd do this:

 

// sort ascending, maximum of 15 results returned
SELECT * FROM tablename ORDER BY columnname ASC LIMIT 0,5
So by default, when you use LIMIT n, you are telling the db to return a maximum of n results. But if you have 2 numbers separated by a comma, the first one specifies an offset, and the 2nd one specifies the limit. So in the example, we have 20 rows total, and we want 5 per page. So for the first page, the offset is 0, because internally, the row list starts at 0, not 1. Then we tell the db to only give us 5 results from how many we select (which we selected all results, since we don't have any conditions to filter the data or anything). For page 2, we'd use LIMIT 1,5. For page 3, we'd use LIMIT 3,5 and so on.

 

The database determines which rows are offset markers by how many results are returned from the query divided by the limit you specify. So if you do a select query that yields 20 results, and you put a limit of 5 on it, well 20 / 5 = 4, so there are 4 offset markers (0-3). So offset 0 tells the database to give you rows 1-5, Offset 1 tells the database to give you rows 6-10, #2 : 11-15 and #3 : 16-20. If there are say, only 18 rows, then the last offset would only have 3 rows.

 

 

It will not affect how many results are shown per page, except for whatever "remainder" is left over from the result set, on the last page. For example, if you have 20 rows and select all 20, and use pagination to show 5 rows per page, you will have 4 pages with 5 results each. If you delete the 2nd row on page 1 and then refresh the page, what will happen is the query will select 5 rows per page the same as before. So when you refresh, it will still show 5 results on page 1, but what you had as the first row on page #2 will now be bumped up to the last row on page #1, and then on your last page, there will only be 4 rows.

 

 

Thank you josh, I just one last question. How would I go to the next page on the pagination? like from the 20 rows if I am on the page with 5 rows how would I get to the 6-11?

Link to comment
Share on other sites

You'd build pagination links. Usually people have them somewhere at the top and bottom of all the data that's echoed out. And usually people do it with links that look similar to this: "First Prev 2 3 [4] 5 6 Next Last" Where there's links to go to first and last page, next and previous, and some links to go to a couple pages around the the current page. There is an example of how to do this in the pagination tutorial link I gave you, and virtually any other "pagination" tutorial out there will demonstrate this, and virtually any script you find out there will include it, as it's one of the signature features of a pagination.

Link to comment
Share on other sites

Basically all your questions seem to boil down to "How do I paginate my data?" to which the summed up answer this this: read the tutorial links I gave you, or google "php pagination tutorial" if you wanna look for some up-to-date coding examples (again, as I mentioned before, the principle is timeless, but the code in the links I gave you is out of date). IOW Pagination encompasses virtually all of the functionality you're looking to do.

Link to comment
Share on other sites

Basically all your questions seem to boil down to "How do I paginate my data?" to which the summed up answer this this: read the tutorial links I gave you, or google "php pagination tutorial" if you wanna look for some up-to-date coding examples (again, as I mentioned before, the principle is timeless, but the code in the links I gave you is out of date). IOW Pagination encompasses virtually all of the functionality you're looking to do.

Honestly man, thank you very much! have a good one! I would donate but I am a student!!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.