Jump to content

reverting back to a selected line in a script


kingb00zer

Recommended Posts

hey I have recently written a pretty big script and have come to a point where if I can do what IM asking about I may ahve to re write. I have a lot of if statements in thsi script which is starting to get pretty complex and basically I ahve declared a variable at say for example line 10 and then an if statement where in some cases it may change the variable on line 10s value but it needs to run the code above it again. is there a way I can put in a revert back to line 11 type function or something to go abck up?

Link to comment
Share on other sites

place the code in a function and pass the current varible value to it to processfunction dothis($value){process $value}line 10 $var= "whatever"dothis($var);blah. blah$var = "somethingelse";dothis($var);
when you say fucntion dothis($var), I dont understand what variable I am supposed to put into the parenthisis. do I just make one up? this is one fo the functions I am tryng to create
function getprice()		{			if ($selling == 1)			{			$drugprice1= mysql_query("SELECT value FROM heroin"); // find price of that drug			while($row=mysql_fetch_array($drugprice1))			{			$drugprice= $row['value'];			}			}			else if ($selling == 2)			{			$drugprice1= mysql_query("SELECT value FROM weed"); // find price of that drug			while($row=mysql_fetch_array($drugprice1))			{			$drugprice= $row['value'];			}			}			else if ($selling == 3)			{			$drugprice1= mysql_query("SELECT value FROM xtc"); // find price of that drug			while($row=mysql_fetch_array($drugprice1))			{			$drugprice= $row['value'];			}			}			else if ($selling == 4)			{			$drugprice1= mysql_query("SELECT value FROM coke"); // find price of that drug			while($row=mysql_fetch_array($drugprice1))			{			$drugprice= $row['value'];			}			}			else if ($selling == 5)			{			$drugprice1= mysql_query("SELECT value FROM specialk"); // find price of that drug			while($row=mysql_fetch_array($drugprice1))			{			$drugprice= $row['value'];			}			}			else if ($selling == 6)			{			$drugprice1= mysql_query("SELECT value FROM acid"); // find price of that drug			while($row=mysql_fetch_array($drugprice1))			{			$drugprice= $row['value'];			}			}		}

when I run the script it says "Parse error: syntax error, unexpected ';', expecting '{' in /home/druglord/public_html/selloff.php on line 28" which is the function getprice() line

Link to comment
Share on other sites

you can give any name of parameter. parameter scope is only in function.more details here http://www.php.net/manual/en/functions.arguments.php

when I run the script it says "Parse error: syntax error, unexpected ';', expecting '{' in /home/druglord/public_html/selloff.php on line 28" which is the function getprice() line
Check before the function declaration there as it says its expecting a semicolon there.so its getting a syntax erroranother thing, not related to your problem, but it can be better code readbility if you use switch case instead of repaetedly using if-else
Link to comment
Share on other sites

you can give any name of parameter. parameter scope is only in function.more details here http://www.php.net/manual/en/functions.arguments.phpCheck before the function declaration there as it says its expecting a semicolon there.so its getting a syntax erroranother thing, not related to your problem, but it can be better code readbility if you use switch case instead of repaetedly using if-else
Not sure I understand the whole parametre thing. I thought setting a function was as simple as naming it and then putting the code within that function as what the function is supposed to do. I also thought that changing it into switch would make it easier to read. (im just too used to if else lol)
Link to comment
Share on other sites

Not sure I understand the whole parametre thing. I thought setting a function was as simple as naming it and then putting the code within that function as what the function is supposed to do.
yes function do that as you have expected but there is so called parameters which are come inside the paranthese of the functions. when you declare a function if you need to pass any value from outisde you need to declare some arguments. everytime when you will call a function you can pass specific value to that function. now inside that function you can use that parameter and work with it.Parameter is local variable which exist only inside function. You may want to specify which part of the 'function' you did not get it.Did you check the link it has full resource about function parameter? there is good elaboration there.
Link to comment
Share on other sites

yes function do that as you have expected but there is so called parameters which are come inside the paranthese of the functions. when you declare a function if you need to pass any value from outisde you need to declare some arguments. everytime when you will call a function you can pass specific value to that function. now inside that function you can use that parameter and work with it.Parameter is local variable which exist only inside function. You may want to specify which part of the 'function' you did not get it.Did you check the link it has full resource about function parameter? there is good elaboration there.
I checked the link and it still didnt register to me, maybe I need to spend a day off form work looking into this a ittle bit more as mostly do my coding after work when im tired and sligthly distracted.
Link to comment
Share on other sites

you are looking into sending a value to a function and then return another value related to search$getDrugPrice = getprice($selling);thenfunction getprice($selling) { if ($selling == 1) { $tablename= "heroin"; // find price of that drug } else if ($selling == 2) { $tablename= "weed"; // find price of that drug } else if ($selling == 3) { $tablename= "xtc"; // find price of that drug } else if ($selling == 4) { $tablename = "coke"; // find price of that drug } else if ($selling == 5) { $tablename= "specialk"; // find price of that drug } else if ($selling == 6) { $tablename= "acid"; // find price of that drug } $sql= mysql_query("SELECT value FROM $tablename"); while($row=mysql_fetch_array($sql)) { $drugprice= $row['value']; } return $drugprice;}now $getDrugPrice will equal the price for the specific drug, by sending reference value '$selling' in getprice($selling);

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...