cyandi_man Posted January 26, 2009 Share Posted January 26, 2009 Where can I stick my function that formats my phone number string from my database before it is displayed on the page? I am trying to add the following function: function format_tel_num($contactNumber){ //<-- contactNumber is a field on database. return preg_replace('/\d{3}/', '$0.', str_replace('.', null, trim($contactNumber)), 2); } $contactNumber = format_tel_num($contactNumber); Into the following php file below - containing the following code without breaking my loop? do i need to make this function a global function outside of the loop? --query that gathers info from database-- $result= mysql_query($query) $nrows=mysql_num_rows($result); if ($nrows == 0) { echo "There are no clinics listed with that criteria at this time. Please choose again."; } else{ while ($row = mysql_fetch_array($result)) { extract($row); if($logos!="../logos/profs/spacer.gif"){ echo " code that displays data from database"; } else { echo " coding that dislpays slightly different data from database"; } } } Thank you guys - you all have been great in answering my questions so far! Quote Link to comment https://forums.phpfreaks.com/topic/142427-trying-to-have-a-function-within-a-loop-help-please/ Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 Outside the loop, functions are global as they are, put it outside the loop and call it inside the loop and it should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/142427-trying-to-have-a-function-within-a-loop-help-please/#findComment-746218 Share on other sites More sharing options...
cyandi_man Posted January 26, 2009 Author Share Posted January 26, 2009 Outside the loop, functions are global as they are, put it outside the loop and call it inside the loop and it should work fine. EXCELLENT BOSS! i thought so... it works! thanks a million! Quote Link to comment https://forums.phpfreaks.com/topic/142427-trying-to-have-a-function-within-a-loop-help-please/#findComment-746297 Share on other sites More sharing options...
cooldude832 Posted January 26, 2009 Share Posted January 26, 2009 Outside the loop, functions are global as they are, put it outside the loop and call it inside the loop and it should work fine. EXCELLENT BOSS! i thought so... it works! thanks a million! When you get further in PHP you will look into OOP probably and Scope is the #1 issue in OOP programing. However what is cool with OOP is you can do some sweet things that will blow your mind the first time you do something non trivial in OOP Quote Link to comment https://forums.phpfreaks.com/topic/142427-trying-to-have-a-function-within-a-loop-help-please/#findComment-746305 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.