How can I display the max value array, edited in a class - arrays

I need to find the worker with highest salary in PHP and display only him (his name, position and salary). Made several tries in the IF statement but none of them lead to what i needed.
class Workers {
public $name;
public $position;
public $salary;
private function Workers($name, $position, $salary){
$this->name = $name;
$this->position = $position;
$this->salary = $salary;
}
public function newWorker($name, $position, $salary){
// if ( ) {
return new Workers($name, $position, $salary);
// }
// else return NULL;
}
}
$arr = array();
$arr[] = Workers::newWorker("Peter", "work1", 600);
$arr[] = Workers::newWorker("John", "work2", 700);
$arr[] = Workers::newWorker("Hans", "work3", 550);
$arr[] = Workers::newWorker("Maria", "work4", 900);
$arr[] = Workers::newWorker("Jim", "work5", 1000);
print_r($arr);
This is my code and like that it will display all workers i have created but i need to output only the one with highest salary (worker 5 - Jim with 1000 salary)

You can use this snippet:
$max = null;
foreach ($arr as $worker) {
$max = $max === null ? $worker : ($worker->salary > $max->salary ? $worker : $max);
}
Or this, as more clarity:
$max = null;
foreach ($arr as $worker) {
if (!$max) {
$max = $worker;
} elseif ($worker->salary > $max->salary) {
$max = $worker;
}
}
$max now contains a worker with maximum salary.

Related

function doesn't work in PHP

this a part of a class, trying to handle arrays and numbers in the response content but i encountered a problem:
public function is_jsonable($content){
if(is_array($this->content) || is_numeric($this->content)){
json_decode($this->content,true);
}
elseif (null !== $this->$content && !is_string($this->$content) && !is_numeric($this->$content)) {
throw new Exception('Response Content is Invalid !');
}
return $this->content;
}
/**
* Set Response's content
*
* #param string
*
*/
function __construct($content = false, $httpCode = 200)
{
$this->httpCode = $this->httpStatuses[$httpCode];
$this->header('content-type', 'text/html');
$this->content = $content;
}
public function header($name, $value)
{
$this->headers[$name] = $value;
return $this;
}
/**
* Returns the Response as a string
*
* #return string
*
*/
public function __toString()
{
if(!headers_sent()){
header("$this->httpProtocole 200 $this->httpCode");
foreach ($this->headers as $name => $value) {
header($name.':'.$value,false);
}
}
return $this->content;
}
};
and this is the index :
<?php
ini_set('display_errors', 1);
require_once dirname(__DIR__) . '/Core/Http/Response.php';
use \Core\Http\Response;
$test = new Response(200);
echo $test ;
?>
the problem is that is_jsonable doesn't work, it works fine when i just instantiate the class with a string value
enter image description here

When changing templates on custom block the content gets deleted from the DB

