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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    241
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Check if last row is read help??

    I am dynamically creating a php page that will build an array from the database. What I would like to do is add a comma after each row, except for the last one.

    I have looked for a function to do this, and I had even thought I could just check if there is a next row. Not sure what to do.

    Here is what my code looks like:
    Code:
    $Data = "<\?php \n";
    fwrite($Handle, $Data);
    $Data = "\$q = strtolower(\$_GET[\"q\"]); \n";
    fwrite($Handle, $Data);
    
    $Data = "if (!\$q) return; \n";
    fwrite($Handle, $Data);
    
    $Data = "\$items = array( \n";
    fwrite($Handle, $Data);
    
    
    
    
    $i = 1;
    while ($row = mysql_fetch_assoc($result)) {
        $Data = "  \"$i\"=>\"$row[common_name]\" \n";
        fwrite($Handle, $Data);
        $i++;
    }
    
    
    
    $Data = "); \n";
    fwrite($Handle, $Data);
    $Data = "foreach (\$items as \$key=>\$value) { \n";
    fwrite($Handle, $Data);
    $Data = "	if (strpos(strtolower(\$value), \$q) !== false) { \n";
    fwrite($Handle, $Data);
    $Data = "		echo \"\$value\n\"; \n";
     fwrite($Handle, $Data);
    $Data = "	} \n";
     fwrite($Handle, $Data);
    $Data = "} \n";
    fwrite($Handle, $Data);
    $Data = "?> \n";
    fwrite($Handle, $Data);
    There is some error checking for the file opening and database connection as well.

    This seems so simple, its not funny.

    Thanks

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Not sure, but you'd need to use a join() like
    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
        
    $Data[] = "  \"$i\"=>\"$row[common_name]\" \n";
        
        
    $i++;
    }

    $Data=join(',',$Data)
    fwrite($Handle$Data); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    241
    Thanks
    6
    Thanked 0 Times in 0 Posts
    abduraooft,

    Thanks for the suggestion, but I get an error about [] doesn't like strings.

    I had thought I could look at the last item in the array, but its not an array. So I am not sure, maybe I should build all the values into an array, then output them.

    Thanks again,


  •  

    Posting Permissions

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