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.
<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.
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:
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:
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
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.
$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:
So this is what you guys helped me to build, much appreciated. http://apps.facebook.com/lanevents/index.php