To explain the scenario:
I have this table in MySQL DB
+-------------------------------------------------------------------------+
| message_id | client_id | admin_id | message | date_posted | read_status |
+-------------------------------------------------------------------------+
I am selecting all the messages and grouping them in a 2D Array for the same client so that the result migh look like
$messages = array(
"4" => array ("338", "4", "1", "message1", "20170904 120803", "0"),
"5" => array ("339", "5", "1", "message2", "20170904 120807","0")
);
The result I'm getting is similar but each value in the array is duplicated
array (size=12)
'message_id' => string '22' (length=2)
0 => string '22' (length=2)
'client_id' => string '14' (length=2)
1 => string '14' (length=2)
'admin_id' => string '1' (length=1)
2 => string '1' (length=1)
'message' => string 'hii I'm new to this' (length=19)
3 => string 'hii I'm new to this' (length=19)
'date_posted' => string '2017-04-22 17:17:13' (length=19)
4 => string '2017-04-22 17:17:13' (length=19)
'read_status' => string '0' (length=1)
5 => string '0' (length=1)
This is my query
$grouped_messages = array();
foreach ($connect->query("SELECT DISTINCT * FROM request ORDER BY client_id") as $row) {
var_dump($row);
$client_id = $row['client_id'];
if (!isset($grouped_messages[$client_id])) {
$grouped_messages[$client_id] = array();
}
$grouped_messages[$client_id][] = $row;
}
foreach ($grouped_messages as $client_id => $client_messages) {
echo '<div>';
echo '<p>Messages for client #' . $client_id . '</p>';
foreach ($client_messages as $message) {
foreach($message as $column) {
echo $column;
}
}
echo '</div>';
}
Any Ideas on why that is happening?
PS The same client can have multiple messages meaning multiple rows in this table that's what the code it puts all those messages from the same client into the associative array!
Fetching both numeric and text keys is a default behaviour of PDO fetch.
You can change it, in your case this should be done by setting attribute PDO::ATTR_DEFAULT_FETCH_MODE to PDO::FETCH_ASSOC:
$connect->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
Or you can do it when you instantiate new PDO object.
More modes here.
Related
i'm working on a small text based web game for a few friends to play (for nostalgia) but i'm having trouble with the very core of the game, the attacking/defending.
Each player has x amount of each troop type (more can be purchased and will be lost on atk/def)
so far I have a table with troop types and stats and a table with troop qty linked with user id and troop id.
I'm using the user id to get the troop id and qty using;
// Get attacker troop quantities.
$i = 0;
$attacker_deets = $conn->prepare("SELECT * FROM troop_qty WHERE user_id= :attacker_id");
$attacker_deets->bindParam(':attacker_id', $attacker_id);
$attacker_deets->execute();
$attacker_deets_results = $attacker_deets->fetchAll(PDO::FETCH_ASSOC);
foreach($attacker_deets_results as $atk_key=>$atk_result) {
echo "<pre>"; var_dump($atk_result); echo "</pre>";
}
which outputs;
array (size=4)
'troop_qty_id' => string '1' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '1' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_qty_id' => string '2' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '2' (length=1)
'qty' => string '100' (length=3)
I'm then using the troop id to get the troop details;
// get attacker troop details.
$attacker_troop_deets = $conn->prepare("SELECT * FROM troops WHERE troop_id= :atk_troop_id");
$attacker_troop_deets->bindParam(':atk_troop_id', $attacker_deets_results[$i]['troop_id']);
$attacker_troop_deets->execute();
$returned_results = $attacker_troop_deets->fetchAll(PDO::FETCH_ASSOC);
foreach($returned_results as $key=>$result) {
echo "<pre>"; var_dump($result); echo "</pre>";
}
++$i;
which in total gives me;
array (size=4)
'troop_qty_id' => string '1' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '1' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_id' => string '1' (length=1)
'troop_name' => string 'Fist Puncher' (length=12)
'troop_atk' => string '1' (length=1)
'troop_def' => string '1' (length=1)
array (size=4)
'troop_qty_id' => string '2' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '2' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_id' => string '2' (length=1)
'troop_name' => string 'Stick Waver' (length=11)
'troop_atk' => string '2' (length=1)
'troop_def' => string '1' (length=1)
Now where i'm stuck is I need to be able to compare the troop_atk of First Puncher with the defence of Stick Waver which is gotten from a different set of duplicate query's getting the defenders details, i also need to be able to multiply and divide the atk and def variables.
so how would i go about achieving this? i would assume i would need to give each field in the array their own variable, but how? i have tried using ${"troop_name" . $i} = $attacker_deets_results[$i]['troop_name']; but it only ever outputs the last entered name.
any help would be amazing. thanks.
Edit: To clear things up a little, my goal is to get the troop qty and multipy it by both atk and def then use these numbers to do some other math with the same fields from the defending player which I'll use to decrease the qty field.
Maybe you could modify your code a little and do the following.
Fetch the data about the user's troops in one query:
$results = $conn->query("SELECT troop_qty.user_id as uid, troop.troop_id, troop.troop_name, troop_qty.qty, troop.troop_atk, troop.troop_def FROM troop_qty join troop on troop.troop_id=troop_qty.troop_qty_id WHERE troop_qty.user_id=:attacker_id
Create a new array which contains information about all user's troops:
$userData = array(
'id' => $attacker_id,
'troops' => array());
while (($res = $results->fetch_assoc()) != null) {
$troopData = array(
'name' => $res['troop_name'],
'qty' => $res['qty'],
'atk' => $res['troop_atk'],
'def' => $res['troop_def']);
$userData['troops'][$res['troop_id']] = $troopData;
}
As result you would have something like this (vardump($userData)):
array (size=2)
'id' => int 2
'troops' =>
array (size=2)
1 =>
array (size=4)
'name' => string 'Fist Puncher' (length=12)
'qty' => string '100' (length=3)
'atk' => string '1' (length=1)
'def' => string '1' (length=1)
2 =>
array (size=4)
'name' => string 'Stick Waver' (length=11)
'qty' => string '100' (length=3)
'atk' => string '2' (length=1)
'def' => string '1' (length=1)
I don't know how exactly you want to manipulate the data later on in the game but I think this would ease the work.
Btw. I've thrown away fetchAll() function because I personaly think it is not a good practice to fetch all data into memory and then modify it or do whatever with it. It's a waste of the memory and when you would deal with a big amount of data, at some point that could cause you some troubles.
I want to get Doctrine to return a hydrated array with the values being the id for the key, then all values inside an array of results (i.e. if there are multiple items with same ID, then return ID with multiple results in array).
This is the current function I do:
public static function getMedia($em, $entity, $id = NULL)
{
$dql = 'SELECT m.id, m.url, m.nb, m.lang
FROM iMT\Entity\Media m INDEX BY m.id JOIN iMT\Entity\\' . $entity . ' r WITH m.id = r.id';
if($id) {
$dql .= " WHERE r.id = ?1";
}
$q = $em->createQuery($dql);
if($id) {
$q->setParameter(1, $id);
}
return $q->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
}
Which returns:
array (size=44)
479600 =>
array (size=4)
'id' => int 479600
'url' => string 'pois/479600/Nonna.JPG' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
479615 =>
array (size=4)
'id' => int 479615
'url' => string 'pois/479615/Tramways.jpg' (length=51)
'nb' => null
'lang' => string 'fr' (length=2)
479580 =>
array (size=4)
'id' => int 479580
'url' => string 'pois/479580/ATLAS.jpg' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
479581 =>
array (size=4)
'id' => int 479581
'url' => string 'pois/479581/P'tit_sushi.jpg' (length=54)
'nb' => null
'lang' => string 'fr' (length=2)
However, I need the output to be:
array (size=44)
479600 =>
array (size=2)
array (size=4)
'id' => int 479600
'url' => string 'pois/479600/Nonna.JPG' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
array (size=4)
'id' => int 479600
'url' => string 'pois/479600/OtherPic.JPG' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
Would I need to create my own AbstractQuery::HYDRATE_ARRAY or is there something available that does what I need?
I'm using the result by checking if it contains a key that matches the ID of the current item (e.g. if(isset($data[$item])) // where $item = 479600 then output images), maybe there's a better way to check for the results?
EDIT
I've updated my function to return:
$result = $q->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
$data = array();
$count = count($result);
for($i = 0; $i < $count; $i++) {
if(!isset($data[$result[$i]['id']])) {
$data[$result[$i]['id']] = array(
$result[$i]
);
} else {
$data[$result[$i]['id']][] = $result[$i];
}
}
return $data;
Which returns something more to what I want:
array (size=44)
479600 =>
array (size=1)
0 =>
array (size=4)
'id' => int 479600
'url' => string 'pois/479600/Nonna.JPG' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
479577 =>
array (size=2)
0 =>
array (size=4)
'id' => int 479577
'url' => string 'pois/479577/AOMC.JPG' (length=47)
'nb' => null
'lang' => string 'fr' (length=2)
1 =>
array (size=4)
'id' => int 479577
'url' => string 'pois/479577/Buffet AOMC.jpg' (length=54)
'nb' => null
'lang' => string 'fr' (length=2)
Can this be improved? Is there any Doctrine functions that can help, or should I leave my for() loop?
The problem with using INDEX BY together with a JOIN is that the result that doctrine gives you might not contain all data that's fetched from the database.
In your case the database might return multiple rows containing the same value for m.id (because of the JOIN). But each subsequent row containing the same value for m.id will overwrite the previous one (because of the INDEX BY m.id).
Doctrine does not come with a hydrator that can solve this problem out of the box. You shall indeed need to implement your own. Read more about creating custom hydration modes.
Alternative
Another solution would be to not use INDEX BY in this case.
You could write a repository method that translates the result given by Doctrine to the array you want to have. Other parts of your application can then call that repository method.
This is probably easier than creating a custom hydration mode.
Update
The translation can look like this:
$data = array();
foreach ($q->getArrayResult() as $row) {
if (!isset($data[$row['id']])) {
$data[$row['id']] = array();
}
$data[$row['id']][] = $row;
}
return $data;
My result set come from a group/count query:
SELECT m.type, COUNT(m.type)
FROM sent_message m
WHERE m.user_id = :user_id
AND (m.sent_at >= :start_date OR m.sent_at <= :end_date)
GROUP BY m.type
And it's in the form of
an array nested into another array:
array (size=2)
0 =>
array (size=2)
'type' => string 'sms' (length=3)
'count' => string '1' (length=1)
1 =>
array (size=2)
'type' => string 'email' (length=5)
'count' => string '9' (length=1)
Is there any way to easily check if a given type index exists without looping? For avoiding this:
$resultSet = $repository->getAllCount(); // Returns the nested array
$viewBag = array('sent_sms_count' => 0, 'sent_email_count' => 0); // Init
foreach($resultSet as $result) :
if('sms' == strtolower($result['type'])
$viewBag['sent_sms_count'] = $result['count'];
if('email' == strtolower($result['type'])
$viewBag['sent_email_count'] = $result['count'];
endforeach;
There's no way I'm aware of without looping. But you can re-index the SQL results into a format you like:
foreach ($resultSet as $result) {
$viewBag['sent_' . strtolower($result['type']) . '_count'] = $result['count'];
}
lets say we have an array like this
from a mysql function like
function getGroups($limit = 10) {
$data = $this->fetchAll ( 'SELECT gid, `group`, information, tag FROM groups
GROUP BY tag LIMIT ' . $limit );
return $data;
}
Resulting
array
0 =>
array
'gid' => string '6' (length=1)
'group' => string 'Media' (length=5)
'tag' => string 'advertising' (length=11)
1 =>
array
'gid' => string '8' (length=1)
'group' => string 'Fashion' (length=10)
'tag' => string 'shorts' (length=7)
2 =>
array
'gid' => string '7' (length=1)
'group' => string 'Automotive' (length=8)
'tag' => string 'cars' (length=5)
3 =>
array
'gid' => string '1' (length=1)
'group' => string 'Fashion' (length=7)
'tag' => string 'tshirt' (length=6)
i need to display somehow to this ( something like )
array
0 =>
array
'group' => string 'Media'
'tags'
array
0 => string 'advertising'
1 =>
array
'group' => string 'Fashion'
'tags'
array
0 => string 'short'
1 => string 'tshirt'
2 =>
array
'group' => string 'Automotive'
'tags'
array
0 => 'cars'
simpler
group tag
media advertising
fashion short
fashion tshirt
automotive cars
to
media
advertising
fashion
short
tshirt
automotive
cars
what is the best way to do this? from php array? or from the mysql ?
Are you trying to get a list of groups, plus all the tags for each group? GROUP_CONCAT() is the right way to get the tag list but you want to be grouping by group, not by tag:
function getGroups($limit = 10) {
$data = (array) $this->fetchAll (
'SELECT `group`,
GROUP_CONCAT(DISTINCT `tag` ORDER BY `tag`) AS `tags`
FROM `groups`
GROUP BY `group` LIMIT ' . $limit
);
foreach ($data as $i => $row) {
$data[$i]['tags'] = explode(',', $row['tags']);
}
return $data;
}
I would add
ORDER BY group,tag
so the result set has all rows of the same group together.
Starting a new can then be done in php by comparing to the previous group and close/starting the new group when it has changed.
actually using hashes makes more sense...
$names = array();
$query = ''SELECT gid, `group`, information, tag FROM groups GROUP BY tag LIMIT ' . $limit';
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if ($num_rows) {
while ($row = mysql_fetch_array($result)) {
array_push($names[$row['group']], $row['tag']);
}
}
Try this
function getGroups($limit = 10) {
$data = $this->fetchAll ( 'SELECT gid, `group`, information, tag FROM groups
GROUP BY group LIMIT ' . $limit );
return $data;
}
This will group you data with group, or you can use group_concat but then you will get short, tshirt something like this.
I have a var dump of my sql query which return the following
I wanna to count in the array below that how many rows of myID = 5 are there. How would I do that. I am using php. Thanks in advance
array
0 =>
object(stdClass)[17]
public 'myID' => string '5' (length=1)
public 'data' => string '123' (length=3)
1 =>
object(stdClass)[18]
public 'myID' => string '5' (length=1)
public 'data' => string '123' (length=3)
2 =>
object(stdClass)[19]
public 'relativeTypeID' => string '2' (length=1)
public 'data' => string '256' (length=3)
3 =>
object(stdClass)[20]
public 'myID' => string '4' (length=1)
public 'data' => string '786' (length=3)
object(stdClass)[21]
public 'myID' => string '4' (length=1)
public 'data' => string '786' (length=3)
Do you always have the same value of data for the same myID? In other words, is data functionally dependant on myID?
If so, you can get the database to do this for you:
SELECT myID, data, COUNT(*) AS cnt
FROM (your query here)
GROUP BY myID, data
This would give you results like the following:
myID data cnt
'5' '123' 3
'2' '256' 1
'4' '786' 2
Or, you can use a foreach statement, like:
$count = 0;
foreach($arr as $item)
{
// Given that your item is an stdClass Object you access its property with "->"
// if an array $item["myID"] instead
if ( $item->myID == '4' )
{
$count ++;
}
}