How to use array in function to get only value I want - php

I am trying to work this function in wordpress theme the way where I can get value only what I want and not all to gather. I am not much familiar with using array in function so I need you expert help to make it works and I would really appreciate that.
Here is my code
function gallery_artist($arg=null, $arg2=null, $arg3=null){
global $png_gallery_meta;
$png_gallery_meta->the_meta();
$gallery = get_post_meta(get_the_ID(), $png_gallery_meta->get_the_id(), TRUE);
$gallery_value = $gallery['image_artist'];
$artist_info = $gallery_value;
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
$author_full_name = $author_first_name . ' ' . $author_last_name;
$user_id = '<a href=" ' . get_author_posts_url(basename($string)) . ' " title="more submissions of ' . $author_first_name .' '. $author_last_name . ' " >' . $artist . '</a>';
$arg = $author_first_name;
echo $arg;
$arg2 = $author_last_name;
echo $arg2;
$arg3 = $user_id;
echo $arg3;
}
After than I want to use this function to get any value I want and not unnecessary to render all three to gather. Means if I want only first name than I pass value only for that and it will render only first name so on..
Here how I want to use something. but you can suggest any code and type of function I have no issue.
<?php call_user_func_array('gallery_artist', array('first_name','last_name','artist_id') ) ?>
Big Big thanks to you all..

Try replacing the bottom section with this:
if(!is_null($arg))
{
$arg = $author_first_name;
echo $arg;
}
if(!is_null($arg2))
{
$arg2 = $author_last_name;
echo $arg2;
}
if(!is_null($arg3))
{
$arg3 = $user_id;
echo $arg3;
}

Related

PHP: How do I change a variable dependent on another variable? (Newbie stuff)

I'm pretty new to webcoding and would like to improve a bit in php, but I'm already stuck :/
What I am trying to do is to have a sentence like
"Simon likes apples"
Now I have an input form for the name so I can choose the name. So it looks like
"$name likes apples"
<table>
<tr>
<td><input type="text" id="name1" name="name1"></td>
<td><input type="submit" id="submit" name="submit" value="Apply"></td>
</tr>
</table>
</div>
<div id="text">
<?php
if(isset($_REQUEST['submit'])) {
$name1 = $_REQUEST['name1'];
echo "$name1 likes apples.";
}
?>
</div>
</form>
Everything fine till now. Whatever name I type in replaces $name. Now what I want to do is to change the "likes" to "like" whenever i type in a pronoun (I, You, They etc.), which makes sense obviously. Now here is where I don't know what to do. My current code (which doesnt work) looks like this:
<?php
if(isset($_REQUEST['submit'])) {
$name1 = $_REQUEST['name1'];
if ($name1 = "I", "You") {
$verb = "like";
}
else {
$verb = "likes";
}
echo "$name1 $verb apples";
}
?>
Also is there a way to make it case insensitive?
there are few problems in your code .
single = means SET my variable to whatever string is after . so if you want to see if your variable is equal to a string like You , you have to use == , it will return true or false .
now we want to say if my variable was You or I , change my second variable to like . thats gonna be the output :
if ($name1 == "I" || $name1 == "You") {
$verb = "like";
}
else {
$verb = "likes";
}
so now it says if $name1 was equal to I OR(||) $name1 was equal to You , change the $verb to like .
we use || as OR and && as AND.
if you want echo variables and string you should use . as split between your variable and string . so it will be like this :
echo $name1 . $verb . "apples";
it's kind of like + but not in a math way , it just means add .
UPDATE
yes . there is a way to check your string case insensitive . you have to use strcasecmp() .
in your code , it should be like :
if (strcasecmp($name1,"I") == 0 || strcasecmp($name1,"You") == 0 ) {
$verb = "like";
}
else {
$verb = "likes";
}
Case insensitive replacement, $lut table defines what should be replaced with what. You may place it inside the replaceMe function if you don't want to change it over time.
Code:
<?php
$lut = [
'like' => [
'I',
'You',
'They',
'We',
],
'likes' => [
'she',
'he',
'it',
'someone',
'somebody',
],
];
function replaceMe(string $name, array $lut) : string
{
foreach ($lut as $key => $value) {
$nameLower = strtolower($name);
$valueLowerArr = array_map(
function($input) {
return strtolower($input);
},
$value
);
if (in_array($nameLower, $valueLowerArr)) {
return strtolower($key);
}
}
return '';
}
$name = 'She';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
$name = 'I';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
$name = 'iT';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
$name = 'TheY';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
$name = 'nobody';
echo "$name = " . replaceMe($name, $lut) . '<br>' . PHP_EOL;
Gives Result:
She = likes<br>
I = like<br>
iT = likes<br>
TheY = like<br>
nobody = <br>

Alphabetize Petfinder API List

