Getting collection names in mongodb php - collections

Here's my db:
$db = new Mongo("mongodb://u:pw#server.com:37068/dbname");
I want to echo a list/array of collections (the names) on the database.
How can I do that? Thanks.

You can use listCollections: http://php.net/manual/en/mongodb.listcollections.php
<?php
$db = new Mongo("mongodb://u:pw#server.com:37068/dbname");
$list = $db->listCollections();
foreach ($list as $collection) {
echo "$collection \n";
}
?>

Related

How do I query a non-Wordpress table?

I have created a new table within the same database that contains the Wordpress tables. How can I query this specific table from a custom php page? The page can't seem to connect to the db. It's been a while since I have had to do any php/mysql coding, so forgive my ignorance.
<?php
global $wpdb;
$sql = "SELECT * FROM table_name";
$result = mysqli_query($wpdb, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["ID"]. " " . $row["content"]. "<br>";
}
} else {
echo "0 results";
}
?>
$wpdb is an instance of the wpdb class. You make calls to it like the following:
global $wpdb;
//Prepare a SQL Query
$query = $wpdb->prepare("SELECT * FROM my_custom_table WHERE column_foo = %s", $someVariable);
$results = $wpdb->get_results($query);
foreach($results as $result) {
//do a thing
}

wordpress plugin visit counter not working properly

I'm trying to make a plugin for wordpress visitor counter and limit counts of visitor. I wrote this code, but counter variable increments 4 times or 5 times every time I refresh the page. Basically plugin is for free visits after free visits user should be logged in to visit website. Please check the code.
<?php
$count=0;
function ip_get(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$new_ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$new_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$new_ip = $_SERVER['REMOTE_ADDR'];
}
global $wpdb;
$table_name=$wpdb->prefix.'wbs_user_ips';
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(empty($results)){
$wpdb->insert($table_name,
array(
'user_ip'=>$new_ip,
'user_count'=>'1',
'user_status'=>'0',
)
);
}
if(!empty($results)) // Checking if $results have some values or not
{
$count=0;
foreach($results as $row){
$ip= $row->user_ip;
$count=$row->user_count;
}
if($ip==$new_ip){
echo $count++;
$wpdb->query($wpdb->prepare("UPDATE $table_name
SET user_count=".$count."
WHERE user_ip = %s",$ip));
}
}
}
?>
I think there is an error in your foreach loop.
And please make sure you use the correct hook
<?php
$count=0;
function ip_get(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$new_ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$new_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$new_ip = $_SERVER['REMOTE_ADDR'];
}
global $wpdb;
$table_name=$wpdb->prefix.'wbs_user_ips';
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(empty($results)){
$wpdb->insert($table_name,
array(
'user_ip'=>$new_ip,
'user_count'=>'1',
'user_status'=>'0',
)
);
}
if(!empty($results)) // Checking if $results have some values or not
{
$count=0;
foreach($results as $row){
$ip= $row->user_ip;
$count=$row->user_count;
if($ip==$new_ip){
echo $count++;
$wpdb->query($wpdb->prepare("UPDATE $table_name
SET user_count=".$count."
WHERE user_ip = %s",$ip));
}
}
}
}
?>

how to list a repeater sub fields with label and value in Advanced Custom Field?

I've searched and I could not find any solution to list a repeater field rows with Label of sub field and its value.
in my case I want to list a repeater field sub fields with Label and value.
for example :
'Sub Field Label' = 'Value'
is there any way to do this ?
If you know the labels you want to retrieve from your Repeater Field, just use the standard method:
if( have_rows('repeater_field_name') ):
while ( have_rows('repeater_field_name') ) : the_row();
echo 'Label = ' . get_sub_field('sub_field_name') . '<br>';
endwhile;
endif;
If you aren't in a single post/page or outside The Loop, just add the $post_id as the second parameter to your ACF function calls. For example: have_rows('repeater_field_name', $post_id).
If you don't know the label names, I guess you could use get_fields() to get an array of all custom fields for the current post and iterate it. Something like:
$fields = get_fields($post->ID);
foreach ($fields as $field_type => $field) {
if ( $field_type == 'repeater_field' ) {
foreach ($field as $row) {
foreach ($row as $label => $value) {
// In this case you should be aware that
// $value could be an Array too...
echo $label . ' = ' . $value;
}
}
}
}
Anyway, I recommend you to take a look at ACF Documentation. It's complete, clear and with lots of code snippets covering the most common uses.
<?php $args = array('post_type' => 'post');
$the_query = new WP_Query( $args );
query_posts( $args );
while ( have_posts() ) : the_post();
$field = get_field_object('field_name');
echo $field['label']; //print label name
echo the_field('field_name'); //and its value
endwhile; wp_reset_query(); ?>
please try this hope help to you

Counting the while result at Wordpress

I have this featured listing and the code needs to count how many active featured listings the user has. I can't make the while loop work here in Wordpress. Here's code I used:
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=post&posts_per_page=-1&author='.$user_ID);
$FeaturedAdsCount = 0;
while ($wp_query->have_posts()) : $wp_query->the_post();
$featured_post = "0";
$post_price_plan_activation_date = get_post_meta($post->ID, 'post_price_plan_activation_date', true);
$post_price_plan_expiration_date = get_post_meta($post->ID, 'post_price_plan_expiration_date', true);
$todayDate = strtotime(date('d/m/Y H:i:s'));
$expireDate = strtotime($post_price_plan_expiration_date);
if(!empty($post_price_plan_activation_date)) {
if(($todayDate < $expireDate) or empty($post_price_plan_expiration_date)) {
$featured_post = "1";
}
}
if($featured_post == "1") { $FeaturedAdsCount++; }
endwhile;
$wp_query = null; $wp_query = $temp;
echo $FeaturedAdsCount;
The output of $FeaturedAdsCount is always zero no matter if the user has active featured listing. Am I missing something here? Sorry I am still learning in PHP, self-taught actually. :) Thank you in advance!
PS: This is the postmeta table:
Okay! After hours of reading and googling, it says that when calling get_post_meta inside a loop, always use get_the_id() instead of $wp_query->post->ID or $post->ID. So the final get_post_meta must be like this to get a result inside a loop:
$post_price_plan_activation_date = get_post_meta(get_the_id(), 'post_price_plan_activation_date', true);
$post_price_plan_expiration_date = get_post_meta(get_the_id(), 'post_price_plan_expiration_date', true);
Thank you xphan for all the advice! :)

