Professional Web Applications Themes

elegant way of doing something else the last time through a loop? - PHP Development

HI list Is there an elegant way to know when the last time through the loop is going to be and to do something else? I want to search through a table by "exploding" the search string and then compounding my own sql string by working through the array. >From my example below, you can see I use a foreach to loop through the array. Arguably I could first determine the amount of elements in the array and then use a for instead of a foreach, but I'm not sure if that will help ( will probably need a switch ...

  1. #1

    Default elegant way of doing something else the last time through a loop?

    HI list

    Is there an elegant way to know when the last time through the loop is
    going to be and to do something else?


    I want to search through a table by "exploding" the search string and
    then compounding my own sql string by working through the array.
    >From my example below, you can see I use a foreach to loop through the
    array. Arguably I could first determine the amount of elements in the
    array and then use a for instead of a foreach, but I'm not sure if that
    will help ( will probably need a switch instead if you want to work with
    the sheer array elements), however, the if statement inside the loop is
    meant to "strip" out "the" and "and", meaning that it won't much help to
    use that approach anyway.

    Anyway, as you can see my problem lies with the SQl when the last
    element is reached, then it should NOT add another "and" or "or".

    My attempts at "backtracking" the $sql string/array and start writing
    the end part of the string obviously fails.

    Any help with my "logic", ie, how do/would you guys do this?

    Thanks


    $table_name = "test";
    if ($_POST[any_all] == "any") {
    $logic = "or";
    }
    elseif ($_POST[any_all] == "all") {
    $logic = "and";
    }

    $string = $_POST[text];
    $phrases = explode(" ", $string);

    $sql = "select * from $table_name where ";

    foreach ($phrases as $key=>$val) {
    if (($val != "the") && ($val != "and")) {
    $sql.= "name like '%$val%' $logic";
    }
    }

    $length = strlen($sql);
    $newlen = $length - 4;
    $sql[$newlen].= " order by name";
    echo $sql;


    Petre Agenbag Guest

  2. #2

    Default Re: [PHP] elegant way of doing something else the last time through a loop?

    Hi,

    Monday, July 14, 2003, 9:11:11 PM, you wrote:
    PA> HI list

    PA> Is there an elegant way to know when the last time through the loop is
    PA> going to be and to do something else?


    PA> I want to search through a table by "exploding" the search string and
    PA> then compounding my own sql string by working through the array.
    >>From my example below, you can see I use a foreach to loop through the
    PA> array. Arguably I could first determine the amount of elements in the
    PA> array and then use a for instead of a foreach, but I'm not sure if that
    PA> will help ( will probably need a switch instead if you want to work with
    PA> the sheer array elements), however, the if statement inside the loop is
    PA> meant to "strip" out "the" and "and", meaning that it won't much help to
    PA> use that approach anyway.

    PA> Anyway, as you can see my problem lies with the SQl when the last
    PA> element is reached, then it should NOT add another "and" or "or".

    PA> My attempts at "backtracking" the $sql string/array and start writing
    PA> the end part of the string obviously fails.

    PA> Any help with my "logic", ie, how do/would you guys do this?

    PA> Thanks


    PA> $table_name = "test";
    PA> if ($_POST[any_all] == "any") {
    PA> $logic = "or";
    PA> }
    PA> elseif ($_POST[any_all] == "all") {
    PA> $logic = "and";
    PA> }

    PA> $string = $_POST[text];
    PA> $phrases = explode(" ", $string);

    PA> $sql = "select * from $table_name where ";

    PA> foreach ($phrases as $key=>$val) {
    PA> if (($val != "the") && ($val != "and")) {
    PA> $sql.= "name like '%$val%' $logic";
    PA> }
    PA> }

    PA> $length = strlen($sql);
    PA> $newlen = $length - 4;
    PA> $sql[$newlen].= " order by name";
    PA> echo $sql;

    I would do this

    $like = '';
    foreach ($phrases as $key=>$val) {
    if (($val != "the") && ($val != "and")) {
    if(!empty($like)) $like .= ' '.$logic.' ';
    $like.= "name like '%$val%'";
    }
    }
    $sql .= $like;
    --
    regards,
    Tom

    Tom Rogers Guest

  3. #3

    Default Re: [PHP] elegant way of doing something else the last timethrough a loop?

    Sheer genius my man!

    Thanks!

    On Mon, 2003-07-14 at 13:38, Tom Rogers wrote:
    > Hi,
    >
    > Monday, July 14, 2003, 9:11:11 PM, you wrote:
    > PA> HI list
    >
    > PA> Is there an elegant way to know when the last time through the loop is
    > PA> going to be and to do something else?
    >
    >
    > PA> I want to search through a table by "exploding" the search string and
    > PA> then compounding my own sql string by working through the array.
    >
    > >>From my example below, you can see I use a foreach to loop through the
    > PA> array. Arguably I could first determine the amount of elements in the
    > PA> array and then use a for instead of a foreach, but I'm not sure if that
    > PA> will help ( will probably need a switch instead if you want to work with
    > PA> the sheer array elements), however, the if statement inside the loop is
    > PA> meant to "strip" out "the" and "and", meaning that it won't much help to
    > PA> use that approach anyway.
    >
    > PA> Anyway, as you can see my problem lies with the SQl when the last
    > PA> element is reached, then it should NOT add another "and" or "or".
    >
    > PA> My attempts at "backtracking" the $sql string/array and start writing
    > PA> the end part of the string obviously fails.
    >
    > PA> Any help with my "logic", ie, how do/would you guys do this?
    >
    > PA> Thanks
    >
    >
    > PA> $table_name = "test";
    > PA> if ($_POST[any_all] == "any") {
    > PA> $logic = "or";
    > PA> }
    > PA> elseif ($_POST[any_all] == "all") {
    > PA> $logic = "and";
    > PA> }
    >
    > PA> $string = $_POST[text];
    > PA> $phrases = explode(" ", $string);
    >
    > PA> $sql = "select * from $table_name where ";
    >
    > PA> foreach ($phrases as $key=>$val) {
    > PA> if (($val != "the") && ($val != "and")) {
    > PA> $sql.= "name like '%$val%' $logic";
    > PA> }
    > PA> }
    >
    > PA> $length = strlen($sql);
    > PA> $newlen = $length - 4;
    > PA> $sql[$newlen].= " order by name";
    > PA> echo $sql;
    >
    > I would do this
    >
    > $like = '';
    > foreach ($phrases as $key=>$val) {
    > if (($val != "the") && ($val != "and")) {
    > if(!empty($like)) $like .= ' '.$logic.' ';
    > $like.= "name like '%$val%'";
    > }
    > }
    > $sql .= $like;
    > --
    > regards,
    > Tom
    >
    Petre Agenbag Guest

  4. #4

    Default Re: [PHP] elegant way of doing something else the last time througha loop?

    I always do it this way:

    $condition='';
    foreach ($phrases as $key=>$val) {
    if (($val != "the") && ($val != "and")) {
    $condition.= "name like '%$val%' $logic";
    }
    }
    $condition=substr($condition, 0, strlen($condition) - strlen($logic));
    $sql .= $condition;
    >
    > $length = strlen($sql);
    > $newlen = $length - 4;
    > $sql[$newlen].= " order by name";
    > echo $sql;
    >
    >
    >
    Marek Kilimajer Guest

  5. #5

    Default Re: [PHP] elegant way of doing something else the last time through a loop?

    On 14 Jul 2003 13:11:11 +0200, you wrote:
    >Anyway, as you can see my problem lies with the SQl when the last
    >element is reached, then it should NOT add another "and" or "or".
    The short answer is "implode()".
    >Any help with my "logic", ie, how do/would you guys do this?
    if ($_POST[any_all] == "any") {

    any_all should be a string literal or a variable name. Try cranking up your
    warning levels.

    <?

    function build_sql_statement($table_name, $words, $glue) {
    $sql = "SELECT * FROM $table_name WHERE ";
    $newwords = array();

    for ($i = 0; $i < sizeof($words); $i++) {
    if ($words[$i] != 'the' && $words[$i] != 'and') {
    $newwords[] = "name LIKE '%" . $words[$i] . "%'";
    }
    }

    $sql .= join(" $glue ", $newwords);

    $sql .= " ORDER BY name";

    return ($sql);
    }

    $table_name = "test";
    $any_all = "any";
    $string = "this is the test";

    /* get our logic choice */
    $logic = 'AND';
    if (isset($any_all) && $any_all == 'any') {
    $logic = 'OR';
    }

    /* break the incoming string into single words */
    $phrases = explode(" ", $string);

    /* generate a SQL statement from the incoming words */
    $sql = build_sql_statement($table_name, $phrases, $logic);

    echo ($sql);

    ?>

    David Otton Guest

  6. #6

    Default Re: [PHP] elegant way of doing something else the last timethrough a loop? SOLVED

    Following works nicely for me now, thanks all!


    <?php
    include ("main_class.php");
    $db = new my_db_class;
    $db ->connect("localhost","user","password","table");
    $table_name = "main";
    if ($_POST[any_all] == "any") {
    $logic = "or";
    }
    elseif ($_POST[any_all] == "all") {
    $logic = "and";
    }
    elseif ($_POST[any_all] == "exact") {
    $logic = "exact";
    }
    $string = $_POST[text];
    $phrases = explode(" ", $string);

    $sql = "select * from $table_name where ";

    if (($logic == "or") || ($logic == "and")) {
    $temp = '';
    foreach ($phrases as $key=>$val) {
    if (($val != "the") && ($val != "and") && ($val != " ") && ($val !=
    "")) {
    if (!empty($temp)) $temp .= ' '.$logic.' ';
    $temp.= "name like '%$val%' ";
    }
    }
    $sql.=$temp;
    } elseif ($logic == "exact") {
    $sql.="name like'%$string%'";
    }
    $sql.= " order by name";
    //echo $sql;
    $db->query($sql);
    $db->draw_table();

    ?>


    On Mon, 2003-07-14 at 14:00, Marek Kilimajer wrote:
    > I always do it this way:
    >
    > $condition='';
    > foreach ($phrases as $key=>$val) {
    > if (($val != "the") && ($val != "and")) {
    > $condition.= "name like '%$val%' $logic";
    > }
    > }
    > $condition=substr($condition, 0, strlen($condition) - strlen($logic));
    > $sql .= $condition;
    > >
    > > $length = strlen($sql);
    > > $newlen = $length - 4;
    > > $sql[$newlen].= " order by name";
    > > echo $sql;
    > >
    > >
    > >
    >
    Petre Agenbag Guest

  7. #7

    Default Re: [PHP] elegant way of doing something else the last time through a loop?

    Marek Kilimajer <kilimajerwebglobe.sk> wrote:
    > I always do it this way:
    >
    > $condition='';
    > foreach ($phrases as $key=>$val) {
    > if (($val != "the") && ($val != "and")) {
    > $condition.= "name like '%$val%' $logic";
    > }
    > }
    > $condition=substr($condition, 0, strlen($condition) - strlen($logic));
    > $sql .= $condition;
    another way to truncat the string:

    $logic_len = strlen($logic);
    $condition = substr($condition, 0, -($logic_len));

    > >
    > >$length = strlen($sql);
    > >$newlen = $length - 4;
    > >$sql[$newlen].= " order by name";
    > >echo $sql;
    > >
    > >
    > >
    >
    Curt
    --


    Curt Zirzow Guest

  8. #8

    Default Re: elegant way of doing something else the last time through a loop?

    On Mon, Jul 14, 2003 at 01:11:11PM +0200, Petre Agenbag wrote:
    >
    > HI list
    Hi Petre.
    > I want to search through a table by "exploding" the search string and
    > then compounding my own sql string by working through the array.
    I do alot of this. I have a solution which offloads the slight extra
    CPU onto the database server, while simplifying the PHP code a little.
    > the if statement inside the loop is
    > meant to "strip" out "the" and "and", meaning that it won't much help to
    > use that approach anyway.
    I've got a more flexible way of doing that too. How about this:


    $sql = "SELECT * FROM $table_name WHERE ";
    if ($_POST['any_all'] == 'ANY') {
    $logic = 'OR';
    $sql .= '1=0';
    }
    elseif ($_POST['any_all'] == 'ALL') {
    $logic = 'AND';
    $sql .= '1=1';
    }
    else
    die ('form hacking detected');

    // protect from nasty user input
    $string = ereg_replace( '[^a-z0-9]+', ' ', strtolower($_POST['text']) );

    $skip = array(
    'the' => 1,
    'and' => 1,
    'a' => 1,
    );

    foreach (explode(' ', $string) as $val) {
    if (!$skip[$val]) {
    $sql .= $logic . " name like '%$val%'";
    }
    }

    $sql .= ' ORDER BY name';


    --
    Paul Chvostek <paulit.ca>
    it.canada [url]http://www.it.ca/[/url]
    Free PHP web hosting! [url]http://www.it.ca/web/[/url]

    Paul Chvostek Guest

Similar Threads

  1. For loop using time
    By Ray at in forum ASP
    Replies: 1
    Last Post: September 19th, 03:44 PM
  2. [PHP] Setting execution time for a loop
    By Mike Ford in forum PHP Development
    Replies: 1
    Last Post: September 10th, 11:51 AM
  3. Replies: 17
    Last Post: August 12th, 04:19 PM
  4. Replies: 0
    Last Post: July 15th, 03:13 PM
  5. Loop for a period of time.
    By Mamper webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 3
    Last Post: July 11th, 04:14 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139