I am making pagination in core PHP (actually first time). As user will press the pagination button for example user pressed second pagination button. then this query generates:
SELECT * FROM product LIMIT 12, 12
It is executing perfectly as I expected. but the main problem is that if user wants to do filtration on that specific page for suppose user wants all items which are present in page 2 and category_id must be 3 then this query generates:
SELECT * FROM products WHERE category_id IN (3) LIMIT 12, 12
But I am getting the empty resultset... What I am doing wrong?
The pages are based on the number of lines your query returns. You cannot apply a filter on a single page on the DB side as any additional Wheres would change the elements in the pages of the previous query.
In your case, adding WHERE category_id IN (3) results in no lines, making your new page empty.
You could implement filters in your frontend, or backend AFTER the query returns the result, and your filter would remove items from the DB result.
Ideally, every time your users interact with the UI, it would result in a new call to the DB with the filters being applied on the query. then show the result to your user.
If I understood correctly you want to change the WHERE clause in any way between pages and you not want any previous records be repeated.
If you have a unique id in your table and that id increases with each page you can use for a bookmark. I have a simple procedure to make that column. Then you can create a column specifically for this app.
You create a bookmark column and add it to the WHERE clause.
This column will give you a bookmark to save the user's position.
You save this column's value from the last record.
Then in your query you can add this position your query in the WHERE clause
WHERE `bookmark` > $position AND ...
Then you can make any change between any pages at any time you want to the WHERE clause and you will never repeat a previous record.
Making this new column is very simple and does not require much storage.
Use the same ORDER BY from your page query and make your WHERE 1.
$sql = SELECT `product_id` FROM `products` WHERE 1 ORDER BY `category`, `description`";
$results = mysqli_query($conn,$sql);
while(list($product_id) = mysqli_fetch_array($results, MYSQLI_NUM)){
$bookmark[] = $product_id;
}
foreach($bookmark as $position => $product_id){
$sql = "UPDATE `products` SET `bookmark` = $position WHERE `product_id`=$product_id";
}
Related
I have a page that prints a log from the db.
On the page I have a form that can filter the viewed log by several parameters.
I also have separation by page numbers because I show on each page only specific number of records.
The question is, if someone's filters the log by a parameter, aka using the form.
How do I show him a relevant list of page numbers that will include his filter?
The form uses POST, the page number list is just a collection of links to
log.php?page=x
How do I combine them both?
You need two things, the total number of records and the current page records. So you also need to do two queries.
Lets say your WHERE is something like
$where = "filter_a = 'b'";
Then your queries are:
$qAll = "SELECT COUNT(*) FROM table ".$where;
$qPage = "SELECT * FROM table ".$where." LIMIT 10,20
Limit used is an example, but i hope you get the idea
I am building a site where I'm constantly adding new links. The links are organized in a mysql database with id being the main index starting at 1. I only want to display the top five links from the database on my site so, in my php code I assign the database to an array so I can only call the top 5 links to the site. My problem is that every time I add new links to the database I have to change the id numbers manually. I want the newest link to start at id number 1 and the one below that to automatically become id number 2 and so on. Is there a way to do that or is there a better way to organize my database?
dont make any changes to the id(if you're auto incrementing),just modify your Mysql Query to retrieve data from your table by ID in descending order.
SELECT * FROM your_table ORDER BY id DESC LIMIT 5
Seems to be a simple question for someone who knows well SQL, but not for me.
Anyhow, here is a sample table:
Primary (and only) key is on id. So basically the scenario is as fallows. User may add images. Newly added images are inserted with comune_number value 0 avoiding duplicates (on file name = image via PHP). Images inserted can be assigned to a category from another table. The same image can be assigned to many categories, for each is inserted a new row with the category id (comune_number). Relation between the two tables is on comune_number.
I would like to show the images, with a checkbox checked for which is assigned already.
My question is simple. How to include all images once, but where the same image is already assigned, include that comune_number instead of 0. I don't care about the order. The result I would like to achieve is something like this:
I'm aware of GROUP BY, so if I try
mysql_query("SELECT * FROM banner WHERE `comune_number` = 0");
or
mysql_query("SELECT * FROM banner GROUP BY `image`");
I end up with the same (not wanted) result.
Most likely I have to combine two queries in one, but I can't figure out which and how.
Note1: I have tried many combinations in phpMyAdmin, based on my (little) knowledge and on what I found with Google (including Stackoverflow), but none of them resulted as shown.
Note2: MySQL version 5.1
Either MYSQL only or combined with PHP solutions are welcome.
EDIT
I need to keep the comune_number. So when I show the images, I need to know the id.
My queries must rely on comune_number, so when I need comune_number = 10, the result should be as on second image above.
EDIT2
It seems I wasn't made myself clear. So what I want, when user is watching category of id 10, show him all the images once, and mark the ones which were assigned to that category.
Another example is here. So if user is watching category (comune_number) of id=9, show every picture once, and mark the two assigned to it.
Based on your SQLFiddle and all the comments here is the updated query:
SELECT r.*
FROM (SELECT b.*
FROM banner b
WHERE b.comune_number = 9
UNION ALL
SELECT b2.*
FROM banner b2
WHERE b2.comune_number = 0
GROUP BY b2.image) r
GROUP BY r.image;
Live DEMO.
select * from
(SELECT * FROM banner order by
FIELD(comune_number,10,0))
abc group by `image`;
fiddle
Updated:
Query with filter condition:
select * from
(SELECT * FROM banner
where comune_number in (10,0) order by
FIELD(comune_number,10,0))
abc group by `image`;
an important tip. When you use GROUP BY all the field you put in your field list, must be in GRUOP BY clause or must be into an aggragate function. MySql has a strange behaviour, don't sign the error but if you try in another DBMS your query:
SELECT * FROM banner GROUP BY image
You have an error.
MySql applies for fields not present in group by an implicit limit 1.
Solution about your issue:
SELECT * FROM banner b1
WHERE b1.comune_number <= 'YOUR_CATEGORY'
AND NOT EXISTS
(SELECT 'X' FROM banner b2
where b2.image = b1.image
and b2.comune_number > b1.comune_number
AND b2.comune_number <= 'YOUR_CATEGORY')
EDIT
I've changed query, now I put a condition about input category. Where you find YOUR_CATEGORY put the value of category you see (i.e. 9).
I am not sure whether this is the exactly you need, but I think it can be helpful.Try this:
Select if(comune_number = 10, comune_number,0), id, image from table
You may change comune_number as per your convenience. Further nesting in IF condition is also possible.
I am trying to sort the information given to me by the API of an engineering journal. I have extracted the following information into a table:
ID (integer),
Journal Entry Name (Text),
Description (Text),
Page Length (integer),
Has Media (boolean)
Each "Journal Entry" has only one ID associated with it. Each ID also has other characteristics that are not returned by the API but that I want to use to sort. They are:
Category (Things like Econ, Math, Biology. Each ID can have more than one category)
Boolean values (Things like requiring special subscriptions)
I have created a second table in the following format:
ID (integer),
Category (text),
Boolean1 (bool),
Boolean2 (bool),
Boolean3 (bool)
Since each ID can have more than one category, when this occurs another row is added to this table. The idea being that any given row only has one ID and one category in it.
What I want to do is this:
Be able to find the top ten categories when it comes to
Highest Journal Entry (ID) count
Highest Total Page Length
Highest Journal Entry count where the "Has Media" boolean is true
Create a means of navigating like "pagination" where each page shows the nth results of the aforementioned top ten.
So if I chose to Highest Journal Entry count method, the first page would be show IDs, Names, and Descriptions of all the Journal entries in the category with the highest count.
My plan has been to create a new table where the numbers one through ten are in the first column, and then populate the second column with the top ten categories. Then I can use a process similar to pagination in which the nth page only shows the values with the corresponding category from the original value. However I can't seem to be able to make this top ten list/matrix, nor do I know if it there is a better way.
Unfortunately I am not a MySQL or PHP coder by trade, and have only gotten this far by lots and lots of googling. I have been completely unable to find any guides for a navigation method like the one I want. And since I don't know the proper terminology, I am just trying random google searches at this point.
Is this the best way to go about it? Or would it be better to create a third table of some sort? Is there perhaps an easier way to do this with something that can use the PHP and MySQL code I already wrote?
Not sure I really understand what you're going for here, but my best guess is that you probably want to combine your two initial tables and have category be a set rather than an individual term so you can have a single entry per unique ID.
Then you'd just need to write calls for each of your top ten finds as needed. Since each id can have an unknown number of categories I would start with a limit of 10 and then process the returns starting with the top match, grab its categories, if there are more than 10 grab the first 10, if there are less than 10 grab what there are, update the amount you're looking for (if there were 4 then you're now looking for 6), and move on to the next best match.
Maybe something like this:
categories = null;
$delimeter = ',';
$count = 1;
$top10 = array();
$result = mysql_query("
SELECT *
FROM table_name
ORDER BY page_length DESC
LIMIT 10");
while($row = mysql_fetch_array($result) && $count <=10)
{
$categories = $row['categories'];
$id = $row['id'];
$splitcontents = explode($delimeter, $catagories);
foreach($splitcontents as $category){
if (!in_array($catagory,$top10)){
$top10[$count] = array(
'category'=>$category,
'journal_id'=>$id
);
$count++;
}
}
}
I have a database containing 40 pictures. These pictures are displayed using a pager.
I have a URL query: http://www.test.com/photo.php?id=15. This sets the current picture ID to 15. This picture is the 5th one on page 2.
How can I get the page a picture is on given only the ID?
SQL
select * from photo limit 40
PHP
$i=1;
while($r){
if($r[id]=$id) $cur_picRank = $i;
$i++;
}
$curPage = ceil( $cur_picRank / $pagesize);
Is this correct?
Easy-peasy.
You just have to count pictures shown before.
if them ordered by id, just SELECT count(*) where id < $id
So, you will get a position and now can divide it by page size.
For the different ordering you have to get field value first (e.g. to order by name get a name by id and then use it in condition)
As for your approach, it is NOT correct.
Selecting whole table to get just ONE row is always BAD idea.
Though this code would not work anyway. There is no loop exit condition and a counter doesn't count