Manipulate last iteration of foreach inside foreach

I've created an array from three variables I get through a foreach loop.
//loop through each post
foreach($loop as $p){
//get the meta and taxonomy data
$term_list = get_the_term_list($p, "mountains",true);
$name = trim($term_list, "1");
$wtr_long = get_post_meta($p,"wtr_longitude",true);
$wtr_lat = get_post_meta($p,"wtr_latitude",true);
//Add to Array
$map_array[] = array ($name => $wtr_lat . "|" . $wtr_long);
}
Now this array should then deliver the data that will populate my Google Map, for that I'm using below code.
foreach( $map_array as $a){
foreach ($a as $key => $value) {
$pieces = explode("|", $value);
$trimmed_key = trim($key, "1");
$name = trim($trimmed_key);
?>
{latitude: <?php echo $pieces[0]; ?>,
longitude: <?php echo $pieces[1]; ?>,
html: <?php echo $name; ?>},
<?php }} ?>
This works almost fine (although it's probably not the cleanest code, tips on this would be appreciated too). The issue I'm having is that in the last iteration of the foreach inside the other foreach, I need to add a ], resulting in:
html: <?php echo $name; ?>}],
I can escape a single foreach, but doing it on a foreach inside a foreach is driving me insane. Any help would be greatly appreciated.
I'm not a php develeopor but you may want to build the collection of strings you want to echo first. After the collection is complete you can add the ']' the the last element. Then echo each element in your collection of strings.
atbyrd, your approach gave me a couple new issues, but did in the end lead to a working solution. Thank you for that.
Below the code that fixed the issue, hope it'll be helpful to someone else in the future.
//Add to Array
$map_string .= '{latitude: "' . $wtr_lat . '", longitude: "' . $wtr_long . '", html: "' . $name .'"},~!~';
//$map_string .= '{latitude: "' . $wtr_lat . '", longitude: "' . $wtr_long . '", html: "name"},~!~';
}
//Remove last three "~!~" characters from the string not to have one too many arrays when exploding
$clean_map_string = substr($map_string, 0, -3);
//Now I can explore on ~!~ to get a new array with the correct strings
$map_array = explode('~!~', $clean_map_string);
$i = 0;
$length = count($map_array)-1;
//Inserts all the markers
foreach($map_array as $value){
if( $i != $length ){
echo $value;
}
else {
echo str_replace("},", "}],", $value);
}
$i++;

Resources