Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.