0

Firstly, Thaks for taking a look at my question.

I have a function that works perfectly for me, and I want to call another function from within that function however I'm getting all kinds of issues.

Here are the functions then I'll explain what I'm needing and what I'm running into.

They are probably very messy, but I'm learning and thought I'd try get fancy then clean it up.

function GetStation($id){

$x_db_host1="localhost"; // Host name
$x_db_username1="xxxx"; // Mysql username
$x_db_password1="xxxx"; // Mysql password
$x_db_name1="xxxx"; // Database name

// Connect to server and select databse.
mysql_connect("$x_db_host1", "$x_db_username1", "$x_db_password1");
mysql_select_db("$x_db_name1");

// SQL Query Setup for Station Name
$sql="SELECT * FROM stations WHERE ID = $id LIMIT 1";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){ 
$retnm = $rows['CallSign'];
}
mysql_close();
echo $retnm;
} // Closes Function



// List Delegates Function!!!!!!!!!!!!!!!!!!!
function ListDelegates(){

    $x_db_host1="xxx"; // Host name
    $x_db_username1="xxx"; // Mysql username
    $x_db_password1="xxxx"; // Mysql password
    $x_db_name1="xxxx"; // Database name

// Connect to server and select databse.
    mysql_connect("$x_db_host1", "$x_db_username1", "$x_db_password1");
    mysql_select_db("$x_db_name1");

    $q = "SELECT * FROM delegates";
    $result = mysql_query($q);
/* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "There are no delegates to display";
      return;
   }
   /* Display table contents */

    echo "<table id=\"one-column-emphasis\" summary=\"Delegates\"><thead>";
    echo "<thead><tr><th>ID</th><th>Name</th><th>Station</th><th>Spec Req</th><th>BBQ</th><th>DIN</th><th>SAT</th><th>SUN</th></tr>";
    echo "</thead><tbody>";

    for($i=0; $i<$num_rows; $i++){
        $d_id  = mysql_result($result,$i,"DID");
        $d_name1 = mysql_result($result,$i,"DFName");
        $d_name2 = mysql_result($result,$i,"DLName");
        $d_name = $d_name1 . " " . $d_name2;
        $d_spec1  = mysql_result($result,$i,"DSpecRe");
        $StatNm  = mysql_result($result,$i,"DStation");
        $d_st_name  = GetStation($StatNm);

    if ($d_spec1=="0"){ $d_spec = "-"; }
    else {$d_spec = "<a href=\"javascript:void(window.open('http://xxxxxxx.xxx/xsreq.php?id=$d_id','','width=300,height=250,left=0,top=0,resizable=no,menubar=no,location=no,status=yes,scrollbars=no'))\">YES</a>"; }

        $d_bbq1  = mysql_result($result,$i,"Dbbq"); // BBQ
    if ($d_bbq1=="0"){ $d_bbq = "-"; }
    else {$d_bbq = "NO"; }  

        $d_din1  = mysql_result($result,$i,"Dconfdinner"); // Dinner
    if ($d_din1=="0"){ $d_din = "-"; }
    else {$d_din = "NO"; }  

        $d_sat1  = mysql_result($result,$i,"DConfSat"); // Saturday
    if ($d_sat1=="0"){ $d_sat = "-"; }
    else {$d_sat = "NO"; }  

        $d_sun1  = mysql_result($result,$i,"DConfSat"); // Sunday
    if ($d_sun1=="0"){ $d_sun = "-"; }
    else {$d_sun = "NO"; }  

    echo "<tr><td>$d_id</td><td><strong>$d_name</strong></td><td>$d_st_name</td><td>$d_spec</td><td>$d_bbq</td><td>$d_din</td><td>$d_sat</td><td>$d_sun</td></tr>";

   }
    echo "</tbody></table></br>";
}

So I output ListDelegates() in a page and it displays a nice table etc.

Within ListDelegates() i use the GetStation() function.

This is because the table ListDelegates() uses contains the station ID number not name so I want GetStation($id) to output the station name

The problem I'm having is it seems GetStation() is outputting all names in the first call of the function so the first row in the table and is not breaking it down into each row and just one at a time :S

Here's what I think (I'm probably wrong) ListDelegates() is not calling GetStation() for each row it's doing it once even though it's in the loop. ??