I'm using the Petfinder API for Wordpress plugin. The plugin defaults to listing animals based on how old the Petfinder entries are, from oldest to newest. I'm trying to figure out a way to either do newest to oldest, or alphabetize based on animal names.
The data is loaded via the following code:
function get_petfinder_data($api_key, $shelter_id, $count, $pet = '') {
// If no specific pet is specified
if ( $pet == '' ) {
// Create request URL for all pets from the shelter
$request_url = 'http://api.petfinder.com/shelter.getPets?key=' . $api_key . '&count=' . $count . '&id=' . $shelter_id . '&status=A&output=full';
}
// If a specific pet IS specified
else {
// Create a request URL for that specific pet's data
$request_url = 'http://api.petfinder.com/pet.get?key=' . $api_key . '&id=' . $pet;
}
// Request data from Petfinder
$petfinder_data = #simplexml_load_file( $request_url );
// If data not available, don't display errors on page
if ($petfinder_data === false) {}
return $petfinder_data;
And the code that creates the list looks like this:
function get_all_pets($pets) {
foreach( $pets as $pet ) {
// Define Variables
$pet_name = get_pet_name($pet->name);
$pet_type = get_pet_type($pet->animal);
$pet_size = get_pet_size($pet->size);
$pet_age = get_pet_age($pet->age);
$pet_gender = get_pet_gender($pet->sex);
$pet_options = get_pet_options_list($pet);
$pet_description = get_pet_description($pet->description);
$pet_photo_thumbnail = get_pet_photos($pet, 'medium');
$pet_photo_all = get_pet_photos ($pet, 'large', false);
$pet_more_url = get_site_url() . '/adopt/adoptable-dogs/?view=pet-details&id=' . $pet->id;
$pet_pf_url = 'http://www.petfinder.com/petdetail/' . $pet->id;
// Create breed classes
$pet_breeds_condensed = '';
foreach( $pet->breeds->breed as $breed ) {
$pet_breeds_condensed .= pet_value_condensed($breed) . ' ';
}
// Create options classes
$pet_options_condensed = '';
foreach( $pet->options->option as $option ) {
$option = get_pet_option($option);
if ( $option != '' ) {
$pet_options_condensed .= pet_value_condensed($option) . ' ';
}
}
// Compile pet info
// Add $pet_options and $pet_breeds as classes and meta info
$pet_list .= '<div class="vc_col-sm-3 petfinder ' . pet_value_condensed($pet_age) . ' ' . pet_value_condensed($pet_gender) . ' ' . $pet_breeds_condensed . ' ' . $pet_options_condensed . '">' .
'<div class="dogthumbnail">' .
'' . $pet_photo_thumbnail . '<br>' .
'</div>' .
'<a class="dogname" href="' . $pet_more_url . '">' . $pet_name . '</a><br>' .
'<span> ' . $pet_age . ' • ' . $pet_gender . '<br>' .
'<div class="dogbreed">' . $pet_breeds_condensed . '</div>' .
'<a class="morelink" href="' . $pet_more_url . '">Learn More <i class="fas fa-angle-right"></i></a><br>' .
'</div>';
}
// Return pet list
return $pet_list;
Here's an example of the XML that the Petfinder API spits out (right now there are 25 pet entries in the full thing):
<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
<header>
<version>0.1</version>
<timestamp>2018-06-11T17:32:34Z</timestamp>
<status>
<code>100</code>
<message/>
</status>
</header>
<lastOffset>25</lastOffset>
<pets>
<pet>
<id>31035385</id>
<shelterId>IL687</shelterId>
<shelterPetId/>
<name>Chanel</name>
<animal>Dog</animal>
<breeds>...</breeds>
<mix>yes</mix>
<age>Adult</age>
<sex>F</sex>
<size>M</size>
<options>...</options>
<description>...</description>
<lastUpdate>2014-12-14T17:59:49Z</lastUpdate>
<status>A</status>
<media>...</media>
<contact>...</contact>
</pet>
</pets>
</petfinder>
I'd like to sort all entries by either "name" or "lastUpdate". I've been looking at a lot of posts about sorting XML element objects but they either don't seem to work or I can't figure out how to apply them specifically to my code. I'm not super well-versed in this stuff, so any assistance is much appreciated!!
After a LOT of research and trial and error, I figured out how to organize alphabetically by animal name. Posting this in case anybody is ever trying to figure out the same.
First of all, I was wrong in my assumption of which sections I might need to be editing. It was actually line 723 of the plugin file. Here's how I modified the code for that section:
// Display a list of all available dogs
else {
// Access Petfinder Data
$petfinder_data = get_petfinder_data($api_key, $shelter_id, $count);
// If the API returns without errors
if( $petfinder_data->header->status->code == '100' ) {
// If there is at least one animal
if( count( $petfinder_data->pets->pet ) > 0 ) {
//Sort list of dogs ALPHABETICALLY by NAME
$petSXE = $petfinder_data->pets->children();
$petArray = array();
foreach($petSXE->pet as $d) {
$petArray[] = $d;
}
function name_cmp($a, $b) {
$va = (string) $a->name;
$vb = (string) $b->name;
if ($va===$vb) {
return 0;
}
return ($va<$vb) ? -1 : 1;
}
usort($petArray, 'name_cmp');
$pets = $petArray;
// Compile information that you want to include
$petfinder_list = get_type_list($pets).
get_age_list($pets) .
get_size_list($pets) .
get_gender_list($pets) .
get_options_list($pets) .
get_breed_list($pets) .
get_all_pets($pets);
}
This is adapting the solution I found in this thread: sort xml div by child node PHP SimpleXML

Pluralize text in function

I'm looking to be able to pluralize "Slide" in the below function:
// Changes the default download button text
function ps_download_button($args) {
$download_text = 'Download ' . '(' . get_field('no_slides') . ' Slide)';
$args['text'] = $download_text;
return $args;
}
add_filter( 'edd_purchase_link_args', 'ps_download_button' );
This is my first stab at writing custom PHP functions. I've managed to find related code but I'm not sure how to integrate it with the above:
function plural( $amount, $singular = '', $plural = 's' ) {
if ( $amount === 1 ) {
return $singular;
}
return $plural;
}
Well you can use ternary for that.
function ps_download_button($args) {
$amount = intval(get_field('no_slides'));
$download_text = 'Download ' . '(' . $amount . ') Slide'. (($amount>1)?'s':'');
$args['text'] = $download_text;
return $args;
}
That's the simplest way, and no need for a function. If you don't understand how ternary works, take a look at this question.

printing an variable of an object that's inside an array that's inside an object

Well, ive been trying to get my crm to print multiple contacts for each company but i cant get it to work
Company is a class,companycontactis a class
//class called company
function __construct($idklanten,$naam,$adres,$postcode,$stad,$contacten){
$this->idklanten=$idklanten;
$this->naam=$naam;
$this->adres=$adres;
$this->postcode=$postcode;
$this->stad=$stad;
$this->contacten=$contacten;
}
//class called contact
function __construct($idcontactklanten,$voornaam,$tussenvoegsel,$achternaam,$tel,$email,$klantID){
$this->idcontactklanten=$idcontactklanten;
$this->voornaam=$voornaam;
$this->tussenvoegsel=$tussenvoegsel;
$this->achternaam=$achternaam;
$this->tel=$tel;
$this->email=$email;
$this->klantID=$klantID;
}
//getname for a contact
function getNaam() {
if(strlen($this->gettussenvoegsel()) == 0) {
return $this->getvoornaam()." ".$this->getachternaam()."";
}
else {
return $this->getvoornaam() . " " . $this->gettussenvoegsel() . " " . $this->getachternaam();
}
}
//function for getting the names from my object company,array with objects of contacts
function getcontacten(){
$ct=$this->contacten[$teller];
$txt="";
for($teller=0;$teller<10;$teller++){
$txt+=$ct->getNaam()."<br>";
}
return $txt;
}
then on my index page when i call getcontacten() it does not work comparing to my other get function which do work. it just outputs a 0
Any help is appreciated
Your biggest error would be the following:
$txt+=$ct->getNaam()."<br>";
Should be
$txt.=$ct->getNaam()."<br>";
Because to append to a string you use ".=", not "+=".
Also I don't know if the other part of you code works, I would write something like the following:
$txt = "";
foreach ($this->contacten as $ct){
$txt .= $ct->getNaam() . "<br />";
}
return $txt;
or
$txt = "";
for ($i = 0; $i < count($this->contacten); $i++){
$txt .= $this->contacten[$i]->getNaam() . "<br />";
}
return $txt;

getting input from URL

I made a script with the PHP Teamspeak Framework to check if a user is online.
I want to get input from the URL, so index.php?in=myname
My script:
<?php
global $input;
$input = $_GET["in"];
function OnlineTS() {
require_once("/ts3/libraries/TeamSpeak3/TeamSpeak3.php");
$ts3_VirtualServer = TeamSpeak3::factory("serverquery://ts.mydomain.com:10011/?server_port=9987");
global $input;
if ($input != "") {
$client = $ts3_VirtualServer->clientGetByName($input);
echo "<h2>" . $input . ' is online on teamspeak in channel "' . $ts3_VirtualServer->channelGetById($client['cid']) . '"</h2>';
}
}
OnlineTS();
?>
trying index.php?in=myname (who is online) doesn't work.
Can I get some help?
Try:
$input = $_REQUEST["in"];
echo $input;

Categories