SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Putting preg_match in PHP swtich

    This script works, appending the footnote "SE Asian Islands" at the bottom of a table column if that column contains at least one item from the array $IslesEurasia.

    PHP Code:
    $IslesEurasia join("|",array("jpn""twn""phl""bor""sum"));
    if(
    preg_match("/$IslesEurasia/i",$row['Geog2']))
      
    $Footnote "SE Asian Islands";
    else
      print 
    "<br />";
    }
    echo 
    $Footnote 
    I'd like to turn it into a PHP switch, so it can display one or more footnotes, based on several arrays. For example, consider the PHP switch below. If a table column includes the ID for both an Asian island and Madagascar (an African island), then it would display this at the end:

    *Asian island
    * African island

    But I'm not sure exactly how to wrwite this switch. Any suggestions? Thanks.

    PHP Code:
    $IslesEurasia join("|",array("jpn""twn""phl""bor""sum"));
    $IslesAfrica join("|",array("mad"));
    switch (
    preg_match("/$IslesEurasia/i",$row['Geog2']))
    {
       case 
    '$IslesEurasia':
         
    $Footnote "SE Asian Islands";
       break;
       case 
    '$IslesAfrica':
         
    $Footnote "African Islands";
       break;
       default:
       break;


  2. #2
    SitePoint Guru enygmadae's Avatar
    Join Date
    Sep 2002
    Location
    Dallas, Tx.
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think more what you're looking for is a result of the preg_match and then looping through those to see which are found...

    PHP Code:
       $arr=array("jpn","twn","phl","bor","sum");
       foreach(
    $arr as $key => $value){
       
    preg_match("/".$value."/",$row['Geog2'],$matches);
       foreach(
    $matches as $ikey => $ivalue){
       switch(
    $value){
       case 
    "Eurasia"$Footnote.="SE Asian Islands"; break;
       case 
    "Africa"$Footnote.="African Islands"; break
       }
       }
       } 
    or something...
    PHP News, Views and Community: http://www.phpdeveloper.org

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm confused. First, how do I implement your script - something like this?:

    PHP Code:
    $query "SELECT * FROM gwecoareasexp WHERE TypeX = 'eco'";
     
    $result mysql_query($query);
     while(
    $row mysql_fetch_array($result)) {
       
    $Geog[] = $row['Geog2'];
    echo(
    $row['Geog2']).'';
    }
    echo 
    $Footnote;
     
    $arr=array("jpn","twn","phl","bor","sum");
       foreach(
    $arr as $key => $value){
       
    preg_match("/".$value."/",$row['Geog2'],$matches);
       foreach(
    $matches as $ikey => $ivalue){
       switch(
    $value){
       case 
    "Eurasia"$Footnote.="SE Asian Islands"; break;
       case 
    "Africa"$Footnote.="African Islands"; break;
       }
       }
       }
    ?> 
    Second, how can it recognize "mad" (ID of Madagascar) if it isnt in the array. Do I need to add a second array, something like this?:

    PHP Code:
     $arr=array("jpn","twn","phl","bor","sum");
     
    $arr2=array("mad");
       foreach(
    $arr as $key => $value
    I guess another possibility might be to put all the world's islands in one array, then match them to their native continents via my database table. I would have to sort it all out somehow so that one or more footnotes were displayed, depending on how many continents were represented.

    Thanks.

  4. #4
    SitePoint Guru enygmadae's Avatar
    Join Date
    Sep 2002
    Location
    Dallas, Tx.
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay, what field in the SELECT are you trying to match? Geog2?
    PHP News, Views and Community: http://www.phpdeveloper.org

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by enygmadae
    okay, what field in the SELECT are you trying to match? Geog2?
    Yes. Actually, your script is easier to understand than I at first realized. But I'm still confused by the array. I don't know if you just included a single array as an example, assuming I'd know to add additional arrays, or if you had another strategy in mind.

    After I get some basic scripts squared awy, I'll join some tables together. Thus, if field Geog2 contains "bor," then my scripts would display "Borneo," from the field Names in a second table.

    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •