Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Mongo::__construct is deprecated following php.net, and it's faster 3 times than MongoClient!!
Try it yourself, run the below code which insert 1M docuements, it will finish in ~20 sec the replace mongo with mongoClient which will finish in ~180 sec
<?php
function mongo_connect($db, $collection) {
$mongo = new Mongo( 'mongodb://localhost' );
return $mongo->$db->$collection;
}
$mongodb='notificator1';
$mongocollection='tok';
$collection= mongo_connect($mongodb, $mongocollection);
echo $time = microtime(true). "\n";
$i=0;
while ( $i < 1000000 ) {
$row = Array('title' => "test #$i");
$collection->save($row);
++$i;
}
echo microtime(true) - $time;
?>
replace mongo_connect:
function mongo_connect($db, $collection) {
$m = new MongoClient('mongodb://localhost:27017');
$db = $m->selectDB($db);
return $collection = new MongoCollection($db, $collection);
}
Mongo is faster but that IS NOT A GOOD THING.
But the answer to your actual question is in the code:
$mongo = new Mongo( 'mongodb://localhost' );
The default write concern of Mongo is 0 which means it won't bother to check if the write actually worked whereas MongoClient will.
Here is a good post that explains the differences between Mongo and MongoClient form one of the mantainers of PHP and the MongoDB Driver: http://derickrethans.nl/mongoclient.html .
Now, you may be wondering why we are replacing Mongo with MongoClient across the board. The biggest reason is that the new class will have acknowledged writes on by default
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
It's my store method where I am getting IP through request from my view
public function store(Request $request)
{
$ip['ip_range'] = $request->input('ip_range');
$ip['min_ip'] = $request->input('ip_range');
$ip['max_ip'] = $request->input('ip_range');
$ip_data = Ip::create($ip);
session()->flash('msg', ' Successfully created');
return view('ip.index');
}
In above I want if IP comes as
192.168.2.9
192.168.1.2
It should be saved as 2 values .... Mean it should separate it on /r/n and where I found /n I create a new value and so on.
And next thing is that if user enter as
192.168.1.0-3
Here a - comes at last one 192.168.1-3 should be save in ip_range and 192.168.1.0 in min_ip while 192.168.1.3 as max_ip.
I hope this is what you were looking for:
public function store(Request $request) {
$ip['ip_range'] = $request->input('ip_range');
$arrIps = explode("\r\n", $ip['ip_range']);
foreach($arrIps as $strIp) {
$arrRange=explode("-",$strIp);
if(count($arrRange)>1){
$ip['min_ip']= $arrRange[0];
$arrDot=explode(".",$arrRange[0]);
$arrDot[3]=$arrRange[1];
$ip['max_ip']= implode(".",$arrDot);
}
else
$ip['min_ip']=$ip['max_ip']="";
$ip_data = Ip::create($ip);
}
session()->flash('msg', ' Successfully created');
return view('ip.index');
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm learning about functions in my programming class and no matter what I do I just cant get this to work. Here is the problem that needs fixing:
What's wrong here?
The getWage() function is supposed to calculate overtime (time and a half) for any hours worked above 40. The function is working just fine but when we send the $hoursWorked and $hourlyWage from THIS program the function does not calculate the overtime as time and a half!
Can you fix it? The wage should be $605.63 not $573.75
Here is my function:
<?php
function getWage($hourlyWage, $hoursWorked) {
if ($hoursWorked <= 40)
return round(($hourlyWage * $hoursWorked), 2);
else
return round((($hourlyWage * 40) +
($hourlyWage * 1.5 * ($hoursWorked - 40))), 2);
}
?>
and here is where I try to call it to my program:
<?php
include("incWageFunctions.php");
$hourlyWage = 12.75;
$hoursWorked = 45;
$wage = getWage($hoursWorked, $hourlyWage);
print("<p>Your hourly wage is $$hourlyWage and you worked
$hoursWorked hours.</p>");
print("<p>Your wages are $$wage.</p>");
?>
Its not calculating the overtime no matter what... Where am I going wrong?! Also, how would you call multiple functions to the same program to calculate different things? Can I just call it with the name after the include statement? Or do I have to have it written out with the { } like my first function in this question??
You interchanged $hoursWorked and $hourlyWage
Change $wage = getWage($hoursWorked, $hourlyWage);
To $wage = getWage($hourlyWage, $hoursWorked);
You have mixed up the arguments. In php-file you have coded:
$wage = getWage($hoursWorked, $hourlyWage);
However, in incWageFunctions.php you have written:
function getWage($hourlyWage, $hoursWorked)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
When trying to access a method I've defined in a PHP class from within another method in the same class, I get the following error:
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /var/www/.../TOC.php on line 57
Previously, I'd these functions sitting outside of any class, just including them where I needed them. Having moved them into one, at first I thought I must be having the issue this user was having, but with an instance of the class, I can call my render method from another file with no problems. Commenting out the line this->printTreeArray($sectionProjects); eliminates the error. Here's my class:
<?php
class TOC{
private function printTreeArray($sectionProjects){
echo "var TOCnodes = [\n";
//Print each section, with another loop to print each one's problems
$i = 0;
foreach($sectionProjects as $sectionProject){
if($i != 0){
echo ",\n";
}
$project = $sectionProject->getProject();
//Get due date for mouseover text
$due = $project->getDueDate('F jS, Y h:i A');
$q = new ProjectProblemQuery();
$projectProblems = $q->findByRelProjectId($project->getId());
$pId = $i + 1;
echo "{id: $pId, pId: 0, name: \"Project $pId\", title: \"Due: $due\", isParent: true, open: true}";
//Print this section's problem list
$probId = 1;
foreach($projectProblems as $projectProblem){
echo ",\n";
$nodeId = ($pId * 10) + $j;
echo "{id: $nodeId, pId: $pId, name: \"$pId.$probId\", url: \"#\", target: \"_top\"}";
$j++;
}
$i++;
}
echo"];\n";
}
public function render($section){
if(! $section instanceof Section){
echo "$section is not an instance of Section!";
}
else{
echo "<ul id=\"TOCtree\" class=\"ztree\"></ul>\n";
echo "<script type=\"text/javascript\">\n";
$sectionProjects = $section->getSectionProjects();
this->printTreeArray($sectionProjects);
echo "$(document).ready(function () {
$.fn.zTree.init($(\"#TOCtree\"), setting, TOCnodes);
});";
echo "</script>";
}
}
}
This is my first time asking a question on SO - generally I've been able to find answers to my coding dilemmas, but this one has me baffled. I realize there's a lot of not-elegant stuff going on in this class, so if there's a point of style I can use to avoid this sort of error in the future, I'd greatly appreciate getting to learn it.
You should use $this instead of this.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public function run() {
$this->step = $this->$_POST("step", 1);
$this->xml = new XMLFile();
$common_data = array(
'STEPCONTENT' => $this->get_step_content(),
'STEPNUMBER' => $this->step,
'STEPTITLE' => $this->get_step_title()
);
echo $this->parse($this->common_template, $common_data);
This gives the exception:
Fatal error: Method name must be a string in
C:\xampp\htdocs\test\openad\install\InstallOpenAdServer.php on line 674
Why?
This is the culprit
$this->step = $this->$_POST("step", 1);
You cannot use an $_POST super global array as a function. If you are trying to access the vairable from the $_POST, you can simple do this
$this->step = $_POST["step"];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I get the following dump & error when running the attached code. What I'm confused by is that $procID appears to be returned as a string, but as soon as I attempt to pass it again, its an object? How do I get it to be/stay a string? Thanks.
object(stdClass)#2 (1) {
["processId"]=> string(13)
"Genesis114001" } string(311)
"Genesis114001" string(293) " Genesis
" Catchable fatal error: Object of
class stdClass could not be converted
to string in
C:\wamp\www\SugarCE\testSOAPShawn.php
on line 15
<?php
set_time_limit(0);
require_once('nusoap.php');
require_once('BenefitSOAP.php'); //WSDL to PHP Classes
$client = new SoapClient('C:\wsdl\BenefitDeterminationProcess_BenefitDialogueServiceSOAP.wsdl', array('trace' => 1));
$procID = $client->start(array("prefix"=>"Genesis"));
$respXML = $client->__getLastResponse();
$requXML = $client->__getLastRequest();
echo "<p/>";
var_dump($procID);
//echo "<p/>";
var_dump($respXML);
//echo "<p/>";
var_dump($requXML);
$exchange = $client->exchangeOptions(array("processId"=>$procID)); //LINE 15
$end = $client->stop(array("processId"=>$procID));
?>
Whatever the $client->start() method is returning, it is typed as an object. You can access the properties of the object using the -> operator:
$procID = $client->start(array("prefix"=>"Genesis"));
...
$exchange = $client->exchangeOptions(array("processId"=>$procID->processId));
This was probably an array, but is getting typed into an object. Thus, you end up with the stdClass.
Another (and possibly better) way to do this is to type the return. That way, you don't have to make a new array for later passing as argument:
$procID = (array) $client->start(array("prefix"=>"Genesis"));
...
$exchange = $client->exchangeOptions($procID);
$end = $client->stop($procID);