Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create function !!

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
0
0
I have a large switch statement to compare the $id and $link variables with many different values, and want to use the following code in this statment:
--------------------------------------------------------------
$sql = "select * from my_table where my_id = $id and my_link = '$link' ";
$result = $nav->execute($sql, $db, "mysql");
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++) {
$data = mysql_fetch_object($result);
echo "$data->my_link";
}
----------------------------------------------------------------
execute() is a command of a class included in the top of the code.
how can I can create a function contain this code to use it in the switch statement?

Thanks in advance
 
to create the following code:

$sql = "select * from my_table where my_id = $id and my_link = '$link' ";
$result = $nav->execute($sql, $db, "mysql");
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++) {
$data = mysql_fetch_object($result);
echo "$data->my_link";
}
 
exactly, and thanks for answer.
 
hopw about something like:

Code:
fuction nameoffuntion(paramters)
{
 $sql = "select * from my_table where my_id = $id and my_link = '$link' ";
    $result = $nav->execute($sql, $db, "mysql");
    $rows = mysql_num_rows($result);
    for ($y = 0; $y < $rows; $y++) {
      $data = mysql_fetch_object($result);
    echo "$data->my_link"; 

return valutobereturned;
}

Is that what you want???
 
thank you vacunita,
here a sample code

Code:
<?php

include("navbar.php");

$nav = new navbar;
$nav->numrowsperpage = 5;

if ($cat == "three"){
           switch ($sec) {
			case "1":
    $sql = "select * from my_table where my_id = 1 and my_link = 'my_lnk1' ";
    $result = $nav->execute($sql, $db, "mysql");
    $rows = mysql_num_rows($result);
    for ($y = 0; $y < $rows; $y++) {
      $data = mysql_fetch_object($result);
    echo "$data->my_link"; 
    }
    		break;
			
			case "2":
    $sql = "select * from my_table where my_id = 2 and my_link = 'my_lnk2' ";
    $result = $nav->execute($sql, $db, "mysql");
    $rows = mysql_num_rows($result);
    for ($y = 0; $y < $rows; $y++) {
      $data = mysql_fetch_object($result);
    echo "$data->my_link"; 
    }
    		break;
	//etc, here go the other cases using the same query with diferents values of my_id and my_link.	
			
			}
   }
 $links = $nav->getlinks("sides", "on");
for ($y = 0; $y < count($links); $y++) {
  echo $links[$y] . "&nbsp;&nbsp;";
}

?>
i want to create a user-defined function to use it in every case because it is a large switch statement .
 
If the values of 'my_id' and 'my_link' are the only ones changing and they change as in your example, then you should be able to get rid of the switch statement and write your code as:
Code:
    $sql = "select * from my_table where my_id = $sec and my_link = 'my_lnk" . $sec . "'";
    $result = $nav->execute($sql, $db, "mysql");
    while ($data = mysql_fetch_object($result)
        echo $data->my_link . ' ';
//  I replaced the following lines with the above while statement
//    $rows = mysql_num_rows($result);
//    for ($y = 0; $y < $rows; $y++) {
//      $data = mysql_fetch_object($result);
//    echo "$data->my_link";
//    }
Ken
 
The funtion would be something like this using the values of $my_id, and $my_link, would look something like this:
Code:
function my_function($Sent_id,$Sent_link){

 $sql = "select * from my_table where my_id = $Sent_id and my_link = $Sent_link ";
    $result = $nav->execute($sql, $db, "mysql");
    $rows = mysql_num_rows($result);
    for ($y = 0; $y < $rows; $y++) {
      $data = mysql_fetch_object($result);
    return "$data->my_link";
    }


}


and you would call it in your code somethin like this:

Code:
...
if ($cat == "three"){
           switch ($sec) {
            case "1":
echo my_function(1,'mylink1');   
    }
break;
...

this will echo the final echo statement in your original code.


Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top