I have a custom block for Concrete5 I built which uses multiple template files associated with it. If I apply the template to the block when I am initially adding the block to the page everything works fine. However if I then try to change templates after the block has already been set I run into issues. When saving changes with the new template, all my content gets deleted from the DB; so everything in that current row equals null except the block id "bID", the bID will change to the next increment.
I do not know why this happens!! I feel like I ran into a similar a long time ago but don't remember how it was resolved. Any advice would be great!
My template files are just standard html in a php file with the <?php defined('C5_EXECUTE') or die("Access Denied."); ?> at the top of the file.
My controller (which is my suspect for the issue right now) look like this:
<?php
namespace Concrete\Package\ThemeCaboodle\Block\GridBlock;
use Concrete\Core\Block\BlockController;
use Database;
use Page;
use Concrete\Core\Editor\LinkAbstractor;
use Core;
use File;
use View;
use BlockType;
class Controller extends BlockController
{
public $defaultBlockClassList = '';
public $defaultEntriesClassList = 'unit-md-4';
protected $btTable = 'btGrid';
protected $btExportTables = array('btGrid', 'btGridEntries');
protected $btInterfaceWidth = "600";
protected $btWrapperClass = 'ccm-ui';
protected $btInterfaceHeight = "550";
protected $btCacheBlockRecord = true;
protected $btExportFileColumns = array('thumbnailFID');
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = false;
protected $btIgnorePageThemeGridFrameworkContainer = true;
public function getBlockTypeDescription()
{
return t("Easily add a grid with prebuilt templates to your site using the grid block");
}
public function getBlockTypeName()
{
return t("Grid");
}
public function getFileObject($fID) {
return File::getByID($fID);
}
public function getSearchableContent()
{
$db = Database::get();
$rows = $db->Execute('SELECT * FROM btGridEntries WHERE bID = ?', array($this->bID));
$content = '';
foreach ($rows as $row) {
$content .= $row['title'].' ';
$content .= $row['description'].' ';
}
return $content;
}
public function view()
{
$this->set('entries', $this->getEntries());
$this->set('block', $this->getBlockData());
$this->addHeaderItem('<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>');
}
public function add()
{
$this->requireAsset('core/file-manager');
$this->requireAsset('core/sitemap');
$this->requireAsset('redactor');
}
public function edit()
{
$this->requireAsset('core/file-manager');
$this->requireAsset('core/sitemap');
$this->requireAsset('redactor');
$this->set('entries', $this->getEntries());
$this->set('block', $this->getBlockData());
}
public function composer()
{
$this->edit();
}
public function registerViewAssets($outputContent = '')
{
$al = \Concrete\Core\Asset\AssetList::getInstance();
$this->requireAsset('javascript', 'jquery');
}
public function duplicate($newBID)
{
parent::duplicate($newBID);
$db = Database::get();
$rows = $db->Execute('SELECT * FROM btGridEntries WHERE bID = ?', array($this->bID));
while ($row = $rows->FetchRow()) {
$db->execute('INSERT INTO btGridEntries (bID, thumbnailFID, fallbackFID, title, description, classList, buttonText, sortOrder, externalLinkURL, internalLinkCID, fileFID) values(?,?,?,?,?,?,?,?,?,?,?)',
array(
$newBID,
$row['ENTRY_thumbnailFID'],
$row['ENTRY_fallbackFID'],
$row['ENTRY_title'],
$row['ENTRY_description'],
$row['ENTRY_classList'],
$row['ENTRY_buttonText'],
$row['ENTRY_sortOrder'],
$row['ENTRY_externalLinkURL'],
$row['ENTRY_internalLinkCID'],
$row['ENTRY_fileFID']
)
);
}
}
public function delete()
{
$db = Database::get();
$db->delete('btGridEntries', array('bID' => $this->bID));
parent::delete();
}
public function save($args)
{
$db = Database::get();
$db->execute('DELETE from btGridEntries WHERE bID = ?', array($this->bID));
parent::save($args);
if (isset($args['ENTRY_sortOrder'])) {
$count = count($args['ENTRY_sortOrder']);
$i = 0;
while ($i < $count) {
$externalLinkURL = $args['ENTRY_externalLinkURL'][$i];
$internalLinkCID = $args['ENTRY_internalLinkCID'][$i];
$fileFID = $args['ENTRY_fileFID'][$i];
switch (intval($args['ENTRY_linkType'][$i])) {
case 1:
$externalLinkURL = '';
$fileFID = 0;
break;
case 2:
$internalLinkCID = 0;
$fileFID = 0;
break;
case 3:
$externalLinkURL = '';
$internalLinkCID = 0;
break;
default:
$externalLinkURL = '';
$internalLinkCID = 0;
$fileFID = 0;
break;
}
if (isset($args['ENTRY_description'][$i])) {
$args['ENTRY_description'][$i] = LinkAbstractor::translateTo($args['ENTRY_description'][$i]);
}
$db->execute('INSERT INTO btGridEntries (bID, thumbnailFID, fallbackFID, title, description, classList, buttonText, sortOrder, externalLinkURL, internalLinkCID, fileFID) values(?,?,?,?,?,?,?,?,?,?,?)',
array(
$this->bID,
intval($args['ENTRY_thumbnailFID'][$i]),
intval($args['ENTRY_fallbackFID'][$i]),
$args['ENTRY_title'][$i],
$args['ENTRY_description'][$i],
$args['ENTRY_classList'][$i],
$args['ENTRY_buttonText'][$i],
$args['ENTRY_sortOrder'][$i],
$externalLinkURL,
$internalLinkCID,
$fileFID
)
);
++$i;
}
}
}
public function getBlockAssetPath() {
$bt = BlockType::getByHandle('buckets_block');
return Core::make('helper/concrete/urls')->getBlockTypeAssetsURL($bt);
}
public function getBlockData()
{
$db = Database::get();
$row = $db->GetRow('SELECT * FROM btGrid WHERE bID = ?', array($this->bID));
if ($row['bgFID']) {
$row['BG'] = \File::getByID($row['bgFID'])->getVersion()->getRelativePath();
}
return $row;
}
public function getEntries()
{
$v = View::getInstance();
$db = Database::get();
$rows = $db->GetAll('SELECT * FROM btGridEntries WHERE bID = ? ORDER BY sortOrder', array($this->bID));
// in view mode, linkURL takes us to where we need to go whether it's on our site or elsewhere
$entries = array();
foreach ($rows as $row) {
// Generate the URL based on what the linkType is
if ($row['externalLinkURL'] =='' && !$row['fileFID'] && $row['internalLinkCID']) {
$c = Page::getByID($row['internalLinkCID'], 'ACTIVE');
$row['linkURL'] = $c->getCollectionLink();
} elseif ($row['externalLinkURL'] =='' && !$row['internalLinkCID'] && $row['fileFID']) {
$f = File::getByID($row['fileFID']);
$row['linkURL'] = $f ? $f->getVersion()->getRelativePath() : '';
} elseif ($row['externalLinkURL']!='') {
$row['linkURL'] = $row['externalLinkURL'];
} else {
$row['linkURL'] = '';
}
// Thumbnail
$thumbnail = $row['thumbnailFID'] ? File::getByID($row['thumbnailFID'])->getVersion()->getRelativePath() : $v->getThemePath().'/no-image.jpg';
$row['thumbnail'] = $thumbnail;
$fallback = $row['fallbackFID'] ? File::getByID($row['fallbackFID'])->getVersion()->getRelativePath() : $v->getThemePath().'/no-image.jpg';
$row['fallback'] = $fallback;
$row['description'] = LinkAbstractor::translateFrom($row['description']);
$entries[] = $row;
}
return $entries;
}
public function getClassList($string) {
$array = explode(',',$string);
if (count($array) > 0) {
return implode(' ',$array);
}
}
}
I apologize for the pretty long file in advance ;) i just want to make sure you can see all possible issues
I think your problem is that in the duplicate function, once you got your results back from your select you put an ENTRY_ prefix everywhere like in $row['ENTRY_thumbnailFID'] when it should really be $row['thumbnailFID'] according to your screenshot

