Jump to content

[SOLVED] Turn This Code Into Function?


thesaleboat

Recommended Posts

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

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.

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" />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.