php count users post - count

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'];

Related

Count number of posts have particular meta key or value?

What I want to achieve is display the number of posts which have particular meta key or value I am getting a list of posts and meta key and value but don't know how to display them I'm storing data using repeatable fields. Storing work properly.
Now, for example, I have age meta value in two posts so how can I count no of a post with age. Age = No of post 2.
My Code :
global $wpdb;
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta WHERE (meta_key = 'repeatable_fields') ");
$array = wp_json_encode($query);
print_r($array);
Outout :
[{"meta_id":"312","post_id":"108","meta_key":"repeatable_fields","meta_value":"a:2:{i:0;a:2:{s:4:\"name\";s:6:\"Zaheer\";s:5:\"phone\";s:3:\"123\";}i:1;a:2:{s:4:\"name\";s:6:\"Sageer\";s:5:\"phone\";s:11:\"09190219218\";}}"},{"meta_id":"323","post_id":"121","meta_key":"repeatable_fields","meta_value":"a:2:{i:0;a:2:{s:9:\"iif_label\";s:4:\"City\";s:11:\"iif_details\";s:7:\"karachi\";}i:1;a:2:{s:9:\"iif_label\";s:3:\"Age\";s:11:\"iif_details\";s:2:\"12\";}}"},{"meta_id":"329","post_id":"126","meta_key":"repeatable_fields","meta_value":"a:1:{i:0;a:2:{s:9:\"iif_label\";s:3:\"Age\";s:11:\"iif_details\";s:2:\"12\";}}"},{"meta_id":"332","post_id":"128","meta_key":"repeatable_fields","meta_value":"a:3:{i:0;a:2:{s:9:\"iif_label\";s:7:\"Country\";s:11:\"iif_details\";s:8:\"Pakistan\";}i:1;a:2:{s:9:\"iif_label\";s:4:\"City\";s:11:\"iif_details\";s:9:\"Islamabad\";}i:2;a:2:{s:9:\"iif_label\";s:3:\"Age\";s:11:\"iif_details\";s:2:\"12\";}}"}]
You could try something like this:
$count_age = $wpdb->get_col( $wpdb->prepare(
"
SELECT count(meta_id)
FROM {$wpdb->prefix}postmeta
WHERE meta_value LIKE '%%%s%%'
",
'Age'
));
More about get_col() here: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column

Convert SQL query into Drupal 7 PHP rules

SELECT address
FROM user_address
WHERE username = '$user->name'
ORDER BY time DESC
LIMIT 1
Here is the SQL query that I can understand. How is it possible to convert it into Drupal's 7 PHP? I'm trying to figure that out for a day, and I tried different approaches, but it seems that I am just bad in that.
You can use db_select :
$results = db_select('user_address', 'ua')
->fields('ua', array('address'))
->condition('ua.username', $user->name, '=')
->orderBy('ua.time', 'DESC')
->range(0,1)
->execute()
->fetchAll();
var_dump($results);
Otherwise you can use db_query if you want to write entire SQL :
$results = db_query("SELECT address
FROM user_address
WHERE username = :username
ORDER BY time DESC
LIMIT 1 ", array(':username' => $user->name))->fetchAll();
var_dump($results);
Finally you can use db_query_range :
$page = 0;
$limit = 1
$results = db_query_range("SELECT address
FROM user_address
WHERE username = :username
ORDER BY time DESC",
$page * $limit,
$limit,
array(':username' => $user->name))
->fetchAll();
var_dump($results);
Try this:
$result = db_select('user_address', 'ua')
->fields('ua', array('address'))
->condition('ua.username', $user->name)
->execute()
->fetchAll();
For that we use db_select() or db_query() - first one preferable.
$query = db_select('user_address', 'u');
// Add condition and fields
$query->condition('u.username', ‘james’)
$query->fields('u’ array('u.address'));
// execute it
$result = $query->execute();
foreach ($result as $record) {
// Do something with each $record
}
For more see
https://www.drupal.org/docs/7/api/database-api/dynamic-queries/introduction-to-dynamic-queries.
update: see condition portion. Also, you can put this in a module or php executable area of your site or via drush command line.
Change the username James to match your need
$result = $result = db_select('usr_address','u')
->fields('u',array('address','uid'))
->range(0,1)
->orderby('time', 'DESC')
->condition('u.uid',$uid,'=')
->execute();
here is how it actually worked.
Thank you for your suggestions, but at the end I made it. By myself. Well, kinda ;D

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!

Show post count in Wordpress

Is it possible to get the exact counter of each post for display in a theme I'm making? So for example if I have 100 posts, the last post will have a number of 100, the one before that: 99, and so on. I can't use the post's ID on this since this is always incremented by 2 (most of the time) making it highly inappropriate as a counter.
Try this, look at the results you will get the idea.
Don't forget the array starts with 0 not 1.
Each array item will contain the post ID. so if Item(key) nr 15 is the same as the current page ID, then the current item is #16(15+1)
<?php
global $wpdb;
$query = "SELECT `ID`
FROM `$wpdb->posts`
WHERE `post_status` = 'publish'
AND `post_type` = 'post'
ORDER BY `post_date` ASC
"
;
$count = $wpdb->get_col($query);
var_dump($count);
moar info: wpdb
Do look at the query if you agree with my order.
You'll have to use a "counter" there, something like this:
$i=1;
while(have_posts):
....
<h2><?php echo $i.' - ';?><?php the_title();?>
...
$i++;
endwhile;

Wordpress wpdb select from multipe tables

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

Resources