Jump to content

Recommended Posts

This is a specific question about passing array values in functions, and I'm either not doing it right or it can't be done.  My project involves a membership directory listing which has company information and contact people at each company, and there is more to do, but I want to understand the concept of using functions for my repetitive code, so I need to know why what I am trying does not work.  First I started with a working sample that can be seen here: http://www.nmlta.org/NewMemDir/testjoin.php

 

All I did was take the code that lists the items in my arrays and created two functions: listCompany(), and listContact(), and I call the functions where I think they need to go, but here is what the result is so far: http://www.nmlta.org/NewMemDir/testjoin3.php

 

I am attaching both codes for anyone to review, minus my connection info.  I am really interested in figuring this out, so I appreciate your help.  Thanks.

testjoin.php

testjoin3.php

Link to comment
https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/
Share on other sites

You need to send the $row array into the function to be able to access the array in the function.

Change the lines in your code to these.

function listCompany($row)
function listContact($row)

echo "<tr><td>";
    
    listCompany($row);
    }
	// This is the end of the Company info
	
    listContact($row);
    }
	echo "</td></tr>";

php, correctly, has local variable scope in functions. this is so that the only affect a function will have on the calling code is at the point where it is called. this also helps with the design of a function, since you must define what inputs it needs, so that you can define a call time parameter for each input.

 

i recommend that you get your overall logic to work first. then you will be able to see what repetitive code there is in it that would benefit by using a function.

So, based on suggestions in another post, I have adjusted my working code to try and hold my results in a $company_array, and test the listing using a foreach loop instead of a while loop, so I can understand this process.  Here are the "before" (working) and "after" (not working) sections of code:

 

Before:

// Start building the table for showing results using "while" loop
$lastCompany = '';
echo "<table border='1' cellpadding='10' cellspacing='0' bgcolor='#f0f0f0' style='margin-left:100px; margin-bottom:20px'>";
// Here is the "while" loop
while($row = mysql_fetch_array( $result )) {
    if ($lastCompany != $row['comp_name']) {
    // OK we have a new Company to output
    $lastCompany = $row['comp_name']; # Track the "last" company we processed
    echo "<tr><td>";
    
    listCompany($row);
    }
    // This is the end of the Company info
    
    listContact($row);
    }
    echo "</td></tr>";
    
echo "</table>";

 

And here is the "After":

$company_array = mysql_fetch_array($result);

// Start building the table for showing results using "while" loop
echo "<table border='1' cellpadding='10' cellspacing='0' bgcolor='#f0f0f0' style='margin-left:100px; margin-bottom:20px'>";
// Here is the "while" loop
$lastCompany = '';
foreach($company_array as $row) {
    if ($lastCompany != $row['comp_name']) {
    // OK we have a new Company to output
    $lastCompany = $row['comp_name'];
    
    echo "<tr><td><p><b>";
    
    listCompany($row);
    }
    
    listContact($row);
    
    echo "</td></tr>";}
    
echo "</table>";

 

I don't understand the results of this, which can be viewed here: http://www.nmlta.org/NewMemDir/testlist.php

 

Please let me know what I've done wrong... thanks.
 

OK, I gave this my best shot... here are the three lines of code that I thought would do that:

 

// Retrieve all the data from the "company" table
$query = "SELECT * FROM company LEFT JOIN contact ON company.company_id = contact.company_id WHERE comp_county = 'BERNALILLO' ORDER BY company.comp_name, contact.cont_rank";

$result = mysql_query($query)
or die(mysql_error());

$company_array = mysql_fetch_array($result);
 

Prior to changing the code, my loop was:  while($row = mysql_fetch_array( $result )) {

My goal is to store the query result in the $company_array placeholder for handling by the code that will come later... but I'm obviously not doing it quite right... please let me know what I need to do.  Thanks.

You would need to use the while loop to gather all the results from the query, the way you have it it only gets the first row.

$company_array = array();
while($row = mysql_fetch_assoc( $result )) {
  $company_array[] = $row;
}

I also prefer using mysql_fetch_assoc instead of mysql_fetch_array cause you don't get twice the array size and the array provided is keyed by the row name.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.