I have a notifications section on my site and I am getting data from my table. There can be child notifications assigned to their parent, so I have parent_id in the table as shown below:
Below is the array I am getting
Array
(
[0] => stdClass Object
(
[user_title] => Mr
[user_firstname] => Richard
[user_lastname] => Knight-Gregson
[assignee_title] => Mr
[assignee_firstname] => Richard
[assignee_lastname] => Knight-Gregson
[created] => 2015-03-26 11:40:20
[id] => 61
[user_id] => 88
[description] => message for user rkg uid 88
[assignee] => 88
[parent_id] =>
[cc] =>
[bcc] =>
[status] => 0
)
[1] => stdClass Object
(
[user_title] => Mr
[user_firstname] => Richard
[user_lastname] => Knight-Gregson
[assignee_title] => Mr
[assignee_firstname] => Richard
[assignee_lastname] => Knight-Gregson
[created] => 2015-03-26 12:45:47
[id] => 71
[user_id] => 88
[description] => This is child
[assignee] => 88
[parent_id] => 61
[cc] =>
[bcc] =>
[status] => 0
)
[2] => stdClass Object
(
[user_title] => Mr
[user_firstname] => Ted
[user_lastname] => Chiles
[assignee_title] => Mr
[assignee_firstname] => Richard
[assignee_lastname] => Knight-Gregson
[created] => 2015-03-27 10:11:56
[id] => 207
[user_id] => 47
[description] => cc 88
[assignee] => 88
[parent_id] =>
[cc] => 88
[bcc] => 112
[status] => 0
)
[3] => stdClass Object
(
[user_title] => Mr
[user_firstname] => Richard
[user_lastname] => Knight-Gregson
[assignee_title] => Mr
[assignee_firstname] => Richard
[assignee_lastname] => Knight-Gregson
[created] => 2015-03-27 10:17:31
[id] => 209
[user_id] => 88
[description] => sadfasdfasd
[assignee] => 88
[parent_id] =>
[cc] => 88
[bcc] => 0
[status] => 0
)
)
Here is the code I have at the moment to loop through the array, what I need to add to this is to get all elements that have a parent_id and then display them under their repsective parent in a list...
<table class="notifications-table" border="1">
<tr>
<th>S No.</th><th>Date</th><th>Employee</th><th id="details">Details</th><th>Assignee</th><th>Action</th>
</tr>
<?php foreach($notifications as $row){ ?>
<?php foreach($row as $k){
} ?>
<tr <?php if($row->status){ echo "class='resolved'"; } ?> id="row-<?=$row->id;?>">
<td><?=$row->user_id?></td><td><?=date('d M, H:i',strtotime($row->created));?></td>
<td><?=$row->user_title.' '.$row->user_firstname.' '.$row->user_lastname;?></td>
<td>
row id is : <?=$row->id.' == '.$row->parent_id; ?>
<?php //echo $row->id.' == '.$row->parent_id; ?>
<?=$row->description; ?><span data-parent-id="<?=$row->id;?>" data-user-id="<?=$row->user_id;?>" id="add_not" data-featherlight="#send_child_message" data-id="add_child_notification" class="glyphicon glyphicon-plus"></span>
</td>
<td><?=$row->assignee_title.' '.$row->assignee_firstname.' '.$row->assignee_lastname;?></td>
<td><input <?php if($row->status){ echo "disabled"; } ?> value="<?=$row->id;?>" data-action="not" type="checkbox" /></td>
</tr>
<?php } ?>
</table>
I have tried a few things but just cant get what I want!
Regards!
The code you have seems correct. I was able to take most of your structure and iterate over it with and got what you see in screenshot:
I was able to see 2 rows of info because I only used 2 elements (not all of them).
Ok, I see now what you are trying to do. You want to display details of the parent id element - which in the example, would be my "row 2" since row id 71 is a 'child of' row id 61.
If my assumption is correct, you need to associate the elements with each other based on this idea of parent & child.
Before your "Details" closing td, you can add...
<?php foreach($notifications as $row2){ ?>
<?php if ($row2->id == $row->parent_id) : ?> PARENT DESC: <?= $row2->description; ?><?php endif; ?>
<?php } ?>
Which should now let you pull in some parent property values.
Related
How to get "file_url" from this data?
Response can contain few objects, and how to parse it?
Code that I use:
<?php
$r34data = simplexml_load_file('https://rule34.xxx/index.php?page=dapi&s=post&q=index&tags=green_hair&limit=2');
print_r($r34data);
Response:
SimpleXMLElement Object ( [#attributes] => Array ( [count] => 69175 [offset] => 0 ) [post] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [height] => 2039 [score] => 1 [file_url] => https://wimg.rule34.xxx/images/3820/01e0a587f6e303c34669a4f79c994100.png [parent_id] => [sample_url] => https://rule34.xxx/samples/3820/sample_01e0a587f6e303c34669a4f79c994100.jpg [sample_width] => 850 [sample_height] => 667 [preview_url] => https://rule34.xxx/thumbnails/3820/thumbnail_01e0a587f6e303c34669a4f79c994100.jpg [rating] => e [tags] => 1boy 2girls annette_(fire_emblem) annette_fantine_dominic ass back back_view bare_back big_ass big_breasts blue_eyes blue_lingerie breasts_outside byleth_(fire_emblem) byleth_(fire_emblem)_(male) byleth_(male) ffm_threesome fingering fingering_partner fire_emblem fire_emblem:_three_houses french_kiss green_hair kissing light_blue_lingerie lingerie long_hair lysithea_(fire_emblem) lysithea_von_ordelia medium_breasts medium_hair nintendo nipples orange_hair pink_eyes purple_lingerie pussy pussy_juice pussy_juice_trail quan_ming sex threesome vaginal_penetration white_hair [id] => 4324333 [width] => 2600 [change] => 1608630074 [md5] => 01e0a587f6e303c34669a4f79c994100 [creator_id] => 503169 [has_children] => false [created_at] => Tue Dec 22 09:21:59 +0000 2020 [status] => active [source] => https://www.pixiv.net/en/artworks/86455582 [has_notes] => false [has_comments] => false [preview_width] => 150 [preview_height] => 117 ) ) [1] => SimpleXMLElement Object ( [#attributes] => Array ( [height] => 1600 [score] => 10 [file_url] => https://wimg.rule34.xxx/images/3820/4a88c87d1cb18928bcee85207bf1c61b.jpeg [parent_id] => [sample_url] => https://rule34.xxx/samples/3820/sample_4a88c87d1cb18928bcee85207bf1c61b.jpg [sample_width] => 850 [sample_height] => 618 [preview_url] => https://rule34.xxx/thumbnails/3820/thumbnail_4a88c87d1cb18928bcee85207bf1c61b.jpg [rating] => e [tags] => 1boy 1boy1girl 1girls all_fours anal anal_sex barefoot bent_over big_breasts christmas clothed_male_nude_female clothing cum cum_in_ass cum_inside cum_leaking doggy_style ejaculation ejaculation_while_penetrated faceless_male feet female female_focus forehead_protector green_hair hair_bun headband huge_breasts kneeling long_hair looking_at_viewer looking_back male multicolored_hair naked naruto naruto_(series) naruto_shippuden nude open_mouth orange_eyes orange_hair pakura penetration pussy santa_costume smile soles standing tied_hair toes two_tone_hair vagina [id] => 4324142 [width] => 2202 [change] => 1608622103 [md5] => 4a88c87d1cb18928bcee85207bf1c61b [creator_id] => 631992 [has_children] => false [created_at] => Tue Dec 22 07:28:01 +0000 2020 [status] => active [source] => [has_notes] => false [has_comments] => false [preview_width] => 150 [preview_height] => 108 ) ) ) )
Please help!
(don't ask why I parsing rule34)
Hi You can conver it to the array and work with is easy
libxml_use_internal_errors(TRUE);
$objXmlDocument = simplexml_load_file("https://rule34.xxx/index.php?page=dapi&s=post&q=index&tags=green_hair&limit=2");
if ($objXmlDocument === FALSE) {
echo "There were errors parsing the XML file.\n";
foreach(libxml_get_errors() as $error) {
echo $error->message;
}
exit;
}
$objJsonDocument = json_encode($objXmlDocument);
$arrOutput = json_decode($objJsonDocument, TRUE);
echo "<pre>";
print_r($arrOutput);
I can't for the life me return single values of my multidimensional array from database. For example, I want to return the value of each key to return in table columns.
This is the array coming from the database:
Array
(
[0] => stdClass Object
(
[student_id] => lvzr0001
[fname] => Hamza
[lname] => ibrahimi
[student_email] => Hamza_ib2#hotmail.com
[course] => CPAN 220 D30
[full_name] => asdfddd
[institution] => asdf
[position] => sadf
[proc_email] => Hamza_ib2#hotmail.com
[phone] => 14168334599
[address] => 20 edgecliffe golfway
[city] => Toronto
[prov_state] => Ont
[postal_code] => M3C 3A4
[country] => Canada
[comments] => adfadsfasdfasdfas
)
[1] => stdClass Object
(
[student_id] => 56gfdsgdfs
[fname] => zxcv
[lname] => cvcvz
[student_email] => dsfb2#hotmail.com
[course] => ELIC 101 90
[full_name] => dfa
[institution] => asdf
[position] => zxcvzxcv
[proc_email] => zxcvzxvc#hotmail.com
[phone] => 4002452345
[address] => 102 yorkland st
[city] => Richmond Hill
[prov_state] => Ont
[postal_code] => l4s1a1
[country] => Canada
[comments] => xzcvzxcv
)
)
I want to get a specific column result like this:
<?php
global $wpdb;
$displayQuery = $wpdb->get_results("SELECT * FROM viewoffcampusproctorrequests");
echo '<pre>';
print_r ($displayQuery);
echo '</pre>';
foreach($displayQuery as $key => $value){
foreach($value as $title => $description){
echo $description['fname'];
}
}
But I only get the entire key values when I echo $description;. How would I access each key item in my array?
You're close: $value is an object, so you can retrieve a particular key/value pair. If you want the value for fname for each object:
foreach($displayQuery as $key => $value){
echo $value->fname;
}
I am having a very strange issue with printing out element of an array.
I am attempting to print out some elements of an array inside of a php foreach Here is exactly what the array looks like
[2] => Array
(
[id] => 3
[body] => dsfgdfgd
[has_subquestion] => 1
[is_subquestion] => 0
[ordering] => 2
[is_manditory] => 0
[created] => 2013-01-09 12:06:47
[parent_id] => 0
[sub] => Array
(
[0] => Array
(
[id] => 4
[body] => dfgdfg
[has_subquestion] => 1
[is_subquestion] => 1
[ordering] => 0
[is_manditory] => 0
[created] => 2013-01-09 11:24:20
[parent_id] => 3
)
[1] => Array
(
[id] => 23
[body] => gsdgdf
[has_subquestion] => 1
[is_subquestion] => 1
[ordering] => 14
[is_manditory] => 0
[created] => 2013-01-09 12:56:33
[parent_id] => 3
)
)
)
[3] => Array
(
[id] => 5
[body] => dfgdfg
[has_subquestion] => 1
[is_subquestion] => 0
[ordering] => 3
[is_manditory] => 0
[created] => 2013-01-09 12:06:47
[parent_id] => 0
[sub] => Array
(
[id] => 6
[body] => dfgdfg
[has_subquestion] => 0
[is_subquestion] => 1
[ordering] => 3
[is_manditory] => 0
[created] => 2013-01-08 13:37:07
[parent_id] => 5
)
)
Notice that the first one has 2 [sub]'s and the second only has one. This is my code for printing them
echo count($question['sub']);
foreach($question['sub'] as $s):
echo '<li>
<input type="hidden" name="sub[id]" value="'. $s['id'] .'" />
<input type="hidden" name="sub[parent]" value="'. $question['id'] .'" />
'. $s['body'] .'</li>';
endforeach;
this is what it is printing
2
dfgdfg
gsdgdf
8 <--count (which should be 1 not 8)
6 <--each of the following are the first letter/number in the sub array
d
0
1
3
0
2
Can anyone see what I am doing wrong?
Run
print_r($array).
It should also give you your answer in recursive fashion
You will need to update your code to see if the [sub] array is multidimensional or not. I would do it by checking if sub has a key that will only match if there is only a single array:
if (array_key_exists('id', $question['sub']) {
// This is a single array so wrap it in an array so that the foreach logic works
$question['sub'] = array($question['sub']);
}
... continue as normal with your foreach loop.
use is_array() and call the function recursivly ?
I am currently working with the following array,
Array
(
[0] => Array
(
[0] => Array
(
[candidate_id] => 41
[show_on_site] => urban talent
[first_name] => Barney
[surname] => Harwood
[gender] => male
[talent] => presenter
[skill] =>
[DOB] => 1983-11-30
[Location] => London
[contact_telephone] => 01234 567890
[contact_email] => barney#bluepeter.co.uk
[height] => 5' 1"
[eyes] => Brown
[hair_colour] => brown
[hair_length] => medium
[accents] => Native Northern, others include - Yorkshire, Liverpool, Manchester, Birmingham, Cockney, RP, Welsh, Scottish, German, American
[training] => n/a
[unions] => Member of the British Academy of Composers & Songwriters
Equity & MU Member
[visible] => yes
[availability] => yes
[availability_number] => 9999
[availability_order] => 0
[availability_comments] => BARNEY IS THE LATEST BLUE PETER PRESENTER AND CAN BE SEEN ON AIR MONDAYS & TUESDAYS AT 4.30PM ON BBC
[spotlight_url] =>
[youtube_showreel] =>
[date_created] => 2011-11-02 10:44:37
[created_by] => 1
)
)
[1] => Array
(
[0] => Array
(
[candidate_id] => 42
[show_on_site] => urban talent
[first_name] => Simon
[surname] => Ainley
[gender] => male
[talent] => actor
[skill] =>
[DOB] => 1987-06-12
[Location] => Huddersfield
[contact_telephone] => 01484 532751
[contact_email] => simonainley#the-factory.co.uk
[height] => 5' 1"
[eyes] => blue
[hair_colour] => brown
[hair_length] => short
[accents] => Accents
[training] => Training
[unions] => Union Membership
[visible] => yes
[availability] => yes
[availability_number] => 9999
[availability_order] => 0
[availability_comments] => Availability Comments
[spotlight_url] => http://www.google.com
[youtube_showreel] => http://www.youtube.com/watch?v=sP4NMoJcFd4
[date_created] => 2011-11-08 11:28:12
[created_by] => 1
)
)
)
as you can see it is an array within an array type situation, I am trying to loop through it to pull out the first_name + surname of each entry, however I get the following error when I try to do it,
A PHP Error was encountered
Severity: Notice
Message: Undefined index: firstname
Filename: admin/candidate_list.php
Line Number: 5
I looping through it like this at the moment,
<?php foreach ($candidates as $k => $v) : ?>
<li><?php echo $v[0]['first_name']. " ".$v[0]['surname']; ?></li>
What am i doing wrong?
What does this give you?
<?php foreach ($candidates as $k => $v) print_r($v); ?>
I don't know how $v['id'] would be defined either given that array, so there's something else you're leaving out...
And you're using site_url()... Do I smell CodeIgniter? ;)
Your array is too deep for that loop. I am assuming that $candidates is the name of the whole big array? You don't need each candidate in their own array, try getting your results in an array that has one less level and that foreach will work.
<?
for($i=0; $i=50; $i++){
foreach ($candidates[$i] as $v){
?>
<li><?php echo $v[0]['first_name']. " ".$v[0]['surname']; ?></li>
<?
}
}
?>
I have decided to follow http://www.artfulsoftware.com/mysqlbook/sampler/mysqled1ch20.html
So now I am looking for some help with the code.
I am using their data for my testing,
So, I visualized the tree being like so:
array('value' => 'Richard Shakespeare',
array('value' => 'Henry',
array('value' => 'Joan'),
array('value' => 'Margaret'),
array('value' => 'William',
array('value' => 'Susana',
array('value' => 'Elizabeth Hall',
array('value' => 'John Bernard'))),
array('value' => 'Hamnet'),
array('value' => 'Judith',
array('value' => 'Shakespeare Quiney'),
array('value' => 'Richard Quiney'),
array('value' => 'Thomas Quiney'))),
array('value' => 'Gilbert'),
array('value' => 'Joan',
array('value' => 'William Hart'),
array('value' => 'Mary Hart'),
array('value' => 'Thomas Hart'),
array('value' => 'Micheal Hart')),
array('value' => 'Anne'),
array('value' => 'Richard'),
array('value' => 'Edmond')),
array('value' => 'John'));
So if we want to insert that into the database we want to end up with
Array
(
[0] => Array
(
[value] => Richard Shakespeare
[left] => 1
[right] => 46
)
[1] => Array
(
[value] => Henry
[left] => 2
[right] => 43
)
[2] => Array
(
[value] => Joan
[left] => 3
[right] => 4
)
[3] => Array
(
[value] => Margaret
[left] => 5
[right] => 6
)
[4] => Array
(
[value] => William
[left] => 7
[right] => 24
)
[5] => Array
(
[value] => Susana
[left] => 8
[right] => 13
)
[6] => Array
(
[value] => Elizabeth Hall
[left] => 9
[right] => 12
)
[7] => Array
(
[value] => John Bernard
[left] => 10
[right] => 11
)
[8] => Array
(
[value] => Hamnet
[left] => 14
[right] => 15
)
[9] => Array
(
[value] => Judith
[left] => 16
[right] => 23
)
[10] => Array
(
[value] => Shakespeare Quiney
[left] => 17
[right] => 18
)
[11] => Array
(
[value] => Richard Quiney
[left] => 19
[right] => 20
)
[12] => Array
(
[value] => Thomas Quiney
[left] => 21
[right] => 22
)
[13] => Array
(
[value] => Gilbert
[left] => 25
[right] => 26
)
[14] => Array
(
[value] => Joan
[left] => 27
[right] => 36
)
[15] => Array
(
[value] => William Hart
[left] => 28
[right] => 29
)
[16] => Array
(
[value] => Mary Hart
[left] => 30
[right] => 31
)
[17] => Array
(
[value] => Thomas Hart
[left] => 32
[right] => 33
)
[18] => Array
(
[value] => Micheal Hart
[left] => 34
[right] => 35
)
[19] => Array
(
[value] => Anne
[left] => 37
[right] => 38
)
[20] => Array
(
[value] => Richard
[left] => 39
[right] => 40
)
[21] => Array
(
[value] => Edmond
[left] => 41
[right] => 42
)
[22] => Array
(
[value] => John
[left] => 44
[right] => 45
)
)
So the issue comes to mind of, How best to do this?
My solution was:
$container = array();
function children($item){
$children = 0;
foreach($item as $node)
if(is_array($node))
$children += children($node)+1;
return $children;
}
function calculate($item, &$container, $data = array(0,0)){
//althought this one is actually of no use, it could be useful as it contains a count
$data[0]++; //$left
$right = ($data[0]+(children($item)*2))+1;
//store the values in the passed container
$container[] = array(
'value' => $item['value'],
'left' => $data[0],
'right' => $right,
);
//continue looping
$level = $data[1]++;
foreach($item as &$node)
if(is_array($node))
$data = calculate($node, $container, $data);
$data[1] = $level;
$data[0]++;
return $data;
}
calculate($tree, $container);
How efficient it is I do not know.
But now onto the queries.
To select all descendants of a node we can use
SELECT child.value AS 'Descendants of William', COUNT(*) AS `Level`
FROM tester AS parent
JOIN tester AS child ON child.`left` BETWEEN parent.`left` AND parent.`right`
WHERE parent.`left` > 7 AND parent.`right` < 24
GROUP BY child.value ORDER BY `level`;
To select all descendants of a node, to a specific depth we can use
Note that we are selecting Descendants of William to a depth of 2
Williams left: 7, Williams right: 24, Levels: 2
SELECT child.value AS 'Descendants of William', COUNT(*) AS `Level`
FROM tester AS parent
JOIN tester AS child ON child.`left` BETWEEN parent.`left` AND parent.`right`
WHERE parent.`left` > 7 AND parent.`right` < 24
GROUP BY child.value HAVING `level` <= 2 ORDER BY `level`;
So that's easy enough.
But now I want to know a few things,
Note that in the actual database as well as left/right all rows have a unique id, and a "parent" column containing their inviteers id, or null if not invited
Lets say I want to insert David as a child of Judith, How do I do that?
Lets say I want to get Mary Hart's Parent, and the Parents Parent (array('Henery', 'Joan', 'Mary Hart')), How do I do that?
Lets say I want to delete William Hart from Joan How so I do that?
To update/delete you will need to increase/decrease left/right values of all elements of branch.
Examples of queries you can find here.
How efficient it is I do not know.
Nested sets works VERY slowly with big trees on update/insert/delete. And very fast to select.
So use this model only with static data, which will be stored without changes most of the time, and this tree will not contain thousands of nodes (or any update will take minutes to complete). Materialized path works much faster.
to get the parents of a node you need nodes with left_id < child.left_id and right_id > child.right_id, if you only want the direct ancestor choose the one from previous set with the highest left_id.
to delete a node remove it and then lower twice all left/right ids that are greater than deleted element right id. if( leftId > deleted.leftId ) leftId-=2 same for rightId
to insert a node make some space for it adding+2 to all nodes with leftId > parent.rightId then parent.rightId += 2 then insert node with leftId = parent.rightId-2 and rightId=parent.rightId-1
All of your questions can be solved very easy if you use a DFS for each relationship and then use your function calculate() again if you want it more detailed.