Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Problem with Undefined offset in my function

    Hi,

    I am trying to step through my data and then output it

    The data is in this style:

    {A_one|A_two|A_three|A_four|A_five}
    {B_one|B_two}
    {C_one|C_two|C_three|C_four}

    ...
    etc.
    ...

    What I my script needs to do is select the first data element from all the sets of data, then select the second data element from all the sets and then the third and so on. If there is no third element then the script needs to select the first element of that set again and then continue with the second in that set ( but continuing as normal with all other sets ).

    So each set continues outputting sequentially and on ending returns to beginning until the desired number of groups are created..

    In this case I have set the number of groups to 10.

    My output is:

    Notice: Undefined offset: 5 in /home/com567b/public_html/auto_change.php on line 35 Notice: Undefined offset: 6 in /home/com567b/public_html/auto_change.php on line 35
    Created New Group: 1
    A_one B-two C-three D-four E-five H-eight

    Notice: Undefined offset: 5 in /home/com567b/public_html/auto_change.php on line 35 Notice: Undefined offset: 6 in /home/com567b/public_html/auto_change.php on line 35
    Created New Group: 2
    A_one B-two C-three D-four E-five H-eight

    etc ... up to New Group: 10

    The new groups are all the same.

    I am having problems incrementing the counter
    and taking it back to the beginning.

    Any help much appreciated.

    This is my script:
    ( It includes test data )

    PHP Code:
    <?php 
    ini_set 
    ("display_errors""1");
    error_reporting(E_ALL);

    set_time_limit(10);
    function 
    inStr($needle$haystack){
        return @
    strpos($haystack$needle) !== false;
        }

    function 
    str_replaceFirst($s,$r,$str){
        
    $l strlen($str);
        
    $a strpos($str,$s);
        
    $b $a strlen($s);
        
    $temp substr($str,0,$a) . $r substr($str,$b,($l-$b));
        return 
    $temp;
        }

    function 
    sqn_select($pass){
       
    $mytext $pass;
       
    $idx 0;
       while(
    inStr("}",$mytext)){
           
    $rbracket strpos($mytext,"}",0);
           
    $tString substr($mytext,0,$rbracket);
           
    $tStringToken explode("{",$tString);
           
    $tStringCount count($tStringToken) - 1;
           
    $tString $tStringToken[$tStringCount];
           
    $tStringToken explode("|",$tString);
           
    $tStringCount count($tStringToken) - 1;
           
    $replace $tStringToken[$idx++];
           
    $tString "{".$tString."}";
           
    $mytext str_replaceFirst($tString,$replace,$mytext);
       }
       return 
    $mytext;
    }    

    $groups 10;
    $Db_data ="
    {A_one|A_two|A_three|A_four|A_five}
    {B-one|B-two|B-three|B-four|B-five|B-six|B-seven|B-eight|B-nine|B-ten|B-eleven|B-twelve}
    {C-one|C-two|C-three}
    {D-one|D-two|D-three|D-four|D-five|D-six|D-seven}
    {E-one|E-two|E-three|E-four|E-five|E-six|E-seven|E-eight|E-nine|E-ten}
    {F-one|F-two|F-three|F-four}
    {G-one|G-two}
    {H-one|H-two|H-three|H-four|H-five|H-six|H-seven|H-eight|H-nine}
    "
    ;

        for (
    $grp1$grp<= $groups  $grp++) {
        
            
    $Db_data str_replace("'","`",$Db_data);
            
            
    $new_group sqn_select($Db_data);
            
    $new_group str_replace(chr(13).chr(10),"<br />".chr(13).chr(10),$new_group);
                
            
    $wordCount explode(" ",$new_group);
            
    $wordCount count($wordCount);
            
            echo 
    "<br>Created New Group: $grp<br>$new_group<br><br>";
                            
            } 
    // end for
            
          
    echo "<br><b>Total $grp New Groups created</b><br>";
    ?>
    My desired output from the included test data is:

    A_one B-one C-one D-one E-one F-one G-one H-one
    A_two B-two C-two D-two E-two F-two G-two H-two
    A_three B-three C-three D-three E-three F-three G-one H-three ---> notice G goes back to one because there are only 2 elements
    A_four B-four C-one D-four E-four F-four G-two H-four ---> notice C goes back to one because there are only 3 elements
    A_five B-five C-two D-five E-five F-one G-one H-five
    A_one B-six C-three D-six E-six F-two G-two H-six ---> notice A goes back to one because there are only 5 elements
    A_two B-seven C-one D-seven E-seven F-three G-one H-seven
    A_three B-eight C-two D-one E-eight F-four G-two H-eight
    A_four B-nine C-three D-two E-nine F-one G-one H-nine
    A_five B-ten C-one D-three E-ten F-two G-two H-one

    Thanks for any help



    .
    Last edited by jeddi; 11-07-2013 at 08:42 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Post Some thoughts

    I take it the warnings you get are from the $replace = $tStringToken[$idx++]; line and that is because you are not doing an isset($tStringToken[$idx++]) to check if something exists in that array position first.

    The $idx is never set back 0 at the start of the loop for each row so that probably just keeps incrementing out of the array index range.

    I'm wondering if it would be helpful to put the data in to a 2D array structure first, so you can then easily access the items doing $data[$x][$y] and checks with isset($data[$x][$y]).

    You might need to keep a record of the max value you are up to for each row so you can do the A_one, A_two, ..., A_five, and back to A_one again, for example $aIdx, $bIdx, $cIdx, etc and then if greater than count of items in the row then set back to 0 during a loop to build the $myText.
    Last edited by DougMck; 11-07-2013 at 09:03 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •