Wordpress wpdb select from multipe tables - wordpress

I have a three tables for my wordpress plugin.
videos = id, name
playlists = id, name
video_playlist = id, video_id, playlist_id
how do I get multiple results for multiple tables.
ie, I am busy editing a playlist and would like to display all videos in the playlist.
so the ID for the playlist you are viewing is passed and that is referenced against the video_playlist table to obtain all the video IDs.
Now to take it one step further I would like to also display the names for the Videos.
Here is what I currently have.
<?php if(isset($update)) {
$rows = $wpdb->get_results("SELECT * FROM $table_play_vid WHERE playlist_id = $update->id");
foreach($rows as $row){
echo $row->video_id;
}} ?>

Try something like this.
?php if(isset($update)) {
$rows = $wpdb->get_results("SELECT vp.video_id, v.name FROM $table_play_vid vp, videos v WHERE vp.playlist_id = $update->id and vp.video_id=v.id");
foreach($rows as $row){
echo $row->video_id." ".$row->name;
}} ?>

I think it's a common MySQL query.
SELECT thistable.column, thattable.column FROM thistable,thattable WHERE thistable.something = thattable.something

Related

Want to create a dynamic page to list content by letter

I want to create a dynamic page in WP theme so that when I pass a letter from 'A-Z' it will display all the posts with the title starting from that letter.
Can you please tell me how to proceed?
If you are using themes and getting the index key via a query parameter then you can create a new custom theme file and add the following code to get the list of posts.
$thePostIdArray = null;
$indexkey = $_GET['indexkey'];
if ($indexkey!=null){
$querystr = "
SELECT wposts.ID
FROM $wpdb->posts wposts
WHERE UPPER(wposts.post_title) like '".$indexkey."%'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_title ASC
";
$thePostArray = $wpdb->get_results($querystr);
$i = 0;
foreach ($thePostArray as $currentPost){
$thePostIdArray[$i] = $currentPost->ID;
$i++;
}
After that just just a matter of going through the post array and displaying them.

MODx Revo Gallery: How to set tags by choosing from list and filter albums by them in resourses

I need to get albums from gallery for 'Closes' which has categoties 'dress, skirts...' and collections (each element from subcategory can relate to different collections) and I need get closes elements filtering by categories, collections, and other parameters. But it's difficult to input the same tags in each Gallery item when creating it. How can I make such a catalog with Gallery?
And even more – each gallery item (element) have multiple images of the same item, so now i create Albums -> Subalbums -> Items.
I'm not sure can I do this with Gallery extra? May be use MIGx somehow... and code Gallery.
Im not sure entirely what your after but I would be tempted to create my own PHP snippet that searches the ModX database its self. Eg:
//TEMP VAR SEARCH
$sql = 'SELECT * FROM `modx_site_tmplvar_contentvalues` WHERE `value` LIKE "%WHAT EVER TAGS YOU HAVE%"';
$tvqresult = mysql_query($sql);
$num_rowstvq = mysql_num_rows($tvqresult);
while ($rowtvq = mysql_fetch_array($tvqresult)) {
$contid = $rowtvq['contentid'];
//MAIN QUERY
$mainsql = 'SELECT * FROM `modx_site_content` WHERE `id` = ' . $rowtvq['contentid'] . ' AND `template` = 12 AND `published` = 1 AND `deleted` = 0';
$resultmain = mysql_query($mainsql);
while ($row = mysql_fetch_array($resultmain)) {
echo "[[Ditto? &parents=`134` &documents=" . $row['id'] . " &tpl=`tempchunk`]]";
}//END MAIN LOOP
}
NOTE: This is just normal SQL - with Revo I believe you will had to make some slight adjustments to have it work with PDO.

Paginate, search & sort WordPress $wpdb query for external table

I have a custom table (ERA_Data) in my WP database, which I'd like to add pagination, column sort & search to.
Currently the following code successfully grabs the data (over 4000 entries) & displays them;
<?php
global $wpdb;
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data;");
echo "<table>";
foreach($ERA_Data as $ERA_Data){
echo "<tr>";
echo "<td>".$ERA_Data->PartNo."</td>";
echo "<td>".$ERA_Data->Make."</td>";
echo "<td>".$ERA_Data->Carline."</td>";
echo "<td>".$ERA_Data->Model."</td>";
echo "<td>".$ERA_Data->Description."</td>";
echo "<td>".$ERA_Data->Start_Year."</td>";
echo "<td>".$ERA_Data->End_Year."</td>";
echo "</tr>";
}
echo "</table>";
?>
Just so you know, this is all new to me - so I'm happy to read up on further info, but if anyone can provide some code examples that may help - I'd be extremely appreciative.
With regards to pagination, I think 50 or 100 items per page would do.
I'd imagine sort order & search will be far more useful in any case.
I would juts work on this line for anything you need:
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data;");
so change it like this for showing only a 100, 50, etc. results
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data LIMIT 100;");
or
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data LIMIT 50;");
and for pagination, this link would help so much What is the best way to paginate results in SQL Server
for sorting, depending on your column, use this syntax
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data ORDER BY ***COLUMN NAME HERE***;");
Let me know if you have any questions!

php count users post

i am having some problem counting users posts from db, it's kind of confusing because all the id's/row are in the same table.
the table:
categories | id1 | id2 | title | msg | userid
<?php
$query = "SELECT * FROM topics WHERE authorid='".$id2."'";
$rows = mysql_query($query);
echo $rows['id2'] ;
?>
Thank's
Read the documentation. mysql_query doesn't return rows. And the way you access it, it's more like you expect a single row there. Besides, you may want to connect to the database (depends on your php.ini, though, IIRC).
Try this:
<?php
$query = "SELECT COUNT(*) as count FROM topics WHERE authorid='".$id2."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row['count'] ;
?>
$query = "SELECT COUNT(*)AS count FROM topics WHERE authorid='".$id2."'";
Then after you fetch the row (this will depend on what database library you're using), you can do something like:
echo $rows['count'];

Show Only Pings (Pingbacks+Trackbacks) Number on Wordpress

Is there a way to display just ping counts (number) on Wordpress?
Actually there are comments_number function but that showing total count of comments, pingbacks and trackbacks.
The following code works on WordPress 2.9.1. It may work on other versions, but I only tested it against 2.9.1.
<?php
global $wpdb;
$post_id = get_the_ID();
$total_ping_count = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback'");
$total_approved_pings = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback' and comment_approved = 1");
$post_ping_count = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback' and comment_approved = 1 and comment_post_id = $post_id");
echo "The total number of pings on this site is $total_ping_count.\n";
echo "The total number of approved pings on this site is $total_approved_pings.\n";
echo "The total number of approved pings on this post is $post_ping_count.\n";
?>
The above code gives counts just for pingbacks. If you want trackbacks instead of pingbacks simply change comment_type = 'pingback' to comment_type = 'trackback' or if you want a combined count change it to comment_type IN ('pingback', 'trackback').
Not exactly sure what you want: only show pingbacks? If so, and I haven't tried it, but Template Tags/wp list comments « WordPress Codex shows listing pingbacks and options.

Resources