Jump to content

Add and call items from an array


NotSureILikePHP

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/297524-add-and-call-items-from-an-array/
Share on other sites

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 */

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.

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

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.

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>';

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.