need one field to be different in loop

Discussion in 'PHP' started by mnymkr, Jun 23, 2007.

  1. #1
    OK here is my problem

    I have like 20 fields in sql

    so i wrote a foreach loop to list the contents of all my fields, no problem

    i need one column to be different

    so since this is an associative array i could go in and type in every field name using a while loop

    but i am thinking there has to be a different way if 19 of them need to be the same and only one needs to be different

    any ideas.
     
    mnymkr, Jun 23, 2007 IP
  2. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    could you give an example?
    as far as i understand, you can fix it by placing the different fieldname outside the foreach loop.
     
    UnrealEd, Jun 23, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3

    Ok this will dumb your whole database table, or whatever you have specified in your query.

    while ($rows = mysql_fetch_field($result))
    {echo '<th><a href="?'.$go.'sort='.$rows->name.'&amp;order=ASC">UP</a>'.$rows->name.'<a href="?'.$go.'sort='.$rows->name.'&amp;order=DESC">DOWN</a></th>';}
    
    echo "</tr>";
    
    
    while ($carrot = mysql_fetch_assoc($result)) {
    echo "<tr>";
    foreach($carrot as $list)
       {
          echo "<td>$list</td>";
         }
    echo "</tr>";    
         
         }
    Code (markup):
    I need to hyper link one of the fields but not all of the fields.
     
    mnymkr, Jun 23, 2007 IP
  4. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    just use $carrot['fieldname'] instead of the foreach, like this:
    while ($carrot = mysql_fetch_assoc($result)) {
      echo "<tr>";
      echo "<td>" . $carrot['fieldname'] . "</td>";
      echo "</tr>";    
    }
    PHP:
    if you only need 1 field from the database, you can modify your query so it will only return 1 field:
    SELECT fieldname FROM mytable
    Code (markup):
    will only return the values in the "fieldname" column
     
    UnrealEd, Jun 23, 2007 IP
  5. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #5
    ah,

    yeah i knew i could to that but was trying to find a workaround since ONLY 1 of 20 fields has to be different,

    so i was looking for make all of them this way but this one not this way.

    all 20 fields will be columns in a table
     
    mnymkr, Jun 23, 2007 IP