Get minimum value in an array and then get index

I want to get the minimum value in an array and then get the index of that item, in one step without writing my own loop (if I have to please let me know).
I know I can just do the
$b = ($a | Measure -Minimum).Minimum
But then I have to do
[array]::IndexOf($a, $b)
And while that is normally okay, I'm looking for a way to do it once because I'm running this MANY MANY times in a loop.
Thanks!
EDIT: One step meaning without looping through the array twice
Personally, I might consider a different data structure. Maybe something sorted to begin with...
This code may work for your needs:
$myArray = 5,66,4,33,2,9,9,12
$index = 0
$minIndex = 0
$minValue = [int]::MaxValue
$myArray | % { if ($minValue -gt $_) {$minValue = $_; $minIndex = $index}; $index++ }
"MinIndex $minIndex = MinValue $minValue"
its a problem of type, try like this:
$myArray = [int[]]5,66,4,33,2,9,9,12
$minvalue=[int]($myArray | measure -Minimum).Minimum
$myArray.IndexOf($minvalue)
Here are 6 different options for you...
cls
$myArray = #(5,66,4,33,2,9,9,12)
$iterations = 50000
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minValue = [int]($myArray | Measure-Object -Minimum).Minimum
$minIndex = $myArray.IndexOf($minValue)
}
}).TotalSeconds
"measure-object with indexOf: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$index = 0
$minIndex = 0
$minValue = [int]::MaxValue
$myArray | % { if ($minValue -gt $_) {$minValue = $_; $minIndex = $index}; $index++ }
}
}).TotalSeconds
"pipeline with compare: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minIndex = 0
$minValue = [int]::MaxValue
foreach ($index in 0..($myArray.count-1)) {
if ($myArray[$index] -lt $minValue) {
$minValue = $myArray[$index]
$minIndex = $index
}
}
}
}).TotalSeconds
"foreach-loop with compare: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$h = [System.Collections.ArrayList]::new()
foreach ($index in 0..($myArray.count-1)) {
$null = $h.add([tuple]::Create($myArray[$index], $index))
}
$h.Sort()
$minIndex = $h[0].Item2
}
}).TotalSeconds
"quicksort of a list of tuples: $t"
Add-Type -TypeDefinition #"
using System;
using System.Linq;
public static class My {
public static int indexOfMin(int[] arr){
int min = arr.Min();
return Array.IndexOf(arr, min);
}
}
"#
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minIndex = [my]::indexOfMin($myArray)
}
}).TotalSeconds
"custom type and IndexOf: $t"
Add-Type #"
public static int[] indexOfMin(int[] arr){
int min = int.MaxValue;
int minIndex = 0;
for (int i = 0; i < arr.Length; i++) {
if (arr[i]<min) {
min = arr[i];
minIndex = i;
}
}
return new int[] {min, minIndex};
}
"# -name My2 -Namespace System
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minValue, $minIndex = [my2]::indexOfMin($myArray)
}
}).TotalSeconds
"custom type and looping: $t"

