Professional Web Applications Themes

any way to avoid millions of 'if' args??? please. - PHP Development

I'm only 6php months old and I'm a little stuck>>> I'm building the front end for a database but Can someone please tell me how to avoid the millions of permutations for all the 'if' arguments to compose complex database queries? ie: every available combination of every posted variable I think I should be using NULL instead of 'a' if VAR not set... and I think I should be stuffing all VARS into an array (somehow?) and then manipulate the array items[0][1][2][3][4]etc to serve to a loop which should then spit out the required combination specifically as required??? PLEASE tell ...

  1. #1

    Default any way to avoid millions of 'if' args??? please.

    I'm only 6php months old and I'm a little stuck>>>
    I'm building the front end for a database but

    Can someone please tell me how to avoid the millions of permutations for all
    the 'if' arguments to compose complex database queries?

    ie: every available combination of every posted variable

    I think I should be using NULL instead of 'a' if VAR not set...

    and I think I should be stuffing all VARS into an array (somehow?) and then
    manipulate the array items[0][1][2][3][4]etc to serve to a loop which should
    then spit out the required combination specifically as required???

    PLEASE tell me I'm not dreaming
    If someone could even just direct me to a good tutorial I will be enormously
    gratefull

    TNX in advance
    peter butler
    cape_town
    south_africa


    Peter Guest

  2. #2

    Default Re: any way to avoid millions of 'if' args??? please.

    Peter Butler wrote: 
    Not sure what exactly you're talking about. Some example would have been nice. So I guess
    you're talking about for example checkboxes.

    Name the checkboxes related to one group like this group1[] and you will get all checked
    box values in an array $_REQUEST['group1'].

    Regards
    Stefan
    Stefan Guest

  3. #3

    Default Re: any way to avoid millions of 'if' args??? please.

    Peter Butler wrote: 

    Not really - conditional statements are a key part of programming. But you
    could try "switch"
     

    If you're checking to see if a variable is set, try isset(), or !isset() for
    when it isn't set.
     

    foreach ($_POST as $key=>$value
     


    http://www.php.net/tut.php - tutorial
    http://www.php.net/manual/en/index.php - PHP manual


    --
    Tony Garcia
    Web Right! Development
    Riverside, CA
    www.WebRightDevelopment.com


    Tony Guest

  4. #4

    Default Re: any way to avoid millions of 'if' args??? please.


    Stefan Rybacki <net> wrote in message
    news:net... 
    > Not sure what exactly you're talking about. Some example would have been[/ref]
    nice. So I guess 
    get all checked 

    OK, lets see...

    I have 10 text boxes to enter keywords into a form page (The master search
    page)

    These boxes are either set or not set when passed to the result page as set
    or unset variables.

    Depending on which variables have been set, the set variables are used to
    formulate the query.

    I have got this to work perfectly... but I am convinced that I'm using the
    dinosour method. ie.:

    if ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a &&
    $i==a && $j==a)
    { do thing AA}
    elseif ($a!=a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a==a && $b!=a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing CC}
    elseif ($a==a && $b==a && $c!=a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing DD}
    elseif ($a==a && $b==a && $c==a && $d!=a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing EE}
    elseif ($a==a && $b==a && $c==a && $d==a && $e!=a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing FF}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f!=a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing GG}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g!=a && $h==a
    && $i==a && $j==a)
    { do thing HH}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h!=a
    && $i==a && $j==a)
    { do thing II}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i!=a && $j==a)
    { do thing JJ}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j!=a)
    { do thing KK}

    /* then different combos of two's */
    elseif ($a!=a && $b!=a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a!=a && $b==a && $c!=a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a!=a && $b==a && $c==a && $d!=a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a!=a && $b==a && $c==a && $d==a && $e!=a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a!=a && $b==a && $c==a && $d==a && $e==a && $f!=a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    etc, etc, etc...........

    /* then different combos of threes */
    elseif ($a!=a && $b!=a && $c!=a...
    {do thing wada-wada...
    elseif ($a!=a && $b!=a && $c==a...
    {do thing wada-wada...
    elseif ($a!=a && $b!=a && $c==a...
    {do thing wada-wada...
    etc, etc, etc...........

    /* then different combos of fours */
    elseif ($................................................ .......
    elseif ($................................................ .......
    elseif ($................................................ .......
    etc, etc, etc...........

    /* then different combos of fives */
    elseif ($................................................ .......
    elseif ($................................................ .......
    elseif ($................................................ .......
    etc, etc, etc...........

    /* then different combos of sixes */
    elseif ($................................................ .......
    elseif ($................................................ .......
    elseif ($................................................ .......
    etc, etc, etc...........

    /* then different combos of sevens */
    elseif ($................................................ .......
    elseif ($................................................ .......
    elseif ($................................................ .......
    etc, etc, etc...........

    /* then different combos of eights */
    /* then different combos of nines */

    /* until... three thousand six hundred and forty-nine lines of code later
    we get to make*/
    elseif ($a!=a && $b!=a && $c!=a && $d!=a && $e!=a && $f!=a && $g!=a && $h!=a
    && $i!=a && $j!=a)
    { do thing X}

    - OK... For those eagle eyes out there... I actually have 14 different
    fields in which to set variables... two pairs of which are grouped together
    as max/min values,
    but I do really have 3649 lines of stuff that is slowing down my DUAL PIII
    500 on saves.

    This can't be the right way... really???


















    Peter Guest

  5. #5

    Default Re: any way to avoid millions of 'if' args??? please.

    I noticed that Message-ID: <co.za> from Peter
    Butler contained the following:
     

    I still don't get what you are trying to do but I'd agree there must be
    an easier way.
    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  6. #6

    Default Re: any way to avoid millions of 'if' args??? please.


    Geoff Berrow <co.uk> wrote in message
     

    I am trying to get php to dynamically create the correct combination of
    arguments to formulate a complex Mysql query, based on which combination of
    $vars have been passed to the result.php script.

    Instead of physically listing every single possible combination of 14
    different variables...

    I hope that sounds clearer
    TNX 4 THE TROUBLE!!! btw


    Peter Guest

  7. #7

    Default Re: any way to avoid millions of 'if' args??? please.

    I noticed that Message-ID: <co.za> from Peter
    Butler contained the following:
     

    Well If you are basing a query on 14 variables then, as you've worked
    out, there are a lot of combinations. Most people here will be thinking
    that you can do this with the query itself but we need to know the exact
    nature of the problem.

    Lets take a simple case. Say I sell shoes and I have three fields,
    size, colour and type.

    After sanitizing the input from the form (using
    mysql_real_escape_string() ) we could end up with three variables $size,
    $colour and $type

    The query would then be something like

    $sql= "SELECT * FROM `shoes` WHERE size='$size' AND colour='$colour' AND
    type='$type'";
    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  8. #8

    Default Re: any way to avoid millions of 'if' args??? please.

    Geoff Berrow wrote: 
    >
    >
    > Well If you are basing a query on 14 variables then, as you've worked
    > out, there are a lot of combinations. Most people here will be thinking
    > that you can do this with the query itself but we need to know the exact
    > nature of the problem.[/ref]

    Thats what I stated before. An example of what he is trying to do would be nice. ;)
     

    Yes or seperated with OR, whatever without an example of what he really wants it's just
    guessing.

    So Peter please show us what you're trying to do. Thanks.

    Regards
    Stefan
    Stefan Guest

  9. #9

    Default Re: any way to avoid millions of 'if' args??? please.

    On Fri, 15 Jul 2005 19:01:19 +0200, "Peter Butler"
    <com> wrote:
     

    I once had a similar problem with sql statements in VisualFoxpro, so I
    will give you a possible solution using FoxPro terminology.

    The solution I found was to have your input variables have two states:

    1. Null , if there was no choice made, or

    2. have the value the input operator chooses including all formatting
    for constructing an SQL statement.

    Then concatenate all of the fields and our statement should work.

    The concatenation is the key. The choices an input operator makes
    should return to the system not the field value but rather the
    correctly formatted part of the SQL statement that would match the sql
    statement for that field value. For an unchosen field, you would get a
    null.

    If you would post some sample code, I could try to show you an
    example. Post a sample of the html code and post a sample of the sql
    for a group of selected fields as if they had been chosen. Then I
    will try to do the concatenation manually.

    I have not tried this on the web, but it should work.


    Best regards,

    Al
    http://www.affordablefloridainsurance.com
    http://www.americanbestmortgages.com
    http://www.americanaffordablelifeinsurance.com
    AF Guest

  10. #10

    Default Re: any way to avoid millions of 'if' args??? please.


    Stefan Rybacki <net> wrote in message
    news:net...
     
    wants it's just 

    Sorry... I had this posted in another string. OK, lets see...

    I have 10 text boxes to enter keywords into a form page (The master search
    page)

    These boxes are either set or not set when passed to the result page as set
    or unset variables.

    Depending on which variables have been set, the set variables are used to
    formulate the query.

    I have got this to work perfectly... but I am convinced that I'm using the
    dinosour method. ie.:

    if ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a &&
    $i==a && $j==a)
    { do thing AA}
    elseif ($a!=a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a==a && $b!=a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing CC}
    elseif ($a==a && $b==a && $c!=a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing DD}
    elseif ($a==a && $b==a && $c==a && $d!=a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing EE}
    elseif ($a==a && $b==a && $c==a && $d==a && $e!=a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing FF}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f!=a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing GG}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g!=a && $h==a
    && $i==a && $j==a)
    { do thing HH}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h!=a
    && $i==a && $j==a)
    { do thing II}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i!=a && $j==a)
    { do thing JJ}
    elseif ($a==a && $b==a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j!=a)
    { do thing KK}

    /* then different combos of two's */
    elseif ($a!=a && $b!=a && $c==a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a!=a && $b==a && $c!=a && $d==a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a!=a && $b==a && $c==a && $d!=a && $e==a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a!=a && $b==a && $c==a && $d==a && $e!=a && $f==a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    elseif ($a!=a && $b==a && $c==a && $d==a && $e==a && $f!=a && $g==a && $h==a
    && $i==a && $j==a)
    { do thing BB}
    etc, etc, etc...........

    /* then different combos of threes */
    elseif ($a!=a && $b!=a && $c!=a...
    {do thing wada-wada...
    elseif ($a!=a && $b!=a && $c==a...
    {do thing wada-wada...
    elseif ($a!=a && $b!=a && $c==a...
    {do thing wada-wada...
    etc, etc, etc...........

    /* then different combos of fours */
    elseif ($................................................ .......
    elseif ($................................................ .......
    elseif ($................................................ .......
    etc, etc, etc...........

    /* then different combos of fives */
    elseif ($................................................ .......
    elseif ($................................................ .......
    elseif ($................................................ .......
    etc, etc, etc...........

    /* then different combos of sixes */
    elseif ($................................................ .......
    elseif ($................................................ .......
    elseif ($................................................ .......
    etc, etc, etc...........

    /* then different combos of sevens */
    elseif ($................................................ .......
    elseif ($................................................ .......
    elseif ($................................................ .......
    etc, etc, etc...........

    /* then different combos of eights */
    /* then different combos of nines */

    /* until... three thousand six hundred and forty-nine lines of code later
    we get to make*/
    elseif ($a!=a && $b!=a && $c!=a && $d!=a && $e!=a && $f!=a && $g!=a && $h!=a
    && $i!=a && $j!=a)
    { do thing X}

    - OK... For those eagle eyes out there... I actually have 14 different
    fields in which to set variables... two pairs of which are grouped together
    as max/min values,
    but I do really have 3649 lines of stuff that is slowing down my DUAL PIII
    500 on saves.

    This can't be the right way... really???






















    Peter Guest

  11. #11

    Default Re: any way to avoid millions of 'if' args??? please.

    I noticed that Message-ID: <co.za> from Peter
    Butler contained the following:
     
    >
    >Sorry... I had this posted in another string. OK, lets see...
    >
    >I have 10 text boxes to enter keywords into a form page (The master search
    >page)[/ref]

    Sorry Peter, this still isn't an explanation.

    What would a typical query look like from these ten variables?



    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  12. #12

    Default Re: any way to avoid millions of 'if' args??? please.

    On Sat, 16 Jul 2005 19:38:16 +0200, "Peter Butler"
    <com> wrote:

    snip 
    I agree with you there has to be a better way, but it is hard to
    envision this using $a's, and $b's, and a's, and b's.

    Can you give us a more concrete example of the actual fields and data?


    Best regards,

    Al
    http://www.affordablefloridainsurance.com
    http://www.americanbestmortgages.com
    http://www.americanaffordablelifeinsurance.com
    AF Guest

  13. #13

    Default Re: any way to avoid millions of 'if' args??? please.


    Geoff Berrow <co.uk> wrote in message
     


    nb: I have used the value of 'a' as being equal to default or unset or
    'search all' *// I am thinking of changing this to NULL instead.

    OK here's a chunk of actual code.
    line 194 => 207 file: result.php
    =========================begin:

    elseif ($district!=a && $ward==a && $estatename==a && $category==a &&
    $style==a && $substyle==a && $varietal!=a && !$vintage && !$vintage_a &&
    !$vintage_b && $award==a && !$price_a && !$price_b)


    echo"show all wine estates from $district that cultivate $varietal vines
    ";
    $query = "SELECT DISTINCT `estatename`, `code`, `tel`, `email`, `region`,
    `owner` FROM `estates` WHERE `$district`='$district' AND `$varietal` =
    '$varietal' AND `serve`='y' ORDER BY `estatename` ASC";
    $result = mysql_db_query($db, $query, $connection) or die("Error in
    query");
    $x=0;
    while ($myrow = mysql_fetch_row($result))
    {
    $x++;
    echo"<tr bgcolor=`#B8F8B6`><td><a
    href=\"details.php?id=$myrow[0]\">$myrow[0]</a></td><td>$myrow[1]-$myrow[2]<
    /td><td>$myrow[4]</td><td><a
    href='mailto:$myrow[3]'>$myrow[3]</a></td></tr>" ;
    $myrow = mysql_fetch_row($result);
    echo"<tr bgcolor=`#F0F385`><td><a
    href=\"details.php?id=$myrow[0]\">$myrow[0]</a></td><td>$myrow[1]-$myrow[2]<
    /td><td>$myrow[4]</td><td><a
    href='mailto:$myrow[3]'>$myrow[3]</a></td></tr>" ;
    }
    }



    Peter Guest

  14. #14

    Default Re: any way to avoid millions of 'if' args??? please.


    Geoff Berrow <co.uk> wrote in message
     

    YES... you will always have THREE different variables BUT you will also have
    EIGHT different possible combinations of those three variables:

    if none are set *// show me all shoes
    elseif only A is set *// show me a specified size in any colour and any
    type
    elseif only B is set *// show me a specified colour in any size and any
    type
    elseif only C is set *// show me a specified type in any colour and any
    size
    elseif only A&B is set *// show me specified size & colour but in ANY TYPE
    elseif only A&C is set *// show me specified size & type but in ANY COLOUR
    elseif only B&C is set *// show me specified colour & type but in ANY SIZE
    elseif A&B&C is set *// show me ONLY shoes of single size in ONE colour and
    of ONE TYPE

    this is quick and easy as long as U only have few variabes... (3 in above
    case)
    now try do the same with 15 variables and I think U get my point.

    My problem is not in structuring the actual sql query... my problem is to
    create a single dynamic if( ) which will
    depend on which variables have been set.






    Peter Guest

  15. #15

    Default Re: any way to avoid millions of 'if' args??? please.

    I noticed that Message-ID: <co.za> from Peter
    Butler contained the following:
     

    Ah, now we are getting somewhere. Let me see if I have this right.

    You have a POST array containing your search variables and some of them
    may not be used. Well how about this. Your query only need search on
    the ones that have been set. So you can discard those variables that
    contain a. You also want to discard some other stuff such as your
    submit button.

    Here is a code snippet which simulates what I think you are trying to
    do. I assume the names of your text boxes are the same as the column
    names in your database.

    <?php

    $_POST['district']="some_district";
    $_POST['ward']="a";
    $_POST['estate']="some_estate";
    $_POST['category']="a";
    $_POST['style']="some_style";
    $_POST['substyle']="a";
    $_POST['varietal']="a";
    $_POST['vintage']="a";
    //etc, etc,
    $_POST['submit']="submit";

    $exclude=array("submit","a");
    $searchterms=array();
    $conditions="";

    foreach($_POST as $key=>$value){
    if(!in_array($value,$exclude)){
    //perhaps another if () to do something different if it's a price
    $searchterms[]="$key = '".mysql_real_escape_string($value)."'";
    }
    }
    if(!empty($searchterms)){
    $conditions=implode(" AND ",$searchterms);
    }
    $sql=" SELECT * FROM table WHERE $conditions";
    echo $sql;

    ?>




    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  16. #16

    Default Re: any way to avoid millions of 'if' args??? please.


    Geoff Berrow <co.uk> wrote in message
    news:com... 

    It is actually a GET form and I'm not creating an array... although by now I
    sort of think that I maybe should ... reason is I haven't any array
    experience and dunno fully what and how they can cork for me.

    I'll get back re: the rest shortly... have to disect it into my brain
    first....."BIG TNX"

     


    Peter Guest

  17. #17

    Default Re: any way to avoid millions of 'if' args??? please.

    On Sun, 17 Jul 2005 12:10:50 +0200, "Peter Butler"
    <com> wrote:
     
    snip

    Here is the sample of code you gave us.

    elseif ($district!=a && $ward==a && $estatename==a && $category==a &&
    $style==a && $substyle==a && $varietal!=a && !$vintage && !$vintage_a
    &&!$vintage_b && $award==a && !$price_a && !$price_b)

    $query = "SELECT DISTINCT `estatename`, `code`, `tel`, `email`,
    `region`,
    `owner` FROM `estates` WHERE `$district`='$district' AND `$varietal` =
    '$varietal' AND `serve`='y'...

    So let me take a stab at solving your problem.

    I would do something like this, (note not correct php coding, this is
    just a conceptual write up. I'll leave it to you to work out the
    code.)

    As I under stand it, you give a web visitor the option to input these
    fields:

    $district
    $ward
    $estatename
    $category
    $style
    $substyle
    $varietal
    $vintage
    $vintage_a - I take it this is either a high or low vintage year
    $vintage_b - I take it this is either a low or high vintage year
    $award
    !$price_a - same comment for price
    $price_b - same comment for price

    (Just as an aside, I would call your two fields vintage_high and
    vintage_low, and price_high and price_low. Otherwise some time late
    at night when things aren't working and you are running on lots of
    caffeine, you will mistake $vintage_a for $vintage_b or vice versus
    and spent time trying to figure out what happened. With the "_high"
    and "_low" naming convention, or something similar, you won't have
    this problem.)

    From these I assume, correct me if I am wrong, that all except the two
    vintage and the two price variables, have distinct values. And price
    and vintage are ranges of number, like 1990 to 1999, or $9.99 to
    $15.99. Whereas the other variables have distinct values, like
    district can be, to make up examples, North, South, East, West.

    If this is so, your problem becomes easier because you need on two
    routines to make this work, a routine that processes numerical ranges,
    and a routine that processes distinct values.

    We'll come back to this in a second.

    So let's build the sql statement in little steps, one at a time.

    First let's construct the php to build an sql statement for only one
    field, district, and for only one district. Write a routine that
    takes the input from your users and constructs the sql statement,
    something like this, (again not coded correctly, only conceptually):

    $query = "SELECT DISTINCT `estatename`, `code`, `tel`, `email`,
    `region`, `owner` FROM `estates` WHERE `$district`='$district' '...

    I am not sure how you are coding your fields on the html page, it
    would help if you were to post a url to a sample page.

    Let's take a simple example of processing just one district. Thus if
    someone picks district = "North", php ends up with a variable like
    this

    $DistrictSQLSnippet = 'district = " . $district;

    and you build the SQL like this

    $query = "SELECT DISTINCT `estatename`, `code`, `tel`, `email`,
    `region`, `owner` FROM `estates` WHERE <? echo $DistrictSQLSnippet; ?>

    Try this and when you get all of the formatting to work. I don't have
    all of the correct formatting, but get this to work first.

    Then when you have it working, go to the next step of selecting
    multiple districts. Again not knowing how you have built your html
    fields, I would suggest using an array of district variables,
    something like this

    create an array to hold your districts

    then process your array

    $SQlDistrictStatement = "";
    foreach ($district as $districtSQL)
    {
    build your sql statement here
    $SQLDistrictStatement = $SQLDistrictStatement . ' and district = "' .
    $districtSQL . '" ';
    )

    use similar processing logic to handle not's and or's.

    But you should be able to construct the Where statement for multiple
    districts.

    Just try to build on processing set of code to construct an SQL where
    statement for districts. Once you have this working, you should be
    able to make the similar code work for the other "nonrange" or
    distinct variables.

    Do a similar processing segment of code to build the SQL statement for
    a range of values, ie vintage and price.

    To make it easier on yourself, only test an sql statement builder
    segment on one variable at a time.

    Once you have them all perfected, as selecting only on one field at a
    time, then code php to build an sql routine to concatenate all of the
    variables that I call $SQLDistrictStatement together and you should
    have you final SQL variable to put into your SQL statement.

    Now early I said this would be easier for you with only two types of
    fields, distinct and range fields. Here is why.

    To make this even more sophisticated, you could use variable variables
    to reuse the SQL statement builder by repeating the processing code
    for each distinct variable and repeating your SQL statement builder
    for range variables. You might not want to do this right now, and
    just construct an SQL statement builder for each variable.

    Hope this helps.


    Best regards,

    Al
    http://www.affordablefloridainsurance.com
    http://www.americanbestmortgages.com
    http://www.americanaffordablelifeinsurance.com
    AF Guest

  18. #18

    Default Re: any way to avoid millions of 'if' args??? please.

    I noticed that Message-ID: <co.za> from Peter
    Butler contained the following:
     
    >
    >It is actually a GET form and I'm not creating an array... although by now I
    >sort of think that I maybe should ...[/ref]

    Doesn't matter. If you use GET the variables end up in the $_GET array
    Personally, with 15 textboxes I'd use POST. The URL must look
    horrendous!
     

    I remember when I first read about arrays, I couldn't see what use they
    were. It took me a while to figure out how useful they could be. Once
    you get the concept that you can loop through arrays using foreach it's
    amazing what they can do. 

    Well I hope I've saved you several thousand lines of code :-)
    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  19. #19

    Default Re: any way to avoid millions of 'if' args??? please.

    On Sun, 17 Jul 2005 20:47:22 +0200, "Peter Butler"
    <com> wrote:

    snip 
    Arrays are actual very easy to work with in php and relatively easy to
    learn

    Here are three examples:

    1. you buld an array
    $nameTypes = array("tom", "bill", "mary");

    you process the array from front to back
    foreach ($nameTypes as $name)
    {
    do your processing here on the $name variable
    example - echo $name . ' - ';
    }

    The "foreach" is a great command. Read up on it.

    In the above example, it takes apart $nameTypes and assigns each
    $nameTypes to the variable $name. Then you do all of your processing
    on the variable named $name.

    For example, the above code should give us

    tom - bill - mary -


    2. Processing arrays as individual variables.

    I use this on my urls, for example,
    http://www.domain.com/index/beaches/florida/hillsborough


    $URLVariables = explode("/",$PATH_INFO);
    ($Variables becomes an array)
    returns index/beaches/florida/hillsborough and give me these variables
    $URLVariable[0] = index
    $URLVariable[1] = beaches
    $URLVariable[2] = florida
    $URLVariable[3] = hillsborough


    $NoOfVariables = count($URLVariables);
    this counts the number of variables in the array $Variables
    and lets me test in other code how many fields or info I have passed
    in a URL

    $LSubject = $URLVariables[1];
    sets the subject to the first $URLVariable

    $LState = $URLVariables[2];
    sets the state to the second $URLVariable

    $LCounty = $URLVariables[3];
    sets the county to the third $URLVariable


    3. here is a really nifty use of arrays

    $CityArray = file("http://domainname.com/states/" . $LState .
    "_cities.txt");
    this reads a list of cities in a given state, as specified by the
    variable $LState, so in this case the file might be found at
    http://domainname.com/states/floridacities.txt, as an example.

    The above php code also builds the array $CityArray


    $NoOfRows = ceil( count($CityArray)/4);

    This line counts the number of cities in the array $CityArray and
    using ceil(), picks a number of cities to process into a 4 groups.
    The "ceil()" and the "/4" do this.

    then I build my html table like this
    $RowCounter = 0;

    foreach ($CityArray as $City)
    - remember from above this processes each value in $CityArray as the
    variable $City

    {
    $RowCounter = $RowCounter + 1;

    if ($RowCounter > $NoOfRows)
    {
    $RowCounter = 1;
    echo '</td><td ID="' . $LState . $LSubject . '_3002" width="25%"
    valign="top">';
    }

    echo $City

    }

    This takes the list of cities found in my file, which looks like this

    Alachua
    Alford
    Altamonte Springs
    Altha
    Anna Maria
    Apalachicola
    Apopka
    Arcadia
    Archer
    Astatula
    Atlantic Beach
    Atlantis
    Auburndale
    Aventura
    Avon Park
    Bal Harbour
    Baldwin
    Bartow
    Bascom
    Bay Harbor Islands
    Bay Lake
    Bell
    Belle Glade
    Belle Isle
    Belleair
    Belleair Beach

    and creates a four column html table. There is more to the above.
    Here is all of the actual php code I use turn the above list into a
    four column html table. It looks complicated, but is actually very
    simple to build.


    <div align="center">
    <center>

    <table ID="<? echo $LState; ?>_<? echo $LSubject; ?>_2000" border="0"
    cellpadding="0" cellspacing="0"
    style="border-collapse: collapse" bordercolor="#111111" width="100%">
    <tr ID="<? echo $LState; ?>_<? echo $LSubject; ?>_2001">
    <td ID="<? echo $LState; ?>_<? echo $LSubject; ?>_2002"
    width="25%" valign="top">

    <?

    $LinkLine1 = '<p class="smalltable"><a class="smalltable" href="' .
    $URL . $LSubject . '/' . $LState. '/';
    $LinkLine2 = '"><img border="0" src="' . $WebSite .
    'images/mortgage_pointer.png" alt="';
    $LinkLine3 = $UState . ' ' . $USubject . ' Mortgages & Home Loans"
    width="10" height="10">';
    $LinkLine4 = '<a class="smalltable" href="' . $URL . $LSubject . '/' .
    $LState . '/';
    $LinkLine5 = '">';
    $LinkLine6 = '</a></p>';

    $CityArray = file($WebSite . "states/" . $LState . "_cities.txt");
    $NoOfRows = ceil( count($CityArray)/4);

    $RowCounter = 0;

    foreach ($CityArray as $City)
    {
    $RowCounter = $RowCounter + 1;

    if ($RowCounter > $NoOfRows)
    {
    $RowCounter = 1;
    echo '</td><td ID="' . $LState . $LSubject . '_3002" width="25%"
    valign="top">';
    }

    $LCity = strtolower(str_replace(" ", "_", $City));
    echo $LinkLine1 . $LCity;
    echo $LinkLine2 . $City;
    echo $LinkLine3;
    echo $LinkLine4 . $LCity;
    echo $LinkLine5 . $City . $LinkLine6;

    }

    ?>

    </td>
    </tr>
    </table>

    </center>
    </div>

    if I include the above file into any html page, and give it the values
    for $ WebSite, state, subject, it builds a very nice table of cities
    and urls to city pages.

    Anyway read up on arrays. They are very simple to start to use and
    very powerful once you know how to use them.



    Best regards,

    Al
    http://www.affordablefloridainsurance.com
    http://www.americanbestmortgages.com
    http://www.americanaffordablelifeinsurance.com
    AF Guest

  20. #20

    Default Re: any way to avoid millions of 'if' args??? please.

    ----- Original Message -----
    From: Geoff Berrow <co.uk>
     
    >
    > Well I hope I've saved you several thousand lines of code :-)[/ref]


    That is abolutely awesome.........!!! You most certainly have...
    I've just gone through the array stuff ie.:: $key=>&val / in_array / implode
    and a quick working sample page works perfectly!!!
    I'm extremely gratefull to all who dragged me in the right direction and had
    the patience to see me through!!!
    Cookies and milk for everyone concerned. TNX AGAIN






    Peter Guest

Page 1 of 2 12 LastLast

Similar Threads

  1. onMetaData got 64 args instead of one
    By phiphou in forum Macromedia Flash Flashcom
    Replies: 4
    Last Post: February 4th, 06:49 AM
  2. catch args from sendAllSprites
    By rascreek in forum Macromedia Director Lingo
    Replies: 5
    Last Post: October 29th, 02:08 PM
  3. question regarding multiple args to sub
    By ktom in forum PERL Miscellaneous
    Replies: 4
    Last Post: August 15th, 08:16 PM
  4. command line args?
    By Kevin Thorpe in forum PHP Development
    Replies: 1
    Last Post: July 23rd, 11:30 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