samedi 27 juin 2015

Adding up values in array

I am trying to count elements in an array separated by one or more zeroes (0) Example: [3,2,1,0,5,4,0,0,8,4]. 3+2+1 , 5+4 , 8+4 Output: [6,9,12] I have written the following function:

function countElementsInArray(){
$array = [1,2,4,0,9,8,4,0,7,1,2,6];

$countArr = [];
$count = 0;
$test = 0;

for($i = 0; $i < count($array); $i++){
    if( $array[$i] != 0 ) {
       $count += $array[$i];
    } 
    //else if($array[$i] == 0 || $array[$i - 1] == 0){
    else{
        $count = 0;
        $test += 1;
    }
    $countArr[$test] = $count;
}
return $countArr;

}

This works fine if I have only one "0" in the array. I can't figure out how to add up the values if I have 2 zeroes. Any idea how to solve this problem?

Laravel Polymorphic many to many relationship issue

I have a Many to Many Polymorphic relationship setup between a few models and everything seems to be working just fine with one slight issue...

If I add the same relation $book->genres()->save($scifi) multiple times, it shows up multiple times in my database. Maybe this is intentional, maybe it's not. If I'm overlooking something, I'd like to have it working the "Laravel" way before I go and start making methods to ensure that only one relationship of that kind is in the DB at a time.

Does load_file() in SIMPLE HTML DOM have timeout settings?

I use SIMPLE HTML DOM for parsing. Some pages take a very long time when i try to get contents. I need to set limit for function load_file() (for example 10 sec) -> go to the next page. Please without socket!

Chrome is requesting HTTPS suddenly and I can't log into admin area

I recently moved a wordpress site from one hostgator account to another. It's now an addon domain. For the last couple days everything was working fine.

Then suddenly Chrome started giving me this error when I tried to reach my admin area: enter image description here

Proceeding didn't help either as I don't have an SSL, so it returns a 404. enter image description here

In fact it doesn't matter what URL hijinks I play, any time I access the admin area it tries to use HTTPS.

This is only on Chrome. It appears that Chrome is requesting an HTTPS address. I've tried commenting out all the https code in wp-login.php but no dice.

I've tried uninstalling Chrome, clearing cookies, etc. No good.

I'm at my wit's end.

PHP included HTML relative path of external JS & CSS

I am trying to use PHP to read, then modify and echo an HTML file.

The included HTML file contains external JS, CSS references - all relative paths

for example...

<script src="js/myJavascript.js"></script>

Problem : The location of the PHP modifier file is not the same as the location of the included HTML file, and therefore the external includes are not loaded. I guess...

The solution of using absolute paths to reference external resources in the HTML file is not ideal to say the least...

What can be done to tell PHP that the path context of the included HTML file is the same as the directory from which it is being included and NOT the directory of the modifier file?

Thanks!

SELECT query with apostrophe

In my database, I have a column named storeName with a value called Joe's Kitchen.

When user enters Joe's Kitchen, I would store it in a variable named storeName and do a select query on it like this: "SELECT * FROM shops WHERE storename='".$storeName."'". Problem now is that the value contains apostrophe, how should I go about this ?

I have tried the method below but it is not working

$storeName = mysqli_real_escape_string($db->getConnection(),$_POST["storeName"]);

.NET web service and php client

I am new to php client interaction with .NET web service. I have looked around and aware of posts like the one below but there are not the answer to my question.

How to make a PHP SOAP client and store the result xml in php variables

http://ift.tt/1eOTCI5

Consume a .Net web service using PHP

A PHP SOAP client (to request and get response) is needed for .NET web service (http://localhost:8080/someexample/myservice.wsdl). The input/request would look like this:

<the_input name="secondyear">
  <authen>
    <Teacher>
     <teacherid>MrX</teacherid> 
     <password>t_pass</password> 
    </Teacher>
    <Student>
     <studentid>kids</studentid> 
     <password>s_pass</password> 
    </Student>
   </authen>
 <parameters>
    <parameter name="maths" value="123" /> 
    <parameter name="physics" value="abc" />
    <parameter name="sports" value="def" /> 
 </parameters>
</the_input>

Here is what i got but not working and i am not sure i am on the right track:

<?php 
 require_once("lib/nusoap.php");
 $url="http://ift.tt/1NkBRuL";
 $client = new SoapClient($url);

 $teacherid='MrX';
 $tpassword='t_pass';
 $studentid='kids';
 $spassword='s_pass';
 $ns = "http://example.com/";

 //Body of the Soap Header.
 $headerbody = array ('Teacher' => array('teacherid' => $teacherid,
                                      'password' => $tpassword ),
                      'Student' => array('stendentid' => $studentid,
                                       'password' => $spassword ));
 //Create Soap Header.
 $header = new SOAPHeader($ns, 'Authen', $headerbody);

 //set the Headers of Soap Client.
 $headerparam=$client->__setSoapHeaders($header);

 $name ='secondyear';
 $service_name = array('the_input'=>$name);

 $result =$client->__SoapCall($service_name,$headerparam)
 print_r ($result);
?>

I read somewhere in order to authenticate to a web service, one should put authentication parameter in php header, so what goes to the php body then or is it needed? is the parameters? parameter has the format of name and value, how do i do that in php array? any relevant example available? as mentioned i've done my research and not clear how to solve this problem.