How do you get multiple fields from a database row? - php

Thanks for reading, I was wondering if anyone knew if there is any way to retrieve multiple fields from a row in a database table automatically. What I mean is something similar to retrieving multiple fields from columns with code like this
SELECT *
FROM table1
WHERE column1 LIKE 'info'
ORDER BY date_added DESC
LIMIT 4
My plan is to have four fields in a row, which will not always contain data, and have php code populate tabs.Some pages may only have one tab while others might have four. The problem is each column will probably need a different name. I don't know if this can be done and don't know if I explained it very well. Any input would be appreciated, thank you.
Sorry for not making it clear. I use that line of code for retrieving rows that have 'info' in the 'column1' column limited to four results. I should probably not have added that line of code into this question. What I am trying to do is have an automated system that will retrieve multiple fields from a single row and populate tabs. At present most rows have part of a youtube video code which, using php, gets inserted into the rest of the code on the webpage. What my goal is is to have multiple youtube videos contained in separate tabs fed in from a database. Some rows might only contain one video while others may contain four.
The problem is that I need to have four columns, one for each video, and I think the columns will need different names. My thinking is if there is data in one of the columns then, due to the code, it will contain the word 'youtube' so if I search that column of that row to see if it contains the word 'youtube' and if it does then add it to the webpage. My problem is I am not sure how to repeat that for the other tabs using other columns. If I each separately in each tab then the pages with only one or two youtube videos will have the extra code remaining in the other tabs.
Sorry this probably doesn't make much sense either. Its like the thinking of a madman. As you can probably tell I am not the most experienced man in the world but thanks for reading.

