Php array from DB Result - php

How can i convert result from Database to somehow multidimensional array?
Example:
Records of users in DB
I tried coding something like this(Not code but illustration):
1 user => John Adams
2 users => Peter Greenman and John Adams
3 users => Sarah Josh,Peter Greenman and John Adams
50 users => Sarah Josh,Peter Greenman and 48 others.
How can i display records with the number of users in the DB just like i stated above
Thanks

declare array outside loop and store database value inside loop
$names = array();
while($stmt->fetch()){$names[]= database row;}
print_r($name);

Related

Outputting each value in array

I have this foreach loop nested inside a while loop:
foreach ($_SESSION['arrayPoints'] as $value) {
echo $value;
}
This outputs the all the values stored in the array, 543216 as shown below, but as the foreach loop is in the while loop, 543216 is repeated until there are no more rows:
I need the foreach loop to output each value in the array separately for each row.
So for example (using the image above and the array values of 543216):
I need the output to show 5 for the first row, then 4 for the second row, 3 for the 3rd row, 2 fourth row and so on (as shown below).
Dave Jackson |TEXT BOX| |DROP-DOWN| 5
Bobby Brown |TEXT BOX| |DROP-DOWN| 4
Daniel Grey |TEXT BOX| |DROP-DOWN| 3
Richard Green |TEXT BOX| |DROP-DOWN| 2
David Bolt |TEXT BOX| |DROP-DOWN| 1
Jason Moore |TEXT BOX| |DROP-DOWN| 6
How can this be done?
EDIT:
print_r($_SESSION['arrayPoints']);
outputs:
Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 [5] => 6 )
-The While loop: while ($data = mysqli_fetch_assoc($result)) { is a query fetch action.
-Each row shown in the image above is in order with each number in the array. So first row = 5 , second row = 4 and so on.
Thanks in advance.
Your current code says that you want to print every number for each data fetched from DB, which is not what you want. You want a single number printed for each data. So forget the foreach. What you need is:
echo $_SESSION['arrayPoints'][$i];
where $i is the current row being fetched (you need to set and increment it).

Loop on Codeigniter

I have a table that contains an unknown number of elements. These represent business sectors.
Array ([0] => 1 [1] => 3 [2] => 6 [3] => 7)
In this case this activity is linked to topics 1, 3, 6 and 7.
I Have some members who have indicated their fields of interest when they register. Interest fields correspond to sectors of activities.
When a new activity is announced, I would like to send an email to members who have checked at least one common interest with the activity. But I have no idea how to retrieve the email of all those members().
In the database the interests of members is stored like this: 246
The first member would be interested in sectors 2, 4 and 6.
I know how to do if there is only one business sector for the activity (example if value = 1).
function readMemberInterest($value) {
$datas = $this->db->select("email")
->from($this->table)
->like('interet',$value,'both');
return $datas;
}
But I do not know where and how to place my LOOP and my OR (is for example value = Array ([0] => 1 1 => 3 [2] => 6 [3] => 7) ).
Briefly, I want to retrieve email members who have at least one common interest with activity. Any tips?
I hope to be clear, my English is not so great ...
Thanks

how to store data in associative array in php

here is my mysql table data:
snapshot is this
responsible_party_name delayedtasks
BJ Hansen 40
Chris Wolstenholme 14
Bob Strobel 8
Tim Faltemier 8
Carli Lyon 6
Mary Lynn Wilmore 6
I have store data in associative array format: I have to store data like this in key value form how could do that. Here is my array snapshot:
Array
(
[0] => Chris Wolstenholme
[responsible_party_name] => Chris Wolstenholme
[1] => 14
[assigned_todo_count] => 14
)
i want like : in key value pair
['chris']=>30
['ankit']=>20
here is the snapshot of associative array in php , how could put data in key value pair in php.

CakePHP HABTM find ... not returning expected results

I'm hoping somebody can clear up this issue I am having. None of the other answers on SO seemed to help me out for some reason.
I have 2 tables with a HABTM relationship. Publications have many authors, and authors have many publications. In my case, I am attempting to output a list of all of the publications in the database, along with their corresponding authors.
I have the following tables:
publications:
id title
0 TestPublication
authors:
id firstname lastname middle
0 John Doe A
authors_publications:
id author_id publication_id
0 0 0
The 'id' column of each table is set as the primary key.
My Publication model looks like:
class Publication extends AppModel {
var $name = 'Publication';
var $hasAndBelongsToMany = array(
'Author'=>array(
'className'=>'Author'
)
);
}
And finally, the PublicationsController has the following function:
function index() {
$publications = $this->Publication->find('all');
$this->set('publications', $publications);
}
Here is what publications now contains:
Array ( [0] => Array ( [Publication] => Array ( [id] => 0 [title] => TestPublica [type_id] => 1 ) [Author] => Array ( ) ) )
Why is this? I am expecting (perhaps that is my problem...) that the author John Doe should be present in the Author array. If it should be, where am I going wrong? Do I need a bindModel call somewhere in there?
Or...is the code actually executing the way it should and my expectations are incorrect? If so, how would I return a list of all of the publications along with all of their authors?
Thanks for your time!
I believe I found the problem. I was manually inserting records into the DB for testing purposes. I set the primary key id's on the first record of each table to zero. SQL DBs typically (or always?) start the first record with a primary key index of 1, not 0. I'm not sure WHY cake did not work with this, but changing the id's to 1 solved the problem. Weird...

PHP/MySQL - Analyzing common sets across multiple sets

Let's say I have two tables, people and families.
families has two fields - id and name. The name field contains the family surname.
people has three fields - id, family_id and name - The family_id is the id of the family that that person belongs to. The name field is that person's first name.
It's basically a one to many relationship with one family having many people.
I want to get a lists of name sets, ordered by the highest occurrence of the largest set of names across families.
That probably doesn't make much sense...
To explain what I want further, we can score each set of names. The 'score' is the array size * number of occurrences across families.
For example, let's say two names, 'John' and 'Jane' both existed in three families - That set's 'score' would be 2*3 = 6.
How could I get an array of sets of names, and the set's 'score', ordered by each set's score?
Sample Result Set (I've put it in a table layout, but this could be a multi-dimensional array in PHP) - Note this is just randomly thought up and doesn't reflect any statistical name data.
names | occurrences | score
Ben, Lucy | 4 | 8
Jane, John | 3 | 6
James, Rosie, Jack | 2 | 6
Charlie, Jane | 2 | 4
Just to clarify, I'm not interested in sets where:
The number of occurrences is 1 (obviously, just one family).
The set size is 1 (just a common name).
I hope I have explained my somewhat complex problem - if anyone needs clarification please say.
OK, got it:
<?php
require_once('query.lib.php');
$db=new database(DB_TYPE,DB_HOST,DB_USER,DB_PASS,DB_MISC);
$qry=new query('set names utf8',$db);
//Base query, this filters out names that are in just one family
$sql='select name, cast(group_concat(family order by family) as char) as famlist, count(*) as num from people group by name having num>0 order by num desc';
$qry=new query($sql,$db);
//$qry->result is something like
/*
Array
(
[name] => Array
(
[0] => cathy
[1] => george
[2] => jack
[3] => john
[4] => jane
[5] => winston
[6] => peter
)
[famlist] => Array
(
[0] => 2,4,5,6,8
[1] => 2,3,4,5,8
[2] => 1,3,5,7,8
[3] => 1,2,3,6,7
[4] => 2,4,7,8
[5] => 1,2,6,8
[6] => 1,3,6
)
[num] => Array
(
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => 4
[5] => 4
[6] => 3
)
)
$qry->rows=7
*/
//Initialize
$names=$qry->result['name'];
$rows=$qry->rows;
$lists=array();
for ($i=0;$i<$rows;$i++) $lists[$i]=explode(',',$qry->result['famlist'][$i]);
//Walk the list and populate pairs - this filters out pairs, that are specific to only one family
$tuples=array();
for ($i=0;$i<$rows;$i++) {
for ($j=$i+1;$j<$rows;$j++) {
$isec=array_intersect($lists[$i],$lists[$j]);
if (sizeof($isec)>1) {
//Every tuple consists of the name-list, the family list, the length and the latest used name
$tuples[]=array($names[$i].'/'.$names[$j],$isec,2,$j);
}
}
}
//Now walk the tuples again rolling forward, until there is nothing left to do
//We do not use a for loop just for style
$i=0;
while ($i<sizeof($tuples)) {
$tuple=$tuples[$i];
//Try to combine this tuple with all later names
for ($j=$tuple[3]+1;$j<$rows;$j++) {
$isec=array_intersect($tuple[1],$lists[$j]);
if (sizeof($isec)>0) $tuples[]=array($tuple[0].'/'.$names[$j],$isec,$tuple[2]+1,$j);
}
$i++;
}
//We have all the tuples, now we just need to extract the info and prepare to sort - some dirty trick here!
$final=array();
while (sizeof($tuples)>0) {
$tuple=array_pop($tuples);
//name list is in $tuple[0]
$list=$tuple[0];
//count is sizeof($tuple[1])
$count=sizeof($tuple[1]);
//length is in $tuple[2]
$final[]=$tuple[2]*$count."\t$count\t$list";
}
//Sorting and output is all that is left
rsort($final);
print_r($final);
?>
I am sorry I just realized I use a query lib that I can't source in here, but from the comment you will easily be able to create the arrays as in the section "Initialize".
Basically what I do is starting with the pairs I keep an array of the families all the names in the current name list belong to, then intersect it with all not-yet tried names.
Will this work?
SELECT
f.name AS 'surname',
GROUP_CONCAT(DISTINCT p.name ORDER BY p.name) AS 'names',
COUNT(DISTINCT p.name) AS 'distinct_names',
COUNT(p.id) AS 'occurrences',
COUNT(DISTINCT p.name) * COUNT(p.id) AS 'score'
FROM
families f
LEFT JOIN people p ON ( f.id = p.family_id )
GROUP BY
f.id
ORDER BY
f.name

Categories