Wordpress database query results not displaying - database

So, I'm creating a Wordpress plugin which, when activated, creates a new table in the database and then stores entries from a form into that table.
The form submission is working perfectly and entries are successfully storing in the table.
Now I'm trying to create a shortcode which will retrieve a list of all the entries from the table and display them in a dropdown menu so that the use can select which entry to view.
The query to retrieve the data from the table appears to be working, but the "foreach" loop is not populating the dropdown list.
Here is a screenshot of the table contents:
And here is the code for the shortcode:
function cpe_history_select(){
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
global $wpdb;
$history_tbl = $wpdb->prefix . "cpe_history";
$history_query = "SELECT id,submitDate FROM $history_tbl WHERE userID=$user_id ORDER BY submitDate DESC";
$cpe_history = $wpdb->get_results ( $history_query );
if(!empty($cpe_history)){
$output = '<form action="" method="post">';
$output .= '<select id="cpe_history" name="cpe_history">';
$output .= '<option value="">Select</option>';
foreach($cpe_history as $cpe_entry){
$entry_id = $cpe_entry->id;
$entry_date = $cpe_entry->submitDate;
$ouput .= '<option value="' . $entry_id . '">' . $entry_date . '</option>';
}
$output .= '</select>';
$output .= '</form>';
}
return $output;
}
add_shortcode('cpe-history','cpe_history_select');
So, as you can see, I am trying to get just the "id" and "submitDate" fields from the table and display the list of Submit Dates as options in the dropdown list, with the entry's "ID" as the value of the option.
I am logged in as the user with user_id = '1', so there is definitely entries to display, but I'm only getting the initial "Select" option and nothing else.
http://sandbox.graphicdetail.co.nz/cpe-test/
Where have I gone wrong?
PS - Weird thing is, I've created plugins before making similar queries and looping through the results and have not had this sort of issue before. :-/

OMG! I found the problem and I can't believe it was something so simple and obvious!
It was a simple typo!
On the line:
$ouput .= '<option value="' . $entry_id . '">' . $entry_date . '</option>';
I had simply left the first "t" out of "$output"! DUH!

Related

how to get checkbox value as checked from database in php?

in database values are stored as "A,B,C,D,E,F" etc. now i have to fetch those data in the form for update. i have to check the checkbox if it matches the value with database value.
$rs=sql("select sectionName from sections where sectionName != ' ' order by sectionName");
$cat_options="";
foreach($rs as $d){
$option=$d['sectionName'];
$cat_options.= "<label><input type='checkbox' name='category[]' value='$option'>$option</label><br/>";
}
It retrieve list of category from one table.
$a = explode(",", $category);
$a is array of name of category that should be checked in list of category.
I already knew that there are lots off topics on net related to my topics. I tried them but no luck.
$query="SELECT sectionName from sections";
$result = #mysql_query ($query);
while($drop=#mysql_fetch_array($result))
{
echo "<input type='checkbox' name='category[]' value=$drop[sectionName]>$drop[sectionName]";
}

Trying to Select Single Value from Joomla 2.5 Database

I am trying to determine if a certain module is published or not. Here is the script I am using to query the database:
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('published');
$query->from('#__modules');
$query->where('module = mod_modulename');
$options = $db->loadObjectList();
When I try to return $options; I am getting a blank screen. Why won’t this just tell me if it is a 1 or 0 value and how can I fix it? Thanks.
Well I have it cleared up that my query was incomplete, so that you all for those notes. I now am having trouble getting the values that are loaded in the object list to print on the screen. I have tried return print_r and a foreach loop but nothing is showing up. Is there a way to test and find out if the object list is empty? It should not be as I see the value in the database table...
You forgot adding the following
$db->setQuery($query);
So completed your query would be
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('published');
$query->from('#__modules');
$query->where('module = mod_modulename');
$db->setQuery($query); //this is what you forgot
$options = $db->loadObjectList();
Edit: You also don't need to set $db as reference anymore (&)

Joomla 3 JDatabase how to echo rows

I can't find valid statement for fetching rows and displaying items in JDatabase.
My code looks like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('item_id, item_name'));
$query->from('#__items');
How to print out these items in table?
The docs page is here: http://docs.joomla.org/Accessing_the_database_using_JDatabase/3.0
you need to add something like this:
$db->setQuery($query);
$results = $db->loadObjectList();
This will give you an array of objects where each object is a row.
This page: http://docs.joomla.org/Accessing_the_database_using_JDatabase/1.5 is for Joomla! 1.5, but still has (imho) the best list of the possible functions to get your data. Most are still valid I think.
To output the $results array you use something like this:
foreach ($results as $row) :
echo $row->item_id;
echo $row->item_name;
endforeach;
Try like this:
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('item_id, item_name');
$query->from('#__items');
// sets up a database query for later execution
$db->setQuery($query);
// fetch result as an object list
$result = $db->loadObjectList();
For more detail see the link Accessing the database using JDatabase/3.0 . For more methods of how to fetch result you can use loadResult(),loadRow(),loadAssoc(),loadObject(),loadResultArray(),loadRowList(),loadAssocList().
You can also refer this Accessing the database using JDatabase/1.5 .
You can also refer this link for how to
Developing a Model-View-Controller Component/3.0/Introduction
Developing a Model-View-Controller Component/2.5
Hope it help you.
You don't add the array to your select query, you simply add the values, separated by a comma, like so:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('item_id, item_name')
$query->from('#__items');
$db->setQuery($query);
$results = $db->loadObjectList();

