How to convert Meters to Foot using PHP - php

I have used this code which shows an error:ie if my meters is 175m but when i convert it does'nt show that thing
<?php
function metersToFeetInches($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*3.2808399;
$valFeet = (int)$valInFeet;
$valInches = round(($valInFeet-$valFeet)*12);
$data = $valFeet."′".$valInches."″";
if($echo == true)
{
echo $data;
} else {
return $data;
}
}
?>
<?php
$feetInches = metersToFeetInches(1.75,false);
echo $feetInches;
?>

This is one of those things you can easily find on Google, but I guess you didn't look further than the first link.
Anyways, you should do it like this.
<?php
function metersToFeet($meters) {
return floatval($meters) * 3.2808399;
}
?>
But why is this better than the code you posted? Well, functions are not supposed to do everything. You should write a function for a certain action, not one big function with everything you can every need. Because if you need to add something to that function or change the order of actions, it's a hell of a lot of work.
Furthermore, your function has an option $echo. But why would you need that? You can add such an option to every function, but PHP has a nice commando for that: echo. So instead, it's way better to write echo metersToFeet(10). Or $value = metersToFeet(10) if you need to save the result in a variable.

<?php
function metersToFeetInches($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*3.2808399;
$valFeet = (int)$valInFeet;
$valInches = round(($valInFeet-$valFeet)*12);
$data = $valFeet."′".$valInches."″";
if($echo == true)
{
echo $data;
} else {
return $data;
}
}
?>

Related

Use return in php function with if statement

Is there a way to use return in a function with an if statement?
I would like to see either the function was executed until the if statement or not.
This would help me to check if the sql query would be executed as well.
I know to 100% I only could check it on the sql response, but I am looking for the shortest way to figure out if the content of a function was executed or not.
Here is an example:
<?php
function hi($i)
{
return (1==$i){echo "Hello"; };
}
$i = '1';
echo hi($i);
?>
I try to avoid to use it like this, since I always require to add an return before the end of the if statement:
<?php
function hi($i)
{
if(1==$i){
echo "Hello";
};
return true;
}
$i = '1';
echo hi($i);
?>
<?php
function ifReturn($input){
$message="";
if ($input == 3){
$message ="input is 3";
} else {
$message = "input is everything BUT 3";
}
return $message;
}
echo '<p>'.ifReturn(3).'</p>';
echo '<p>'.ifReturn(2).'</p>';

Comparison of words from the database and output of the result

I need to check the words received from the database with the user's entered word and if there is a match, then output its value from the database, and if not, then output what the user entered.
The code below works fine if there is a match.
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
}
}
}
}
I'm an amateur in PHP, just learning. And I can't figure out how to output $d_typeplace if no match is found.
I tried to add
else {
echo $d_typeplace;
}
, but I get an array of words from the user entered.
I will be grateful for any help. Also for any suggestions for improving this code.
---Addition---
I apologize for my English. This is a problem in the Russian language, I need to take into account the morphology. To do this, the database has a list of words and their analog, for example, X = Y. I get these words and compare what the user entered. If he entered X, then we output Y. If he led Z, which is not in the database, then we output Z.
Thus, we check $d_typeplace with $d_typeplace_raw and if there is a match, we output $d_typeplace_morf, which is equal to $d_typeplace_raw. And if not, then $d_typeplace (it contains the value that the user entered).
Oh, I'm sorry, I understand myself that I'm explaining stupidly)
I cannot quite understand what you are asking: you need to output the string entered by the user, but you can only print an array?
If this is the case, I think you parsed the string before, in order to therefore you need to do join again the values contained in the array.
Try with:
else {
echo implode(" ", $d_typeplace);
}
--- EDITED ---
Try with:
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
$found = false;
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
$found = true;
break;
}
}
if (!$found) {
echo $d_typeplace;
}
}
}
But I think it would be more efficient, if you implemented the second code snippet written by #Luke.T
I'm presuming you were trying to add the else like this?
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
} else {
echo $d_typeplace;
}
}
}
}
Which was outputting an array because the for loop was continuing, if you add a break like so...
echo $d_typeplace;
break;
It should stop outputting an array. Depending on your use case you could however perform similar functionality directly in your sql query using LIKE ...
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('
SELECT ego_slovoforma_v_predlozhnom_padezhe
FROM dEzpra_jet_cct_tip_mest_obrabotki
WHERE vozmozhnyi_variant_mesta LIKE %' . $d_typeplace . '%');
if ($typeplace_results) {
//Echo result
} else {
echo $d_typeplace;
}
}

