Results 1 to 6 of 6

Thread: PHP Arrays & If Statements

  1. #1
    Join Date
    Jan 2008
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking PHP Arrays & If Statements

    hello

    I am new to php and i am trying to split a statement and trying to validate it but its giving me T_IF error. Can i call "if" to validate Arrays?

    My script is following;



    $__strText = "love,is,life/noew,0,<a href,o";
    $__chunk = explode(",",$__strText);



    foreach ($__chunk as $html) {
    $counter = $counter +1
    If ($html == "<a href")
    {
    echo "$counter found";
    }
    }



    Thanks for your help

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Not sure what your trying to do or what the original sting is but I think this would work

    PHP Code:
    $__strText "love,is,life/noew,0,<a href,o";
    $__chunk explode(",",$__strText);



    foreach (
    $__chunk as $html) {
    if (
    $html[$counter] == "<a href")
    {
    echo 
    "$counter found";
    }
    $counter $counter +1


  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    bluewalrus, $html[$counter] is treating $html as an array-- foreach is taking each array item (in this case strings) out of the array, so now you don't need to use a counter. You could use a while() loop instead and THEN use a counter with $__chunk, but not as is.

    I'm not sure what "$counter" is used for, but if you want to count the number of instances the string appears in the array, this would work:

    PHP Code:
    $__strText "love,is,life/noew,0,<a href,o";
    $__chunk explode(",",$__strText);

    $counter=1;
    foreach (
    $__chunk as $html) {
    if (
    $html == "<a href")
    {
    $counter $counter +1;
    }
    }
    echo 
    "$counter found"
    The T_IF error is generated in the original code because you forgot the semicolon at the end of the +1 line. The order of what you were doing didn't make much sense to me, so I moved it around. If you want the original order just add the semicolon to the end of the line. "If" should not be capitalized. It appears that it doesn't matter in this case, but lowercase/uppercase letters do matter some of the time so you should get into the habit of always using lowercase unless there's a reason not to.


    Also, you can just use $counter++; as a shortcut to increase it by one.


    And finally, you could probably use another function actually designed to count and skip a step or two here. But anyway, the code above will work.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    yea i completely misread this whole thing i think or my head was somewhere else.. either way my comment should be disregarded and not used.

  5. #5
    Join Date
    Jan 2008
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you guys i am so used of vbscript thats why i use upercase and lowercase there is another question if anyone kindly reply

    I want to display picture database 5 pictures in row how i will add "</tr><tr>" after 5 records in vbscript we used Mod of Counter but in php i dont know can someone explain me?

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can try this with foreach().
    PHP Code:
    echo "<table><tr>";
    $tdcount=0
    foreach(......)
    {
       echo 
    $img;
       
    $tdcount++;
       if (
    $tdcount==5)
       {
          
    $tdcount 0;
          echo 
    "</tr><tr>";
       }
    }
    echo 
    "</tr></table>"

    That is the basic approach. Personally I don't like using foreach() loops unless the operations are generally simple (this is not, really). So I would use foreach(), etc. to generate an array with all of the images in it, then use a while() or for() loop to generate the table. But if you aren't getting much more complex than this, foreach() should work enough.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •