Help with loops

Discussion in 'PHP' started by x0x, Sep 29, 2008.

  1. #1
    This is what I had

    
    $variable = mysql_fetch_array(mysql_query("SELECT * FROM mystuff WHERE id>'0' ORDER BY id DESC LIMIT 1"));
    
    PHP:
    After that linethe $variable variable is used a lot of times to display one row...


    Now I have decided that I need to pull more than one row, so I made this loop:

    
    $get = mysql_query("SELECT * FROM $tab[tourney] WHERE id > 0 ORDER BY id DESC LIMIT 3");
    
    while ($newvariable = mysql_fetch_array($get)) {
    
    return = $newvariable 
    }
    $newvariable = $variable;
    
    
    PHP:
    i know I can't use the return to get the variable from the loop, but what can I do to pull more than one row? Or what can I do to get the newvariable from the loop if its possible?
    I cannot place the { bracets very far from eachother. I have html content and then php and html again, it will be a big mess.
     
    x0x, Sep 29, 2008 IP
  2. linkexchange1984

    linkexchange1984 Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    linkexchange1984, Sep 29, 2008 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    I just wrote it so you get the idea, it's not the actual code, it would be too long to put here.

    the loop ends with } how can I pull the values inside the loop?
     
    x0x, Sep 29, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    $get = mysql_query("SELECT * FROM $tab[tourney] WHERE id > 0 ORDER BY id DESC LIMIT 3");
    
    $x = array();
    
    while ($newvariable = mysql_fetch_array($get)) {
    
     $x[] = $newvariable['value']; 
    
    }
    
    $variable = $x;
    
    PHP:
     
    php-lover, Sep 29, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    It will not be a mess if you use PHP editor software which auto highlight both pair of brackets / curly brackets if you click on one.

    First of all, do you get an error or what?
    and what do you mean by pull more than 1 row?
    I can help you but I need an example / more details
     
    ads2help, Sep 29, 2008 IP
  6. orokusaki

    orokusaki Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    PHP Code is a mess period, but if you want cleaner code, I just posted on another thread: USE CodeIgniter
     
    orokusaki, Sep 30, 2008 IP
  7. spc

    spc Well-Known Member

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    125
    #7
    Here i'm sharing a piece of code from one of my works..
    I used some functions for db works...like executequery, getNumRows etc.

    Hope this will have some idea to get values using loop from mysql queries.

    
    $q="select * from employee_training where training_id=$t_id";
    $res2=executeQuery($q);
    $row=getNumRows($q); 
    $k=0;
    while ($rs = mysql_fetch_array($res2))     
         {  			
           $tr[$k]=$t_id;
           $id[$k]=$rs["employee_id"];
           $adid[$k]=$rs["added_by_id"];
           $lu[$k]=$rs["last_update"];
           $k++; 
         }
    
    if($row>0)
        {
    $remarks="
    <table style='font-size:12px; color:#0066FF;' width=418 border=1 cellpadding=2 cellspacing=0 bordercolor=#f0f0f0>
    <tr>
      <td><b>Training ID</b></td>
      <td><b>Employee ID</b></td>
      <td><b>Added By</b></td>
      <td><b>Updated On</b></td>
    </tr>";
    
    for ($k=0;$id[$k]!=NULL;$k++)
     {
      $remarks.="
    <tr>
      <td>$tr[$k]</td>
      <td> <a href=id_search.php?iid=$id[$k]  target=_blank>$id[$k]</a></td>         
      <td><a href=id_search.php?iid=$adid[$k] target=_blank>$adid[$k]</a></td>
      <td> $lu[$k]</td>
    </tr>";                  
     }
    $remarks.="</table>";
    $init=0;
    }
    else if ($row==0)
         {
         $remarks="<font style='font-size:10px; color:#0066FF;'>No Information Found for this Training.</font>";
         $init=1;
         }
    
    
    Code (markup):
    Rep me if it helps you!
     
    spc, Sep 30, 2008 IP