After some time and research I have come up with an answer to my question. Just to clarify what I was trying to do, I wanted to have 1 to 4 youtube clips on my page with thumbnails below. Only one clip would be displayed at a time.
I created 4 new columns in the database, video1, video2, video3, video4, and in these I put the youtube video code or set them to NULL. Now I created tabs that would only be displayed if the video field was not NULL.
<?php
if ($video1 != NULL) { ?>//skips if there are no youtube videos in database
<div id="tabs" class="ui-tabs">
<ul class="ui-tabs-nav">
<li class="youtbe-li"><a href="#tabs-1">
<img src="http://img.youtube.com/vi/<?=$subject['video1']?>/0.jpg"></a></li>
//uses a thumbnail from youtube
<?php
if ($video2 != NULL) { ?>
<li class="youtbe-li"><a href="#tabs-1">
<img src="http://img.youtube.com/vi/<?=$subject['video2']?>/0.jpg"></a>
</li>
<?php }?>//repeat for the rest
</ul>
<div id="tabs-1">
<div class="youtube-clip">
<iframe width="730" height="436"
src="//www.youtube.com/embed/<?=$subject['video1']?>"></iframe>
</div>
</div>//repeat for the rest
</div>

Related

PHP SQL: Dynamically Creating and Deleting Columns in Database

I'm creating a carousel/image slider plugin for WordPress and I've hit a wall. There's going to be an indefinite amount of user input and I need to know how to handle this.
I currently have six static inputs: transition_time, loop_carousel, stop_on_hover, reverse_order, navigation_arrows, and show_pagination and the variable amount of info will come from the images the user wants to use. So this could be anywhere from zero to infinite.
I want to be able to create/delete X amount of columns in the DB.
So starting out there will be zero images, meaning six columns. If a user adds two images I want to have eight columns, two created. If the user deletes them then I want to go back to my original six.
I'm guessing this is possible but how and is this a good idea or should I just have a set amount of images?
You Are Doing It Wrong™.
Changing a table definition should be an exceptional event.
Use two tables, one to model the Carousel, one to store image information, then link them.
Table Carousel:
id
[more fields here]
Table Image:
id
carousel_id (reference to the containing Carrousel)
[more fields]

Insert an object into MySQL database field

i'm developing a web platform in codeigniter (first time with CI) to calculate quotes for a growing number of different products.
The problem i'm facing is that each of my products have different sets of 10+ options but I want to save this data to just one table in my database. I have previously used a different table for each product allowing the table structure to represent the different sets of options however this isn't very scalable with our growing product range.
After some research it appears one solution would be using the 'serialize' function to store all of my post data (from the quote form) for each product in one column and then unserialize when I want to use this data...
Is serializing the data the best approach and would anyone be able to provide a simple example to show how to handle the insert from a form submission / retrieving the data?
Thanks very much in advance
EDIT: Searching will only ever on a product type or unique id. My thoughts were to have a table like 'id, product_type, product_options' with the product_options containing the serialized data?
EDIT 2: Taking an EAV approach seems like a good shout. I'm used to querying and returning a single result object to be passed into the view ($query->quote_ref, $query->quote_date for example). Could anyone point me in the right direction on how to use the single quote's data when the query would return multiple rows, one for each attribute?
As you mention you would not need to search on the serialized data, then yes this approach will be fine. I would opt for json_encode as the data is more human readable in this form.
Then code will depend on your DAL but a basic example would be:
$productOptions=array($_POST['option1'], $_POST['option2']);//etc will need to validate data
$databaseMapper->product_type='product type';
//your product_options column is suitable sized varchar
$databaseMapper->product_options=json_encode($productOptions);
$databaseMapper->save();
To retrieve:
$databaseMapper->loadById(20);
//$productOptions is a standard php array
$productOptions = json_decode($databaseMapper->product_options, true);
EDIT re displaying in your view.
This is codeignitor specific (whereas the above is not).
Based on code from here: http://ellislab.com/codeigniter/user-guide/general/views.html
In your controller:
//code similar to above to retrieve product options data, ideally contained within a model
$data['productOptions']=$productOptions;
$this->load->view('content', $data);
in your view:
<ul>
<?php foreach ($productOptions as $option):?>
<li><?php echo $option;?></li>
<?php endforeach;?>
</ul>

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.

Content Tagging

I'm trying to create a small Web App to categorize certain type of YouTube videos, when users submits a video they will choose what categories this video falls under and they will tag it with ready-made tags, for example:
Video one - Category: Ad - Tags: cute, funny, has animal in it.
I'm trying to sketch my Database for that (I'm using MySQL), so far I have two ideas.
Idea 1:
Table Videos with ID and Category columns, another table Tags with ID and Tag columns while Videos.ID and Tags.ID are linked together. So when the user tries to filter search results by tags, the query will have more conditions (AND Tag = 'something' AND Tag = 'other thing').
Idea 2:
One table Videos with Category and Tags columns, tags are stored as a string separated by commas, when the user tries to filter search results by tags, the query will more conditions (AND Tags LIKE '%something%' AND Tags LIKE '% other thing%).
So the question is: Is there any better method? I already think that the 1st one is wasteful (Each video might have up to 40 ready-made tags) and the 2nd one is clumsy. If not, which one do you think is better?
Creating a additional table linking video id and tag id together is the correct solution. Filtering is done by creating additional INNER JOIN conditions. A comma separated list just won't do - it drastically limits your selection and query possibilities.
Idea 1 looks good. Creating a separate table for storing tags helps in selection.

Retrieving content from multiple tables .. is possible?

I have 4 tables ...
"content" - Master list of content like title, desc, and contentType
"albums" - Albums made up of several photos
"photos" - Individual photo entries
"photosLinks" - Links photos to album(s)
"tags" - Tags used for searching and finding content
Is it possible to retrieve the photos for the content query if the contentType == 'album' in one query/shot?
For the tags, I can do a simple GROUP_CONTACT() since all we need is the text of the word.
I am not sure of the best approach to handle if(an album) other than reading the result (from php) and then making another query to get the photos linked.
Any tips or advice will be appreciated.
Thanks!!
Thomas
It's possible, but you'd end up with duplicate rows. I.e. you could just join, but you'd end up with something like:
Artist Album Photo
Cowboy Mouth Are you with me? [[Front Cover]]
Cowboy Mouth Are you with me? [[Back Cover]]
If you want some way of associating more than one row in one table to a single row of the result, then I am not aware of a means of doing that. You could do something like store a serialized data structure for one of the photo lists, but that of course doesn't follow the RDBMS philosophy too well.

Categories