I am trying to fetch one array from an Oracle database and another from a MySQL database.
But the arrays I get back are in different formats, and I am not able to display them in an HTML table.
$result = $instance->prepare("SELECT * from dd ");
$result->execute();
while ($row = $result->fetchAll(PDO::FETCH_ASSOC))
{
$rows2 = $row;
}
$array = oci_parse($conn, "SELECT * FROM DD");
oci_execute($array);
while($row=oci_fetch_array($array, OCI_ASSOC))
{
$rows1 =$row;
}
print_r($rows1);
print_r($rows2);
$output = array_merge($rows1,$rows2);
print_r($output);
Here $instance and $conn are the respective connection variables.
The outputs I get after printing the two arrays are:
Array from Oracle:
Array ( [CLIENTID] => 2 [CLIENTNAME] => dsd [PHONENO] => 53556535 [ADDRESS] => Mumbai [ROUTE] => 4323 )
Array from MySQL:
Array ( [0] => Array ( [ClientId] => 3 [ClientName] => PQR [PhoneNo] => 786483744 [Address] => Pune [Route] => 3329 ) )
And after merging I get this array:
Array ( [CLIENTID] => 2 [CLIENTNAME] => dsd [PHONENO] => 53556535 [ADDRESS] => Mumbai [ROUTE] => 4323 [0] => Array ( [ClientId] => 3 [ClientName] => PQR [PhoneNo] => 786483744 [Address] => Pune [Route] => 3329 ) )
How do I print this in a table in HTML format?
You can not just merge two arrays in this case.try code below;
$all = array();
$all[] = $rows1;
foreach($rows2 as $k2=>$v2) {
$new=array();
$new['CLIENTID'] = $v2['ClientId'];
$new['CLIENTNAME'] = $v2['ClientName'];
$new['PHONENO'] = $v2['PhoneNo'];
$new['ADDRESS'] = $v2['Address'];
$new['ROUTE'] = $v2['Route'];
$all[]=$new;
}
$str='<table><tr><th>CLIENTID</th><th>CLIENTNAME</th><th>PHONENO</th><th>ADDRESS</th><th>ROUTE</th></tr>';
foreach($all as $arr) {
$str.='<tr><td>'.$arr['CLIENTID'].'</td><td>'.$arr['CLIENTNAME'].'</td><td>'.$arr['PHONENO'].'</td><td>'.$arr['ADDRESS'].'</td><td>'.$arr['ROUTE'].'</td>';
}
$str.='</table>';
echo $str;
If $rows1 is an array-array like $rows2, you should iterate same loop for $rows1 also
Related
I have a question about my array. How can I get only data of milestone in my milestone array?
I have a query which is a prefixed array. I get data of milestones and milestonefases (this is milestone parts).
This is my code:
$stones_fases = array();
while ($row = $db->fetchassoc($result)){
$milestonefase = array();
$milestone = array();
foreach ($row as $mkey => $mvalue){
$milestone[$mkey] = $mvalue;
foreach ($row as $fkey => $fvalue){
$milestonefase[$fkey] = $fvalue;
}
}
if (!isset($stones_fases[$milestone['milestone_id']])){
$stones_fases[$milestone['milestone_id']] = $milestone; //['client']['milestone_verkocht_id']
}
$stones_fases[$milestone['milestone_id']][$milestonefase['milestonefase_id']] = $milestonefase['milestonefase_titel'];
}
I get this:
Array
(
[int] => Array
(
[milestone_id] => int
[milestone_titel] => string
[client] => string
[milestone_verkocht_id] => 99
[milestonefase_id] => 10
[milestonefase_titel] => string
[milestonefase_milestone_id] => 6
[10] => string
[11] => string
)
)
But I want this:
Array
(
[int] => Array
(
[milestone_titel] => string
[client] => string
[milestone_verkocht_id] => int
[10] => string
[11] => string
)
)
My query is this:
$project = $_COOKIE['project'];
$query = " SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '{$project}' ";
$result= $db->query($dbh, $query);
I solved my own problem!
If i do this:
while ($row = $db->fetchassoc($result)){
$stones_fases[$row['milestone_id']]['milestone_titel'] = $row['milestone_titel'];
$stones_fases[$row['milestone_id']]['milestone_client'] = $row['client'];
$stones_fases[$row['milestone_id']]['milestonesfases'][$row['milestonefase_id']] = $row['milestonefase_titel'];
}
I get what i expected:
Array
(
[2] => Array
(
[milestone_titel] => Beheer opleveren
[milestone_client] => stackoverflow
[milestonesfases] => Array
(
[1] => Menu bouwen
[2] => Pagina beheer CMS
[3] => Projecten CMS
[4] => Portfolio
[5] => Footer inbouwen
)
)
)
Look at that bunch of code what I wrote a day ago.
Do you see that nonsens of code what I wrote?
Take a look at my code which I wrote 1 minute ago!
I was struggling this for 3 days and now I solved my own problem!
I need to make my array better.
I am getting data from database and i have milestones and milestone_parts. i want two-dimensional array. I need data of milestones in the first dimension and milestone_parts in the second dimension.
With this code:
$query = "
SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`,
b.omschrijving AS `milestonefase_omschrijving`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '99'
";
$result= $db->query($dbh, $query);
while ($row = $db->fetchassoc($result))
{
$stone = array($row['milestone_verkocht_id'], $row['milestone_id'], $row['milestone_titel'], $row['client']);
$fase = array($row['milestonefase_milestone_id'],$row['milestonefase_id'],$row['milestonefase_titel']);
$stone[] = $fase;
echo '<pre>'; print_r($stone); echo '</pre>';
}
I get this as result
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 10
[2] => string
)
)
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 11
[2] => string
)
)
but I need (with names) this:
Array
(
[milestone_verkocht_id] => 99 // This is project id
[milestone_id] => 6
[milestone_title] => string
[client] => string
[10] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 10
[milestone_title] => string
)
[11] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 11
[milestone_title] => string
)
[12] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 12
[milestone_title] => string
)
)
Can you help me or do you have a solution? Help me please!
you can cycle each field returned by the query, checking the field name and making new arrays
$stones = array();
while ($row = $db->fetchassoc($result)) {
$fase = array();
$stone = array('milestones' => array());
foreach ($row as $k => $v) {
if (strpos($k, 'milestonefase_') === 0) {
$fase[$k] = $v;
} else {
$stone[$k] = $v;
}
}
if(!isset($stones[$stone['milestone_id']])) {
$stones[$stone['milestone_id']] = $stone;
}
$stones[$stone['milestone_id']]['milestones'][$fase['milestonefase_id']] = $fase;
}
echo '<pre>'.print_r($stones, true).'</pre>';
Edit
made some changes in order to match the request. Now we use $stones to store the information we already have on a milestone, adding to it the different "$fase" returned from the query
Probably a more clean way is to retrieve all the information with two different queries one for milestones and the other for the fases
Edit2
Added a sub-array for the milestone fases
This is probably a stupidly easy question, but I'm suffering from brainfade ... I'm using a SELECT statement to pull data from a MySQL Database then looping through it with a WHILE loop to push the returned data to the array '$arr', using the code below;
$query="SELECT idBaptism as id, baptismDate as eventDate, concat(baptismForename,' ',baptismSurname) as name, churchName, tbLocation.idLocation as locationid, location, clat as lat, clng as lng FROM tbBaptism, tbLocation, tbChurch WHERE tbBaptism.idLocation=tbLocation.idLocation AND tbBaptism.idChurch=tbChurch.idChurch";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[] = $row;
}
}
My question is I want to append the text 'Baptism' onto the end of each row of the array, what's the easiest way to do this??
As an example, here is the sample output of two rows of the array produced from the code above using print_r;
Array ( [0] => Array ( [id] => 1 [eventDate] => 1874-03-08 [name] => Henry Stanley [churchName] => St. Leonards [locationid] => 28 [location] => Halwell, Devon, UK [lat] => 50.366001 [lng] => -3.720500 )
[1] => Array ( [id] => 2 [eventDate] => 1870-11-06 [name] => Valentine Joles [churchName] => St. John The Evangelist [locationid] => 27 [location] => East Horrington, Somerset, UK [lat] => 51.218143 [lng] => -2.600683 )
I want to add value pair on the end of each row, such as [type] => 'Baptism' ...
Simply push the new element to the fetched array $row and then push it to the array $arr, like this:
// your code
$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
$row['type'] = 'Baptism';
$arr[] = $row;
}
}
I am trying to get values from MySQL array as alphabetically, and I want results like:
[B] => Array (
[id] => 6
[firstname] => Bon
[lastname] => Jone
),
Array (
[id] => 7
[firstname] => bon
[lastname] => doe
)
[H] => Array
(
[id] => 1
[firstname] => Hassan
[lastname] => Ilyas
)
[J] => Array
(
[id] => 5
[firstname] => John
[lastname] => Doe
)
Here is the code of what I have tried.
edited
$result = mysqli_query($GLOBALS['conx'],"SELECT * FROM $users_table ORDER BY firstname ASC");
while ($row = mysqli_fetch_assoc($result)) {
extract($row);
$row['alphabets'] = ucfirst($firstname[0]);
$data[] = $row;
}
But it outputs like this:
[0] => Array
(
[id] => 6
[firstname] => Bon
[lastname] => Jone
)
[1] => Array
(
[id] => 7
[firstname] => bon
[lastname] => doe
)
[2] => Array
(
[id] => 1
[firstname] => Hassan
[lastname] => Ilyas
)
[3] => Array
(
[id] => 5
[firstname] => John
[lastname] => Doe
)
How can I get output like this:
A
Adam Smith
Alan smith
B
Bone Doe
Bone Joe
J
John Smith
.... etc
ucfirst returns the entire string with the first letter capitalised, you'll need to grab just the first letter with substr first.
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
extract($row);
$key = strtoupper($firstname[0]))
$data[$key][] = $row;
}
To get the output (roughly) like you want, try something like:
foreach ($data as $letter => $rows)
{
echo "<h1>{$letter}</h1>", PHP_EOL;
foreach ($rows as $row)
{
echo "<p>{$row['firstname']} {$row['lastname']}</p>", PHP_EOL;
}
}
Your attempt will not do what you want. To achieve this, you will have to create a nested array, then on each iteration check the first letter of the firstname. Check if that letter already exists as a key in the array, and create an item in your array with an empty array as its value, it if it doesn't, and then add the row data to it to that newly created empty array.
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
$firstLetter = strtoupper($row["firstname"][0]); // convert to upper case so all are the same
if (array_key_exists($firstLetter, $data) === false) $data[$firstLetter] = [];
// now just add the row data:
$data[$firstLetter][] = $row;
}
First use Order in your MySQL Query: ... ORDER BY firstname ASC
Create Arrays for Alphabets using PHP:
$Alphabetic_Array = array();
while ($row = mysqli_fetch_assoc($result)) {
$Alphabetic_Array[$row['firstname'][0]][] = $row;
}
First of all, I suggest you to sort records in your query, otherwise you have to sort resulting array before grouping it:
SELECT * from tablename
ORDER BY firstname ASC, lastname ASC
Then, you can use array_map to group your array:
$result = array();
array_map
(
function( $row ) use( &$result )
{
$result[strtoupper(substr($row['firstname'],0,1))][] = $row;
},
$data
);
(Note that we have to call $result by reference)
Now the $result is an array like this:
Array
(
[B] => Array
(
[0] => Array
(
[id] => 7
[firstname] => bon
[lastname] => doe
)
[1] => Array
(
[id] => 6
[firstname] => Bon
[lastname] => Jone
)
)
[H] => Array
(
[0] => Array
(
[id] => 1
[firstname] => Hassan
[lastname] => Ilyas
)
)
[J] => Array
(
[0] => Array
(
[id] => 5
[firstname] => John
[lastname] => Doe
)
)
)
If you want/can use PDO instead of mysqli_, you can obtain desired result directly from MySQL query. Set your query in this way:
SELECT UPPER(SUBSTR(firstname,1,1)) as initial, tablename.* from tablename
ORDER BY firstname ASC, lastname ASC
and then, fetching in in this way:
$data->fetchAll( PDO::FETCH_ASSOC|PDO::FETCH_GROUP );
your $data will contain desired array, without need of additional processing.
The PDO::FETCH_GROUP option groups fetched results by first column returned by query, in your case the capitalized firstname first letter.
What about letting MYSQL do the logic?
SELECT * FROM users_table, SUBSTRING(firstName, 1, 1) as initial ORDER BY firstname ASC
Then you just can append each line to an array
while ($row = mysqli_fetch_assoc($result)) {
$Alphabetic_Array[$row['initial'] = $row;
}
It's just pseudo code but I think the idea is pretty clear
I have a php /mysqli query and I want to populate an array with the results:
$query3 ="SELECT * FROM conditions";
$results = array();
if ($result = mysqli_query($conn, $query3)){
while($row = mysqli_fetch_assoc($result))
{
$results[] = $row;
}
}
print_r($results);
Something is wrong here -its making arrays within arrays I think. (to be honest I am confused by this result)
How do I do this correctly!
Array (
[0] => Array ( [condition_id] => 1 [condition_name] => Epilepsy )
[1] => Array ( [condition_id] => 2 [condition_name] => ASD )
[2] => Array ( [condition_id] => 3 [condition_name] => BESD )
[3] => Array ( [condition_id] => 4 [condition_name] => HI )
[4] => Array ( [condition_id] => 5 [condition_name] => Medical )
[5] => Array ( ...
Thanks for all the help - now how should I create what I actually want which is one array with key=>value like this:
array (1=>epilepsy, 2=>ASd...) - the numbers refer to the primary key.
How do I populate an array from this query please?
Change your code as below :
while($row = mysqli_fetch_assoc($result))
{
$results[$row['condition_id']] = $row['condition_name'];
}
Move to PDO, Luke.
$results = $pdo->query("SELECT FROM conditions")->fetchAll(PDO::FETCH_KEY_PAIR);
print_r($results);
Whoops! Is that all the code?