Calling a method inside of a method while both are in the same PHP class

I assume this is possible with PHP but I am having some trouble getting it to work. I am trying to minimize the amount of code that is being duplicated by creating only one instance of the html that is supposed to display inside of a php class method. This is the part I only want to create once.
public function display($dbCon){
$content = $obj->content;
$contentSEO = $obj->contentSEO;
$contentLink = $obj->contentLink;
if(!empty($content) && !empty($contentSEO) && !empty($contentLink)){
$content = str_replace("$contentSEO","<small>$contentSEO</small>",$content);
printf("%s", $content);
} elseif(!empty($content) && empty($contentSEO) && !empty($contentLink)){
$content = str_replace("$content","$content",$content);
printf("%s", $content);
} elseif(!empty($content) && !empty($contentSEO) && empty($contentLink)){
$content = str_replace("$contentSEO","<small>$contentSEO</small>",$content);
printf("%s", $content);
} elseif(!empty($content) && empty($contentSEO) && empty($contentLink)){
printf("%s", $content);
} else {
echo "Error";
}
}
Now this is inside of a class and I want the following method to call on this one above after the sql query is performed. The goal with my intent here is that in case I need to call on a similar function with the same HTML, I can simply just call on this one already created instead of coding it all over again. The second portion looks like this.
public function content1($dbCon){
if($res = $this->dbConnection->query("SELECT * FROM content WHERE status = '1' and id = '1'")) {
while($obj = $res->fetch_object()) {
$this->display($dbCon);
}
}
}
Now the simple way would be to simply add them both in the same function and this does work. like the example below.
public function content1($dbCon){
if($res = $this->dbConnection->query("SELECT * FROM content WHERE status = '1' and id = '1'")) {
while($obj = $res->fetch_object()) {
$content = $obj->content;
$contentSEO = $obj->contentSEO;
$contentLink = $obj->contentLink;
if(!empty($content) && !empty($contentSEO) && !empty($contentLink)){
$content = str_replace("$contentSEO","<small>$contentSEO</small>",$content);
printf("%s", $content);
} elseif(!empty($content) && empty($contentSEO) && !empty($contentLink)){
$content = str_replace("$content","$content",$content);
printf("%s", $content);
} elseif(!empty($content) && !empty($contentSEO) && empty($contentLink)){
$content = str_replace("$contentSEO","<small>$contentSEO</small>",$content);
printf("%s", $content);
} elseif(!empty($content) && empty($contentSEO) && empty($contentLink)){
printf("%s", $content);
} else {
echo "Error";
}
}
}
}
However, doing it this way would require me to always copy and paste the same code. when it is needed. I rather just create the function with the query and call on the if statement function to display the HTML. I thought I could simply call it like so inside of the function right after the while statement like so:
$this->display($dbCon);
But instead I keep getting the same error message I planted in the code in case it didn't work. Any help with this would be greatly appreciated.
I think it's because you need to call $this->display($obj); instead of $this->display($dbCon);.

How to add content to wordpress loops?

