Anyone know a good script that would take data from a mysql table, import it into a php array, and then sort it? I found tutorials on how to print mysql data, and understood that easily enough... and then found a tutorial on how to sort an array in php, and that made enough sense, but I can't seem to figure out how to connect the two.
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);
<?php
// demo array to sort
$latest_array = array(
array('news','1234567890','sdf','asdpojq'),
array('news','1234567892','uruy','xbuqiwpo'),
array('comment','1234567893','fghj','mjktyu'),
array('article','1234567891','cvmo','pjklgg'),
array('news','1234567894','qwenb','asbhtyhj'),
);
$sort_field = 3; // enter the number of field to sort
// compare function
function cmpi($a, $b)
{
global $sort_field;
return strcmp($a[$sort_field], $b[$sort_field]);
}
// do the array sorting
usort($latest_array, 'cmpi');
// demo output
echo '<pre>';
print_r($latest_array);
?>
Name:
Anonymous2005-12-13 5:33
no. nobody knows. go away.
Name:
Anonymous2005-12-13 6:19
>>1
You mean something like mysql_fetch_array() and sort()?
Also you fail for using mysql and for having shitty php code.
Now go away and die.
Name:
Anonymous2005-12-13 7:12
Stop using php
Stop using mysql
You're probably using Apache; stop using that too.
Ruby on Rails + Postgres + lighttpd
Name:
Anonymous2005-12-13 12:24
>>4
pfft.
Clearly Java + Spring + JSF + Hibernate + Oracle + SUN AS + EJB3
Oh yeah, that'll be $1 million in consulting fees too.
Name:
Anonymous2005-12-13 12:47
>>5
I'd use that stack if I were being paid by the hour, or per line of code. PROFIT!
Name:
Anonymous2005-12-13 23:39
>>1
I can't even imagine someone who can't figure this out, so I'm not very good at answering this question, but replace echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";
with the obvious part of the second piece of code.
Then ignore people telling you to use Ruby, because it's too Web 2.0 (but mod_perl is pretty cool), and MySQL is just fine for doing stupid trivial stuff like this. lighttpd is pretty cool though.
Name:
Anonymous2005-12-14 3:09
>>7
I think "Web 2.0" works best if you ignore all the people who are using the term, as well as the term itself. Paul Graham has a nice essay regarding this (which is where I found out about the term; I hadn't even heard it previously) where he says that we haven't upgraded to a new version of anything; we're just doing it how it's supposed to be done. And why should we require special terminology to describe things not being botched?
Name:
Anonymous2005-12-14 4:31 (sage)
And what's wrong with 'ORDER BY' ?
Name:
Anonymous2005-12-14 8:12
Huh? Ruby and Web 2.0?
Besides "Web 2.0" being a totally fagtastic term (actually, that's being totally unfair to faggots), what does Ruby have to do with it? It's just another language.
Name:
Anonymous2005-12-14 10:11
>>10
I assume >>7 was talking more about Rails than Ruby. Rails could easily be marketed as "the premier platform for Web 2.0 applications" or some similar bullshit. It's got "Ajax helpers" and everything.
Name:
Anonymous2005-12-14 10:37
I feel sorry for Rails. Every last blogging retard is looking for something to validate their worthless drivel. If they can stuff themselves under the same umbrella as Rails, they'll do it.
I feel sorry for Javascript too. zOMG Ajax!
Name:
Anonymous2005-12-14 12:00
I'd like to see the j in Ajax become as flexible as the P in LAMP. One language isn't enough.
Name:
Anonymous2005-12-14 13:20
>>6
Lol on truth, the master plan has been completed:
1. Get paid per line using J2EE
2. Write a million lines of code (for a small application)
3. PROFIT
>>6 >>14
You guys aren't doing anything until I get my million bucks!
Name:
Anonymous2005-12-22 22:34 (sage)
>>ALL
STFU & GTFO NOOBS
you know nothing.
Name:
Anonymous2006-03-09 18:35
>>1
nab the 'hack' from codegrrl.com phpfanbase for expoting the memberlist & customise it to your needs. It simply prints a page of comma seperated values sorted by whatever you want.
I was going to do a very sloppy (probably wouldn't work) OOP version for OP, but I got lazy. A lot of the script is kinda.. theoretical? I am not used to PHP's oop, but I thought I would give it a go. You guys an mess with it. Can't guarantee it will work any.
function Build ($i="") {
switch ($i) {
case "connect":
$this->mysqli = new mysqli($host, $user, $pass, $name);
# Add your own errors
break;
case "disconnect":
$this->mysqli->close();
break;
}
}
}
class Main extends Connect {
private $query, $result, $num;
function __construct () {
Connect::Build("connect");
}
function Query () {
$this->query = "select * from contacts";
$this->result = Connect::Build("connect")->query($this->query);
$this->num = Connect::Build("connect")->num_rows($this->result);
}
function __destruct () {
Connect::Build("disconnect");
}
}
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);
$num=@mysql_num_rows($result); //you missed an underscore
$rows=array();