SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: need a loop

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need a loop

    in the code below, there are three items in the array. but in the actual script there are more like thirty. i'm trying to streamline the code but can't come up with a loop to consolidate all the "$row=..." lines. can somebody help me out?

    basically, i think i need a for or foreach loop, but i'm not sure (thus the reason i'm posting this :). there are several reasons i'm trying to do this: 1. cleaner code, 2. the number of items in the array keep changing because i haven't decided what the end product is going to be, 3. modularity, and 4. so i understand what i'm doing better.

    tia.

    while($row = mysql_fetch_array($result)) {
    $row[0] = formatCSVField($row[0]);
    $row[1] = formatCSVField($row[1]);
    $row[2] = formatCSVField($row[2]);
    $output .= "$row[0],$row[1],$row[2]\n";
    }

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
     $rows = array();
     while(
    $row mysql_fetch_array($result))
       {
       
    array_push($rows,$row);
       } 
    Basically combines every iteration through your resultset into one big array $rows:
    Code:
     Array
       [0] => Array
     	   [ID] => 1
     	   [Name] => Product1
     	   [Description] => Product 1's description
     	   [Price] => $4.95
       [1] => Array
     	   [ID] => 2
     	   [Name] => Product 2
     	   [Description] => Product 2 is nice too
     	   [Price] = $99.94
    Then, if you want to iterate through the array, you can do this:
    PHP Code:
     for($i=0;$i<count($rows);$i++)
       {
       echo 
    'Product ID: '.$rows[$i]['ID'].'<br />';
       echo 
    'Product Name: '.$rows[$i]['Name'].'<br />';
       
    // etc, etc, etc
       

    Make sense?
    Aaron Brazell
    Technosailor



  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't think that's what i'm looking for Sketch. i'll try to elaborate:
    starting out:

    $row[0] = a string
    $row[1] = another string
    $row[2] = something else

    the formatCSVField function adds double quotes to the beginning and end of each item, giving:

    $row[0] = "a string"
    $row[1] = "another string"
    $row[2] = "something else"

    and finally:
    output .= "$row[0],$row[1],$row[2]\n";

    should return:
    "a string","another string","something else"

    the reason for all of this is i want to export data to a csv file. the formatCSVField function wraps all the items in double quotes so if there are commas, single quotes, or double quotes in the data items, the export won't crash out.

    does that clarify things? sorry if i didn't provide enough info originally.

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe something like:
    PHP Code:
    while($row mysql_fetch_assoc($result)) {
        
    $rows = array();
        foreach(
    $row as $key=>$val) {
            
    $rows[] = formatCSVField($val);
        }
        
    $output[] = join(','$rows);
    }
    $finaloutput join("\n"$output); 

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps simpler:
    PHP Code:
    while($row mysql_fetch_assoc($result))
    {
            
    $output[] = join(','array_map('formatCSVField',$row));
    }
    $finaloutput join("\n"$output); 

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks HardCoded and markl999. i tried the simpler of the two solutions first and it works like a champ. i got a couple of ideas for the foreach method too :) thx again!

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this might be better in a new thread, but since it's directly related to the fix i got in this thread i thought i'd keep it together.

    the loop that fixed my export script by wrapping things up in double quotes has hosed my upload script (no surprise.)

    here's a snippet of the upload script:
    $username=$_SESSION['username'];
    $handle = fopen($uploadfile,"r");
    while ($data = fgetcsv($handle, 1000, ","))
    {
    $query = "INSERT INTO table (username, field1, field2, field3)
    VALUES('$username', '".$data[0]."', '".$data[1]."', '".$data[2]."')";
    $result = mysql_query($query) or die;
    }

    now that everything is wrapped up in double quotes the VALUES arguments aren't cutting it. i'm looking for a solution similar to the previous fix. is there a way i can change the VALUES line to something like

    VALUES('$username', $new_array);

    where $username is still the session variable and $new_array contains all the fields from the csv file that are wrapped up in double quotes?

    i apologize if i'm not making sense. i'm having trouble getting my mind wrapped around the issue...

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a little permanent hairloss and couple of days later, i think i found a solution. it's amazing how such a small amount of code can generate such copius amounts of frustration...

    while ($data = fgetcsv ($handle, 10000, ',', '"'))
    {
    $num = count($data);
    for ($c=0; $c < $num; $c++)
    {
    $data[$c] = "'".mysql_real_escape_string($data[$c])."'";
    }
    $new_data = implode(',', $data);
    $query = "INSERT INTO table (username,$main_string)
    VALUES('$username',$new_data)";
    $result = mysql_query($query) or die(mysql_error());
    }


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
  •