Jump to content

functions calling functions


wrave

Recommended Posts

Immense apologies right up front because I know this topic has no doubt been cussed and discussed but I have attempted to do a search and I keep erroring out. The error page said to contact a moderator but I couldn't find a place to do that. Anyway...

 

I am building some functions that I hope will reduce some of my typing. I have to files with functions in them and they are in my PATH. No problems there. One file contains functions to access my databases (my_db_func.php) and one that contains functions to help me display results (my_disp_func.php).

 

I am attempting to build a generic function to display query results. I have a function that extracts the field names returned from a query. I then take those results and pass them to a function that starts building an HTML table and populates the table header cells with the field names. It doesn't close the HTML table but leaves it open so I can construct the remainder of the table with the results of a query to my database. Using these three functions I can execute a query and get a nice looking table display.

 

So I got to thinking it would take things one step further and eliminate a couple of calls by executing the query once and passing the results to a function I call 'disp_query_results_HTML($data)'. So I attempted to place the calls to the 'extract_fld_names()' and 'disp_fld_names_HTML()' within my query results display function. Everything works beautifully if I make the calls individually but when I place a function call within a function it comes to a halt and refuses to continue with any code after the first function is called.

 

After testing it appears that all of the data from my database query is still there. I read in the manual that PHP allows one function to call another. The display function file has a "require_once" command loading the file that contains the data manipulation functions so I know it is found. The field names are extracted. I don't get any error messages, the top field names part of the display functions correctly, but after the table header is displayed, the remainder of the calling function fails. I just can't seem to figure out why this isn't working.

 

Here's the functions...

 

From my_disp_func.php...

 

[pre]

function disp_query_results_HTML($data) {

 

  $fld_names = extract_fld_names($data);

  disp_fld_names_HTML($fld_names);

 

  $row_count = mysql_num_rows($data);

  $field_count = mysql_num_fields($data);

 

  while ($row = mysql_fetch_array($data, MYSQL_NUM)) {

    echo "<tr>";

for ($i = 0; $i < $field_count; ++$i) {

if ($row[$i] == "") {

  echo "<td> </td>\n";

} else {

  echo "<td>$row[$i]</td>\n";

}

}

echo "</tr>\n";

  }

  echo "</tr></table><br />";

}

 

function disp_fld_names_HTML($header_list, $bgcolor = "#ffffdd") {

 

$flds = count($header_list);

 

echo "<table border=1 bgcolor=\"$bgcolor\" cellpadding=3><tr>";

 

for ($idx = 0; $idx <= $flds-1; ++$idx) {

  echo "<th>$header_list[$idx]</th>\n";

}

echo "</tr>\n";

 

}[/pre]

 

And from my_db_func.php...

 

[pre]

function extract_fld_names($data) {

 

  $count = count("$data");

  $loop_count = 0;

  $list[0]= '';

 

  while ($row = mysql_fetch_array($data, MYSQL_NUM)) {

foreach ($row as $key => $value) {

if ($key == 0) {

  $list[$loop_count] = "$value";

}

}

++$loop_count;

  }

return $list;

}[/pre]

 

I am sure this question has been asked before and again I apologise for my problems in searching the forums.

Link to comment
https://forums.phpfreaks.com/topic/94532-functions-calling-functions/
Share on other sites

You might try changing the order of the functions in the code.  This might make a difference.

 

You essentially have:

 

function disp_query_results_HTML($data) {
//...
disp_fld_names_HTML($fld_names);
//...
}

function disp_fld_names_HTML($header_list, $bgcolor = "#ffffdd") {
/...
}

 

You should try this instead:

 

 

function disp_fld_names_HTML($header_list, $bgcolor = "#ffffdd") {
/...
}

function disp_query_results_HTML($data) {
//...
disp_fld_names_HTML($fld_names);
//...
}

Sorry, I made a mistake in what I wrote earlier. The first two function calls inside disp_query_results_HTML() execute correctly.

 

$fld_names = extract_fld_names($data);

disp_fld_names_HTML($fld_names);

 

But disp_query_results() doesn't continue after the second function returns. Maybe I should try putting the remaining code into yet another function and try calling it?

I don't like unanswered questions so here's what I have been doing...

 

I now have a function that will display any arbitrary query in an HTML table except when all columns are selected in the query by using the asterisk (*). That's a problem for another day.

 

I finally decided to extract the results table header list by extracting the field names from the query itself. So now my solution looks like this...

 

function disp_query_results_HTML($query,$data) {

////////////////////////////////////////
// REMOVE the QUERY tail.             //
////////////////////////////////////////
$list = preg_split("[ FROM.*]", $query);
// show_array($list);

////////////////////////////////////////
// REMOVE the QUERY head.             //
////////////////////////////////////////
$list = preg_split("[^SELECT]", "$list[0]");
// show_array($list);

////////////////////////////////////////
// SPLIT the FIELD NAMES from the re- //
// maining array section.             //
////////////////////////////////////////
$pieces = split(",",(string)$list[1]);
$n = count($pieces);
//echo "Count = $n<br /><br />\n";

echo "<table border=\"1\">\n";
echo "<tr>\n";
for ($i = 0; $i < $n; $i++) {
  echo "<th>$pieces[$i]</th>\n";
}
echo "</tr>\n";

  while ($row = mysql_fetch_array($data, MYSQL_NUM)) {
 echo "<tr>";
 foreach ($row as $i => $value) {
	if ($value == "") {
	  echo "<td> </td>";
	} else {
	  echo "<td>$value</td>";
	}
 }
 echo "</tr>\n";
  }
  echo "</tr></table><br />";

}

 

This works although it bothers me that I still have the problem when all of the fields are selected.

 

I realize this solution isn't very elegant but when you're up to your ass in alligators...

I tried using mysql_field_name() and it works but how can I determine the number of fields returned by the query? When I know the fields I can number the calls to...

 

echo mysql_field_name($r,0);

echo mysql_field_name($r,1);

echo mysql_field_name($r,2);

 

...just like the example in the manual shows. But I want the function to be able to iterate through the query fields and I don't see how to obtain that information.

 

why not just use mysql_fetch_assoc?

 

this way the array keys have the field names

 

 

so a

 

SELECT id,name,phone,zip

 

would return an array like

 

$row = array ('id' => '1', 'name' => 'wrave', 'phone' => '555-111-2222', 'zip' => '12345');

 

 

with a foreach($row as $key => $Val)

 

which can give u the both key index and the value

 

or use

$fields=array_keys($row)

 

if u need to seperate the keys from the values (Displayed as a table header)

 

Thanks laffin,

 

I went back to the manual and found the example for mysql_fetch_field() and here is my code to build my table header...

 

  echo "<table border=1><tr bgcolor=\"#ffeedd\">";

$i = 0;

while ($i < mysql_num_fields($data)) {
    $meta = mysql_fetch_field($data, $i);
    if (!$meta) {
        echo "No information available<br />\n";
    }
    echo "<th> $meta->name </th>";
 $i++;
}

echo "</tr>\n";

 

This handles everything.

 

Thank you all. I didn't ever find an answer to my original question as to why the function execution halted after one of the function calls but I expect there is an error in my original code somewhere.

 

Anyway, I now have a function that will turn a query result into an HTML table and I am quite happy to have solved tis problem. I think I can mark this topic solved.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.