echo contact list

Discussion in 'PHP' started by SnoozZz, Aug 10, 2008.

  1. #1
    I have a list of contacts and I want to echo a list of all the names.

    I'm doing it like this
    <?php 
    $sql = "SELECT * FROM contacts WHERE id='$id' ORDER BY contactname DESC"; 
    $result = mysql_query($sql); 
    while ($record = mysql_fetch_object($result)) { 
    ?>
    
    <div align="left"><a><?php echo "$record->contactname";?></a></div></td>
    
    PHP:
    But this is placing all the names underneath each other.

    I need a way to echo it next to each other: example

    name1 name2 name3 name4
    name3 name6 name7 name8

    etc.

    please, any help.
     
    SnoozZz, Aug 10, 2008 IP
  2. nfd2005

    nfd2005 Well-Known Member

    Messages:
    295
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    130
    #2
    
    <div style="width:400px">
    <?php 
    $sql = "SELECT * FROM contacts WHERE id='$id' ORDER BY contactname DESC"; 
    $result = mysql_query($sql); 
    while ($record = mysql_fetch_object($result)) { 
    ?>
    <a style="float:left;"><?php echo "$record->contactname";?></a>
    <? } ?>
    <br style="clear:both;" />
    </div>
    Code (markup):
    Is this what you want to accomplish? You may need to edit the width on the div accordingly.
     
    nfd2005, Aug 10, 2008 IP
  3. kolkka

    kolkka Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Remove the </td> ?
     
    kolkka, Aug 10, 2008 IP
  4. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks alot, but that doesn't work, the names are still underneath each other
     
    SnoozZz, Aug 10, 2008 IP
  5. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This should do what you're looking for...

    <?php 
    $sqlQuery = "SELECT * FROM contacts WHERE id='$id' ORDER BY contactname DESC";
    $sqlPerform = mysql_query($sql);
    
    echo '<table col="4"><tr>';
    $resultCounter = 0;
    
    while ($SQLResult = mysql_fetch_object($sqlPerform)) {
    
    if($resultCounter == 3) {
    
    echo '</tr><tr><td colspan="1"><a>'.$SQLResult->contactname.'</a></td>';
    $resultCounter = 0;
    
    } else {
    
    echo '<td colspan="1"><a>'.$SQLResult->contactname.'</a></td>';
    $resultCounter++;
    
    }
    
    }
    
    echo '</table>';
    ?>
    PHP:
     
    Pos1tron, Aug 10, 2008 IP
  6. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanx, its not giving me any errors, but its also not showing any names.

    Here is what I came up with in the meantime, can this be limited to 4 colums?

    <?php $result = mysql_query("SELECT * FROM contacts");
    
    echo "<table border='1' width='500' bordercolor='#CCCCCC' bgcolor='#FFFFFF'>
    
    ";while($row = mysql_fetch_array($result))
      {
      echo "<td>" . $row['contactname'] . "";
      }
    
    ?>
    PHP:
     
    SnoozZz, Aug 10, 2008 IP
  7. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry, made a bug in my code - fixed below:

    <?php
    $sqlQuery = "SELECT * FROM contacts WHERE id='$id' ORDER BY contactname DESC";
    $sqlPerform = mysql_query($sqlQuery);
    
    echo '<table col="4" border="1" width="500" bordercolor="#CCCCCC" bgcolor="#FFFFFF"><tr>';
    $resultCounter = 0;
    
    while ($SQLResult = mysql_fetch_object($sqlPerform)) {
    
    if($resultCounter == 3) {
    
    echo '</tr><tr><td colspan="1" width="125px"><a>'.$SQLResult->contactname.'</a></td>';
    $resultCounter = 0;
    
    } else {
    
    echo '<td colspan="1" width="125px"><a>'.$SQLResult->contactname.'</a></td>';
    $resultCounter++;
    
    }
    
    }
    
    echo '</table>';
    ?>
    PHP:
    As for that other code:

    <?php
    $sqlQuery = "SELECT * FROM contacts";
    $sqlPerform = mysql_query($sqlQuery);
    
    echo '<table col="4" border="1" width="500" bordercolor="#CCCCCC" bgcolor="#FFFFFF"><tr>';
    $resultCounter = 0;
    
    while ($SQLResult = mysql_fetch_array($sqlPerform)) {
    
    if($resultCounter == 3) {
    
    echo '</tr><tr><td colspan="1" width="125px"><a>'.$SQLResult['contactname'].'</a></td>';
    $resultCounter = 0;
    
    } else {
    
    echo '<td colspan="1" width="125px"><a>'.$SQLResult['contactname'].'</a></td>';
    $resultCounter++;
    
    }
    
    }
    
    echo '</table>';
    ?>
    PHP:
    Hmm... I really need to stop being so helpful... half the people posting here (not you, you just seem to be learning it) have no clue how to use php and just don't want to pay to hire a programmer, so they keep posting to get people to do it for them for free... hmm. We need to charge rep :)
     
    Pos1tron, Aug 10, 2008 IP
    SnoozZz likes this.
  8. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    it works, thanks a lot.
     
    SnoozZz, Aug 10, 2008 IP
  9. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah, I suppose you are right.

    most of the stuff i'm doing is to help others wich i'm not charging for, if they pay me, i'll pay you.

    I appriciate your help.
     
    SnoozZz, Aug 10, 2008 IP
  10. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #10
    $query = "SELECT * FROM contacts";
    $result = mysql_query($query);
    
    echo '<table col="4" border="1" width="500" bordercolor="#CCCCCC" bgcolor="#FFFFFF"><tr>';
    $counter = 0;
    while ($row = mysql_fetch_array($result)) {
       
       echo ($counter % 4 || $counter == 1 || $counter == 0)?'<td colspan="1" width="125px"><a>'.$row['contactname'].'</a></td>': '</tr><tr><td colspan="1" width="125px"><a>'.$row['contactname'].'</a></td>';  
       
       $counter++;
       
    }//end while loop
    
    echo '</tr></table>';
    PHP:
     
    php-lover, Aug 10, 2008 IP
    SnoozZz likes this.
  11. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks guys, now i have so many options to use.
     
    SnoozZz, Aug 11, 2008 IP
  12. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    SnoozZz, Aug 17, 2008 IP