Issues in sorting image-wise - php

I have a table in which there are fields like user name, name, age, activity. In this if new user is added then an image is shown beside that user name.
Until now sorting was done by activity by default, but I want all the newly added users to be shown first in the table by default, how can i sort in this case?
Any hint?

Without knowing, well, anything, about how you're implementing this, it's a bit tricky.
If the data from the table is coming from a database and you're already sorting based on some activity field, then your query just needs to sort on an additional field: instead of ORDER BY activity you might have ORDER BY isnew, activity or ORDER BY image, activity.
Alternatively, if the data is just in a PHP data structure, then you could just do a two stage render of your table - iterate through and write out rows with an image, then again for the ones without.

You need to change your sql query - something like:
SELECT * FROM `users` ORDER BY `created` DESC

Related

MYSQL Queries - my brain is exploding

this is my first question here, sorry if i'm breaking any etiquette.
I'm kinda into coding, but sometimes my brain is hard to swallow path of logic steps.
Currently i'm working on my own small web-app, where i have public events, and i'm making my own guestlist.
By the time, i've solved these things:(i think so)
Getting from Facebook page all events;
From current event view get all attendees;
Live search function and ordering array alphabetically.
To Do:
Complete CHECK button function - when checked, one get removed from
lis;
Other analysis functions.
Problem:
Currently i'm getting all attendees from JSON string, then converting to array, putting it all in database. I can't decide on SQL logic.
I have all list with people - json->array->db then it reads from db and show wich is checked wich one not, like comparing with table that is from JSON.
Current algorithm is - getting json, and in foreach cycle, everytime i load it writes in DB, using INSERT IGNORE it ignores if it's same userid, so i have db of all atendees.
How to arrange my database? I'm thinking about making tables:
guests - USERID ; EVENT ID; NAME; [for huge list of all people]
checkins - USERID; CHECKEDEVENTID; DATETIME; [for getting stats]
My goal is to make "Checking In" door-app, so in the end i see, that those and those users are attending more on those kind of events, than these one...
So how could i make like stats, like - EVENT - attended Y people of X, and more global SQL queries, like, USER Y came to EVENTS A,B,C. Or, most checkings happening at timespan [probably some bars or chart]....
Should i make for each event new table to store all guest there to see all atendee statistics, and checking table for checkin stats?
For the what you refer to as the "Check" feature, it sounds like you want (roughly*) the following tables:
create table users
(
userid float NOT NULL,
username varchar(64)
);
create table events
(
eventid float NOT NULL,
eventname varchar(64),
eventstart date,
eventlength float
);
create table checkin_activity
(
userid float not null,
eventid float not null,
checkin_time date
);
* This is a highly simplified database schema. You'll want to make sure you add the necessary keys, constraints, etc., and make sure the data types on your columns are appropriate. I didn't give that much thought with this quick example.
Using the entries in the USERS and EVENTS tables, you'll populate the CHECKIN_ACTIVITY table with what you refer to as the "Check" button. You can join queries against these tables as needed to run reports and so on.
NOTE: You mention:
Current algorithm is - getting json, and in foreach cycle, everytime i load it writes in DB, using INSERT IGNORE it ignores if it's same userid, so i have db of all atendees
You should avoid writing to the database within a for loop (into a table I didn't account for above; let's call it the EVENT_ATTENDEES table). Instead, build an INSERT ALL query and executing it once so you're not hitting the database's transaction handler n times.
INSERT ALL
INTO event_attendees (eventid, name) VALUES (1, 'John')
INTO event_attendees (eventid, name) VALUES (1, 'Jane')
INTO event_attendees (eventid, name) VALUES (1, 'Colin')
SELECT * FROM dual;
This is especially important if this kind of load is something you'll be doing often.

Maintaining history table for PHP Application

