SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    For loop string concatination

    This is driving me nuts I am so close. Please help!!

    I made a web page with a few drop down boxes. some of which have a selection of ALL.

    I have a js array like
    sitesArray = [1,2,3,4,5,6]
    I turn the js array into a comma delimited string: var sitesStr = sitesArray.join(',')
    and I use jquery to post the string to PHP.

    On the PHP side:

    I grab the post variable and turn it into a php array:
    $siteArray = explode(',',$_POST['sitesStr']);

    This is to be used in the where clause of a sql query.
    Calling one variable from the array works.
    $position = "pl.position = '" . $siteArray[5] . "' ";
    Output is 6

    Trying to append string to a variable behaves unexpectedly.

    PHP Code:
    <?php

    $siteArray 
    explode(',',$_POST['sitesStr']);
    $position sites($siteArray);

    function 
    sites($siteArray) {
      
    $position "( ";
      for (
    $i=0$i count($siteArray); $i++) {
        if (
    $i == 0) {
          
    $position .= "pl.position = '$siteArray[$i]' ";
        } else {
          
    $position .= "or pl.position = '$siteArray[$i]' ";
        }
      }
      
    $position .= " )";
      return 
    $position;
    }
    ?>
    the function returns ( ) like it completely ignores the appending of the for loop!!!

    Where did I go wrong???

    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I would write it like this:

    Code PHP:
    $siteArray = explode(',',$_POST['sitesStr']);
    $conditions=implode(' or ',$siteArray);
    $position = "(".$conditions.")";

    E

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eruna View Post
    I would write it like this:

    Code PHP:
    $siteArray = explode(',',$_POST['sitesStr']);
    $conditions=implode(' or ',$siteArray);
    $position = "(".$conditions.")";

    E
    if done this way wouldn't the implode put an or on every element in the array? cause the ' or ' needs to be left off the first element

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    You have the following line:

    PHP Code:
    for ($i=0$i count($siteArray); $i++) { 
    At the start of the loop, $i is set to zero. Then the condition is checked, is $i greater than the number of items in the array? No, it never will be! Since the condition is not true, the loop contents never get executed.

    A simple mistake, just flip the operator around to be less than ($i < count(…)).

    P.S. Welcome to Sitepoint, jbarby.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jbarby View Post
    if done this way wouldn't the implode put an or on every element in the array? cause the ' or ' needs to be left off the first element
    Nope. Its a delimiter so it would only show between the elements.
    The code example needs a little adjustment, but that's the basic idea.

    E

  6. #6
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eruna View Post
    Nope. Its a delimiter so it would only show between the elements.
    The code example needs a little adjustment, but that's the basic idea.

    E
    Ah good to know Thanks

  7. #7
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    You have the following line:

    PHP Code:
    for ($i=0$i count($siteArray); $i++) { 
    At the start of the loop, $i is set to zero. Then the condition is checked, is $i greater than the number of items in the array? No, it never will be! Since the condition is not true, the loop contents never get executed.

    A simple mistake, just flip the operator around to be less than ($i < count()).

    P.S. Welcome to Sitepoint, jbarby.
    Figures it would be something simple like that.

    Thank you!!!!!!! I was staring for hours trying to figure out why it was not working.


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
  •