ShibSta Posted March 5, 2006 Share Posted March 5, 2006 I am wanting to find the link that has the lowest unique hits and then redirect the visitor to it.I have the value of the unique hits in the varibles $unique1, $unique2, and $unique3.How would I go about doing this?ThanksI have the following code:[code] 1. <?php ob_start() ?> 2. <html xmlns="http://www.w3.org/1999/xhtml"> 3. <head> 4. <title>Title</title> 5. </head> 6. <body> 7. <?php 8. $host = 'localhost'; 9. $username = '---'; 10. $password = '---'; 11. $db_name = '---'; 12. $sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());; 13. $db_connect = mysql_select_db("$db_name")or die("Could not select DB: " . mysql_error());; 14. 15. $count1 = mysql_query("SELECT * FROM s") or die(mysql_error()); 16. $unique1 = mysql_num_rows($count); 17. $count2 = mysql_query("SELECT * FROM s1") or die(mysql_error()); 18. $unique2 = mysql_num_rows($count); 19. $count3 = mysql_query("SELECT * FROM g") or die(mysql_error()); 20. $unique3 = mysql_num_rows($count); 21. 22. 23. if () { 24. include("updatedb1.php"); 25. header("location:--Link Removed--"); 26. //echo "test1"; 27. } 28. if () { 29. include("updatedb2.php"); 30. header("location:--Link Removed--"); 31. //echo "test2"; 32. } 33. if () { 34. include("updatedb3.php"); 35. header("location:--Link Removed--"); 36. //echo "test3"; 37. } 38. 39. ?> 40. </body> 41. </html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/ Share on other sites More sharing options...
ShibSta Posted March 5, 2006 Author Share Posted March 5, 2006 **Bump**I need to get this done ASAP, could someone please assist me?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14385 Share on other sites More sharing options...
ShibSta Posted March 5, 2006 Author Share Posted March 5, 2006 **Bump**Anyone able to assist me please?I'd like to get the solved ASAP.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14462 Share on other sites More sharing options...
ShibSta Posted March 6, 2006 Author Share Posted March 6, 2006 Sheesh, on the 3rd page already.**Bump**Please help, I am stuck.Thankz Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14557 Share on other sites More sharing options...
wickning1 Posted March 6, 2006 Share Posted March 6, 2006 We aren't responding because none of us are sure what kind of help you need.What is the specific problem you are having? Why are you counting 3 different tables? What does each table represent? Why aren't you using SELECT COUNT(*)? Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14572 Share on other sites More sharing options...
wildteen88 Posted March 6, 2006 Share Posted March 6, 2006 Your if statements are completly invalid! They wont even work properly[code]if () { include("updatedb1.php"); header("location:--Link Removed--"); //echo "test1";}[/code]If statements require some form of arguemnt between the opnening closing brackets - ()This is a proper if statement:[code]$a = 2;if($a == 2){ echo '$a equals to 2!';}else{ echo '$a does not equal 2!';}[/code]The below code you use a variable called $count. $count hasn't been defined any where so mysql_num_rows wont be working and most probably is stoping your script from running! You do have variables called $count1, $count2 and $count3. So on lines 16, 18 and 20 you need to change $count to $count1 on line 16, $count to $count2 on line 18 and change $count to $count3 on line 20.[code] 15. $count1 = mysql_query("SELECT * FROM s") or die(mysql_error()); 16. $unique1 = mysql_num_rows($count); 17. $count2 = mysql_query("SELECT * FROM s1") or die(mysql_error()); 18. $unique2 = mysql_num_rows($count); 19. $count3 = mysql_query("SELECT * FROM g") or die(mysql_error()); 20. $unique3 = mysql_num_rows($count);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14584 Share on other sites More sharing options...
ShibSta Posted March 6, 2006 Author Share Posted March 6, 2006 The reason the if statement are blank is because I have no clue as to what arguement to add. In which is what I was asking.As for the $count it was a mistake when transfering it from dreamweaver to the site and removing personal info.I forgot to copy that part.It should be:[code]$count1 = mysql_query("SELECT * FROM s") or die(mysql_error());$unique1 = mysql_num_rows($count1);$count2 = mysql_query("SELECT * FROM s1") or die(mysql_error());$unique2 = mysql_num_rows($count2);$count3 = mysql_query("SELECT * FROM g") or die(mysql_error());$unique3 = mysql_num_rows($count3);[/code]My goal:When somone go to this page it grabs the 3 values from the database and defines the values to the varibles $unique1, $unique2, $unique3.I then want the PHP script to find what have the 3 varibles has the lowest value and then execute the correct include and header:location.Overall Understanding:I am a affiliate on a site aswell as 2 of my other friends. We want referrals so we're going to post on my forums, however we want the same number of visitors to be directed to each of our referral links.Therefore in our post we are going to send them to this script, it will check the database, find which has the lowest amount of unique hits and then add +1 to that number (updates the DB) and sends them to the header:location.When you don't understand a situation please let me know I will do try to explain it better, when nothing is said I feel as if I'm being ignored hince many other topics are receiving a response.Thanks-ShibSta Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14627 Share on other sites More sharing options...
XenoPhage Posted March 6, 2006 Share Posted March 6, 2006 [!--quoteo(post=352070:date=Mar 6 2006, 09:11 AM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 6 2006, 09:11 AM) [snapback]352070[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]$count1 = mysql_query("SELECT * FROM s") or die(mysql_error());$unique1 = mysql_num_rows($count1);$count2 = mysql_query("SELECT * FROM s1") or die(mysql_error());$unique2 = mysql_num_rows($count2);$count3 = mysql_query("SELECT * FROM g") or die(mysql_error());$unique3 = mysql_num_rows($count3);[/code][/quote]First things first. If you're interested in the slowest loading page possible, the above is fine. However, I would recommend, instead, that you allow MySQL to do the counting for you. Observe :[code]list($unique1) = mysql_fetch_array(mysql_query('SELECT COUNT(*) FROM s'));[/code]Or, to break it out into edible chunks :[code]$query = 'SELECT COUNT(*) FROM s';$count1 = mysql_query($query) or die(mysql_error());list($unique1) = mysql_fetch_array($count1);mysql_free_result($count1);[/code][!--quoteo(post=352070:date=Mar 6 2006, 09:11 AM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 6 2006, 09:11 AM) [snapback]352070[/snapback][/div][div class=\'quotemain\'][!--quotec--]My goal:When somone go to this page it grabs the 3 values from the database and defines the values to the varibles $unique1, $unique2, $unique3.I then want the PHP script to find what have the 3 varibles has the lowest value and then execute the correct include and header:location.[/quote]Hrm.. Just off the top of my head :[code]if (($unique1 <= $unique2) && ($unique1 <= unique3)) { # unique1 is the lowest} else if (($unique2 <= $unique1) && ($unique2 <= unique3)) { # unique2 is the lowest} else if (($unique3 <= $unique1) && ($unique3 <= unique2)) { # unique3 is the lowest} else { # You should never reach this point}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14645 Share on other sites More sharing options...
ShibSta Posted March 6, 2006 Author Share Posted March 6, 2006 [!--quoteo(post=352088:date=Mar 6 2006, 10:39 AM:name=XenoPhage)--][div class=\'quotetop\']QUOTE(XenoPhage @ Mar 6 2006, 10:39 AM) [snapback]352088[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hrm.. Just off the top of my head :[code]if (($unique1 <= $unique2) && ($unique1 <= unique3)) { # unique1 is the lowest} else if (($unique2 <= $unique1) && ($unique2 <= unique3)) { # unique2 is the lowest} else if (($unique3 <= $unique1) && ($unique3 <= unique2)) { # unique3 is the lowest} else { # You should never reach this point}[/code][/quote]Which would this code use if all values were the same? Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14709 Share on other sites More sharing options...
XenoPhage Posted March 6, 2006 Share Posted March 6, 2006 [!--quoteo(post=352153:date=Mar 6 2006, 01:50 PM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 6 2006, 01:50 PM) [snapback]352153[/snapback][/div][div class=\'quotemain\'][!--quotec--]Which would this code use if all values were the same?[/quote]Simulate the values and try it out.. :) It should run through the first if() statement if all 3 values are equal. But, it shouldn't matter unless you're looking to make sure one person gets the hit before everyone else.. *grin*So, as an example, the hits will work out like this :0 0 01 0 01 1 01 1 12 1 12 2 12 2 2etc. Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14775 Share on other sites More sharing options...
ShibSta Posted March 6, 2006 Author Share Posted March 6, 2006 [!--quoteo(post=352220:date=Mar 6 2006, 04:04 PM:name=XenoPhage)--][div class=\'quotetop\']QUOTE(XenoPhage @ Mar 6 2006, 04:04 PM) [snapback]352220[/snapback][/div][div class=\'quotemain\'][!--quotec--]Simulate the values and try it out.. :) It should run through the first if() statement if all 3 values are equal. But, it shouldn't matter unless you're looking to make sure one person gets the hit before everyone else.. *grin*So, as an example, the hits will work out like this :0 0 01 0 01 1 01 1 12 1 12 2 12 2 2etc.[/quote]I just wanted to know because if it's random stats are goin' to jump around. Which is not fair.I want it turn by turn as you pointed out it is.I will go try this right now.Thankx Alot! Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-14834 Share on other sites More sharing options...
ShibSta Posted March 7, 2006 Author Share Posted March 7, 2006 Erm, I put an error message after the last else() and the error is being displayed when all values are equal to 1 Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-15152 Share on other sites More sharing options...
XenoPhage Posted March 7, 2006 Share Posted March 7, 2006 [!--quoteo(post=352606:date=Mar 7 2006, 03:30 PM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 7 2006, 03:30 PM) [snapback]352606[/snapback][/div][div class=\'quotemain\'][!--quotec--]Erm, I put an error message after the last else() and the error is being displayed when all values are equal to 1[/quote]Hrm.. can you post your code please? You should never hit that else .... Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-15169 Share on other sites More sharing options...
ShibSta Posted March 7, 2006 Author Share Posted March 7, 2006 [code] <?php ob_start() ?><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><body><?php$host = 'localhost';$username = '----';$password = '----';$db_name = '----';$sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());;$db_connect = mysql_select_db("$db_name")or die("Could not select DB: " . mysql_error());;$count1 = mysql_query("SELECT * FROM link1") or die(mysql_error());$unique1 = mysql_num_rows($count1);$count2 = mysql_query("SELECT * FROM link2") or die(mysql_error());$unique2 = mysql_num_rows($count2);$count3 = mysql_query("SELECT * FROM link3") or die(mysql_error());$unique3 = mysql_num_rows($count3);if (($unique1 <= $unique2) && ($unique1 <= unique3)) { include("link1.php"); header("location:----Link Removed----");} else if (($unique2 <= $unique1) && ($unique2 <= unique3)) { include("link2.php"); header("location:----Link Removed----");} else if (($unique3 <= $unique1) && ($unique3 <= unique2)) { include("link3.php"); header("location:----Link Removed----");} else { echo "An Error Has Occured.";}?></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-15199 Share on other sites More sharing options...
XenoPhage Posted March 8, 2006 Share Posted March 8, 2006 Hrm.. I have no idea.. I used the below code as a test and it works fine...[code]<?php$a = 0;$b = 0;$c = 0;while ($a < 3) { if (($a <= $b) && ($a <= $c)) { print "a is lowest - ($a $b $c)\n"; $a++; } else if (($b <= $a) && ($b <= $c)) { print "b is lowest - ($a $b $c)\n"; $b++; } else if (($c <= $a) && ($c <= $b)) { print "c is lowest - ($a $b $c)\n"; $c++; } else { print "ERROR! - ($a $b $c)\n"; exit; }}?>[/code]As an aside, if you switch to COUNT(*) instead of relying on mysql_num_rows, your code will execute *much* faster as the database grows... Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-15386 Share on other sites More sharing options...
ShibSta Posted March 8, 2006 Author Share Posted March 8, 2006 Would you mind using the count(*) in my script and then posting it as I have no idea how to use it. I learn from reading PHP not the manual... (That thing just confuses me) Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-15481 Share on other sites More sharing options...
XenoPhage Posted March 8, 2006 Share Posted March 8, 2006 [!--quoteo(post=352940:date=Mar 8 2006, 02:25 PM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 8 2006, 02:25 PM) [snapback]352940[/snapback][/div][div class=\'quotemain\'][!--quotec--]Would you mind using the count(*) in my script and then posting it as I have no idea how to use it. I learn from reading PHP not the manual... (That thing just confuses me)[/quote]Sure.. Observe :[code] 1. <?php ob_start() ?> 2. <html xmlns="http://www.w3.org/1999/xhtml"> 3. <head> 4. <title>Title</title> 5. </head> 6. <body> 7. <?php 8. $host = 'localhost'; 9. $username = '---'; 10. $password = '---'; 11. $db_name = '---'; 12. $sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());; 13. $db_connect = mysql_select_db("$db_name")or die("Could not select DB: " . mysql_error());; 14. 15. list($unique1) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM s") or die(mysql_error())); 17. list($unique2) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM s1") or die(mysql_error())); 19. list($unique3) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM g") or die(mysql_error())); 21. 22. 23. if () { 24. include("updatedb1.php"); 25. header("location:--Link Removed--"); 26. //echo "test1"; 27. } 28. if () { 29. include("updatedb2.php"); 30. header("location:--Link Removed--"); 31. //echo "test2"; 32. } 33. if () { 34. include("updatedb3.php"); 35. header("location:--Link Removed--"); 36. //echo "test3"; 37. } 38. 39. ?> 40. </body> 41. </html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4139-find-the-lowest-value/#findComment-15532 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.