Jump to content

**SOLVED** Searching & Displaying from Flat Files - Please Help!


Recommended Posts

I have a flat file database containing various sporting events. I want to be able to have a simple search with a drop down menu containing, for example, a list of states. So that the person would select the state they want events from and then it would just display anything from that state not the whole file. Does that make sense? I could just use a different file for each state but then they might want to search by type of sport or month of the year so i don't want to have everything stored in two or three different files, one is much less effort. Please note: a mySQL or some such database is not an option in my particular situation.

I haven't been able to find anything on the vastness of the web that explains how to do this or if it is even possible.

Thank you in advance your help would be much appreciated.
Here's a sample:

[code]17th|June|2006|NRL|Sharks vs. Sea Eagles|Toyota Park|Sydney|NSW
18th|June|2006|NRL|Bulldogs vs. Cowboys|Carrara Stadium|Gold Coast|QLD
18th|June|2006|NRL|Storm vs. Raiders|Olympic Park|Melbourne|VIC
16th|June|2006|AFL|St. Kilda vs. Adelaide|Telstra Dome|Melbourne|VIC
17th|June|2006|Rugby Union|Cook Cup - Qantas Wallabies v England|Telstra Dome|Melbourne|VIC
17th|June|2006|AFL|Fremantle vs. Geelong|Subiaco|Perth|WA
17th|June|2006|AFL|Brisbane Lions vs. Western Bulldogs|Gabba|Brisbane|QLD
17th|June|2006|AFL|Port Adelaide vs. West Coast|AMMI Stadium|Adelaide|SA
18th|June|2006|AFL|Essendon vs. Melbourne|Telstra Dome|Melbourne|VIC
23rd|June|2006|NRL|Storm vs. Bulldogs|Olympic Park|Melbourne|VIC
24th|June|2006|NRL|Knights vs. Sharks|Energy Australia Stadium|Newcastle|NSW[/code]

That is:
Date|Month|Year|Sport|Event|Venue|City|State

At a later date I would also like to have events added the file via a form, but that's for another day [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
[code]<?php

// Let's say $str = the sample you posted

$tmp_array = explode("\n", $str);
$data = array();

foreach ($tmp_array as $value) {
   $data[] = explode("|", $value);
}

echo '<pre>';
print_r($data);
echo '</pre>';

?>[/code]

Should print:

[code]Array
(
    [0] => Array
        (
            [0] => 17th
            [1] => June
            [2] => 2006
            [3] => NRL
            [4] => Sharks vs. Sea Eagles
            [5] => Toyota Park
            [6] => Sydney
            [7] => NSW
        )

    [1] => Array
        (
            [0] => 18th
            [1] => June
            [2] => 2006
            [3] => NRL
            [4] => Bulldogs vs. Cowboys
            [5] => Carrara Stadium
            [6] => Gold Coast
            [7] => QLD
        )

    [2] => Array
        (
            [0] => 18th
            [1] => June
            [2] => 2006
            [3] => NRL
            [4] => Storm vs. Raiders
            [5] => Olympic Park
            [6] => Melbourne
            [7] => VIC
        )

    [3] => Array
        (
            [0] => 16th
            [1] => June
            [2] => 2006
            [3] => AFL
            [4] => St. Kilda vs. Adelaide
            [5] => Telstra Dome
            [6] => Melbourne
            [7] => VIC
        )

    [4] => Array
        (
            [0] => 17th
            [1] => June
            [2] => 2006
            [3] => Rugby Union
            [4] => Cook Cup - Qantas Wallabies v England
            [5] => Telstra Dome
            [6] => Melbourne
            [7] => VIC
        )

    [5] => Array
        (
            [0] => 17th
            [1] => June
            [2] => 2006
            [3] => AFL
            [4] => Fremantle vs. Geelong
            [5] => Subiaco
            [6] => Perth
            [7] => WA
        )

    [6] => Array
        (
            [0] => 17th
            [1] => June
            [2] => 2006
            [3] => AFL
            [4] => Brisbane Lions vs. Western Bulldogs
            [5] => Gabba
            [6] => Brisbane
            [7] => QLD
        )

    [7] => Array
        (
            [0] => 17th
            [1] => June
            [2] => 2006
            [3] => AFL
            [4] => Port Adelaide vs. West Coast
            [5] => AMMI Stadium
            [6] => Adelaide
            [7] => SA
        )

    [8] => Array
        (
            [0] => 18th
            [1] => June
            [2] => 2006
            [3] => AFL
            [4] => Essendon vs. Melbourne
            [5] => Telstra Dome
            [6] => Melbourne
            [7] => VIC
        )

    [9] => Array
        (
            [0] => 23rd
            [1] => June
            [2] => 2006
            [3] => NRL
            [4] => Storm vs. Bulldogs
            [5] => Olympic Park
            [6] => Melbourne
            [7] => VIC
        )

    [10] => Array
        (
            [0] => 24th
            [1] => June
            [2] => 2006
            [3] => NRL
            [4] => Knights vs. Sharks
            [5] => Energy Australia Stadium
            [6] => Newcastle
            [7] => NSW
        )

)[/code]
Here's a way to search for state:

[code]
<?php

// Let's say $str = the sample you posted
// And $state is the state you want to search for

$tmp_array = explode("\n", $str);

foreach ($tmp_array as $value) {
   $curr = explode("|", $value);
   // check for whatever you want here
   if ($curr[7] == $state) {
      // print it out however you like
      echo "<pre>";
      print_r($curr);
      echo "</pre>";
   }
}

?>
[/code]

You could simply extend that to search for other things.

Monkeymatt
This is driving me nuts. Everything works except the if statement can somebody please have a look and tell me what i'm doing wrong. If I comment out the if statement and just have it print every line from the file it is fine but as soon as i try to get it to check for something i can't get it work.

[code]<html>
<body>
<table border="1" cellpadding="3">
<tr bgcolor="#CCCCCC">
    <td>Date</td>
    <td>Month</td>
    <td>Year</td>
    <td>Sport</td>
    <td>Event</td>
    <td>Venue</td>
    <td>City</td>
    <td>State</td>
</tr>

<?php
$fp = 'events_database.txt';
$tmp_array = file($fp);
$state = 'VIC';

foreach ($tmp_array as $value) {
    $curr = explode("|", $value);
    // check for whatever you want here
    if($curr[7] == $state){
        // print it out however you like
        echo '
            <tr>
                <td>'.$curr[0].'</td>
                <td>'.$curr[1].'</td>
                <td>'.$curr[2].'</td>
                <td>'.$curr[3].'</td>
                <td>'.$curr[4].'</td>
                <td>'.$curr[5].'</td>
                <td>'.$curr[6].'</td>
                <td>'.$curr[7].'</td>
            </tr>';
    }
}
?>
</table>
</body>
</html>[/code]

Add:

var_dump($curr[7]);

Inside the loop. This will tell you what $curr[7] actually is.
My guess is that $curr[7] will have some empty character (space, newline), and this causes the comparison to fail.

If this is the case, use trim($curr[7]) == $state.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.