rkteach Posted December 17, 2008 Share Posted December 17, 2008 Hi all, need help looping a Select count(*) from mysql database. data is pulling from a form where they choose where they heard about us... for marketing. I can show an individual result using /////////////code/////////////// <?php $res = mysql_query("SELECT count(*) FROM Training_Inquiries WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= Contact_Date AND Ad_tracker='Google'"); $res = mysql_fetch_array($res); echo $res[0]." Selected Google.<br>"; ?> /////////////end code/////////////// But what id like to do is some how loop and/or group the results of "Ad_tracker" because the choice is dynamic and my be change or added in an admin (drop down menu) you can see im posting last 30 days if the table "Ad_tracker" = 'Google' and can do this for each choice... but if we add another choice in the admin to the form i would have to code a new entry... If anyone has any idea that would be great... Quote Link to comment https://forums.phpfreaks.com/topic/137404-solved-help-looping-a-select-count/ Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 while ($r = mysql_fetch_array($res)) { echo $r[0]." Selected Google.<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/137404-solved-help-looping-a-select-count/#findComment-717980 Share on other sites More sharing options...
phpian Posted December 17, 2008 Share Posted December 17, 2008 ok off the top of my head here but it might work : "SELECT count(*) as num, Ad_Tracker FROM Training_Inquiries WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= Contact_Date GROUP BY Ad_tracker" while ($r = mysql_fetch_array($res)) { echo $r['num']." Selected ".$r['Ad_Tracker'].".<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/137404-solved-help-looping-a-select-count/#findComment-717995 Share on other sites More sharing options...
rkteach Posted December 17, 2008 Author Share Posted December 17, 2008 Thanks for the quick response, but this doesn't group the input for 'Ad_tracker' my example only filters Google. what i need to do (im guessing) is group the entered choices then show the count(*) in a loop? thanks again for your time. Quote Link to comment https://forums.phpfreaks.com/topic/137404-solved-help-looping-a-select-count/#findComment-717999 Share on other sites More sharing options...
twm Posted December 17, 2008 Share Posted December 17, 2008 <?php $res = mysql_query("SELECT COUNT(Ad_tracker) FROM Training_Inquiries WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= Contact_Date AND Ad_tracker='Google' GROUP BY Ad_tracker"); $res = mysql_fetch_array($res); echo $res[0]." Selected Google.<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/137404-solved-help-looping-a-select-count/#findComment-718005 Share on other sites More sharing options...
phpian Posted December 17, 2008 Share Posted December 17, 2008 i'm not understanding the question now. what is it you want to get out of the query? will it be something like: google : 20 yahoo : 10 another : 5 etc... Quote Link to comment https://forums.phpfreaks.com/topic/137404-solved-help-looping-a-select-count/#findComment-718021 Share on other sites More sharing options...
rkteach Posted December 18, 2008 Author Share Posted December 18, 2008 thanks Violet Crayon & PHPain thats exacty what i needed <?php $res = mysql_query("SELECT count(*) as num, Ad_Tracker FROM Training_Inquiries WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= Contact_Date GROUP BY Ad_tracker"); while ($r = mysql_fetch_array($res)) { echo $r['num']." Selected ".$r['Ad_Tracker'].".<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137404-solved-help-looping-a-select-count/#findComment-718499 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.