sourcecoder Posted November 17, 2010 Share Posted November 17, 2010 Hi all. I have a DB that looks like this : id, name, email and i want to display the email in a title-tag.. say i have 100 names and i want to display the emailadress when i hover the name. So the emailadress is shown in a title-tag. how can i do this? i can do it the hard way and : $resultuser=mysql_query("SELECT * FROM users WHERE id='15'")or die(mysql_error()); $row15=mysql_fetch_assoc($resultuser); echo '<a href="#" alt="" title=$row15['mailadress']>Name : $row15['name']</a>'; But that ain't the easiest way.. and for about 200 users that's a lot of coding when i don't have to do it the hard way, right? there must be a easier way with som kind of array()-code.. Please help me out with this.. / Sourcecoder Link to comment https://forums.phpfreaks.com/topic/218972-row-in-array/ Share on other sites More sharing options...
Pikachu2000 Posted November 17, 2010 Share Posted November 17, 2010 If you want to list all records at once, you can simply use a while() loop. $query = "SELECT * FROM users"; $resultuser=mysql_query($query) or die(mysql_error()); while( $array = mysql_fetch_assoc($resultuser) ) { echo "<a href=\"#\" alt=\"\" title=\"{$array['mailadress']}\">Name: {$array['name']}</a><br>\n"; } Link to comment https://forums.phpfreaks.com/topic/218972-row-in-array/#findComment-1135586 Share on other sites More sharing options...
sourcecoder Posted November 17, 2010 Author Share Posted November 17, 2010 that was a sweet one but if i want to do this the "hard" way and display the 13, 19, 23 and 145th users mailadress then? how can i use it as an array? Like : $array[12], $array[18], $array[22] and so on? Link to comment https://forums.phpfreaks.com/topic/218972-row-in-array/#findComment-1135746 Share on other sites More sharing options...
Pikachu2000 Posted November 17, 2010 Share Posted November 17, 2010 No, not really. You'd need to query the DB for those specific records, then use the same type of loop to spit out the data. If the user id's you want to retrieve are in an array, you could do this $query = "SELECT * FROM users WHERE id IN(" . implode(', ' $array) . ")"; // for numeric values. Strings would need to be quoted in the IN() Using IN() is basically the same as using WHERE id = 14 OR id = 15 OR id = 16 OR . . . Link to comment https://forums.phpfreaks.com/topic/218972-row-in-array/#findComment-1135767 Share on other sites More sharing options...
sourcecoder Posted November 17, 2010 Author Share Posted November 17, 2010 hmm.. i didn't get it this is my code, so far : $res = "SELECT * FROM users WHERE id IN(' . implode(', ' $array) . ')"; $row = $res; $array = $row['mailadress']; and if i want to print out say 3 users mail, i would like to do it like this.. $array[12] $array[14] $array[53] how to do that? / Link to comment https://forums.phpfreaks.com/topic/218972-row-in-array/#findComment-1135781 Share on other sites More sharing options...
Pikachu2000 Posted November 17, 2010 Share Posted November 17, 2010 For example, you have an array of user id's from 20 through 45 that you want to return the records for. It is possible to retrieve the whole table and only echo out the records that correspond to those id's, it wouldn't be very efficient, so only retrieve those records to begin with. $user_ids = range(20, 45); // creates an array with values from 20 - 45 $query = "SELECT * FROM users WHERE id IN(" . implode(', ' $user_ids) . ")"; // The double vs. single quote usage matters here. $result = mysql_query( $query ); while( $array = mysql_fetch_assoc($result) ) { // This is where you'd echo each record as in the previous example } Link to comment https://forums.phpfreaks.com/topic/218972-row-in-array/#findComment-1135784 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.