Nesting Tags In CakePHP

I am trying to get a image to display in a hidden area, this is a section of code I have copied from another page that someone else made. So it all works, you click the button and the hidden area appears and I can get data from the table I made to display and change the css around that, no problem.
$tmp .= $html->tag('span',$html->tag('h3',
The complete code is below, but the line above is where I am working. This Is the start of the hidden area. What I would like to do is have the img image tag inside the span tag. However all my code does, at this time, is to print/echo the location I have put in the database but not display the image. I am not sure if this is something I have done wrong with where I have put the file, webroot/img/events/tempimg.jpg, what is does print is 'img/events/tempimg.jpg'. From that I take it my image is in the right place just that I have opened the tags in the wrong way. Or would it be better to close the span tag 1st this open the img tag, if so how would I make sure it only appears in the hidden area and not on the main page.
$tmp = $html->tag('strong', $NewDate. " - " . $EventTitle.'<br />');
$tmp .= $html->tag('div', substr($EventLoc, 0, 10).'... <br />', array('class' => 'EventInfo'));
$tmp .= $html->tag('span',$html->tag('h3',
__d('EventsHeader', $e['Team']['name'] . ' Events ', true), array('class' => 'EventInfos'))
.$html->tag('img', $EventImage, array('class' => 'EventHide hide'))
.nl2br($EventInfo . ' testing more......'), array('class' => 'EventHide hide')
);
Many Thanks for any help
Glenn Curtis.
Just in case this helps anyone else, this is now I have now built the tag up, with not using the HTML helper
$tmp .= "<div class=\"EventHide hide\">
<h3 class=\"EventInfos\">" .$e['Team']['name'] . " Events - ". $EventTitle ." - ". $EventLoc ."</h3>
<div class=\"\"><img src=\"$EventImage\" width=\"250\" height=\"200\"/></div>
<span class=\"EventDisplayDate\">When : $DisplayDate</span>
<span class=\"EventDisplayLoc\">Where : $EventLoc</span>
<span class=\"EventDisplayDes\">Description<br/>$EventInfo</span>
</div>";
Glenn.

Wordpress - Getting all terms from custom taxonomy from posts

So. I created a custom WordPress taxonomy. And I have multiple posts that use that taxonomy with various terms under this taxonomy. What I'm trying to get WordPress to do is spit out all the taxonomy terms from all the posts. I'm going to stick each of them inside a rel="" tag so I can get a little fancy with jQuery.
I accomplished this with plain old WordPress tags like this:
<?php
$posttags = get_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo '<label><input type="checkbox" rel="' . $tag->slug . '">' . $tag->name .
'</label>';
}
}
?>
It works perfectly. Makes a checkbox and label for each tag. But now I need these custom taxonomy terms instead.
I've been fiddling with:
$categories = get_terms('Year-taxonomy', 'orderby=name&hide_empty=0');
$cats = object_to_array($categories);
Not working so far. Am I on the right track?
Not well versed with the WordPress Codex, but managed to figure it out.
First off there's the function:
function get_custom_terms($taxonomies, $args){
$args = array('orderby'=>'asc','hide_empty'=>true);
$custom_terms = get_terms(array($taxonomies), $args);
foreach($custom_terms as $term){
echo 'Term slug: ' . $term->slug . ' Term Name: ' . $term->name;
}
}
Then the function call wherever need:
<?php get_custom_terms('your_custom_taxonomy_name'); ?>
Function call must be same as function name:
get_custom_terms('your_custom_taxonomy_name');

Resources