Today I wanted to show you how easy it is to add a content editor in your Joomla component form.
Today I want to talk about how you would go about creating a CSV file from scratch, with all of your data from the database.
First we want to have the array of States in our code. See Below.
$states = array("AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID",
"IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH",
"NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA",
"WI","WV","WY");
Then you want to create the list (select list options). Above is the simplest way of creating a state array, but right now the key of each array is 0, 1, 2. This is not what we want, we want the key and value of the array to be the same. So this next line fixes just that.
Wanted to show you how easy it is to use the DISTINCT function in MySQL inside of Joomla.
This is very useful whenever you want to create a list of all the individual values of a specific field in a database table. It only gets that value once, so like below, there is several "Burlington" values in the city field
Table: jos_locations
| id | name | city | state |
| 1 | Coffee House | Graham | NC |
| 2 | Barnes & Nobile | Burlington | NC |
| 3 | Sheetz | Mebane | NC |
| 4 | Starbucks | Burlington | NC |
| 5 | Panera | Burlington | NC |
So this is how you would use distinct to just grab that list of specific values.
// Get a database object
$db = &JFactory::getDBO();
$query = ' SELECT DISTINCT city '.
' FROM #__ locations ';
Now we want to Set the Query.
$db->setQuery($query);
Finally we send the query and return the Results and print it out.
$results = $db->loadObjectList();
print_r($results);
The results will be in an object oriented list like this:
Array ( [0] => stdClass Object ( [city] => Graham ) [1] => stdClass Object ( [city] => Burlington ) [2] => stdClass Object ( [city] => Mebane ) )
So let me know what you think, and hope this helped!