Jump to content

little help getting an array


glennn.php

Recommended Posts

kind people, i'm attempting to print results of a mysql query into alphabetized groups - i can get the results, but i can't figure out how to separate them and sort them... can someone help?

from this:

[code]
$sql = "SELECT `customer`.`username` , `customer`.`homepage`
FROM customer
ORDER BY `customer`.`username` ASC , `customer`.`homepage` ASC";


$result = mysql_query($sql) or die("Query failed");
$row = mysql_fetch_array($result); // change to suit your needs

print "${row[0]} - ${row[1]}";
[code]

obviously i'm only getting the first record's results... i need to get all the records as their added to the db...
what i'd like to be able to do is print 26 groups according username...

can someone show me how to get these results into some kind of array, and then printed, or whatever i need to do?

thanks
glennn
Link to comment
https://forums.phpfreaks.com/topic/7987-little-help-getting-an-array/
Share on other sites

Try the code now.. like it:
[code]$sql = "SELECT username , homepage FROM customer ORDER BY username , homepage";
$result = mysql_query($sql , $conn) or die(print "Query failed: ". mysql_error() );
$lines = mysql_num_rows ( $result );

for ( $i=0;$i<$lines;$i++ )
{
    $row = mysql_fetch_array($result);
    print "${row[0]} - ${row[1]} <br>";
}[/code]
If wont work, let's see what we can do.

D.Soul
try
[code]
$sql = "SELECT username, homepage
        FROM customer
        ORDER BY username, homepage";


$result = mysql_query($sql) or die("Query failed" . mysql_error());

$letter = '';

while (list ($user, $page) = mysql_fetch_row($result)) {

       $initial = strtoupper($user{0});
       if ($initial != $letter) {
           echo "<br><b>$initial</b><br>";
           $letter = $initial;
       }

       echo "$user $page <br>";
}
[/code]
[!--quoteo(post=366982:date=Apr 20 2006, 03:57 PM:name=Darkness Soul)--][div class=\'quotetop\']QUOTE(Darkness Soul @ Apr 20 2006, 03:57 PM) [snapback]366982[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Try the code now.. like it:
[code]$sql = "SELECT username , homepage FROM customer ORDER BY username , homepage";
$result = mysql_query($sql , $conn) or die(print "Query failed: ". mysql_error() );
$lines = mysql_num_rows ( $result );

for ( $i=0;$i<$lines;$i++ )
{
    $row = mysql_fetch_array($result);
    print "${row[0]} - ${row[1]} <br>";
}[/code]
If wont work, let's see what we can do.

D.Soul
[/quote]

both of these worked, and Barand even broke it up for me, like i need - i really need to put each into a separate table cell - i'd like to get each query by alphabet letter - i've tried "select u,h from customer WHERE username LIKE 'a';" but it returned nothing. how can i properly get each letter separately?
[!--quoteo(post=367009:date=Apr 20 2006, 05:39 PM:name=businessman332211)--][div class=\'quotetop\']QUOTE(businessman332211 @ Apr 20 2006, 05:39 PM) [snapback]367009[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I don't currently know databasing yet, I am working on it though.
[/quote]

this is what worked:

select u,h from customer where username LIKE 'A%';

versus what i DID have, " ...LIKE 'A'; "

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.