Footable or Datatables with 10000 rows - php

I need to show the table with 15 columns, wich have about 10000 rows. When I load simply table without Limit, It loads too slow. Because at first there shows all rows, and after that Datatable hides rows. Maybe there are some solutions for this case?
But I need that it shows all pages count for example:
| < Previous | 1 | 2 | 3 | 4 | 5 | ... | 690 | Next > |
Now I'm using simple foreach ($res as $r){ echo "<tr><td>$r->id</td></tr>"; }
In addition, I would like to add that I have in first column script for popup like:
echo <td>'.$rowid.'</td>;

If you work with Datatables with large data like in your case you need to load data like this
https://datatables.net/examples/server_side/simple.html
and you can also add this option
https://datatables.net/reference/option/deferRender

When returning this amount of rows you should probably be using the Ajax functionality of datatables. More info can be found here:
https://datatables.net/examples/data_sources/ajax.html

Related

Updating Database From Static File JSON Feed

I have a PHP script pulling a JSON file that is static and updates every 10 seconds. It has details about some events that happen and it just adds to the top of the JSON file. I then insert them into a MySQL database.
Because I have to pull every event every time I pull the file, I will only be inserting new events. The easy way would be to search for the event in the database (primary keys are not the same), but I am talking about ~4000 events every day, and I do not want that many queries just to see if it exists.
I am aware of INSERT IGNORE, but it looks like it only uses PRIMARY_KEY to do this.
What can I do (preferably easily) to prevent duplicates on two keys?
Example:
I have a table events with the following columns:
ID (irrelevant, really)
event_id (that I need to store from the source)
action_id (many action_ids belong to one event_id)
timestamp
whatever...
And my data is my JSON comes out on the first pull like this:
event_id|action_id|...
1 | 1
1 | 2
1 | 3
2 | 1
2 | 2
2 | 3
Then the next pull is this:
event_id|action_id|...
1 | 1
1 | 2
1 | 3
1** | 4**
1** | 5**
2 | 1
2 | 2
2 | 3
2** | 4**
I only want the rows marked with asterisks to be inserted, and the others to be ignored. Remember, primary_key column id is completely in this table, and I just use it for ubiquity.
What command can I use to "INSERT" every event I pull, but ONLY adding those that aren't duplicated by way of the two columns event_id and action_id.
Thanks.
Create a unique index of both columns.
CREATE
UNIQUE INDEX event_action
ON tablename (event_id, action_id)

Multi-cURL 5000 URLs

I need to check for broken images with db entries. So now I am selecting all the items from table and using CURL to check it is broken or not. I have almost 5000 items in DB and CURL is taking lot of time. For one result, it is showing the total time as 0.07 seconds. My table structure is the following :
+----+----------------------------------------+
| id | image_url |
+----+----------------------------------------+
| 1 | http://s3.xxx.com/images/imagename.gif |
| 2 | http://s3.xxx.com/images/imagename.gif |
| 3 | http://s3.xxx.com/images/imagename.gif |
| 4 | http://s3.xxx.com/images/imagename.gif |
+----+----------------------------------------+
So is there any other idea to check for broken images?. I think I cannot use LIMITS here as I need to check for all items and then print the result. I have user file_get_contents() but it is also taking lot of time.
What you can do here is the following:
Use multi_curl to cURL the images in parallell.
Specify header only (as you're not interested in the image data) and if the status code is anything but 200 OK (or 302/Found), then the image does not exist.
Chunk the 5000 items first, don't run them all with multi_curl. About 50-100 items at a time is fine.

Why does this query return different values?

I'm trying to pull information from my database, but the query I'm using is only pulling all of the values after the first one. I have no LIMIT set, but I did try setting a LIMIT 0,30 with no change. In phpMyAdmin, the query returns what I expect. In my PHP file, it returns what I've explained.
The query is :
SELECT * FROM `mainSite_others` WHERE forGame='$gameName'
gameName is previously provided, and I suspect no errors because it does return at least two values. The forGame value in the database is all the same, a constant "+Stellar+Dawn".
The PHP code is:
while ($gameOther = $database->fetchArray($gameOtherQry)) {
echo $gameOther['otherName'];
}
Don't worry about the $database->fetchArray part, that is just my DB class, which works fine as far as I know.
The table I am extracting from looks like this (this is with all the values contained):
id | forGame | otherType | otherName | otherDesc
9 | +Stellar+Dawn | Character | Car | Car
10 | +Stellar+Dawn | Item | Brugson Burson | a guy
11 | +Stellar+Dawn | Item | Space Pie | A pie from space
I am using mySQLi.
Any ideas? Thank you.
You're probably doing a fetch call BEFORE you reach the while loop, which "loses" the first row of the results.

PHP - Check if data is in a table if so pass it to $_SESSIONS array

Hi guys this is my first post here, there have been similar questions on this forum but I couldn't find one which accurately answered my question or at least it wasn't relevant enough for me to workout an answer - this is my 3rd day using PHP so I am still getting used to it..
I am making a small game for my site where by users can pay a small amount of money to win a prize, the prize is a code which will be emailed to them.
Currently I have my database and script setup so that the same code cannot be issued twice using the boolean Available which is set to 1 if the code has not been issued and 0 if it has
I have my code table setup like so
CodeID | ItemID | CodeType | Available | Code |
1 | 1 | xxxxxxx | 1 | QLLxbttKQcHv4e4k |
2 | 1 | xxxxxxx | 1 | 6JKMumiH35Dke4SS |
3 | 3 | xxxxxxx | 0 | mQ2WyUjqNy5fJiEr |
What I want to do is check each code to see if it is available, if it is I want to take the ItemID and put it in a $_SESSIONS array which I can use in another page to only return results for codes that I have stocked
I am having some trouble checking if the code is available and if it is putting the ItemID in the $_SESSIONS array
Currently I have this to query the database
$stock = $conn -> query('SELECT * FROM codes WHERE Available = 1');
This checks the database and out puts all codes which are currently available, including duplicate ItemIDs
Can anyone help me so that I only take 1 of each ItemID and put that into the $_SESSIONS array which I can use on the next page to only show results with available codes?
This query
SELECT DISTINCT ItemID FROM codes WHERE Available = 1
will return you only unique ItemIDs that are available
PS: prefer to select only particular columns you need, not everything with *

Foreach group and subgroup dynamic loop

I have this problem. I have a table (below) of groups. It's a recursive sort of table because each new group can have a parent group in the same table. So effectively we have a group > subgroup > subgroup > subgroup kinda model.
**id | label | parent_id**
1 | Ceiling| 0
2 | Window | 0
3 | Wall | 0
4 | Small | 2
5 | Large | 2
6 | Large| 1
7 | Paint | 4
So this would give something that looks like this:
Window > Small window > Paint
I've created the forms and table for creating the groups but it's the database query and loops that I'm having trouble with actually getting data into the above format. Bit too much for my brain to handle :(
I'm doing it in this format because I want there to be complete control over the groups and the depth of the subgroups.
I don't really have code to give an example because it's more the problem solving I'm after.
** UPDATE **
A bit more specific: I want to list each parent group (so a group that has a 0 set in parent_id) and it's immediate subgroup, then that groups immediate subgroup (if it has any) etc, etc.
If you want to do it like this, you will allways have to fetch whole table to PHP and then perform the search with php.
However, there is one similar method how you can managet such structure, it is very vell described here:
http://www.sitepoint.com/hierarchical-data-database-2/

Categories