Last posts text limit characters in phpbb - php

I found this code for showing last posts in phpbb forum. I wanna set charatcters limit in post_text.
<?php
// Now let's output the content
// A ted vypsat obsah
while ($row = $db->sql_fetchrow($result))
{
$url = generate_board_url() . "/viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}"; //added fedforum to url
$urlmini = generate_board_url() . "/memberlist.{$phpEx}?mode=viewprofile&u={$row['poster_id']}"; //added fedforum to url
//old line $url = generate_board_url() . "viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}";
echo '<small><a target="_blank" href="' . $url . '">' . $row['post_subject']. '</a><br>'. $row['post_text'] .'<br>od: <a target="_blank" href="' . $urlmini . '">' . ucwords($row['username']).'</a>' . ' v '.'<font style="color:#aaa;">' . date("H:i",$row['post_time']).'</font>', '<br><br></small>';
}
?>
Does anybody know how to set characters limit for $row['post_text'] ?

you can use
substr($row['post_text'],0,40);
or change 40 to any limit as you want

Related

Listing number of rows by group

I have a database with members joining and I am trying to display the number of members from each country.
Here is the code I'm using to show the country flags:
<?
foreach( $sorters as $sortvalue )
if( $sortvalue == '' )
echo '<li> <a href="' . $list_url . $connector . $sortfield .
'=none">None given</a> </li>';
else
echo '<a href="' . $list_url . $connector . $sortfield .
'=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $countrynum . ' fans<br>';
}
?>
I have really looked for the answer, and keep seeing the same type of code, but I haven't been able to get it to work for me.
I've tried this:
$countrynum = SELECT COUNT(name) FROM $table WHERE country = $sortvalue;
which I inserted after the foreach statement. This (and every other iteration of this I've tried gives me Parse error: syntax error, unexpected 'COUNT' (T_STRING) in...
The closest I've come to solving this is by using this code (though, I shouldn't have to reconnect to the db should i?):
<?
foreach( $sorters as $sortvalue )
if( $sortvalue == '' )
echo '<li> <a href="' . $list_url . $connector . $sortfield .
'=none">None given</a> </li>';
else
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT country, COUNT(*) FROM $table GROUP BY country') as $row) {
echo '<a href="' . $list_url . $connector . $sortfield .
'=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $row['COUNT(*)'] . ' fans<br>';
}
?>
This gave me the correct row (member) count, but the same single flag was displayed beside each number instead of the corresponding flag with number of members.
I'm sure this is probably super simple for lots of you, but I'm a newbie who's really trying to learn coding and so far that mostly means cutting, pasting and LOTS of trial and error.
Any assistance (and learning resource suggestions) would be very much appreciated. :)
Since the flag image source is depending on$sorters and $sortvalue, it will be same for all country. Look at the loops. For each $sorters as $sortvalue, the link will be multiple countries but the img source will be same for all.

Call specific variable based on what another variable is set to.(PHP)

