Bikal Basnet
Bikal Basnet

Reputation: 1739

function returning only once, why?

During my coding I really got stuck into this problem. I ran a foreach loop and for every item I had to get a certain value from a function. But I got only one returned. I could not figure out what was happening. I hope you guys surely will. Below is the short version of my program. Database structure is given at last.

<?php


function opendb() {
    mysql_connect("localhost", "root", "root");
    mysql_select_db("something_db");
}

function sql_query($sql) {
    $datas = array();
    if ($res = mysql_query($sql)) {
        $x = 0;

        while ( $data = mysql_fetch_assoc($res) ) {
            $datas[$x] = $data;
            $x += 1;
        }
    }
    return $datas;
}

function get_parent_id($table, $parent, $cid) {

    // cid=>child id
    $sql = "SELECT * FROM $table WHERE id=$cid";
    $datas = sql_query($sql);
    $pid = $datas[0]['parent'];
    $p_id = $datas[0]['id'];
    if ($pid != 0) {
        get_parent_id($table, $parent, $pid);
    } else {

        return $p_id;
    }
}

opendb();

$datas_pkg = sql_query("SELECT * FROM tbl_packages WHERE 1");
foreach ( $datas_pkg as $data_pkg ) {
    echo $data_pkg['destination_id'] . '-->';
    echo $parent_id = get_parent_id('tbl_destinations', 'parent', $data_pkg['destination_id']);
    echo '<br/>';
}
?>

Database structure..

tbl_destinations

+--------+-------------------------+-----------+ 
| id(int)|destination_name(Varchar)|parent(int)| 
+--------+-------------------------+-----------+

tbl_packages

+-------+---------------------+-------------------+
|id(int)|package_name(varchar)|destination_id(int)|
+-------+---------------------+-------------------+

If I did not clear my question please let me know so that I can help you to help me.

Upvotes: 1

Views: 227

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146578

  if($pid!=0)
  {
            get_parent_id($table,$parent,$pid);

  }

You call the function, but never use its value.

Upvotes: 1

Related Questions