Jump to content

Archived

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

glennn.php

little help getting an array

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

Share this post


Link to post
Share on other sites
print_r($nameofyourarray);
this will print out all the array information for you to see, to make it to where you can read it better you can do.
<pre>
<?php
print_r($nameofyourarray);
?>
</pre>
hope this helps.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[!--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?

Share this post


Link to post
Share on other sites
[!--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'; "

Share this post


Link to post
Share on other sites

×

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.