newbie using functions for the first time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aj2000
Forum Newbie
Posts: 11
Joined: Sun May 30, 2004 10:12 pm

newbie using functions for the first time

Post by aj2000 »

Hi im trying to use functions in my program:
i have a set of if thens with a routine ie

Code: Select all

If ($add == "add") {

//run some routine

//function call
ouput();

}

//function

function output() {

//display some data

}

somehow the function is not being called ive included the actual code here with several place where i call the function..nothing is happening as if i never even added it:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Supreme Liquors</title>
<style>
.title
	{
		font-family: Arial, Helvetica, sans-serif;
		 
		font-size : 12px;
	}
</style>
<script language="javascript">
function Category_Check(c_name, c_description, c_image) {

	 
	if (c_name == "") {
	alert("Please enter a catogory title")
	return false;
	} 
	
	if (c_description == "") {
	alert("Please enter a description")
	return false;
	} 
	
	if (c_image == "") {
	alert("Please upload an image")
	return false;
	} 
	
	return true;

}

</script>

</head>
 
<body>
<?php

//INITIAL DB CONNECTION

$link = mysql_connect("mysql", "anujjain", "A4494J");
mysql_select_db("supremeliquors");

$flag = "n";


OUTPUT();


//**********************EDIT**************************************************// 

//IF EDIT EXISTS THEN ALLOW USER TO ACCESS THE ENTRY

if(isset($edit)) {

$query_edit = "SELECT * FROM category where c_id = '" . $edit . "'";
$result_edit = mysql_query($query_edit); 

$my_row = mysql_fetch_array($result_edit);
$flag = "y";
 
$cat_value = $my_row["c_name"];
$des_value = $my_row["c_description"];
$img_value = $my_row["c_image"];

//INVOKE FUNCTION OUTPUT
OUTPUT();

}

//*****************************DELETE**********************************************8
//IF DELETE EXISTS THEN OUTPUT THE VARIABLE

if(isset($delete)) {

$query_delete = "DELETE FROM category where c_id = '" . $delete . "'";
$result_delete = mysql_query($query_delete); 

 

}

//********************************ADD***************************************************88
//IF USER CLICKS ON ADD BUTTON, NEW CUSTOMER ID IS ADDED AND FIELDS ARE THEN SUBMITTED TO DATABASE
If ($add == "add"){
	 
   $query_id = "SELECT MAX(C_ID) FROM category";
   $result_id = mysql_query($query_id); 

  
   
   for ($i = 0; $i < mysql_num_rows($result_id); $i++) { 
   $rowarray_id = mysql_fetch_row($result_id); //)
   $new_c_id = $rowarray_id[$i] + 1; 
   //echo($new_c_id);
   
   }
   
   //$c_image = (substr($c_image,-3));
	
	$query_add = "INSERT INTO category (c_id, c_name, c_description, c_image) VALUES ('" . $new_c_id . "', '" . $c_name . "', '" . $c_description . "', '" . $c_image . "')";
	$result_add = mysql_query($query_add);

	//INVOKE FUNCTION OUTPUT
	OUTPUT();
}

//**************************************SAVE******************************************8
//IF USER CLICKS ON SAVE THEN EDITED FORM IS SUBMITTED TO DB USING UPDATE
If ($save == "save") {

	$query_save = "UPDATE category SET c_name='" . $c_name . "', c_description='" . $c_description . "', c_image='" . $c_image . "' where c_id=" . $c_id; 
	$result_save = mysql_query($query_save);

}





?>

<TABLE width="740" cellpadding="0" cellspacing="0" border="1">
<TR><TD colspan="5" align="center">Logo</TD></TR>

<?


	//GENERATE A TALBE WITH ID, IMAGE, CATEGORY AND DELETE BUTTON FOR EACH RECORD

	// RETRIEVE THE ID, IMAGE AND CATEGORY FROM DB  
  	$query_cat = "SELECT c_id, c_image, c_name FROM category";
  	$result_cat = mysql_query($query_cat); 

FUNCTION OUTPUT() {

	//GENERATE THE ROWS

	for ($i = 0; $i < mysql_num_rows($result_cat); $i++) { 
  		echo("<TR>");
   
   //$myresult = mysql_fetch_array($result_cat);
	//$c_id = $myresult["c_id"];
	//echo($c_id);

  
  	$rowarray = mysql_fetch_row($result_cat);
  	$c_id = ($rowarray[0]);
 	// echo($c_id);
  
 	//GENERATE THE FIELDS 
  	for ($j=1; $j < mysql_num_fields($result_cat); $j++) {
  	 
		$image = (substr($rowarray[$j],-3));
  	
	 //$c_id = $rowarray[$j];
  	 //echo($c_id); 
	 
	 if ($image == "gif") {
	 	echo("<TD><img src=" . $rowarray[$j] . " border=0></TD>");
	 } else {
	 	echo("<TD>" . $rowarray[$j] . "</TD>" );	
	 
	 	}
   	
   	}//END OF IF THEN STATEMENT
		
		//GENERATES A FORM FOR EACH DELETE BUTTON AND PASSES DELETE VARIABLE
	
		echo "<TD><form action=categories_test.php?delete=" . $c_id . " method=POST><input type=submit name=delete" . $c_id . " value=delete></form></td><td><form action=categories_test.php?edit=" . $c_id . " method=POST><input type=submit name=edit" . $c_id . " value=edit></TD><TD><img src=images/clear.gif width=1 height=1 border=0></td></FORM>"; 
	  
    	echo("</TR>");
       
	}//END OF FOR LOOP

}//END OF FUNCTION OUTPUT 

OUTPUT();

?>
<form action="categories_test.php" method="post" name="categories" id="categories">
<? 
if ($flag == "y") {

echo ("<TR><TD width=75>Category ID</TD><TD><input type=text name=c_id size=3 readonly value=" . $edit . "></TD></TR>");
}
?>
<TR><td width="75" class="title">Category Title</td><TD colspan="3"><input type="text" class="title" name="c_name" value="<?echo ($cat_value); ?>" size="25" > </TD><TD><img src="images/clear.gif" alt="" width="1" height="1" border="0"></td></TR>
<TR><td width="75" class="title">Description</td><TD colspan="3"><textarea cols="25" class="title" rows="4" name="c_description"><? echo ($des_value); ?></textarea></TD><TD><img src="images/clear.gif" alt="" width="1" height="1" border="0"></td></TR>
<TR><td width="75" class="title">Category Picture</td><TD colspan="3"><input type="file" class="title" name="c_image" size="50" accept="image/gif" value="<? echo ($img_value); ?>"></TD>

<? if ($flag == "y") {
   echo("<TD><input type=submit name=save value=save width=5 height=5 onclick=return Category_Check(c_name.value, c_description.value, c_image.value);></td>");	
	}
   else {
   echo("<TD><input type=submit name=add value=add new width=5 height=5 onclick=return Category_Check(c_name.value, c_description.value, c_image.value);></td>");
	}
?>
</TR>
<input type="hidden" name="upload" value="upload">
</FORM>




</TABLE>

<?
 mysql_close($link);
?>

</body>
</html>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You have scoping issues (for one).
First i'd put error_reporting(E_ALL); in there, like this

Code: Select all

<?php
error_reporting(E_ALL);
//INITIAL DB CONNECTION
...then fix all the warnings/notices you'll get. Then have a read of http://php.net/manual/en/language.variables.scope.php
Post Reply