Jump to content

row in array?


sourcecoder

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

hmm..

 

i didn't get it :P

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.