I am not exactly sure how to word this in the form of a short question...the above called up many different answers that were not related, but I apologize if this has been asked before.
I am trying to make a variable name itself based the value of another.
I could simply create if statements for each one, but if I were able to declare the variable the way I want, it would save me about 20-30 lines of code, and make future additions much easier.
Here is a better description.
This is the code that I am using at the moment. It is within a shortcode function for wordpress, to create a button based on user-given parameters.
extract(shortcode_atts(array(
'size' => 'medium',
'link' => '#',
'text' => ''
), $atts));
$large_button_img = of_get_option('large_button_arrow_upload');
$button_pos = of_get_option('button_image_position');
if($button_pos == 'right' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'>" . $text . "<img src='" . $large_button_img . "' id='button_img' alt='button image' /></a>";
}elseif($button_pos == 'left' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'><img src='" . $large_button_img . "' id='button_img' alt='button image' />" . $text . "</a>";
}else
{
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
In the above:
The function pulls the value of "size", "link" and "text" from the user given shortcode and generates a button. It sets the class based on the size...
At the moment I have it to where the user can set different images for a large, medium and small button.
Question:
IS IT POSSIBLE
to return the name of the image source based on what size is set to.
soooo basically the equivalent of?
img src = '" . $($size)_button_img . "'
Where it places value of $size in the name of the variable that it pulls to tell it which image source to pull afterwards? (so the proper equivalent of the above would produce something like
img src= '" . $large_button_img ."'
or, if the user has medium selected
img src= '" . $medium_button_img . "'
If possible this save myself having to write if-statements for every possible option (basically copy the set of if-ifelse-else from above, everytime I have a new size setting available)...which eventually may become more of an efficiency issue.
Thanks in advance for any help that can be given :)
ALSO
Please ignore any syntax errors in the above set of code...I am working on this as you read this, so more than likely, if you see something wrong, it has already been fixed.
You can simple use function for your logic:
function somethingWithImage($img, $pos) {
if ($pos == 'right' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'>" . $text . "<img src='" . $img . "' id='button_img' alt='button image' /></a>";
} elseif ($pos == 'left' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'><img src='" . $img . "' id='button_img' alt='button image' />" . $text . "</a>";
} else {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
}
And just call it whenever you want with any arguments.
What you are looking for, are variable variable names.
Short example:
$hello_text = 'Hello World!';
$bye_text = 'Goodbye World!';
$varname = 'hello';
echo ${$varname}_text; // Hello World!
// Is the same as:
echo $hello_text; // Hello World!
As I mentioned, you can use interpolation to create variable variables:
$src = ${"{$size}_button_img"};
But may I suggest using arrays instead? You get cleaner and understandable code:
$sizes = array(
'large' => ...,
'medium' => ...,
);
if(isset($sizes[$size]))
$src = $sizes[$size];

Facebook PHP SDK - Graph API Like

Can someone help me with this, if you go to: https://developers.facebook.com/tools/explorer and use Graph API, GET Method on Connections->Home - You can grab your news feed.
In that newsfeed, the array shows some information based on each individual feed item.
One thing on there is likes which has a sub value of count which counts the number of likes.
Problem is, when I change it to use my applications and access token, the information is still there, expect the count part.
This is really confusing, I have read_stream as a scope.
My code the fetch the likes is:
if(isset($news['likes']['count'])) $likes_count = $news['likes']['count'];
else $likes_count = 0;
Which is part of my foreach loop.
For good measure, here is the function:
function newsitem($profile_pic,$from,$to,$message,$picture,$name,$link,$caption,$description,$icon,$time,$comments,$likes)
{
if($to) $to_section = '<div class="to" >to</div><div class="news-friend-name"><strong><a>' . $to . '</a></strong></div><div class="clear"></div>';
else $to_section = '';
if($message) $message_section = '<div class="message">' . $message . '</div>';
else $message_section = '';
if($picture) $picture_section = '<div class="external-image"><img src="' . $picture . '"/></div><div class="news-external">';
else $picture_section = '<div class="news-external" style="width: 410px;">';
if(!$link) $link='#';
if($name) $name_section = '<div class="news-title"><h3>' . $name . '</h3></div>';
else $name_section = '';
if($caption) $caption_section = '<div class="news-caption"><i>' . $caption . '</i></div>';
else $caption_section = '';
if($description) $description_section = '<div class="news-desc">' . $description . '</div>';
else $description_section = '';
if($icon) $icon_section = '<div class="news-icon" ><img src="' . $icon . '" /></div>';
else $icon_section = '';
$time_converted = time_elapsed($time);
$news = '<div class="news">
<div class="news-friend-thumb"><img src="' . $profile_pic . '"/></div>
<div class="news-content">
<div class="news-friend-name"><strong><a>'. $from . '</a></strong></div>'.$to_section.
'<div class="clear"></div>' . $message_section . $picture_section . $name_section . $caption_section . $description_section .
'</div>
<div class="clear"></div>
<div class="comment-like">' . $icon_section . $time_converted . ' ago • ' . $comments . ' comments • ' . $likes . ' likes</div>
</div>
</div>';
return $news;
}`
I'd appreciate the help, thanks!

PHP/Wordpress - Including an IF function inside an Echo

I have the following WordPress query:
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
echo '<div class="' . $tax_term->slug . '" style="display:none; background:url(' . $whatdoiputhere . ');">';
while ($my_query->have_posts()) : $my_query->the_post();
In the background URL, I need to include an image URL that is generated by the following code:
<?php if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url(); ?>
How would I include this IF function inside the background URL?
I tried this but as you can guess it didn't work:
background:url(' . if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url(); . ');
Would appreciate some help as to how I would do this.
<?php
if (function_exists('z_taxonomy_image_url')) $background_url = 'background:url(' . z_taxonomy_image_url() . ')';
echo '<div class="' . $tax_term->slug . '" style="display:none; ' . $background_url . '">';
?>
Ternary operator:
background:url(' . (function_exists('z_taxonomy_image_url') ? z_taxonomy_image_url() : '') . ');
I'd do something like this:
<?php
if (function_exists('z_taxonomy_image_url')) {
echo "background:url('" . z_taxonomy_image_url() . "');";
}
?>
If you wanted, you could set a default background image via an else clause, otherwise the background definition will just not exist in your CSS.

PHP/WordPress: override page_id in foreach loop

I have a WordPress site and currently the code it set to create titles based on the page title. The titles are: Interior House Painting, Exterior House Painting and Commercial Painting. I would like to override the titles to remove the word "house". This is the code currently:
<?php
// interior_painting: 18
// exterior_painting: 25
// other services: 36
$page_ids = array(18, 25, 36);
$images = array('servicesInterior.jpg', 'servicesExterior.jpg', 'servicesOther.jpg');
foreach ($page_ids as $key => $page_id) {
$page_post = get_post($page_id);
$page_custom_key = 'home_page_info';
$page_link = $page_post->post_name;
$li_class = $page_id == 36 ? 'noMargin' : '';
$title = $page_id == 18 ? 'Interior Painting' : $page_post->post_title . " ";
$title = $page_id == 36 ? 'Commercial Projects' : $page_post->post_title . " ";
$title = $page_id == 25 ? 'Exterior Painting' : $page_post->post_title . " ";
echo '<li class="' . $li_class . '"> <a href="' . $page_link . '">';
echo '<img class="alignright size-full wp-image-349" title="servicesInterior" src="/wp-content/uploads/2011/04/' . $images[$key] . '" alt="" width="200" height="95" /></a>';
echo '<h2>' . $title . '</h2>';
echo get_post_meta($page_id, $page_custom_key, true);
echo '<a class="btn-find-more" href="' . $page_post->post_name . '">FIND OUT MORE</a></li>';
}
?>
The output is this: Interior House Painting, Exterior Painting, Commercial Painting. How do I get "house" removed from "Interior House Painting"?
$title = ucwords(trim(strtolower(str_replace('house', '', $page_post->post_title))));
Though, I am a little curious as to why you would not prefer to just remove the word from the title altogether.
ADDITIONALLY: Somebody else mentioned using preg_replace. That's also entirely possible, but it would be best to do a case-insensitive search:
$title = trim(preg_replace('/house/i', '', $page_post->post_title));
Just use preg_replace('House'|| 'house','',/*Variable Page title is in*/);
You could try replacing the line:
echo '<h2>' . $title . '</h2>';
With something like this:
if (is_page('Interior House Painting')) { echo '<h2>Interior Painting</h2>'; }
if (is_page('Exterior House Painting')) { echo '<h2>Exterior Painting</h2>'; }
else { echo '<h2>' . $title. '</h2>'; }

Categories