Bruce Gust
asked on
How can I get these two for loops to talk to one another politely?
Here's the first piece of this puzzle:
When I do that, I get "id column_name column_dat a."
Perfect.
When I run this for loop:
I get this:
screeningperiodid
screeningtime
bfull
...and just so you can visualize the table this is coming from, here's a screen shot:
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?
$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.' ';
}
When I do that, I get "id column_name column_dat
Perfect.
When I run this for loop:
for($i=1; $i<=$selectTermsCount-1; $i++)
{
echo $selectTermsRow[$i]['column_name'].'<br>';
}
I get this:
screeningperiodid
screeningtime
bfull
...and just so you can visualize the table this is coming from, here's a screen shot:
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
array ( 0 => array ( 'id' => '1', 'column_name' => 'screeningsubperiod', 'column_data' => 'screeningsubperiod_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
The following code
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
array (
array(
"id" => 1,
"column_name" => "screeningsubperiodid",
"column_date" => "screeningsubperiod_data"
),
array(
"id" => 2,
"column_name" => "screeningid",
"column_date" => "screening_data"
),
...
)
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/>";
}
Makes use of the foreach option to include the key of the array in the loop - like thisforeach($array as $key => $value)
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_AS SOC) 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.
Example: PDOStatement::fetchAll(PDO
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'
)
)
What does "=>" do?This is an assignment operator used in the context of array keys and values. It's just syntax.
Open in new window