I want to maintain the history table for my application to track what all the fields were changed by the user.
The following is my bugs_history table structure -
id, bugsid, userid, field_changed, old_value, new_value, created_on, created_by
So my query is, when I'll update my form and submit, how to get the field name that was changed along with old value and new value. And add the modified changes in the above history table.
I have googled a lot for this but didn't get as per my requirments. Please let me know how to achieve this.
If you know the field names (you have a HTML form containing them, so you probably know the names) then build a list of them that you then loop through, building a new SELECT query to get old_value and then an INSERT query to save it. The select would order on created_on DESC and LIMIT 1.
But I see a clear problem here: concurrency. What happens when two users try to edit the same bug (with the same bugsid) at the same time? They would expect the old_value to be the same for both? Or should the two operations be executed sequentially? Or should the last one to edit be warned that he's trying to edit stale data? Which one would get the "latest" created_on?
This right here is your real problem, not writing the code that generates two SQL queries.

Many new MySQL columns with one form

I want to put a form on my website to let users add events to their private calendar. For that, I would like to create a new column in MySQL each time the user add an event (always with the same form, one column for each event title, for example...)
Is it possible?
It is possible using ALTER TABLE
However, would it not be better to have a table called 'events' that holds all of them, with a column called 'userid' which contains the ID of the user the event belongs to.
Then you would know that every event exists in that table, and to get a users events you simply query that one table for rows that contain the users ID in the userid column.
Yes, but it's a very very very bad idea.
Add a table userevents, add a record to that. To get the output you want, have look for how to do pivot queries in mysql.
For this type of app. You can use magic websql (client side database)!
Web SQL Database is a web page API for storing data in databases that can queried using a variant of SQL.
More: http://html5doctor.com/introducing-web-sql-databases/

Tagging pages with PHP and MySQL

I'm fairly new to MySQL and PHP, but I'm getting the hang of it slowly.
I'm working on a site with profile pages. I have a users table in my database and I intend to create a profile table too.
Before I explain my 'problem' I'd like to say I haven't actually began coding an attempted solution. I like to plan my work before I start it, especially when it is going to be complex. Therefore, I can't really display any snippets of code.
What I would like to create is a tagging system for the user profiles. ie. let the profile owner select keywords from an existing list, and associate them with his profile page.
After pondering plenty and reading up on relational databases I gathered I should start by creating two additional tables:
1. tags (tag_id, tag_name)
2. tag_rel (tag_id, user_id)
and create a relationship between each profile to the different tags a user assigned to it, on seperate rows:
(Tag1, UserA)
(Tag2, UserA)
(Tag1, UserB)
etc.
Then when I search for profiles I simply have to select all user_ids with a specific tag_id. Easy enough.
My issue is the PHP and MYSQL Query code when inserting or changing the list.
I would essentially want the user to be able to either choose from a check-list or from a drop-down select list (or anything equivalent) of existing Tags, check or uncheck his selection, and send to enter it to the database.
I can imagine the initial entry of the selected tags is easy enough to achieve with a foreach() function and a simple INSERT query, and enter the array from the form one value at time.
But when the user goes back to change the tags, how would I instruct a query to keep some tags, delete others, and insert new ones in? What is the best method to do something like this?
An Idea:
Should I delete all the existing rows the user has in tag_rel, and insert the new array?
I would use a procedure like this:
at first you fetch the tags a user selected out of the db and pass them to an array (SELECT from tag_rel where user_id = $userId)
then you would go over to let the user operate on the array (adding new tags, deleting old one)
at last you would delete the old entries and insert the completely new ones from the array provided
the simplest method is to delete tag_rel data for that particular user_id then re-insert the new ones

How would I show the latest registered users with php and sql

I just wondering if there is any way could display the latest users by getting it from the database and displaying the most recent registrations the script i am using is written in php and mysql database Thanks
You can use the order by clause to sort based on the registration date (if you're saving it in your database.
You can also only return a certain number of results with the limit function.
If you use auto_increment for user_id field in user table, you might get the latest registered user by select the last inserted record (LIMIT 1) order by user_id from the DB.
Anyway, the best way to deal with this is to add registration_date field to the table as the two answers above said.

Categories