I've got some if statements for my plugin that I would like to execute for every post.
For now, lets just say I want to add "Hello World" to every post
I've tried quite a few things, but I can't quite seem to figure this out.
Quite simply, in my plugin I have:
add_filter('the_post','testing');
function testing($content){
echo "Hello World";
}
Obviously I'm doing something wrong as Hello World doesn't how.
Can anyone point me in the right direction please?
Here is the working code on my index.php page of my theme:
http://pastebin.com/xd1ree8W
Id' like to put the if statement at the top, within a function in my plugin, so it loads for every post.
So this works on my install using it in the functions.php
i didnt modify the get_the_ID() parts but you could easily use the object like so $obj->ID
function everypost_func($obj){
echo 'This is sort of cool';
if (get_post_meta(get_the_ID(), 'other-link', true))
{
$url = get_post_meta(get_the_ID(), 'other-link', true);
$status = 200;
$price = NULL;
//echo "Looks like you've got other-link selected";
}
elseif (get_post_meta(get_the_ID(), 'generic-asin', true))
{
$status = 100;
$asin = get_post_meta(get_the_ID(), 'generic-asin', true);
//echo "Looks like you've got a generic-asin";
if($_COOKIE['countrycode'] == "GB")
{
$reg = "co.uk";
//echo "and we've detected you're in the UK <br>";
}
else
{
$reg = "com";
//echo "and we've detected you're not from the UK <br>";
}
}
else
{
if(($_COOKIE['countrycode'] == "GB") && get_post_meta(get_the_ID(), 'link-uk', true))
{
$asin = get_post_meta(get_the_ID(), 'link-uk', true);
$reg = "co.uk";
$status = 100;
//echo "looks like you're in the UK, and we have a UK link<br>";
}
elseif (get_post_meta(get_the_ID(), 'link-us', true))
{
$asin = get_post_meta(get_the_ID(), 'link-us', true);
$reg = "com";
$status = 100;
//echo "looks like you're not in the UK, but we have a US link for you<br>";
}
elseif (get_post_meta(get_the_ID(), 'link-uk', true)) {
$asin = get_post_meta(get_the_ID(), 'link-uk', true);
$reg = "co.uk";
$status = 100;
//echo "looks like you're not the UK, but we have a UK link<br>";
}
else
{
$status = 404;
//echo "looks like nothing is here for you<br>";
}
}
if($status == 100)
{
$results = get_aws_details($reg, $asin);
$price = $results[0][0];
$url = $results[1][0];
$wishlist = $results[2][0];
//echo "Success";
}
elseif($status == 404)
{
$price = NULL;
$url = get_the_permalink();
$wishlist = NULL;
//echo "404";
}
}
add_action('the_post','everypost_func');
So taking in consideration that you are doing it in a plugin it will depend on the context you are constructing it.
Example in OOP : add_action('the_post',array($this,'everypost_func'));
Maybe try to make it work within the theme file ie functions.php then if it works as you are expecting it then move it in your plugin surrounded with all the goodies necessary.
-- will update more upon OP commentary
You have used perfects filter for the add content but you have make slightly mistake in filter function.
here you have need add "hello word" in content. bellow filter fulfilled your requirement.
function testing($content){
return $content. "Hello World";
}
add_filter('the_post','testing');
here Hello world add on beginning of the content.
I hope it's work for you.
When I read the headline I thought the following snippet might be what you are looking for. This supposes that your chosen theme uses the
the_content()
function to render the post content. After browsing through your paste bin I'm not so sure if this snippet addresses the code you put in the paste bin, but I'm leaving it on as it does answer the headline and might point you in the right direction.
add_filter( 'the_content', 'add_my_content' );
function add_my_content( $content ) {
return "Hello World, \n" . $content;
}

Yii: Customize the results of CAutoComplete

I need to make a dropdown list using CAutoComplete. Everything is set and works fine, here is my code of the action:
<?php
public function actionSuggestCharacter() {
if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
$name = $_GET['q'];
$criteria = new CDbCriteria;
$criteria->condition='`Character` LIKE :keyword';
$criteria->params=array(':keyword'=>"$name%");
$criteria->limit = 5;
$suggestions = zCharacter::model()->findAll($criteria);
$returnVal = '';
foreach($suggestions as $suggestion) {
$returnVal .= $suggestion->Character."\n";
}
if (isset($suggestion)) {
echo $returnVal;
}
$criteria->condition='`Character` LIKE :keyword';
$criteria->params=array(':keyword'=>"%$name%");
$criteria->limit = 5;
$suggestions = zCharacter::model()->findAll($criteria);
$returnVal = '';
foreach($suggestions as $suggestion) {
$returnVal .= $suggestion->Character."\n";
}
if (isset($suggestion)) {
echo $returnVal;
}
}
}
?>
What this code does is that it shows the first 5 matches with the keyword at the beginning and the next 5 matches are with the keyword in any place.
Example. Let's say a user types in the input field "pdd" (doesn't really matter, could be any text), so the results returned by autocomplete will look like:
1. pddtext...
2. pddtext...
3. pdd_some_other_text
4. pdd_text
5. pdd_text
1. text_text_pdd
2. text_pdd_text
3. etc...
The problem is I need to separate these two blocks by some kind of line (<hr> or <div> with the border). How can I do this?
Thank you.
Can't you do something like this?
<?php
public function actionSuggestCharacter() {
if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
...
if (isset($suggestion)) {
echo $returnVal;
}
echo "Hey this is the delimiter\n";
$criteria->condition='`Character` LIKE :keyword';
....
}
}
?>
And then on the client side check for this string and when you encounter ""Hey this is the delimiter" replace it with your separator.

Categories