patternphpMinor
A set of queries in PHP to show occupancy of various cabins
Viewed 0 times
showphpoccupancyqueriesvarioussetcabins
Problem
Okay so as I learn more about PHP and really strive to improve my code I have a few questions about a current setup of mine and how to approach it in the best way.
I think I have all the tools necessary to get this done the right way, but I am having trouble putting them together. I am also trying to separate my code better.
Scenario: the script in question is cabins.php which basically pulls information from the db to populate the page with cabin information based on the cabin id $_GET variable. So the user clicks on a cabin, then the page is populated with that cabins information. There are four sub menus to choose from for each cabin so I am accessing this information using a switch statement, but I am having trouble finding the best way to run my queries.
My data is structured that most of the cabin information is held in 'cabin_content' but I also have 'cabin_large_images' and 'cabin_small_images' which each hold images that are tied to the cabin_content table through cabin_id.
Each sub section requires different information from the cabin_content table, but each sub section ALWAYS requires all the images from cabin_large_images that match the cabin id.
So each section is like this:
-we must query cabin_content and get the specific information we need
-we must query cabin_large_images and get all the images with the cabin_id.
One section requires all of the above plus we have to query cabin_small_images and get all the images from that table with matching cabin id.
So right now I have it set up like this, which I know needs improvement...obviously that is why I am here...Pseudo code:
```
if(isset($_GET['cabin')){
//run the query to get cabin_large_images because we know we need them regardless of the section
if(isset($_GET['type'])){
$type = $_GET['type'];
switch ($type){
case 'a':
//run the query on cabin_content for the specific info we need in this situation
break;
case 'b':
//run the query on cabin_con
I think I have all the tools necessary to get this done the right way, but I am having trouble putting them together. I am also trying to separate my code better.
Scenario: the script in question is cabins.php which basically pulls information from the db to populate the page with cabin information based on the cabin id $_GET variable. So the user clicks on a cabin, then the page is populated with that cabins information. There are four sub menus to choose from for each cabin so I am accessing this information using a switch statement, but I am having trouble finding the best way to run my queries.
My data is structured that most of the cabin information is held in 'cabin_content' but I also have 'cabin_large_images' and 'cabin_small_images' which each hold images that are tied to the cabin_content table through cabin_id.
Each sub section requires different information from the cabin_content table, but each sub section ALWAYS requires all the images from cabin_large_images that match the cabin id.
So each section is like this:
-we must query cabin_content and get the specific information we need
-we must query cabin_large_images and get all the images with the cabin_id.
One section requires all of the above plus we have to query cabin_small_images and get all the images from that table with matching cabin id.
So right now I have it set up like this, which I know needs improvement...obviously that is why I am here...Pseudo code:
```
if(isset($_GET['cabin')){
//run the query to get cabin_large_images because we know we need them regardless of the section
if(isset($_GET['type'])){
$type = $_GET['type'];
switch ($type){
case 'a':
//run the query on cabin_content for the specific info we need in this situation
break;
case 'b':
//run the query on cabin_con
Solution
There is nothing wrong with using multiple queries. Three simple queries versus a single, convoluted monster query. Guess which option will cause you the least problems long term?
You get the extra overhead of parsing three queries, but, its about the same amount of work for the DBMS to get the data in both cases.
Also I think you should consider holding your images in separate files and just hold the file names in the database. You can then use HTML
You get the extra overhead of parsing three queries, but, its about the same amount of work for the DBMS to get the data in both cases.
Also I think you should consider holding your images in separate files and just hold the file names in the database. You can then use HTML
"" tags to reference these. This will make the images "cache"able on the client side, and, will move the work of retrieving the images to the web server, which is much more efficient and will allow for a degree of parallelism when loading the users page.Context
StackExchange Code Review Q#6607, answer score: 3
Revisions (0)
No revisions yet.