thesaleboat Posted October 16, 2008 Share Posted October 16, 2008 Hey Guys (and Girls?) I am wondering if there is a way to turn this code into a function or method *not sure of the proper wording* so that it isn't repeated a thousand times throughout and i can just call it when i want it to run? <?php require_once('dbconnect.php'); mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myusername = $_SESSION['myusername']; $subcategoryID++; $rowalreadyexists = mysql_query("SELECT * FROM `users_subcategories` WHERE `users_subcategories`.`SubCategoryId` = '$subcategoryID' and `users_subcategories`.`username` = '$myusername'") or die(mysql_error()); $rowcount = mysql_num_rows($rowalreadyexists); if (!empty($_SESSION['myusername'])){ if ($rowcount == 1) { echo '<img src="images/Fullcompletion.gif" alt="100% completed" width="18" height="18" />'; } else echo '<img src="images/0completion.gif" alt="0% completed" width="18" height="18" />'; } else echo '<img src="images/0completion.gif" alt="0% completed" width="18" height="18" />'; ?> Link to comment https://forums.phpfreaks.com/topic/128734-solved-turn-this-code-into-function/ Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 Sure, just make a name and wrap some brackets around it. function callme() { // code here } I'm afraid that's about as specific as I can get, though. Were you wanting to wrap that whole thing into a function? Just the connection stuff? Connection and query? Just the output? Plan on abstracting variables out of it? It really depends on how you have designed/plan on designing the rest of your script. Link to comment https://forums.phpfreaks.com/topic/128734-solved-turn-this-code-into-function/#findComment-667200 Share on other sites More sharing options...
thesaleboat Posted October 16, 2008 Author Share Posted October 16, 2008 I was wanting the entire thing to be wrapped into a function as essentially all it does is show one image or another and then just be able to call it with a function call so i could do something like this you are saying... <?php function complete(){ require_once('dbconnect.php'); mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myusername = $_SESSION['myusername']; $subcategoryID++; $rowalreadyexists = mysql_query("SELECT * FROM `users_subcategories` WHERE `users_subcategories`.`SubCategoryId` = '$subcategoryID' and `users_subcategories`.`username` = '$myusername'") or die(mysql_error()); $rowcount = mysql_num_rows($rowalreadyexists); if (!empty($_SESSION['myusername'])){ if ($rowcount == 1) { echo '<img src="images/Fullcompletion.gif" alt="100% completed" width="18" height="18" />'; } else echo '<img src="images/0completion.gif" alt="0% completed" width="18" height="18" />'; } else echo '<img src="images/0completion.gif" alt="0% completed" width="18" height="18" />'; } ?> Link to comment https://forums.phpfreaks.com/topic/128734-solved-turn-this-code-into-function/#findComment-667204 Share on other sites More sharing options...
Maq Posted October 16, 2008 Share Posted October 16, 2008 Yes... Link to comment https://forums.phpfreaks.com/topic/128734-solved-turn-this-code-into-function/#findComment-667214 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.