0

Possible Duplicate:
how to extract data from csv file in php

An assignment I have recently received asks of me to make a mock dating site. A lot of the work is provided, and all that is necessary of me is to write the PHP. The part I am stuck on is regarding looking for 'matches' between queried names and names in the .txt file (a database of names already inputted). The text file looks like this:

Jenny,F,28,INTJ,Gentoo,18,30
Michael,M,22,ISTJ,Windows,20,25
...
Name,Gender,Age,Personality,OS,minage,maxage

The page I am working with prompts me for a name (of one of the people in the .txt file), and then finds potential matches. A 'match' is any person with all of the following: opposing gender, within their respective age ranges, share at least two letters of personality type (eg. ISTP + ESFP = match), and same favorite operating system. It then displays the matches in HTML, which I'm capable of.

My problem is I'm not entirely sure how to cross-reference the text file. I initially thought I should put each line of the .txt into an array element of a larger array:

<?php
    //Initialize the contents of the url (.php?name=$$$) into a variable $inputname
    $inputname = $_GET["name"];
    //Finds how many lines in the singles.txt file
    $lines = count(file("./singles.txt"));
    //Initialize each line into an array
    $matchespre = file("./singles.txt");
?>

...but I'm not sure how I can search for specific characters (INTJ, M, 30, 40, etc etc).

How do I get my desired result? I'm sorry if it's difficult to understand what my end goal is, the question itself is very hard to follow (in my opinion). Apparently I lack the reputation to post an image, else I'd screengrab the assignment question itself to post.

edit; I think maybe what I'm looking for is a way to put each line of the .txt into a string (eg. string1 = Jenny,F,28,INTJ,Gentoo,18,30; string2 = Michael,M,22,ISTJ,Windows,20,25, etc), then explode() each string into an array, so I have a bunch of arrays. I can then search each array sequentially for matching gender, age range, etc. I just lack the encyclopedia of functions PHP has to offer to know exactly what to do.

Community
  • 1
  • 1
gator
  • 3,465
  • 8
  • 36
  • 76
  • 1
    You're smart enough for the task. You just lack a documentation ;)... Here you go: http://www.php.net/manual/ – nirazul Nov 11 '12 at 17:21

2 Answers2

2

If all of the values are in the same order, you can happily put them into an array and sort that for the appropriate values, just make an array and loop through the lines...

// Jenny,F,28,INTJ,Gentoo,18,30

// make an array to put your values in
$array = array();

// loop through the lines
foreach($text as $line) {
  $values = $explode(",",$line);
  $array[]['name'] = $values[0];             // Jenny
  $array[]['gender'] = $values[1];           // F
  $array[]['age'] = $values[2];              // 28
  $array[]['whatever_that_is'] = $values[3]; // INTJ
  $array[]['operating_system'] = $values[4]; // Gentoo
  $array[]['min_age'] = $values[5];          // 18
  $array[]['max_age'] = $values[6];          // 30
}

Sorry, you may want to look for the eligable people...!

// search for the gender in your array
// set the gender you are looking for
$gender = "M";
// make a new array
$nameArray = array();

// loop through your other array
foreach($array as $fieldArr) {
  if($fieldArr['gender'] = $gender;) {
    // echo or add that person to another array,
    //or whatever you want to do
    echo $fieldArr['name'] . "<br />\n";
  }
}
Lucas
  • 1,476
  • 13
  • 20
  • I recommend to split 'whatever_that_is' into a separate array: $array[]['whatever_that_is'] = explode( $values[3], ''); – nirazul Nov 11 '12 at 17:25
  • Thank you! You'll have to excuse me, but I'm not entirely sure how foreach() works; is there a way to do this just using a for loop? My understanding is that they're the same, and it's up to preference what you use; I'm much more comfortable with for rather than foreach. – gator Nov 11 '12 at 17:36
  • foreach() is just a lazymans for() ;) It loops through all the items in an array, as if you were doing $item[$i] or whatever. – Lucas Nov 11 '12 at 18:09
  • Makes sense. I learned C first and it seems more logical to do the longer for loop than a foreach(), but that's just me. Thanks for you help, I really appreciate it! – gator Nov 11 '12 at 18:40
0

I'd split every line into single bits of arguments:

$arrDating = array();
foreach($matchespre as $line) {
    array_push($arrDating, explode(',', $line) );
}
nirazul
  • 3,928
  • 4
  • 26
  • 46
  • Where are the values stored? I'm afraid I don't really follow. edit; actually, I think I kind of get it. It stores them into arrDating[i][j]; I've encountered something similar in C with multidimensional arrays. I imagine this is the PHP equivalent? – gator Nov 11 '12 at 17:37
  • Exactly, value arrDating[0][2] would be the second value of the first line: '28' – nirazul Nov 11 '12 at 17:50
  • Excellent, thank you! Everything is working just peachy now. – gator Nov 11 '12 at 18:39
  • Cool :)... I found an error in my comment above: arrDating[0][2] is of course the **third** value, not the second. – nirazul Nov 11 '12 at 18:43