Jump to content


Photo

little help getting an array


  • Please log in to reply
7 replies to this topic

#1 glennn.php

glennn.php
  • Validating
  • PipPipPip
  • Advanced Member
  • 251 posts
  • LocationAtlanta, GA USA

Posted 20 April 2006 - 07:44 PM

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=auto:0]
$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=auto:0]

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

#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 20 April 2006 - 07:54 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 Darkness Soul

Darkness Soul
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts
  • LocationBrazil; São Paulo.

Posted 20 April 2006 - 07:57 PM

Try the code now.. like it:
$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>";
}
If wont work, let's see what we can do.

D.Soul
(If something is wrong, please tell me. I'm learning this language. Thank you)

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 20 April 2006 - 08:07 PM

try
$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>";
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 glennn.php

glennn.php
  • Validating
  • PipPipPip
  • Advanced Member
  • 251 posts
  • LocationAtlanta, GA USA

Posted 20 April 2006 - 08:41 PM

thanks, you guys, that did it...

#6 glennn.php

glennn.php
  • Validating
  • PipPipPip
  • Advanced Member
  • 251 posts
  • LocationAtlanta, GA USA

Posted 20 April 2006 - 08:58 PM

[!--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) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try the code now.. like it:
$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>";
}
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?

#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 20 April 2006 - 09:39 PM

I don't currently know databasing yet, I am working on it though.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 glennn.php

glennn.php
  • Validating
  • PipPipPip
  • Advanced Member
  • 251 posts
  • LocationAtlanta, GA USA

Posted 20 April 2006 - 10:11 PM

[!--quoteo(post=367009:date=Apr 20 2006, 05:39 PM:name=businessman332211)--][div class=\'quotetop\']QUOTE(businessman332211 @ Apr 20 2006, 05:39 PM) View Post[/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'; "




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users