CakePHP 3 : delete and update array from cookie

I'm using CakePHP 3.2 for writing a shopping cart application.
I'm using cookie to add items to the cart.
Now I want to update and delete value from cart. so that if user clicks on the same product add to cart with a different quantity value the existing record will be deleted and new will be added to cart.
This is my addToCart() method.
public function addToCart()
{
$this->loadModel('Products');
if ($this->request->is('post')) {
$p_id = $this->request->data('product_id');
$p_quantity = $this->request->data('qnty');
$product = $this->Products->get($p_id);
$product->quantity = $p_quantity;
if (!$product) {
throw new NotFoundException(__('Invalid Product'));
}
$cart = $this->Cookie->read('Cart') ? $this->Cookie->read('Cart') : [];
$itemInCart = false;
$itemUpdated = false;
if ($cart != null) {
foreach($cart as $cart_item):
if ($cart_item['id'] == $p_id) {
if ($cart_item['quantity'] != $p_quantity) {
$this->Cookie->delete('Cart.'.$cart_item); // line 148
$cart[] = $product;
$this->Cookie->write('Cart', $cart);
$itemsCount = count($this->Cookie->read('Cart'));
$this->Flash->success('Product updated in cart');
return $this->redirect($this->referer);
}
$itemInCart = true;
}
endforeach;
}
if (!$itemInCart) {
$cart[] = $product;
$this->Cookie->write('Cart', $cart);
$itemsCount = count($this->Cookie->read('Cart'));
if ($itemUpdated) {
$this->Flash->success(__('Product updated in cart'));
} else {
$this->Flash->success(__('Product added to cart'));
}
return $this->redirect($this->referer());
} else {
$this->Flash->success(__('Product is already in cart'));
return $this->redirect($this->referer());
}
}
}
But this is giving error as
Notice (8): Array to string conversion [APP/Controller/OrdersController.php, line 148]
How could I update the quantity value in the cart.
Try the following:
public function addToCart()
{
$this->loadModel('Products');
if ($this->request->is('post')) {
$p_id = $this->request->data('product_id');
$p_quantity = $this->request->data('qnty');
$product = $this->Products->get($p_id);
if (!$product) {
throw new NotFoundException(__('Invalid Product'));
}
$product->quantity = $p_quantity;
$cart = $this->Cookie->read('Cart') ? $this->Cookie->read('Cart') : [];
$itemInCart = false;
$new_cart = [];
if ($cart != null) {
foreach($cart as $cart_item):
if ($cart_item['id'] == $p_id) {
if($p_quantity == 0){
//Removed the item from cart and set itemInCart to true
$itemInCart = true;
}else{
//update the quantity of item
$new_cart[] = $product;
$itemInCart = true;
}
}else{
$new_cart[] = $cart_item;
}
endforeach;
}
if ($itemInCart) {
$this->Cookie->write('Cart', $new_cart);
$this->Flash->success(__('Product updated in cart'));
} else {
$cart[] = $product;
$this->Cookie->write('Cart', $cart);
$this->Flash->success(__('Product added to cart'));
}
return $this->redirect($this->referer);
}
}

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE

