spock9458 Posted August 28, 2013 Share Posted August 28, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/ Share on other sites More sharing options...
fastsol Posted August 28, 2013 Share Posted August 28, 2013 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/#findComment-1447166 Share on other sites More sharing options...
mac_gyver Posted August 28, 2013 Share Posted August 28, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/#findComment-1447167 Share on other sites More sharing options...
spock9458 Posted August 28, 2013 Author Share Posted August 28, 2013 Thanks, that makes sense... and the functions are now working. I wanted to understand that part of it, so that I can work on the logic now without all of the output clutter... I'll see what I can do, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/#findComment-1447182 Share on other sites More sharing options...
spock9458 Posted August 28, 2013 Author Share Posted August 28, 2013 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" loopwhile($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" loopecho "<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. Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/#findComment-1447193 Share on other sites More sharing options...
fastsol Posted August 28, 2013 Share Posted August 28, 2013 We need to see how you are building the $company_array Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/#findComment-1447204 Share on other sites More sharing options...
spock9458 Posted August 28, 2013 Author Share Posted August 28, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/#findComment-1447205 Share on other sites More sharing options...
fastsol Posted August 28, 2013 Share Posted August 28, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/#findComment-1447207 Share on other sites More sharing options...
spock9458 Posted August 28, 2013 Author Share Posted August 28, 2013 That is beautiful... now my new code is working. On to the next step in my project, I appreciate the help to understand how to "load" my array - it makes total sense now. Quote Link to comment https://forums.phpfreaks.com/topic/281639-array-values-not-working-in-function/#findComment-1447211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.