SQL Query master detail style - php

I am working on a php+jquery phone app. The dificulty I am having is with the SQL database calls. I have successfully connected to the Joomla Database. I have retrieved simple information, such as a list of tables. What I want to do, and what I am needing: a list of of data from a specific catagory..(jos_content) that will be restrained by a section id, and a state.
$st = $db->prepare("SELECT * FROM `jos_content` WHERE state=1 and sectionid=6");
this works. This returns a small list that I can sort and filter to get the most recent article entry. BUT, I want to display another select statement when this entry is selected. I have it where it navigates to another page, but how do I relate the 2 statements?
So, you get a list (list of 1 item) that is a featured article. Then clicking on it brings you to the article. and will display the img, text and author and date.
Any help would be awesome. I am just learning this SQL database stuff slowly.
Best,
Corey

I'm confused by what you mean by "I want to display another select statement when this entry is selected". If I understand you correctly, you'll need to use the columns your retrieved from the SELECT statement you've shown to construct a URL

Related

MySQL - Full vs partial row retrieval

I'm programming a website using PHP/MySql to allow visitors to search for real estate listings.
The main page shows the list of advertised apartments, displaying just a small subset of all the available attributes included in the MySql table that contains the apartments listed. The full set of attributes for each apartment is only shown on a secondary webpage, once the user selects a result from the list in the main page. So, if for example, the available features included in the database's table are price, location, number of rooms and surface area, the main page only displays price and location in the results list, and the remaining attributes are displayed only when the user selects a specific result from the list.
I'm wondering what is the best strategy in order to ensure fast response from the database and achieve the highest possible amount of concurrent users: Should I retrieve ALL the columns from the table when showing the full result list of results and avoid querying the database when the user selects a given result (since I already have all the data I need to show), or should I only extract the minimum amount of columns to display in the results list (price and location, following the example above), and fetch the remaining columns for a specific record only when the user selects a specific result?
I'm querying a single table (no joins or complex queries, although I do use a where clause) and the results list is expected to show around 30 to 50 records at a time. I don't have any data regarding how many of the results in the list shown are selected by the user to see additional info, but I would say it's resonable to say that it will select around 60% of them.
Thanks in advance for your help!
I'd fetch the first few rows and then use endless scrolling techniques via ajax. Be sure to have a (sometimes a little outdated) static list of all entries (meaning: cache them) linked to from every page. That way Google can reference every "sigle view object pages".

how design for table that has about 50 column and select user desired column

