SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    While loop's issue

    Ok, so i am working on adding getting and adding multiple data at one time. I have a form generated with data (if a hen has laid, and the date) so i am setting some things so i can start to add the data to the database after words. all goes fine and dandy until i work with while loops. I must admit i have never worked with while loops but think they should not be much different than foreach()

    here's the full list

    PHP Code:
    while ( $i <= $totalHens) {
                
    $henLaid.$i post('hen_'.$i.'_laid');
                
    $henDateLaid.$i post('hen_'.$i.'_date');
                
    $henId.$i post('hen_id'.$i);
                if(
    $henLaid.$i == 'laid'){
                    
    $query .= "($henDateLaid.$i,$henId.$i)";            
                }
                
    $i++;
            } 
    here's the full function

    PHP Code:
    $totalHens post('henCount');
            
    $i post('startAt');
            
    $query "INSERT INTO eggs(dateLaid,laidBy) VALUES";

            while ( 
    $i <= $totalHens) {
                
    $henLaid.$i post('hen_'.$i.'_laid');
                
    $henDateLaid.$i post('hen_'.$i.'_date');
                
    $henId.$i post('hen_id'.$i);
                if(
    $henLaid.$i == 'laid'){
                    
    $query .= "($henDateLaid.$i,$henId.$i)";            
                }
                
    $i++;
            }
            
    $Insert $db->rawQuery($query); 
    and i have tested it. And it is not building the query , so to me that means it is not retrieving the data. But i am not sure.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try:
    PHP Code:
                   $query .= "(" . {$henDateLaid.$i} .",". {$henId.$i} . ")"
    echo out $query to see what the string contains exactly.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks cups. That did not help, but i appercitate it. I did echo $query before and after using your version and all i ever got was
    PHP Code:
    INSERT INTO eggs(dateLaid,laidByVALUES 

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    GAaarch! No I take that back, that does not work!

    I mean, the variable names will just keep overwriting themselves at each iteration so it could be written as
    PHP Code:
           while ( $i <= $totalHens) { 
                
    $henLaid post('hen_'.$i.'_laid'); 
                
    $henDateLaid post('hen_'.$i.'_date'); 
                
    $henId post('hen_id'.$i); 
                if(
    $henLaid == 'laid'){ 
                    
    $query .= "('$henDateLaid',$henId) ";             
                } 
                
    $i++; 
            } 

    echo 
    $query
    ** I guess the date is a date field, if it is you need to quote it with single quotes. If its a unix timestamp then just rm the quotes.


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
  •