Jump to content

Recommended Posts

Okay, so I don't use PHP a lot, being a FoxPro programmer.

 

That said, I've got a dbf file I need to access from a web page (done, actually, thanks to the magnificent Erwin Kooi and his PHP Dbase solution.)

 

However, the question I have is purely PHP related.

 

If I have a "Search" box on my website, and the user enters multiple keywords, how can I program it to where, as it trips through the array (resulting from the dbf file),  it compares EACH INDIVIDUAL WORD in the string to the "KEYWORDS" element in the array so that it can decide whether or not to print that line.

 

I wouldn't even mind feeding the results into their own temporary array so that arranging the results sould be done easier.

 

I have no idea, however, where to begin as I'm pretty new to PHP.  Knowing EXACTLY how to do this sort of thing in another programming language doesn't really help in a DIFFERENT language.   ; )

 

I'd really appreciate a nudge in the right direction.  Thanks!

 

-DW

 

 

Here's the basic code that shows EVERYTHING in the database (see below).  I wouldn't mind using the keywords to dump each individual MATCHING record into its own separate array (which I would then feed to this thing instead of the current $v/$di array) )so that the whole  $incrementpass  bit where it creates a new table row after every three records still functions correctly.

 

It occurs to me; could I take the 16th line down, which is: foreach ($r as $c=>$v))

and do something like (pardon my syntax, I dunno if this is right)      foreach (($r as $c=>$v) where (strpos($string, $v) !== false)) but Instead of $string, I'm guessing I'd have to deal with MULTIPLE $strings in the form of an array? And that's where it gets confusing.

 

In VFP I would dump the keywords to a temp table, trip through it each time it iterated, and dump the results to their own array or temp table, then use that to produce the end result. I wouldn't mind doing that here, I just don't quite know how.

_______________________________________________________________

 

 

    $di = dbase_open("php2/test/dwtest.dbf",0);

 

   echo "<table width=\"75%\" border=\"1\" align=\"center\">";
    echo "<tr>";
    $incrementpass = 9;
    for ($i=1;$i<dbase_numrecords($di);$i++)
    {
        $incrementpass = $i;
        if (fmod($incrementpass,3) == 1 and $incrementpass >=2)
        {
            echo "<tr>";
        } ;
            
       $r = dbase_get_record_with_names($di,$i+1);
       echo "<td> <div align=\"center\">";
       foreach ($r as $c=>$v)
       {
           if ($c == "ITEMNO")
               { echo "Item Number: $v :  " ;
               }
           else if ($c == "NAME")
               { echo "$v : <br> " ;
               }
              else if ($c == "PICURL")
               { $v = rtrim($v) ;
              echo "<img SRC=\"/betapics/$v\" " ;
               }
              else if ($c == "LOCALBUY")
                  {  $v = rtrim($v) ;
                echo "<A HREF=\"$v\" target=\"_blank\">" ;
               }
              else if ($c == "ALTTEXT")
               { echo "alt=\"$v Keywords: " ;
               }
              else if ($c == "KEYWORDS")
               { echo "$v \"  " ;
               }
              else if ($c == "TITLETEXT")
               { echo "title=\"$v \"> </A> " ;
               }
              else if ($c == "DESC")
               { echo "$v <br> " ;
               }
              else if ($c == "CPBUY")
               { $v = rtrim($v) ;
              echo "<A HREF=\"$v\" target=\"_blank\"><IMG SRC=\"/betapics/cpbutn.png\" alt=\"CafePressLink\"></A> : " ;
               }
              else if ($c == "RBBUY")
               { $v = rtrim($v) ;
              echo "<A HREF=\"$v\" target=\"_blank\"><IMG SRC=\"/betapics/rbbutn.png\" alt=\"RedBubbleLink\"></A> : " ;
               }
              else if ($c == "ZAZZBUY")
               { $v = rtrim($v) ;
              echo "<A HREF=\"$v\" target=\"_blank\"><IMG SRC=\"/betapics/zazzbutn.png\" alt=\"ZazzleLink\"></A>" ;
               }
              else if ($c == "BIGPICURL")
               { echo "<br> <A HREF=\"$v\">Click Here for Large Picture </A> " ;
               };
                  ##echo "<br><br><br>$incrementpass<br><br> $c NAME?=$v CONTENT? WHERE-EDNTD_WENT";
        }
             echo "</div></td>";
     if (fmod($i,3)==0 and $i!=0)
     {
         echo "</tr>";
         } }
    echo "</table>";
    echo "<br>lineitem<br>";



    
    dbase_close($di);

 

 

EDIT: Yes, I know it's a tad sloppy.  ; )

Edited by HalfNote5

Wait! I've got it! Since $r is an array of its own, I could simply create an array of keywords (dunno how yet), do a nested foreach/as where it DELETES the row in the array (dunno how yet) if none of the keywords show up. Then $r would behave normally, if I did all this BEFORE running $r thrugh the above script.

Yes, precisely. But I should point out that "database" is a misleading term, if I'm strictly supposed to be using PHP parlance. Technically it's a DBF file which is converted into an array. It's NOT a SQL DB.

 

EDIT: I figure since the array already exists, as it's read from the DBF file, then deleting NON-matches might be easier

Edited by HalfNote5

Which still begs the question; which PHP function/command should I be using to separate my keywords from my search box into a small array (I'll go check out strpos so I'm not completely ignorant of it in the meantime), and also if a nested foreach/as loop is the correct/prefereable way to go about it.

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.