php-general Digest 20 Oct 2013 15:50:29 -0000 Issue 8402
Topics (messages 322325 through 322339):
Re: Best Secure practice for uploading a csv file to import
322325 by: Ashley Sheridan
322326 by: Joshua Kehn
Re: Algorithm Help
322327 by: German Geek
322334 by: German Geek
322335 by: German Geek
322337 by: Ayush Ladia
322338 by: German Geek
If date is greater than
322328 by: John Taylor-Johnston
322329 by: German Geek
322330 by: John Taylor-Johnston
322331 by: John Taylor-Johnston
322332 by: Bastien
322336 by: Ashley Sheridan
322339 by: Tedd Sperling
Re: Switch Statement
322333 by: German Geek
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
On Sat, 2013-10-19 at 18:57 -0400, Joey J wrote:
> Hello All,
>
> I just wanted to see the best way to securely accomplish this task.
> when we want to update a DB we upload to a writable directory instead of
> writing it directly to MySQL, I don't like having writable directories if
> possible.
> Is there a right or better way to accomplish this?
>
> Thanks!
>
There's nothing inherently wrong with having a directory writeable on
your web server, but you should ensure it's running with the least
privileges it requires to complete your task.
So, make sure that the Apache user is also the owner of the directory,
then you only need to give it 755 permissions (it's always unwise to use
777 on a production server).
Another thing you can do is to place the upload directory outside your
web root so that it's not accessible via a browser.
I can see why you wouldn't want to import it directly into the database
though. I recently had to "fix" a script of mine because someone thought
it would be a good idea to change the order of a bunch of fields in a
CSV, and added a new field in the middle rather than at the end. Having
a script in between the CSV and the database can ensure some sort of
data quality check is in-place before importing bad data.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--- End Message ---
--- Begin Message ---
> On Oct 19, 2013, at 7:06 PM, Ashley Sheridan <a...@ashleysheridan.co.uk>
> wrote:
>
>> On Sat, 2013-10-19 at 18:57 -0400, Joey J wrote:
>>
>> Hello All,
>>
>> I just wanted to see the best way to securely accomplish this task.
>> when we want to update a DB we upload to a writable directory instead of
>> writing it directly to MySQL, I don't like having writable directories if
>> possible.
>> Is there a right or better way to accomplish this?
>>
>> Thanks!
>
>
> There's nothing inherently wrong with having a directory writeable on
> your web server, but you should ensure it's running with the least
> privileges it requires to complete your task.
>
> So, make sure that the Apache user is also the owner of the directory,
> then you only need to give it 755 permissions (it's always unwise to use
> 777 on a production server).
>
> Another thing you can do is to place the upload directory outside your
> web root so that it's not accessible via a browser.
>
> I can see why you wouldn't want to import it directly into the database
> though. I recently had to "fix" a script of mine because someone thought
> it would be a good idea to change the order of a bunch of fields in a
> CSV, and added a new field in the middle rather than at the end. Having
> a script in between the CSV and the database can ensure some sort of
> data quality check is in-place before importing bad data.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
Good points by Ash above.
I'd like to mention that because this is user input make sure any database
access is escaped correctly (prepared statements are good) and when/if you
output it should all be HTML escaped.
Best,
-Josh
___________________________
http://byjakt.com
Currently mobile
--- End Message ---
--- Begin Message ---
This is how I would approach/imagine it:
https://docs.google.com/drawings/d/111RISgcHyAg8NXem4H1NXnxByRUydL8GiYlGkobJwus/edit
Tom has been with Andrew 0 times.
Tom has been with Shelly 1 time.
Christine has been with Andrew 2 times.
...
So the Graph maintains who has been with who how often.
For 10 or even 20 kids you might be able to go through all links (brute
force).
The number of links (including the ones with 0 weight) is
#links = n*(n-1)/2
which is the number of links you have to maintain and then check when you
want to know who should go with whom.
So, if
n=10: #links = 10*9/2 = 45
n=20: #links = 20*19/2 = 190
n=30: #links = 30*29/2 = 435
I think even for reasonably large groups a computer can do the job easily.
I would find it quite hard to do it on paper though, so I think you should
program it.
You could simply store the graph in an array, and then optionally persist
it to a db or file:
You would get e.g.:
$graph = array(
'0,1' => 0,
'0,2' => 2,
...
Edit: Actually, maybe you can do it in a two-dimensional array, where no
node is connected to itself:
$n=4;
function init() {
global $n;
$graph = array();
for ($i = 0; $i < $n; ++$i) {
$graph[$i] = array();
for ($j = 0; $j < $n; ++$j) {
$graph[$i][$j] = 0;
}
}
return $graph;
}
$graph = init();
Sorry, I might be running a bit out of time here...
You can use an implementation of a graph, for example this one:
http://pear.php.net/package/Structures_Graph/docs/latest/li_Structures_Graph.html
But it might be overkill as the 2-dimensional array would even do the trick
and there might be less overhead although you are requiring more space than
needed (n*(n-1)/2+n cells more to be exact).
You could store it in a hashmap/associative array like this:
<?php
$graph = array(
'A' => array('B' => 1, 'F' => 2),
'B' => array('A' => 3, 'D' => 4, 'E' => 5),
'C' => array('F' => 6),
'D' => array('B' => 7, 'E' => 8),
'E' => array('B' => 9, 'D' => 10, 'F' => 11),
'F' => array('A' => 12, 'E' => 13, 'C' => 14),
);
(Sorry if large font). Source of idea:
http://www.sitepoint.com/data-structures-4/
Cheers,
T
Tim-Hinnerk Heuer
Twitter: @geekdenz
Blog: http://www.thheuer.com
On 4 October 2013 02:17, Nickolas Whiting <prg...@gmail.com> wrote:
> Round Robin algorithm should solve this and is a fairly quick alogrithm ...
> http://en.wikipedia.org/wiki/Round-robin
>
> An example can be found
> http://forrst.com/posts/PHP_Round_Robin_Algorithm-2zm
>
>
> On Tue, Oct 1, 2013 at 2:51 PM, Floyd Resler <fres...@adex-intl.com>
> wrote:
>
> > Here's my task: A group of kids is going to be staying with different
> host
> > families throughout the next 8 months. The number of kids staying with a
> > host family can range from 2 to 10. When deciding which kids should stay
> > together at a host family, the idea is for the system to put together
> kids
> > who have stayed with each other the least on past weekends. So, if a
> host
> > family can keep 5 kids, then the group of 5 kids who have stayed together
> > the least will be chosen.
> >
> > I can't think of an easy, quick way to accomplish this. I've tried
> > various approaches that have resulted in a lot of coding and being very
> > slow. My idea was to give each group of kids a score and the lowest
> score
> > is the group that is selected. However, this approach wound of iterating
> > through several arrays several times which was really slow. Does anyone
> > have any ideas on this puzzle?
> >
> > Thanks!
> > Floyd
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
> --
> Nickolas Whiting
> Freelance Consultant
>
--- End Message ---
--- Begin Message ---
Try this class:
<?php
// ASSUMES NAMES DON'T HAVE "|" IN THEM!! YOU COULD USE ANOTHER
// CHARACTER COMBO IF NEEDED AND explode ON THAT
class Graph {
protected $data = null;
public function __construct($init = array()) {
$this->data = $init;
}
public function together($who, $with) {
if (!isset($this->data[$who])) {
$this->data[$who] = array();
}
if (!isset($this->data[$who][$with])) {
$this->data[$who][$with] = 1;
return;
}
$this->data[$who][$with]++;
}
public function getLeast($n = 1) {
$values = array();
foreach ($this->data as $who => $withs) {
foreach ($withs as $kwith => $vwith) {
$values[$who .'|'. $kwith] = $vwith;
}
}
asort($values);
$nvalues = array_slice($values, 0, $n);
$pairs = array();
foreach ($nvalues as $k => $v) {
$parts = explode('|', $k);
$pairs[] = array($parts[0], $parts[1]);
}
return $pairs;
}
public function __toString() {
return print_r($this->data, true);
}
}
$graph = new Graph();
$graph->together('A', 'B');
$graph->together('A', 'B');
$graph->together('B', 'C');
$graph->together('A', 'C');
$graph->together('B', 'D');
$graph->together('B', 'D');
$graph->together('B', 'D');
$graph->together('B', 'D');
$graph->together('B', 'D');
echo $graph;
$least = $graph->getLeast(2);
print_r($least);
Tim-Hinnerk Heuer
Twitter: @geekdenz
Blog: http://www.thheuer.com
On 20 October 2013 15:33, German Geek <geek...@gmail.com> wrote:
> This is how I would approach/imagine it:
>
>
> https://docs.google.com/drawings/d/111RISgcHyAg8NXem4H1NXnxByRUydL8GiYlGkobJwus/edit
>
> Tom has been with Andrew 0 times.
> Tom has been with Shelly 1 time.
> Christine has been with Andrew 2 times.
> ...
>
> So the Graph maintains who has been with who how often.
>
> For 10 or even 20 kids you might be able to go through all links (brute
> force).
>
> The number of links (including the ones with 0 weight) is
> #links = n*(n-1)/2
> which is the number of links you have to maintain and then check when you
> want to know who should go with whom.
>
> So, if
> n=10: #links = 10*9/2 = 45
> n=20: #links = 20*19/2 = 190
> n=30: #links = 30*29/2 = 435
>
> I think even for reasonably large groups a computer can do the job easily.
> I would find it quite hard to do it on paper though, so I think you should
> program it.
>
> You could simply store the graph in an array, and then optionally persist
> it to a db or file:
>
> You would get e.g.:
>
> $graph = array(
> '0,1' => 0,
> '0,2' => 2,
> ...
>
> Edit: Actually, maybe you can do it in a two-dimensional array, where no
> node is connected to itself:
>
> $n=4;
> function init() {
> global $n;
> $graph = array();
> for ($i = 0; $i < $n; ++$i) {
> $graph[$i] = array();
> for ($j = 0; $j < $n; ++$j) {
> $graph[$i][$j] = 0;
> }
> }
> return $graph;
> }
>
> $graph = init();
>
> Sorry, I might be running a bit out of time here...
>
> You can use an implementation of a graph, for example this one:
>
> http://pear.php.net/package/Structures_Graph/docs/latest/li_Structures_Graph.html
>
> But it might be overkill as the 2-dimensional array would even do the
> trick and there might be less overhead although you are requiring more
> space than needed (n*(n-1)/2+n cells more to be exact).
>
> You could store it in a hashmap/associative array like this:
> <?php
> $graph = array(
> 'A' => array('B' => 1, 'F' => 2),
> 'B' => array('A' => 3, 'D' => 4, 'E' => 5),
> 'C' => array('F' => 6),
> 'D' => array('B' => 7, 'E' => 8),
> 'E' => array('B' => 9, 'D' => 10, 'F' => 11),
> 'F' => array('A' => 12, 'E' => 13, 'C' => 14),
> );
> (Sorry if large font). Source of idea:
> http://www.sitepoint.com/data-structures-4/
>
> Cheers,
> T
>
> Tim-Hinnerk Heuer
>
> Twitter: @geekdenz
> Blog: http://www.thheuer.com
>
>
> On 4 October 2013 02:17, Nickolas Whiting <prg...@gmail.com> wrote:
>
>> Round Robin algorithm should solve this and is a fairly quick alogrithm
>> ...
>> http://en.wikipedia.org/wiki/Round-robin
>>
>> An example can be found
>> http://forrst.com/posts/PHP_Round_Robin_Algorithm-2zm
>>
>>
>> On Tue, Oct 1, 2013 at 2:51 PM, Floyd Resler <fres...@adex-intl.com>
>> wrote:
>>
>> > Here's my task: A group of kids is going to be staying with different
>> host
>> > families throughout the next 8 months. The number of kids staying with
>> a
>> > host family can range from 2 to 10. When deciding which kids should
>> stay
>> > together at a host family, the idea is for the system to put together
>> kids
>> > who have stayed with each other the least on past weekends. So, if a
>> host
>> > family can keep 5 kids, then the group of 5 kids who have stayed
>> together
>> > the least will be chosen.
>> >
>> > I can't think of an easy, quick way to accomplish this. I've tried
>> > various approaches that have resulted in a lot of coding and being very
>> > slow. My idea was to give each group of kids a score and the lowest
>> score
>> > is the group that is selected. However, this approach wound of
>> iterating
>> > through several arrays several times which was really slow. Does anyone
>> > have any ideas on this puzzle?
>> >
>> > Thanks!
>> > Floyd
>> >
>> >
>> > --
>> > PHP General Mailing List (http://www.php.net/)
>> > To unsubscribe, visit: http://www.php.net/unsub.php
>> >
>> >
>>
>>
>> --
>> Nickolas Whiting
>> Freelance Consultant
>>
>
>
--- End Message ---
--- Begin Message ---
Oh and it also assumes that you don't do
$graph->together('A','B');
// ...
$graph->together('B', 'A'); //!! NO!
If this has to be catered for you could simply sort them when inserting:
public function together($who, $with) {
$sorted = array($who, $with);
sort($sorted);
$who = $sorted[0];
$with = $sorted[1];
if (!isset($this->data[$who])) {
$this->data[$who] = array();
}
if (!isset($this->data[$who][$with])) {
$this->data[$who][$with] = 1;
return;
}
$this->data[$who][$with]++;
}
for the together function.
Tim-Hinnerk Heuer
Twitter: @geekdenz
Blog: http://www.thheuer.com
On 20 October 2013 19:13, German Geek <geek...@gmail.com> wrote:
> Try this class:
>
> <?php
>
> // ASSUMES NAMES DON'T HAVE "|" IN THEM!! YOU COULD USE ANOTHER
> // CHARACTER COMBO IF NEEDED AND explode ON THAT
>
> class Graph {
> protected $data = null;
>
> public function __construct($init = array()) {
> $this->data = $init;
> }
>
> public function together($who, $with) {
> if (!isset($this->data[$who])) {
> $this->data[$who] = array();
> }
> if (!isset($this->data[$who][$with])) {
> $this->data[$who][$with] = 1;
> return;
> }
> $this->data[$who][$with]++;
> }
> public function getLeast($n = 1) {
> $values = array();
> foreach ($this->data as $who => $withs) {
> foreach ($withs as $kwith => $vwith) {
> $values[$who .'|'. $kwith] = $vwith;
> }
> }
> asort($values);
> $nvalues = array_slice($values, 0, $n);
> $pairs = array();
> foreach ($nvalues as $k => $v) {
> $parts = explode('|', $k);
> $pairs[] = array($parts[0], $parts[1]);
> }
> return $pairs;
> }
> public function __toString() {
> return print_r($this->data, true);
> }
> }
>
> $graph = new Graph();
>
> $graph->together('A', 'B');
> $graph->together('A', 'B');
> $graph->together('B', 'C');
> $graph->together('A', 'C');
> $graph->together('B', 'D');
> $graph->together('B', 'D');
> $graph->together('B', 'D');
> $graph->together('B', 'D');
> $graph->together('B', 'D');
>
> echo $graph;
>
> $least = $graph->getLeast(2);
>
> print_r($least);
>
>
> Tim-Hinnerk Heuer
>
> Twitter: @geekdenz
> Blog: http://www.thheuer.com
>
>
> On 20 October 2013 15:33, German Geek <geek...@gmail.com> wrote:
>
>> This is how I would approach/imagine it:
>>
>>
>> https://docs.google.com/drawings/d/111RISgcHyAg8NXem4H1NXnxByRUydL8GiYlGkobJwus/edit
>>
>> Tom has been with Andrew 0 times.
>> Tom has been with Shelly 1 time.
>> Christine has been with Andrew 2 times.
>> ...
>>
>> So the Graph maintains who has been with who how often.
>>
>> For 10 or even 20 kids you might be able to go through all links (brute
>> force).
>>
>> The number of links (including the ones with 0 weight) is
>> #links = n*(n-1)/2
>> which is the number of links you have to maintain and then check when you
>> want to know who should go with whom.
>>
>> So, if
>> n=10: #links = 10*9/2 = 45
>> n=20: #links = 20*19/2 = 190
>> n=30: #links = 30*29/2 = 435
>>
>> I think even for reasonably large groups a computer can do the job
>> easily. I would find it quite hard to do it on paper though, so I think you
>> should program it.
>>
>> You could simply store the graph in an array, and then optionally persist
>> it to a db or file:
>>
>> You would get e.g.:
>>
>> $graph = array(
>> '0,1' => 0,
>> '0,2' => 2,
>> ...
>>
>> Edit: Actually, maybe you can do it in a two-dimensional array, where no
>> node is connected to itself:
>>
>> $n=4;
>> function init() {
>> global $n;
>> $graph = array();
>> for ($i = 0; $i < $n; ++$i) {
>> $graph[$i] = array();
>> for ($j = 0; $j < $n; ++$j) {
>> $graph[$i][$j] = 0;
>> }
>> }
>> return $graph;
>> }
>>
>> $graph = init();
>>
>> Sorry, I might be running a bit out of time here...
>>
>> You can use an implementation of a graph, for example this one:
>>
>> http://pear.php.net/package/Structures_Graph/docs/latest/li_Structures_Graph.html
>>
>> But it might be overkill as the 2-dimensional array would even do the
>> trick and there might be less overhead although you are requiring more
>> space than needed (n*(n-1)/2+n cells more to be exact).
>>
>> You could store it in a hashmap/associative array like this:
>> <?php
>> $graph = array(
>> 'A' => array('B' => 1, 'F' => 2),
>> 'B' => array('A' => 3, 'D' => 4, 'E' => 5),
>> 'C' => array('F' => 6),
>> 'D' => array('B' => 7, 'E' => 8),
>> 'E' => array('B' => 9, 'D' => 10, 'F' => 11),
>> 'F' => array('A' => 12, 'E' => 13, 'C' => 14),
>> );
>> (Sorry if large font). Source of idea:
>> http://www.sitepoint.com/data-structures-4/
>>
>> Cheers,
>> T
>>
>> Tim-Hinnerk Heuer
>>
>> Twitter: @geekdenz
>> Blog: http://www.thheuer.com
>>
>>
>> On 4 October 2013 02:17, Nickolas Whiting <prg...@gmail.com> wrote:
>>
>>> Round Robin algorithm should solve this and is a fairly quick alogrithm
>>> ...
>>> http://en.wikipedia.org/wiki/Round-robin
>>>
>>> An example can be found
>>> http://forrst.com/posts/PHP_Round_Robin_Algorithm-2zm
>>>
>>>
>>> On Tue, Oct 1, 2013 at 2:51 PM, Floyd Resler <fres...@adex-intl.com>
>>> wrote:
>>>
>>> > Here's my task: A group of kids is going to be staying with different
>>> host
>>> > families throughout the next 8 months. The number of kids staying
>>> with a
>>> > host family can range from 2 to 10. When deciding which kids should
>>> stay
>>> > together at a host family, the idea is for the system to put together
>>> kids
>>> > who have stayed with each other the least on past weekends. So, if a
>>> host
>>> > family can keep 5 kids, then the group of 5 kids who have stayed
>>> together
>>> > the least will be chosen.
>>> >
>>> > I can't think of an easy, quick way to accomplish this. I've tried
>>> > various approaches that have resulted in a lot of coding and being very
>>> > slow. My idea was to give each group of kids a score and the lowest
>>> score
>>> > is the group that is selected. However, this approach wound of
>>> iterating
>>> > through several arrays several times which was really slow. Does
>>> anyone
>>> > have any ideas on this puzzle?
>>> >
>>> > Thanks!
>>> > Floyd
>>> >
>>> >
>>> > --
>>> > PHP General Mailing List (http://www.php.net/)
>>> > To unsubscribe, visit: http://www.php.net/unsub.php
>>> >
>>> >
>>>
>>>
>>> --
>>> Nickolas Whiting
>>> Freelance Consultant
>>>
>>
>>
>
--- End Message ---
--- Begin Message ---
Hi,
Indeed making and maintaining the graph looks like the best approach here
to tackle this problem , but what does not seem clear to me is this --
"Suppose a family can host 5 children , then you need to find the set of 5
such nodes out of the total no. of nodes(assume 10) such that the total
weight of all edges connecting the 5*4 nodes is minimum " ,
how do you go about finding this set once you have constructed and
maintained this graph and what will be the complexity??
On Sun, Oct 20, 2013 at 11:49 AM, German Geek <geek...@gmail.com> wrote:
> Oh and it also assumes that you don't do
> $graph->together('A','B');
> // ...
> $graph->together('B', 'A'); //!! NO!
>
> If this has to be catered for you could simply sort them when inserting:
> public function together($who, $with) {
> $sorted = array($who, $with);
> sort($sorted);
> $who = $sorted[0];
> $with = $sorted[1];
> if (!isset($this->data[$who])) {
> $this->data[$who] = array();
> }
> if (!isset($this->data[$who][$with])) {
> $this->data[$who][$with] = 1;
> return;
> }
> $this->data[$who][$with]++;
> }
>
> for the together function.
>
> Tim-Hinnerk Heuer
>
> Twitter: @geekdenz
> Blog: http://www.thheuer.com
>
>
> On 20 October 2013 19:13, German Geek <geek...@gmail.com> wrote:
>
> > Try this class:
> >
> > <?php
> >
> > // ASSUMES NAMES DON'T HAVE "|" IN THEM!! YOU COULD USE ANOTHER
> > // CHARACTER COMBO IF NEEDED AND explode ON THAT
> >
> > class Graph {
> > protected $data = null;
> >
> > public function __construct($init = array()) {
> > $this->data = $init;
> > }
> >
> > public function together($who, $with) {
> > if (!isset($this->data[$who])) {
> > $this->data[$who] = array();
> > }
> > if (!isset($this->data[$who][$with])) {
> > $this->data[$who][$with] = 1;
> > return;
> > }
> > $this->data[$who][$with]++;
> > }
> > public function getLeast($n = 1) {
> > $values = array();
> > foreach ($this->data as $who => $withs) {
> > foreach ($withs as $kwith => $vwith) {
> > $values[$who .'|'. $kwith] = $vwith;
> > }
> > }
> > asort($values);
> > $nvalues = array_slice($values, 0, $n);
> > $pairs = array();
> > foreach ($nvalues as $k => $v) {
> > $parts = explode('|', $k);
> > $pairs[] = array($parts[0], $parts[1]);
> > }
> > return $pairs;
> > }
> > public function __toString() {
> > return print_r($this->data, true);
> > }
> > }
> >
> > $graph = new Graph();
> >
> > $graph->together('A', 'B');
> > $graph->together('A', 'B');
> > $graph->together('B', 'C');
> > $graph->together('A', 'C');
> > $graph->together('B', 'D');
> > $graph->together('B', 'D');
> > $graph->together('B', 'D');
> > $graph->together('B', 'D');
> > $graph->together('B', 'D');
> >
> > echo $graph;
> >
> > $least = $graph->getLeast(2);
> >
> > print_r($least);
> >
> >
> > Tim-Hinnerk Heuer
> >
> > Twitter: @geekdenz
> > Blog: http://www.thheuer.com
> >
> >
> > On 20 October 2013 15:33, German Geek <geek...@gmail.com> wrote:
> >
> >> This is how I would approach/imagine it:
> >>
> >>
> >>
> https://docs.google.com/drawings/d/111RISgcHyAg8NXem4H1NXnxByRUydL8GiYlGkobJwus/edit
> >>
> >> Tom has been with Andrew 0 times.
> >> Tom has been with Shelly 1 time.
> >> Christine has been with Andrew 2 times.
> >> ...
> >>
> >> So the Graph maintains who has been with who how often.
> >>
> >> For 10 or even 20 kids you might be able to go through all links (brute
> >> force).
> >>
> >> The number of links (including the ones with 0 weight) is
> >> #links = n*(n-1)/2
> >> which is the number of links you have to maintain and then check when
> you
> >> want to know who should go with whom.
> >>
> >> So, if
> >> n=10: #links = 10*9/2 = 45
> >> n=20: #links = 20*19/2 = 190
> >> n=30: #links = 30*29/2 = 435
> >>
> >> I think even for reasonably large groups a computer can do the job
> >> easily. I would find it quite hard to do it on paper though, so I think
> you
> >> should program it.
> >>
> >> You could simply store the graph in an array, and then optionally
> persist
> >> it to a db or file:
> >>
> >> You would get e.g.:
> >>
> >> $graph = array(
> >> '0,1' => 0,
> >> '0,2' => 2,
> >> ...
> >>
> >> Edit: Actually, maybe you can do it in a two-dimensional array, where no
> >> node is connected to itself:
> >>
> >> $n=4;
> >> function init() {
> >> global $n;
> >> $graph = array();
> >> for ($i = 0; $i < $n; ++$i) {
> >> $graph[$i] = array();
> >> for ($j = 0; $j < $n; ++$j) {
> >> $graph[$i][$j] = 0;
> >> }
> >> }
> >> return $graph;
> >> }
> >>
> >> $graph = init();
> >>
> >> Sorry, I might be running a bit out of time here...
> >>
> >> You can use an implementation of a graph, for example this one:
> >>
> >>
> http://pear.php.net/package/Structures_Graph/docs/latest/li_Structures_Graph.html
> >>
> >> But it might be overkill as the 2-dimensional array would even do the
> >> trick and there might be less overhead although you are requiring more
> >> space than needed (n*(n-1)/2+n cells more to be exact).
> >>
> >> You could store it in a hashmap/associative array like this:
> >> <?php
> >> $graph = array(
> >> 'A' => array('B' => 1, 'F' => 2),
> >> 'B' => array('A' => 3, 'D' => 4, 'E' => 5),
> >> 'C' => array('F' => 6),
> >> 'D' => array('B' => 7, 'E' => 8),
> >> 'E' => array('B' => 9, 'D' => 10, 'F' => 11),
> >> 'F' => array('A' => 12, 'E' => 13, 'C' => 14),
> >> );
> >> (Sorry if large font). Source of idea:
> >> http://www.sitepoint.com/data-structures-4/
> >>
> >> Cheers,
> >> T
> >>
> >> Tim-Hinnerk Heuer
> >>
> >> Twitter: @geekdenz
> >> Blog: http://www.thheuer.com
> >>
> >>
> >> On 4 October 2013 02:17, Nickolas Whiting <prg...@gmail.com> wrote:
> >>
> >>> Round Robin algorithm should solve this and is a fairly quick alogrithm
> >>> ...
> >>> http://en.wikipedia.org/wiki/Round-robin
> >>>
> >>> An example can be found
> >>> http://forrst.com/posts/PHP_Round_Robin_Algorithm-2zm
> >>>
> >>>
> >>> On Tue, Oct 1, 2013 at 2:51 PM, Floyd Resler <fres...@adex-intl.com>
> >>> wrote:
> >>>
> >>> > Here's my task: A group of kids is going to be staying with different
> >>> host
> >>> > families throughout the next 8 months. The number of kids staying
> >>> with a
> >>> > host family can range from 2 to 10. When deciding which kids should
> >>> stay
> >>> > together at a host family, the idea is for the system to put together
> >>> kids
> >>> > who have stayed with each other the least on past weekends. So, if a
> >>> host
> >>> > family can keep 5 kids, then the group of 5 kids who have stayed
> >>> together
> >>> > the least will be chosen.
> >>> >
> >>> > I can't think of an easy, quick way to accomplish this. I've tried
> >>> > various approaches that have resulted in a lot of coding and being
> very
> >>> > slow. My idea was to give each group of kids a score and the lowest
> >>> score
> >>> > is the group that is selected. However, this approach wound of
> >>> iterating
> >>> > through several arrays several times which was really slow. Does
> >>> anyone
> >>> > have any ideas on this puzzle?
> >>> >
> >>> > Thanks!
> >>> > Floyd
> >>> >
> >>> >
> >>> > --
> >>> > PHP General Mailing List (http://www.php.net/)
> >>> > To unsubscribe, visit: http://www.php.net/unsub.php
> >>> >
> >>> >
> >>>
> >>>
> >>> --
> >>> Nickolas Whiting
> >>> Freelance Consultant
> >>>
> >>
> >>
> >
>
--- End Message ---
--- Begin Message ---
You don't need to maintain the history of which kids stay where unless you
want to for other reasons. You just need to find the children that have
staid the least amount of time together, which this approach would do for
you.
So, when 4 children stay together you say
1 together with 2
1 together with 3
1 together with 4
2 together with 3
2 together with 4
3 together with 4
and that's it. And then you can find the ones that staid together the least
amount of time.
Tim-Hinnerk Heuer
Twitter: @geekdenz
Blog: http://www.thheuer.com
On 20 October 2013 21:53, Ayush Ladia <ayushladia.for...@gmail.com> wrote:
> Hi,
> Indeed making and maintaining the graph looks like the best approach here
> to tackle this problem , but what does not seem clear to me is this --
> "Suppose a family can host 5 children , then you need to find the set of
> 5 such nodes out of the total no. of nodes(assume 10) such that the total
> weight of all edges connecting the 5*4 nodes is minimum " ,
> how do you go about finding this set once you have constructed and
> maintained this graph and what will be the complexity??
>
>
> On Sun, Oct 20, 2013 at 11:49 AM, German Geek <geek...@gmail.com> wrote:
>
>> Oh and it also assumes that you don't do
>> $graph->together('A','B');
>> // ...
>> $graph->together('B', 'A'); //!! NO!
>>
>> If this has to be catered for you could simply sort them when inserting:
>> public function together($who, $with) {
>> $sorted = array($who, $with);
>> sort($sorted);
>> $who = $sorted[0];
>> $with = $sorted[1];
>> if (!isset($this->data[$who])) {
>> $this->data[$who] = array();
>> }
>> if (!isset($this->data[$who][$with])) {
>> $this->data[$who][$with] = 1;
>> return;
>> }
>> $this->data[$who][$with]++;
>> }
>>
>> for the together function.
>>
>> Tim-Hinnerk Heuer
>>
>> Twitter: @geekdenz
>> Blog: http://www.thheuer.com
>>
>>
>> On 20 October 2013 19:13, German Geek <geek...@gmail.com> wrote:
>>
>> > Try this class:
>> >
>> > <?php
>> >
>> > // ASSUMES NAMES DON'T HAVE "|" IN THEM!! YOU COULD USE ANOTHER
>> > // CHARACTER COMBO IF NEEDED AND explode ON THAT
>> >
>> > class Graph {
>> > protected $data = null;
>> >
>> > public function __construct($init = array()) {
>> > $this->data = $init;
>> > }
>> >
>> > public function together($who, $with) {
>> > if (!isset($this->data[$who])) {
>> > $this->data[$who] = array();
>> > }
>> > if (!isset($this->data[$who][$with])) {
>> > $this->data[$who][$with] = 1;
>> > return;
>> > }
>> > $this->data[$who][$with]++;
>> > }
>> > public function getLeast($n = 1) {
>> > $values = array();
>> > foreach ($this->data as $who => $withs) {
>> > foreach ($withs as $kwith => $vwith) {
>> > $values[$who .'|'. $kwith] = $vwith;
>> > }
>> > }
>> > asort($values);
>> > $nvalues = array_slice($values, 0, $n);
>> > $pairs = array();
>> > foreach ($nvalues as $k => $v) {
>> > $parts = explode('|', $k);
>> > $pairs[] = array($parts[0], $parts[1]);
>> > }
>> > return $pairs;
>> > }
>> > public function __toString() {
>> > return print_r($this->data, true);
>> > }
>> > }
>> >
>> > $graph = new Graph();
>> >
>> > $graph->together('A', 'B');
>> > $graph->together('A', 'B');
>> > $graph->together('B', 'C');
>> > $graph->together('A', 'C');
>> > $graph->together('B', 'D');
>> > $graph->together('B', 'D');
>> > $graph->together('B', 'D');
>> > $graph->together('B', 'D');
>> > $graph->together('B', 'D');
>> >
>> > echo $graph;
>> >
>> > $least = $graph->getLeast(2);
>> >
>> > print_r($least);
>> >
>> >
>> > Tim-Hinnerk Heuer
>> >
>> > Twitter: @geekdenz
>> > Blog: http://www.thheuer.com
>> >
>> >
>> > On 20 October 2013 15:33, German Geek <geek...@gmail.com> wrote:
>> >
>> >> This is how I would approach/imagine it:
>> >>
>> >>
>> >>
>> https://docs.google.com/drawings/d/111RISgcHyAg8NXem4H1NXnxByRUydL8GiYlGkobJwus/edit
>> >>
>> >> Tom has been with Andrew 0 times.
>> >> Tom has been with Shelly 1 time.
>> >> Christine has been with Andrew 2 times.
>> >> ...
>> >>
>> >> So the Graph maintains who has been with who how often.
>> >>
>> >> For 10 or even 20 kids you might be able to go through all links (brute
>> >> force).
>> >>
>> >> The number of links (including the ones with 0 weight) is
>> >> #links = n*(n-1)/2
>> >> which is the number of links you have to maintain and then check when
>> you
>> >> want to know who should go with whom.
>> >>
>> >> So, if
>> >> n=10: #links = 10*9/2 = 45
>> >> n=20: #links = 20*19/2 = 190
>> >> n=30: #links = 30*29/2 = 435
>> >>
>> >> I think even for reasonably large groups a computer can do the job
>> >> easily. I would find it quite hard to do it on paper though, so I
>> think you
>> >> should program it.
>> >>
>> >> You could simply store the graph in an array, and then optionally
>> persist
>> >> it to a db or file:
>> >>
>> >> You would get e.g.:
>> >>
>> >> $graph = array(
>> >> '0,1' => 0,
>> >> '0,2' => 2,
>> >> ...
>> >>
>> >> Edit: Actually, maybe you can do it in a two-dimensional array, where
>> no
>> >> node is connected to itself:
>> >>
>> >> $n=4;
>> >> function init() {
>> >> global $n;
>> >> $graph = array();
>> >> for ($i = 0; $i < $n; ++$i) {
>> >> $graph[$i] = array();
>> >> for ($j = 0; $j < $n; ++$j) {
>> >> $graph[$i][$j] = 0;
>> >> }
>> >> }
>> >> return $graph;
>> >> }
>> >>
>> >> $graph = init();
>> >>
>> >> Sorry, I might be running a bit out of time here...
>> >>
>> >> You can use an implementation of a graph, for example this one:
>> >>
>> >>
>> http://pear.php.net/package/Structures_Graph/docs/latest/li_Structures_Graph.html
>> >>
>> >> But it might be overkill as the 2-dimensional array would even do the
>> >> trick and there might be less overhead although you are requiring more
>> >> space than needed (n*(n-1)/2+n cells more to be exact).
>> >>
>> >> You could store it in a hashmap/associative array like this:
>> >> <?php
>> >> $graph = array(
>> >> 'A' => array('B' => 1, 'F' => 2),
>> >> 'B' => array('A' => 3, 'D' => 4, 'E' => 5),
>> >> 'C' => array('F' => 6),
>> >> 'D' => array('B' => 7, 'E' => 8),
>> >> 'E' => array('B' => 9, 'D' => 10, 'F' => 11),
>> >> 'F' => array('A' => 12, 'E' => 13, 'C' => 14),
>> >> );
>> >> (Sorry if large font). Source of idea:
>> >> http://www.sitepoint.com/data-structures-4/
>> >>
>> >> Cheers,
>> >> T
>> >>
>> >> Tim-Hinnerk Heuer
>> >>
>> >> Twitter: @geekdenz
>> >> Blog: http://www.thheuer.com
>> >>
>> >>
>> >> On 4 October 2013 02:17, Nickolas Whiting <prg...@gmail.com> wrote:
>> >>
>> >>> Round Robin algorithm should solve this and is a fairly quick
>> alogrithm
>> >>> ...
>> >>> http://en.wikipedia.org/wiki/Round-robin
>> >>>
>> >>> An example can be found
>> >>> http://forrst.com/posts/PHP_Round_Robin_Algorithm-2zm
>> >>>
>> >>>
>> >>> On Tue, Oct 1, 2013 at 2:51 PM, Floyd Resler <fres...@adex-intl.com>
>> >>> wrote:
>> >>>
>> >>> > Here's my task: A group of kids is going to be staying with
>> different
>> >>> host
>> >>> > families throughout the next 8 months. The number of kids staying
>> >>> with a
>> >>> > host family can range from 2 to 10. When deciding which kids should
>> >>> stay
>> >>> > together at a host family, the idea is for the system to put
>> together
>> >>> kids
>> >>> > who have stayed with each other the least on past weekends. So, if
>> a
>> >>> host
>> >>> > family can keep 5 kids, then the group of 5 kids who have stayed
>> >>> together
>> >>> > the least will be chosen.
>> >>> >
>> >>> > I can't think of an easy, quick way to accomplish this. I've tried
>> >>> > various approaches that have resulted in a lot of coding and being
>> very
>> >>> > slow. My idea was to give each group of kids a score and the lowest
>> >>> score
>> >>> > is the group that is selected. However, this approach wound of
>> >>> iterating
>> >>> > through several arrays several times which was really slow. Does
>> >>> anyone
>> >>> > have any ideas on this puzzle?
>> >>> >
>> >>> > Thanks!
>> >>> > Floyd
>> >>> >
>> >>> >
>> >>> > --
>> >>> > PHP General Mailing List (http://www.php.net/)
>> >>> > To unsubscribe, visit: http://www.php.net/unsub.php
>> >>> >
>> >>> >
>> >>>
>> >>>
>> >>> --
>> >>> Nickolas Whiting
>> >>> Freelance Consultant
>> >>>
>> >>
>> >>
>> >
>>
>
>
--- End Message ---
--- Begin Message ---
I have date strings in my mysql db. yyyy-mm-dd.
I want to parse to see if the date is greater than november 2011 and
less than december 2012.
Is this the right approach? How bad is my syntax?
|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow|
<= $date2)||)
|| {
|| # do something
|||| }
}
|||
--- End Message ---
--- Begin Message ---
<?php
// assuming | is meant to be a space like this:
function dates_range($todaynow) {
$date1 = strtotime("2011-11-01");
$date2 = strtotime("2012-12-31");
if (($todaynow >= $date1) and ($todaynow <= $date2)) {
# do something
}
}
// I would write:
function betweenDates($dateInQuestion, $afterDate, $beforeDate) {
$after = strtotime($afterDate);
$before = strtotime($beforeDate);
$now = strtotime($dateInQuestion);
return $after <= $now && $now <= $before;
}
// in your code you can then write
if (betweenDates('now', '2011-11-01', '2012-12-31')) {
# do something
}
On 20 October 2013 15:44, John Taylor-Johnston
<jt.johns...@usherbrooke.ca>wrote:
> |function dates_range($todaynow)
> { |
> |$date1=strtotime("2011-11-01"**);
> $date2=strtotime("2012-12-31")**;
> if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow| <=
> $date2)||)
> || {
> || # do something
> |||| }
> }
> |||
>
Tim-Hinnerk Heuer
Twitter: @geekdenz
Blog: http://www.thheuer.com
--- End Message ---
--- Begin Message ---
This works for dates after 2011-11-01 but is not stopping after 2012-12-31.
What is wrong with my if statement?
$meetingdate=strtotime($mydata->meetingdate);
$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (($meetingdate >= $date1) and ($meetingtime <= $date2))
{
$mydata->meetingdate
}
John Taylor-Johnston wrote:
I have date strings in my mysql db. yyyy-mm-dd.
I want to parse to see if the date is greater than november 2011 and
less than december 2012.
Is this the right approach? How bad is my syntax?
|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow|
<= $date2)||)
|| {
|| # do something
|||| }
}
|||
--- End Message ---
--- Begin Message ---
Forget it. I saw my own error.
John Taylor-Johnston wrote:
This works for dates after 2011-11-01 but is not stopping after
2012-12-31.
What is wrong with my if statement?
$meetingdate=strtotime($mydata->meetingdate);
$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (($meetingdate >= $date1) and ($meetingtime <= $date2))
{
$mydata->meetingdate
}
John Taylor-Johnston wrote:
I have date strings in my mysql db. yyyy-mm-dd.
I want to parse to see if the date is greater than november 2011 and
less than december 2012.
Is this the right approach? How bad is my syntax?
|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow|
<= $date2)||)
|| {
|| # do something
|||| }
}
|||
--- End Message ---
--- Begin Message ---
Thanks,
Bastien
> On Oct 19, 2013, at 10:44 PM, John Taylor-Johnston
> <jt.johns...@usherbrooke.ca> wrote:
>
> I have date strings in my mysql db. yyyy-mm-dd.
> I want to parse to see if the date is greater than november 2011 and less
> than december 2012.
>
> Is this the right approach? How bad is my syntax?
>
> |function dates_range($todaynow)
> { |
> |$date1=strtotime("2011-11-01");
> $date2=strtotime("2012-12-31");
> if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow| <=
> $date2)||)
> || {
> || # do something
> |||| }
> }
> |||
Easiest to convert to integers and then compare
--- End Message ---
--- Begin Message ---
On Sun, 2013-10-20 at 00:00 -0400, Bastien wrote:
>
> Thanks,
>
> Bastien
>
> > On Oct 19, 2013, at 10:44 PM, John Taylor-Johnston
> > <jt.johns...@usherbrooke.ca> wrote:
> >
> > I have date strings in my mysql db. yyyy-mm-dd.
> > I want to parse to see if the date is greater than november 2011 and less
> > than december 2012.
> >
> > Is this the right approach? How bad is my syntax?
> >
> > |function dates_range($todaynow)
> > { |
> > |$date1=strtotime("2011-11-01");
> > $date2=strtotime("2012-12-31");
> > if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow| <=
> > $date2)||)
> > || {
> > || # do something
> > |||| }
> > }
> > |||
>
> Easiest to convert to integers and then compare
Yes, I was going to ask, why are you storing your dates as strings?
MySQL has a perfectly good DATE type. It's also generally faster
comparing dates within a MySQL query than PHP code.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--- End Message ---
--- Begin Message ---
On Oct 20, 2013, at 4:01 AM, Ashley Sheridan <a...@ashleysheridan.co.uk> wrote:
> Yes, I was going to ask, why are you storing your dates as strings?
> MySQL has a perfectly good DATE type. It's also generally faster
> comparing dates within a MySQL query than PHP code.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
Agreed.
Plus, there are many date functions provided by MySQL that are easier
(possibility faster) than what you can do in PHP.
Check these out:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
tedd
_______________
tedd sperling
tedd.sperl...@gmail.com
--- End Message ---
--- Begin Message ---
<?php
<form method="POST" action="">
<input name="next_step" type="text" value="step20" />
<input type="submit" value="press" />
</form>
<pre>
<?php
function good() {
echo "we're GOOD";
}
function bad() {
echo "BAD";
}
//Switch statement
echo "\nbefore\n";
var_dump($_POST); //post#1
print_r($_POST); //post#2
switch ( $_POST['next_step'] )
{
case 'step20':
{
echo 'step20';
if(!empty($_POST['Cust_Num']))
good();
if(empty($_POST['Cust_Num']))
bad();
break;
} //end step20
} //end switch
?>
works fine when you hit submit. Try to isolate the error by running a
simplified script like this one next time before posting.
Also, you had "pint_r" instead of "print_r" somewhere.
I would also set these in php.ini:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
; OR even
error_reporting = E_ALL & E_DEPRECATED & E_STRICT
; AND
display_errors = On
and correct all errors and warnings if you have time, but even if not...
Question to all: Does this formatting work for you or is it too much,
annoying or anything else bad?
Tim-Hinnerk Heuer
Twitter: @geekdenz
Blog: http://www.thheuer.com
On 30 September 2013 06:28, mrfroasty <mrfroa...@gmail.com> wrote:
> Hello,
>
> I suggest you put default in that switch statement and var_dump the
> $_POST.That should be enough for a programmer to pin point what goes wrong.
>
> P:S
> **You might want to consider versioning your codes to go back into its
> history to see what has changed.
>
> Muhsin
>
> On 09/29/2013 04:33 AM, Ethan Rosenberg wrote:
> > Dear List -
> >
> > I have a working program. I made one change in a switch statement,
> > and it does not work. I'm probably missing something fundamental.
> >
> > Here are some code SNIPPETS... [please note that all my debug
> > statements are at the left margin]
> >
> > Setup...
> >
> > <?php
> > session_start();
> > session_name("STORE");
> > set_time_limit(2400);
> > ini_set('display_errors', 'on');
> > ini_set('display_startup_errors', 'on');
> > error_reporting(-2);
> >
> > ini_set('error_reporting', 'E_ALL | E_STRICT');
> > ini_set('html_errors', 'On');
> > ini_set('log_errors', 'On');
> > require '/home/ethan/P/wk.inc'; //password file
> > $db = "Store";
> > $cxn =mysqli_connect($host,$user,$password,$db);
> > if (!$cxn)
> > {
> > die('Connect Error (' . mysqli_connect_errno() . ') '
> > . mysqli_connect_error());
> > }// no error
> > if($_REQUEST['welcome_already_seen']!= "already_seen")
> > show_welcome();
> >
> > //end setup
> > function show_welcome() //this is the input screen
> > {
> > <snip>
> >
> > echo " <input type='hidden' name='welcome_already_seen'
> > value='already_seen'>";
> > echo " <input type='hidden' name='next_step' value='step20' />";
> >
> > <snip>
> > }
> >
> >
> > //end input screen
> >
> > //Switch statement
> >
> > echo 'before';
> > print_r($_POST); //post#1
> >
> > switch ( $_POST['next_step'] )
> > {
> >
> > case 'step20':
> > {
> > pint_r($_POST); //post#2
> > echo 'step20';
> > if(!empty($_POST['Cust_Num']))
> > good();
> > if(empty($_POST['Cust_Num']))
> > bad();
> > break;
> > } //end step20
> >
> > <snip>
> > } //end switch
> >
> >
> >
> > post#1
> >
> > beforeArray
> > (
> > [Cust_Num] => 123
> > [Fname] =>
> > [Lname] =>
> > [Street] =>
> > [City] =>
> > [state] => NY
> > [Zip] => 10952
> > [PH1] =>
> > [PH2] =>
> > [PH3] =>
> > [Date] =>
> > [welcome_already_seen] => already_seen
> > [next_step] => step20
> >
> > )
> >
> > Cust_Num state and Zip are as entered.
> >
> > The switch statement is never entered, since post#2 is never
> > displayed, and neither good() or bad() functions are entered.
> >
> >
> > TIA
> >
> > Ethan
> >
> >
> >
>
>
> --
> Extra details:
> OSS:Gentoo Linux
> profile:x86
> Hardware:msi geforce 8600GT asus p5k-se
> location:/home/muhsin
> language(s):C/C++,PHP
> Typo:40WPM
> url:http://www.mzalendo.net
> url:http://www.zanbytes.com
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---