Jump to content


Photo

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


  • Please log in to reply
8 replies to this topic

#1 fleabel

fleabel
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 June 2006 - 01:44 AM

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.

#2 Monkeymatt

Monkeymatt
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 14 June 2006 - 02:07 AM

How is this flatfile database configured -- what do the rows look like?

#3 fleabel

fleabel
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 June 2006 - 02:13 AM

Here's a sample:

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

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\" /]

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 02:33 AM

<?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>';

?>

Should print:

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
        )

)

~ D Kuang

#5 fleabel

fleabel
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 June 2006 - 03:32 AM

Sorry i'm a bit thick. Can you please elaborate on how I would use that.

Thanks [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]



#6 Monkeymatt

Monkeymatt
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 14 June 2006 - 03:59 AM

Here's a way to search for state:

<?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>";
   }
}

?>

You could simply extend that to search for other things.

Monkeymatt

#7 fleabel

fleabel
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 June 2006 - 06:53 AM

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.

<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>



#8 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 02:06 PM

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.
~ D Kuang

#9 fleabel

fleabel
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 June 2006 - 12:16 AM

Hallelujah it works!

Sorry i'm a bit thick sometimes [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users