SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    for loop in a function?

    Hi,

    I have a for loop, that i've tested and it works perfectly well, but when I put it in a function I get the message:

    [] operator not supported for strings in these lines:

    $headline[] = $row['headline'];
    $story_id[] = $row['story_id'];


    Any ideas how I can solve this?

    PHP Code:
    $SQL "SELECT headline, story_id from site ORDER BY story_id desc LIMIT 2";      
    $result mysql_query($SQL) OR die(mysql_error()); 

    $row mysql_fetch_array($result);

    $n mysql_num_rows($result);

    $active = ($active=='active' 'inactive' 'active');
    $inactive = ($inactive=='inactive' 'active' 'inactive');


    for (
    $i 1$i <= $n$i++ ) {

    $headline[] = $row['headline']; 
    $story_id[] = $row['story_id']; 
         
        echo 
    '
          <div id="leaderPanel'
    .$n=i.'" class="leadContainer" style="display:inline">
          <a class="leader-'
    .$inactive.'"> '.$headline[0].' <br /> '.$story_id[0].'</a>
          <a class="leader-'
    .$active.'">'.$headline[1].' <br /> '.$story_id[1].'</a>
          </div>
         '


  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error message implies that the variable $headline and $story_id were used to hold string values somewhere before the loop. Find that out.

    You can apply a quick fix by declaring both $headline and $story_id as array just before the loop.
    Code PHP:
    $headline = array();
    $story_id  = array();

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Further to Kailash's answer, the problem is that the arrays are not declared inside the function so are out of its scope when you try and fill them.

    Put the 2 lines in Kailash's andser inside your function.

    Off Topic:


    Nice to see you again
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys


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
  •