I am having a problem while setting up my database file please help me.
I am testing a filehosting script here at http://toorhamza.host56.com/ and I get this error when opening my website
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$' in /home/a6788850/public_html/includes/class.database.php on line 23
This is my class.database.php file and line
<?php
class Database
{
// Singleton object. Leave $me alone.
private static $me;
public $db;
public $host;
public $name;
public $username;
public $password;
public $dieOnError;
public $queries;
public $result;
public $redirect = false;
// Singleton constructor
private function __construct($connect = false)
{
$Config = Config::getConfig();
$this->host = $Config->"localhost";
$this->name = $Config->'test';
$this->username = $Config->'test';
$this->password = $Config->'password';
$this->dieOnError = $Config->dbDieOnError;
$this->db = false;
$this->queries = array();
if ($connect === true)
$this->connect();
}
// Waiting (not so) patiently for 5.3.0...
public static function __callStatic($name, $args)
{
return self::$me->__call($name, $args);
}
// Get Singleton object
public static function getDatabase($connect = true)
{
if (is_null(self::$me))
self::$me = new Database($connect);
return self::$me;
}
// Do we have a valid database connection?
public function isConnected()
{
return is_resource($this->db) && get_resource_type($this->db) == 'mysql link';
}
// Do we have a valid database connection and have we selected a database?
public function databaseSelected()
{
if (!$this->isConnected())
return false;
$result = mysql_list_tables($this->name, $this->db);
return is_resource($result);
}
public function connect()
{
$this->db = mysql_connect($this->host, $this->username, $this->password) or $this->notify('Failed connecting to the database with the supplied connection details. Please check the details are correct and your MySQL user has permissions to access this database.<br/><br/>(host: ' . $this->host . ', user: ' . $this->username . ', pass: ********)');
if ($this->db === false)
return false;
mysql_select_db($this->name, $this->db) or $this->notify();
if($this->isConnected())
{
mysql_set_charset('UTF-8', $this->db);
$this->query("SET NAMES utf8");
}
return $this->isConnected();
}
public function close()
{
if ($this->db)
{
mysql_close($this->db);
}
$this->db = false;
$this->queries = array();
}
public function query($sql, $args_to_prepare = null, $exception_on_missing_args = true)
{
if (!$this->isConnected())
$this->connect();
// Allow for prepared arguments. Example:
// query("SELECT * FROM table WHERE id = :id", array('id' => $some_val));
if (is_array($args_to_prepare))
{
foreach ($args_to_prepare as $name => $val)
{
$val = $this->quote($val);
$sql = str_replace(":$name", $val, $sql, $count);
if ($exception_on_missing_args && (0 == $count))
throw new Exception(":$name was not found in prepared SQL query.");
}
}
$this->queries[] = $sql;
$this->result = mysql_query($sql, $this->db) or $this->notify();
return $this->result;
}
// Returns the number of rows.
// You can pass in nothing, a string, or a db result
public function numRows($arg = null)
{
$result = $this->resulter($arg);
return ($result !== false) ? mysql_num_rows($result) : false;
}
// Returns true / false if the result has one or more rows
public function hasRows($arg = null)
{
$result = $this->resulter($arg);
return is_resource($result) && (mysql_num_rows($result) > 0);
}
// Returns the number of rows affected by the previous operation
public function affectedRows()
{
if (!$this->isConnected())
return false;
return mysql_affected_rows($this->db);
}
// Returns the auto increment ID generated by the previous insert statement
public function insertId()
{
if (!$this->isConnected())
return false;
$id = mysql_insert_id($this->db);
if ($id === 0 || $id === false)
return false;
else
return $id;
}
// Returns a single value.
// You can pass in nothing, a string, or a db result
public function getValue($arg = null)
{
$result = $this->resulter($arg);
return $this->hasRows($result) ? mysql_result($result, 0, 0) : false;
}
// Returns an array of the first value in each row.
// You can pass in nothing, a string, or a db result
public function getValues($arg = null)
{
$result = $this->resulter($arg);
if (!$this->hasRows($result))
return array();
$values = array();
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
$values[] = array_pop($row);
return $values;
}
// Returns the first row.
// You can pass in nothing, a string, or a db result
public function getRow($arg = null)
{
$result = $this->resulter($arg);
return $this->hasRows() ? mysql_fetch_array($result, MYSQL_ASSOC) : false;
}
// Returns an array of all the rows.
// You can pass in nothing, a string, or a db result
public function getRows($arg = null)
{
$result = $this->resulter($arg);
if (!$this->hasRows($result))
return array();
$rows = array();
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
$rows[] = $row;
return $rows;
}
// Escapes a value and wraps it in single quotes.
public function quote($var)
{
if (!$this->isConnected())
$this->connect();
return "'" . $this->escape($var) . "'";
}
// Escapes a value.
public function escape($var)
{
if (!$this->isConnected())
$this->connect();
return mysql_real_escape_string($var, $this->db);
}
public function numQueries()
{
return count($this->queries);
}
public function lastQuery()
{
if ($this->numQueries() > 0)
return $this->queries[$this->numQueries() - 1];
else
return false;
}
private function notify($err_msg = null)
{
if ($err_msg === null)
{
$err_msg = mysql_error($this->db);
}
error_log($err_msg);
if ($this->dieOnError === true)
{
echo "<p style='border:5px solid red;background-color:#fff;padding:12px;font-family: verdana, sans-serif;'><strong>Database Error:</strong><br/>$err_msg</p>";
if (strlen($this->lastQuery()))
{
echo "<p style='border:5px solid red;background-color:#fff;padding:12px;font-family: verdana, sans-serif;'><strong>Last Query:</strong><br/>" . $this->lastQuery() . "</p>";
}
//echo "<pre>";
//debug_print_backtrace();
//echo "</pre>";
exit;
}
if (is_string($this->redirect))
{
header("Location: {$this->redirect}");
exit;
}
}
// Takes nothing, a MySQL result, or a query string and returns
// the correspsonding MySQL result resource or false if none available.
private function resulter($arg = null)
{
if (is_null($arg) && is_resource($this->result))
return $this->result;
elseif (is_resource($arg))
return $arg;
elseif (is_string($arg))
{
$this->query($arg);
if (is_resource($this->result))
return $this->result;
else
return false;
}
else
return false;
}
}
Please help me thanks.
On this line 23 $this->host = $Config->"localhost";
localhost should be using single quotation marks
like this: $this->host = $Config->'localhost';

Resources