5pence Posted March 29, 2007 Share Posted March 29, 2007 Hi Guys, I am new to these pages so hello to everyone! I have an associative array heres a small sample of it: Array ( [/index.html] => 2001 12 [/date-availability.html] => 71 0 [/burner.html] => 31 0 [/jobs.html] => 101 0 [/index_bob.html] => 245 2 ) the keys are pages on the internet (/index.html) the first number is hits 2001 the second number members 12 I want to sort this by members first AND if there are more than two cells that have equal number of members then those cells are sorted by number of hits. NB: The reason I am using an associative array is every visitor is recorded in mySQL, so using the landing page as a key I can easily count them: while ( $record = mysql_fetch_row( $result )) { // capture landing page string $land = $record[2]; // capture record of member $mem = $record[6]; // if association exists, increment count and member if necessary if (isset($landings[$land])) { $pieces = explode(" ", $landings[$land]); $landings[$land] = $pieces[0]+1 . " " . $pieces[1]+=$mem; } // else, create new association, start count at 1 and member at 0 else if (!isset($landings[$land])) { $landings[$land] = 1 . " " . $mem; } } Thanks in advance! 5pence Spence Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 29, 2007 Share Posted March 29, 2007 dude, i have no idea what you're doing or what you're trying to accomplish. but i don't think what you've got so far is an efficient way of doing it. maybe you can give a little more info? Quote Link to comment Share on other sites More sharing options...
5pence Posted March 29, 2007 Author Share Posted March 29, 2007 My table hold this: id int(6) No auto_increment refer varchar(100) landing varchar(100) time bigint(20) date date ip varchar(25) purchase tinyint(1) purchase_time bigint Every visit is recorded (im tracking) I want to make the information useful, This is the table i have created so far using a associative array and html: Landing Page Instances No. of Conversions /bookings.html 66 0 /index.html 2008 12 /date-availability.html 71 0 /burneras.html 31 0 /jobs.html 101 0 /index_.html 245 2 /gallery.html 59 0 /booking-printable.html 37 0 /about.html 93 2 I want the table to be sorted by no. of coversions then no of instances thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted March 29, 2007 Share Posted March 29, 2007 try <?php $data = array ( '/bookings.html' => '66 0', '/index.html' => '2008 12)', '/date-availability.html' => '71 0', '/burneras.html' => '31 0', '/jobs.html' => '101 0', '/index_.html' => '245 2', '/gallery.html' => '59 0', '/booking-printable.html' => '37 0', '/about.html' => '93 2' ); function mysort($a, $b) { list($a1, $a2) = explode (' ', $a); list($b1, $b2) = explode (' ', $b); $a1 = intval($a1); $a2 = intval($a2); $b1 = intval($b1); $b2 = intval($b2); if ($a2 == $b2) { // if conversions are equal if ($a1==$b1) return 0; // sort by instances return $a1 < $b1 ? 1 : -1; } return $a2 < $b2 ? 1 : -1; } uasort($data, 'mysort'); /** * check results */ echo '<pre>', print_r($data, true), '</pre>'; ?> -->[pre] Array ( [/index.html] => 2008 12) [/index_.html] => 245 2 [/about.html] => 93 2 [/jobs.html] => 101 0 [/date-availability.html] => 71 0 [/bookings.html] => 66 0 [/gallery.html] => 59 0 [/booking-printable.html] => 37 0 [/burneras.html] => 31 0 ) [/pre] Quote Link to comment Share on other sites More sharing options...
5pence Posted March 29, 2007 Author Share Posted March 29, 2007 Hi Barand, Thankyou very much - that did the trick perfectly! Yours Thankfully. Spence Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.