I have no idea if this should even work at all... I'm just learning researching then trying things.

Please help me so that I can output station name

4 Answers 4

3

At the end of GetStation, you need to change

echo $retnm;

to

return $retnm;

You are printing out the name from inside the function GetStation, when you are intending to store it in a variable. What ends up happening, is that the result of GetStation is effectively echo'ed on the screen outside of any table row. Content that is inside a table but not inside a table cell gets collected to the top of a table in a browser. If you want to see what I mean, just view source from your browser after loading the page.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks that worked... silly me... But i'm learning so thank you!
0

You don't need to connect to the database in each and every function. Usually you do the database connection at the top of your code and use the handle (in PHP the handle is usually optional) throughout your code. I think your problem is because when you call the function each time it makes a new connection and loses the previous data in the query.

Comments

0

My dear first of all you should place your code of connection with local host and database globally. It should be defined only once. you are defining it in both function.

2 Comments

I've combined these two functions into one page... they had been seperate, hence the reason I had two lots of the connection etc
My dear regardless of different pages. It is always a good practice to code the connect in a seperate file. Then just include that file where ever you want to do database related operation
0

something like this, and as suggested, you should have connection to database established somewhere else

function ListDelegates(){

    $x_db_host1="xxx"; // Host name
    $x_db_username1="xxx"; // Mysql username
    $x_db_password1="xxxx"; // Mysql password
    $x_db_name1="xxxx"; // Database name

    // Connect to server and select databse.
    mysql_connect("$x_db_host1", "$x_db_username1", "$x_db_password1");
    mysql_select_db("$x_db_name1");

    $q = "SELECT * FROM delegates";
    $result = mysql_query($q);
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "There are no delegates to display";
      return;
   }
   /* Display table contents */

    echo "<table id=\"one-column-emphasis\" summary=\"Delegates\"><thead>";
    echo "<thead><tr><th>ID</th><th>Name</th><th>Station</th><th>Spec Req</th><th>BBQ</th><th>DIN</th><th>SAT</th><th>SUN</th></tr>";
    echo "</thead><tbody>";

    for($i=0; $i<$num_rows; $i++){
        $d_id  = mysql_result($result,$i,"DID");
        $d_name1 = mysql_result($result,$i,"DFName");
        $d_name2 = mysql_result($result,$i,"DLName");
        $d_name = $d_name1 . " " . $d_name2;
        $d_spec1  = mysql_result($result,$i,"DSpecRe");
        $StatNm  = mysql_result($result,$i,"DStation");

        $d_bbq1  = mysql_result($result,$i,"Dbbq"); // BBQ
        $d_din1  = mysql_result($result,$i,"Dconfdinner"); // Dinner
        $d_sat1  = mysql_result($result,$i,"DConfSat"); // Saturday
        $d_sun1  = mysql_result($result,$i,"DConfSat"); // Sunday

        //$d_st_name  = GetStation($StatNm);

        $sql="SELECT * FROM stations WHERE ID = $StatNm LIMIT 1";
        while($rows=mysql_fetch_array($result)){ 
            $d_st_name = $rows['CallSign'];
        }


        if ($d_spec1=="0"){ $d_spec = "-"; }
        else {$d_spec = "<a href=\"javascript:void(window.open('http://xxxxxxx.xxx/xsreq.php?id=$d_id','','width=300,height=250,left=0,top=0,resizable=no,menubar=no,location=no,status=yes,scrollbars=no'))\">YES</a>"; }


        if ($d_bbq1=="0"){ $d_bbq = "-"; }
        else {$d_bbq = "NO"; }  


        if ($d_din1=="0"){ $d_din = "-"; }
        else {$d_din = "NO"; }  


        if ($d_sat1=="0"){ $d_sat = "-"; }
        else {$d_sat = "NO"; }  


        if ($d_sun1=="0"){ $d_sun = "-"; }
        else {$d_sun = "NO"; }  

        echo "<tr><td>$d_id</td><td><strong>$d_name</strong></td><td>$d_st_name</td><td>$d_spec</td><td>$d_bbq</td><td>$d_din</td><td>$d_sat</td><td>$d_sun</td></tr>";

    }
    echo "</tbody></table></br>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.