I have table that has 50 column(there isn't any oneToMany relation in it). when admin want to add new row for about 20 columns of this table he can decide which column to show in user side for example:
table has columns: description, address, dateOfConstruct, dateOfRebuild, stars...
in admin panel user can fill mentioned input and each of them has check box that if checked that field can be must shown on user site.
How can I design this? and how should I do select query in user side?
This is an application problem, not a database problem.
Have the app should construct the SELECT statement based on what the user specifies in the UI.
You can always do something like
SELECT $column1, $column2, $column3
FROM ...
of course after validating input, etc. and select only what is necessary and then only display what you have got from the db.
However, if this is not going to cause too much load on the DB I would select everything and show/hide columns depending on what user have selected. A little bit hakish, but it will do the job.

PHP echoing a specific field from a table in MySQL

Firstly, I'm quite new to PHP having only dived in some three weeks ago but loving it as a new thing to learn! I have a specific problem that I cannot seem to find a solution for via Google. I'm running a test page that will form the basis of a final product for a local recreational club that runs competitions and wants to display the results online on their website.
I've created a MySQL database and called it 'results' and imported as a CSV a sample of competition results. My code to connect to the database works as the page displays the "Database Connection Established" message.
The database contains a table called 'z_any_year_results' and the table structure looks like this:-
Record_Number Field Value
1 Field_1 Value_1
2 Field_2 Value_2
3 Field_3 Value_3
4 Field_4 Value_4
5 Field_5 Value_5
I understand how to select the specific table using
mysql_select_db("results") or die(mysql_error());
$data = mysql_query("SELECT z_any_year_results FROM results")
but I need to echo a specific field from the table in a specific section of the web page. So for example, in one section of the page I need to output the field containing the value Field_1 and nearby on the page the field containing the value Value_1. But in another section of the page I need to output the field with the value Field_4 and nearby on the page, the field containing the value Value_4. So I guess my problem is how to extract a specific piece of data from a table to the exclusion of all other records in the table and outout it as an echo on the web page. I cannot find anything on the web that is written in a simple step-by-stepway to help novices like myself understand.
Can anyone point me in the right direction on how to achieve this?
Many thanks in advance.
You are using a type of data design known as key/value design. In other words, each row has the name of a data item and its value. That's not an ideal sort of design for a beginner to use, because it makes for fairly intricate queries.
To answer your question, if you want a certain named field's value you use this query.
SELECT Value FROM z_any_year_results WHERE Name = 'Field4'
But, maybe you want a design that resembles your application's entities a little more closely.
You might have an entity, a table, called, contestant, another called contest, and another called prize.
contestant is a table with columns like contestant_id, surname, givenname, email etc
e.g. 1 , Ellison, Larry, larry#oracle.com
Then you can use queries like SELECT * FROM contest WHERE YEAR(datestart) = 2016 which will make your queries more closely reflect the logic of your application.

MySQL fields in URL

Is there a way to call field rows in a URL without using the column name??
So I currently have a posting site where users can select category or subcategories of choice from drop downs, how it's currently setup my site outputs links to the categories chosen such as..
topics.php?category=Food&sub_cat=Pies
topics.php?sub_cat=Pies
This allows users to go to either one of the links, or both
topics.php?category=Food&sub_cat=Pies
To give more functionality I am looking at adding textboxes instead of drop downs, the problem is users will more than likely enter the data in different boxes than other users, ie.
User 1. catbox: Food subcatbox: Pies
User 2. catbox: Pies subcatbox: Food
So in this case my current URL system won't return accurate results, so my question is would there be a way where "category" or "subcategory" could be replaced and just put the results together without them being listed in 2-5 different fields therefore not returning all the results that = to that value? "food" or "pie" in this example.
topics.php?xxx=Food&xxx=Pies
or
topics.php?xxx=Pies&xxx=Food
Looking at So homepage if you click "php" it will put php in the URL, click mysql and it will put "php+mysql" that sort of thing.
you can use parent child method in your database.your table would be like this
id - parent_id - category_name - depth
when you want to insert a data to your table it's depth will be one plus it's parent depth
when someone post to your page you first take query witch of the inputs has most depth then that will be your subcategory.
Calling field rows via parameters in your URL may be a very bad idea. It's a perfect way to allow a massive SQL injection attack. So, the answer is probably "yes, but HOLY MOLY PLEASE DON'T!"
Now it may be that your code is parsing these out on the back end and protecting them via any of a variety of methods, I can't tell from the amount of code posted.

User Reviews: Implementation of Comments - What technologies to use?

I have set up a company intranet website built with PHP/MySQL and allow users to post reviews. After joining up on this website I have grown to like the "comment" function and would like to add that same functionality to allow users to "comment" directly to other users reviews.
Currently all reviews are stored in a single table in the DB.
1) Should I create another table to then store all the comments since there can be many comments per review?
2) Once I figure out where to store these values can the rest of this functionality be built out in PHP or will other programming need to also be introduced?
Sounds like a good plan. You can have a table like Comments(commentID, reviewID, comment_body, ...). You can then insert a new entry when adding a new comment, or select all comments with a given reviewID to display comments for a given review.
Yes, you will almost certainly implement this in PHP (the same language you use in the rest of your application). You'll also have to edit some HTML, and maybe javascript as well.
Yes and yes.
Comments should be a seperate table, because they're comments, not reviews. They are two different things, therefore, they should not go in the same table.
Once you've created that table with the appropriate references to other tables, it's just a matter of constructing a query which pulls out all of the information you need (e.g. SELECT user.user_name, comment.comment_text, comment.post_time FROM comment, user WHERE comment.user_id=user.user_id AND comment.review_id = 123, where 123 is the ID of the review you're getting comments for).
The exact layout for your comment table will depend on your specific needs, but as a minimum, you'll want to know which review it's a comment for, who posted it, when they posted it, and what they actually posted.
To insert comments, create a form on the page that displays the individual review, and when filled in, create an INSERT query which inserts into your comment table.

Categories