Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How can I get these two for loops to talk to one another politely?

Here's the first piece of this puzzle:

$selectTerms=$conn->prepare("SELECT * FROM test_date");
		$selectTerms->execute();
		$selectTermsCount=$selectTerms->rowCount();

		$selectTermsRow=$selectTerms->fetchAll(PDO::FETCH_ASSOC);

		foreach($selectTermsRow as $column)
		{
			$column_name=array_keys($column);
		}

               foreach($column_name as $name)
		{
		 echo $name.' ';
		}

Open in new window


When I do that, I get "id column_name column_data."

Perfect.

When I run this for loop:

for($i=1; $i<=$selectTermsCount-1; $i++)
			{
				echo $selectTermsRow[$i]['column_name'].'<br>';
			}

Open in new window


I get this:

screeningperiodid
screeningtime
bfull

...and just so you can visualize the table this is coming from, here's a screen shot:

User generated image
I want to combine these two loops in a way where I would get this:

id: 1
column_name: screeningsubperiodid
column_date: screeningsubperiod_data

In other words, I get the column name followed by the value.

How?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I'm not sure I completely understand the objective, so let's work through this a little bit.  From the code snippet after the SELECT query, please do this and post the output in the code snippet here.
$selectTermsRow=$selectTerms->fetchAll(PDO::FETCH_ASSOC);
var_export($selectTermsRow);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Ray, here's the var_export:

array ( 0 => array ( 'id' => '1', 'column_name' => 'screeningsubperiod', 'column_data' => 'screeningsubperiod_data', ), 1 => array ( 'id' => '2', 'column_name' => 'screeningperiodid', 'column_data' => 'screeningperiodid_data', ), 2 => array ( 'id' => '3', 'column_name' => 'screeningtime', 'column_data' => 'screeningtime_data', ), 3 => array ( 'id' => '4', 'column_name' => 'bfull', 'column_data' => 'bfull_data', ), )

Julian, your solution works. Why? What does "=>" do? How does that syntax parse out the column headings and line things up the way I was hoping?

I had tried various loops within loops and I was getting "id" and then all four rows worth of info and then "column_name" and then all four rows worth of info. It was a mess.

How does your code make the magic happen?
Given a return of
array (
array(
  "id" => 1, 
  "column_name" => "screeningsubperiodid",
  "column_date" => "screeningsubperiod_data"
  ),
array(
  "id" => 2, 
  "column_name" => "screeningid",
  "column_date" => "screening_data"
  ),
...
)

Open in new window

The subarrays represent the rows retrieved from the fetch_all. They are associative so they have the column names as the keys.
The following code
foreach($row as $column=> $value) {
  echo "{$column} : $value<br/>";
}

Open in new window

Makes use of the foreach option to include the key of the array in the loop - like this
foreach($array as $key => $value)

Open in new window


You wanted to have each row dump as

Column Name: value

Given each field in the row has the column name as the key we can just pull that out in the loop and use it
All of the PHP functions are documented in the online man pages, so when you see something unfamiliar, the best first step is to look it up.  

Example: PDOStatement::fetchAll(PDO::FETCH_ASSOC) tells us that it returns an array containing all of the result set rows.  Given the PDO::FETCH_ASSOC flags, the method will make each of the rows into an associative array.  Thus you have a return value from the method that is a numerically indexed array of arrays, with each of the inner arrays representing one row of the results set, like this.
array 
( 0 => array 
       ( 'id' => '1'
       , 'column_name' => 'screeningsubperiod'
       , 'column_data' => 'screeningsubperiod_data'
       )
, 1 => array 
       ( 'id' => '2'
       , 'column_name' => 'screeningperiodid'
       , 'column_data' => 'screeningperiodid_data'
       )
, 2 => array 
       ( 'id' => '3'
       , 'column_name' => 'screeningtime'
       , 'column_data' => 'screeningtime_data'
       )
, 3 => array 
       ( 'id' => '4'
       , 'column_name' => 'bfull'
       , 'column_data' => 'bfull_data'
       )
)

Open in new window

What does "=>" do?
This is an assignment operator used in the context of array keys and values.  It's just syntax.