NotSureILikePHP Posted July 28, 2015 Share Posted July 28, 2015 I'm trying to add multiple columns to an array. To simplify I'll just say id and name for now. How do I add and call that in the code. I am an asp developer, though it's been a while, so in asp it's something like array[0],["columnName'] Though in php that dot means to concat. Here's my code. $int = 0; if (($res=db_query('SELECT id, name FROM location')) && db_num_rows($res)) { while(list($loc)=db_fetch_row($res)) { $columns = array ( 'locID' => $loc['location_id'], 'locName' => $loc['site_name'], ); array_push($locs[$int], $columns); $int++; } } [/] I'm not sure if that's working or not because I don't know how to get values from the index of the array in php. Most of the code I google seems like an array can only store one value per index. Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/ Share on other sites More sharing options...
cyberRobot Posted July 28, 2015 Share Posted July 28, 2015 You should be able to access the information using $locs[0]['locID'] Quick question, is $locs just an empty array? If so, you probably don't need $int. You should be able to change this array_push($locs[$int], $columns); To this $locs[] = $columns; Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517582 Share on other sites More sharing options...
NotSureILikePHP Posted July 28, 2015 Author Share Posted July 28, 2015 It is an empty array I'll try that Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517584 Share on other sites More sharing options...
cyberRobot Posted July 28, 2015 Share Posted July 28, 2015 Once you know the array is working, you can use foreach to loop through the array. Note that the following page has an example that uses a foreach loop with a multidimensional associative array: http://php.net/manual/en/control-structures.foreach.php The example is in the 5th white box and starts with the following comment: /* foreach example 3: key and value */ Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517585 Share on other sites More sharing options...
NotSureILikePHP Posted July 28, 2015 Author Share Posted July 28, 2015 That didn't work. It's being called from a different page so I'll include all the code. customers.inc.php $locations = Cust::getLocations($customerID);echo 'locations = '.$locations[0]['location_id']; [/] class.customer.php [code] function getLocations($custID) { $locs = array(); $sql='SELECT DISTINCT l.location_id, l.site_name FROM ' //l.location_id, l.site_name, CONCAT(l.fname, " ", l.lname) AS contactName, email, ' //.'l.phone_number, l.fax_number, c.cust_id, l.address1, l.address2, l.city, ' //.'l.state, l.zip_code, l.contact_number FROM ' .CUST_TABLE.' c ' .' LEFT JOIN '.CUST_LOCATION_TABLE.' l ON (l.cust_id=c.cust_id) ' //.' INNER JOIN '.DEPT_TABLE.' d ON (LOCATE(CONCAT("/", s.dept_id, "/"), d.path) OR d.manager_id=s.staff_id OR LOCATE(CONCAT("/", g.dept_id, "/"), d.path)) ' .' WHERE c.cust_id='.$custID; //.db_input($this->getId()); if (($res=db_query($sql)) && db_num_rows($res)) { while(list($loc)=db_fetch_row($res)) { $columns = array ( 'locID' => $loc['location_id'], 'locName' => $loc['site_name'], ); $locs[] = $columns; } } return $locs; } [/] result is locations = I commented out a lot of that select just to limit it to two columns for now. Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517586 Share on other sites More sharing options...
cyberRobot Posted July 28, 2015 Share Posted July 28, 2015 Is PHP set to show all errors and warnings? Note that you can add the following lines to the top of your script during the debugging process: error_reporting(E_ALL); ini_set('display_errors', 1); Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517587 Share on other sites More sharing options...
cyberRobot Posted July 28, 2015 Share Posted July 28, 2015 Also, do you know if the query is returning any results? I'm not familiar with functions like db_query(), but I would imagine that you could try displaying the result from db_num_rows($res) to find out how many rows were returned. Another thing you could check is if SQL is flagging any errors. You could probably use db_error() for that... Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517590 Share on other sites More sharing options...
NotSureILikePHP Posted July 28, 2015 Author Share Posted July 28, 2015 It is returning resuls, The code was: function getLocations($custID) { $locs = array(); $sql='SELECT DISTINCT l.location_id FROM ' //l.location_id, l.site_name, CONCAT(l.fname, " ", l.lname) AS contactName, email, ' //.'l.phone_number, l.fax_number, c.cust_id, l.address1, l.address2, l.city, ' //.'l.state, l.zip_code, l.contact_number FROM ' .CUST_TABLE.' c ' .' LEFT JOIN '.CUST_LOCATION_TABLE.' l ON (l.cust_id=c.cust_id) ' //.' INNER JOIN '.DEPT_TABLE.' d ON (LOCATE(CONCAT("/", s.dept_id, "/"), d.path) OR d.manager_id=s.staff_id OR LOCATE(CONCAT("/", g.dept_id, "/"), d.path)) ' .' WHERE c.cust_id='.$custID; //.db_input($this->getId()); if (($res=db_query($sql)) && db_num_rows($res)) { while(list($id)=db_fetch_row($res)) { $locs[] = $id; } } return $locs; }[/] When it's like this I can echo out the array which is just the id. However, when I try to add multiple columns to the array I can't echo out anything. Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517591 Share on other sites More sharing options...
cyberRobot Posted July 28, 2015 Share Posted July 28, 2015 When it's like this I can echo out the array which is just the id. However, when I try to add multiple columns to the array I can't echo out anything. Could you show the code where you attempt to echo out the column information? Note that you can quickly see what an array contains using the following line of code: echo '<pre>' . print_r($locs, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517594 Share on other sites More sharing options...
cyberRobot Posted July 28, 2015 Share Posted July 28, 2015 Sorry, I just noticed that you provided the echo statement already. Try changing this echo 'locations = '.$locations[0]['location_id']; To this: echo 'locations = '.$locations[0]['locID']; Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517595 Share on other sites More sharing options...
NotSureILikePHP Posted July 28, 2015 Author Share Posted July 28, 2015 Here's where I echo it out. It's on another php page $locations = Cust::getLocations(116); //This calls the function in the comments aboveecho 'locations = '.$locations[0]['location_id'];[/] Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517597 Share on other sites More sharing options...
cyberRobot Posted July 28, 2015 Share Posted July 28, 2015 Here's where I echo it out. It's on another php page Yay, sorry I didn't notice that you posted the echo statement already. Did you see response #10? Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517598 Share on other sites More sharing options...
NotSureILikePHP Posted July 28, 2015 Author Share Posted July 28, 2015 That did it, it's always something simple. Though it's not returning the data that I want and I think that db_query is doing something weird. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517599 Share on other sites More sharing options...
cyberRobot Posted July 28, 2015 Share Posted July 28, 2015 No problem; glad to help! Quote Link to comment https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/#findComment-1517601 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.