create parent child array from inline data in php - php

actually i have simple problem but i forget how to solve it.. :D
i have data on table with following format
01 Johson 01 Craig
01 Johson 02 Daniel
01 Johson 03 Abbey
02 Dawson 01 Brown
02 Dawson 02 Agust
03 Brick 01 Chev
03 Brick 01 Flinch
so i want it to become an array like this
01 Johson => 01 Craig
``````````````02 Daniel
```````````````03 Abey
`
etc...
how to iterate trough the data and make it array like that...
i'm newby in PHP :))

There are a number of ways to get the outcome, though I am partial to using a mysql group concat:
select
col1,
group_concat(col2) as col2
from
tableName
group by
col1;
This will return that particular column of data in a comma delimted string, which you can then very easily explode inside your PHP code as it comes from the database.
Even though a comma is the default for group_concat, you can easily change it to concat the rows of data on a different string as well.

Is this what you want?
$results = array();
while ($row = mysqli_fetch_assoc($query_result)) {
$parent = $row['parentid'].' '.$row['parentname'];
if (!array_key_exists($parent, $results)) {
$results[$parent] = array();
}
$results[$parent][] = $row['childid'].' '.$row['childname'];
}

Related

Return 0 if no match found from array with MySQL IN statement

I know there are many similar questions and answers, but none of them are working in this case, as I'm using IN statement to get matches from array and my dates are in varchar data type because of its format. Here it is:
I'm trying to check if array items exists in database and get the count for each of them as an array. My SQL query runs fine and gets the results, but the only problem is that I want it to return 0 for array items that does not exist in a database instead of skipping it.
So for example here is my database table:
postId reactedTo reactedDate
126 Like 22 Jun 2019
172 Haha 24 Jun 2019
172 Wow 27 Jun 2019
132 Like 27 Jun 2019
Here is my function to run the SQL Query and get the results as an array
public function reactionsAnalytics() {
global $wpdb;
$tableName = $wpdb->prefix.'reactions';
$dates = $this->getLastNDays(7); //array('22 Jun 2019', '23 Jun 2019', ... ,'28 Jun 2019');
$reacts = $wpdb->get_results("
SELECT reactedDate, count(*) AS count
FROM {$tableName}
WHERE reactedDate IN ('".implode("','", $dates)."')
GROUP
BY reactedDate
", ARRAY_A);
$result = array();
foreach ($reacts as $react) {
$result[] = $react['count'];
}
wp_die(json_encode($result));
}
The expected outout of this function is ["1","0","1","0","0","2","0"], but I'm getting ["1","1","2"]. How can I prevent the $reacts query from skipping not found items and make it output 0 instead?
I've tried using COALESCE, IFNULL and SUM in various variations but got same results without zeroes.
Here is the SQL Fiddle and you can play with it:
http://sqlfiddle.com/#!9/ffbb98/5
Thanks!
Instead of trying to complicate the query, you simply need to change your application (PHP) code slightly. Get the query results for the dates available in the DB. Now, in PHP code, simply check if the count is available for the date or not. If yes, use the count, else set as zero.
// Change the SQL query result to an array with date as key, and count as value
$reacts_mod = array_combine(array_column($reacts, 'reactedDate'),
array_column($reacts, 'count'));
// Now prepare the $result
$result = array();
// Loop over input dates here
foreach ($dates as $dt) {
// If you have count obtained from the query result, then consider that else 0
if ( isset($reacts_mod[$dt]) ) {
$result[] = $reacts_mod[$dt];
} else {
$result[] = 0;
}
// Note: PHP7 code would be simply:
// $result[] = $reacts_mod[$dt] ?? 0;
}

fullCalendar "events: 'somepage.php'" JSON Results not displaying

I am trying to get the events for my fullCalendar implementation with the "events (as a JSON feed)" built in feature.
...
event: 'calendarFill.php',
...
The php file returns the following JSON data:
[{"id":"40","title":"test four","services":"Reflexology (40), foot-soak","start":"Tue Mar 26 2013 13:00:00","end":"Tue Mar 26 2013 13:40:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test two of update. this sentence added at update.","seat":"Side Couch 9","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"},{"id":"41","title":"test four","services":"Foot-Soak, ","start":"Wed Mar 27 2013 19:00:00","end":"Wed Mar 27 2013 19:15:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test of addslashes. what's going to happen?","seat":"Front Chair 2","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"}]
The above does not work - the events are not displayed on the calendar. I've cut and pasted the code directory in my Javascript code:
events: {"id":"40","title":"test four","services":"Reflexology (40), foot-soak","start":"Tue Mar 26 2013 13:00:00","end":"Tue Mar 26 2013 13:40:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test two of update. this sentence added at update.","seat":"Side Couch 9","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"},{"id":"41","title":"test four","services":"Foot-Soak, ","start":"Wed Mar 27 2013 19:00:00","end":"Wed Mar 27 2013 19:15:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test of addslashes. what's going to happen?","seat":"Front Chair 2","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"}],
And, it does not display. But, if I edit the above to remove all the quotes that are not surrounding text values (as below), IT WORKS.
events: [{id:40,title:"test four",services:"Reflexology (40), foot-soak",start:"Tue Mar 26 2013 13:00:00",end:"Tue Mar 26 2013 13:40:00",color:"#e56b15",customerId:21,phone:"555-0404",email:"test4",notes:"test two of update. this sentence added at update.",seat:"Side Couch 9",vip:"yes",confirmed:"yes",knows_policies:"yes",customer_here:0,allDay:false},{id:41,title:"test four",services:"Foot-Soak, ",start:"Wed Mar 27 2013 19:00:00",end:"Wed Mar 27 2013 19:15:00",color:"#e56b15",customerId:21,phone:"555-0404",email:"test4",notes:"test of addslashes. what's going to happen?",seat:"Front Chair 2",vip:"yes",confirmed:"yes",knows_policies:"yes",customer_here:0,allDay:false}],
I am using the documentation at this link: http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/
What am I doing wrong? Should I remove the 'json_encode' from my php and just return a string formatted like the above? I'd like to do it the 'right' way and not some work-around.
Added the php as requested:
// for connection to db
include("../includes/config.php");
include("../includes/mysql_functions.php");
// connection with db
$linkID = db_connect();
$returnValue = array();
$getEventData = mysql_query("SELECT apt_num, services, apt_date_start, apt_date_end, therapist, customer_num, notes, seat, confirmed, knows_policies, here FROM appointments", $linkID);
if ($getEventData != FALSE && mysql_num_rows($getEventData) > 0)
{
while ($theEventData = mysql_fetch_array($getEventData))
{
$getCustomerString = "SELECT first_name, middle_name, last_name, phone, email, vip FROM customer WHERE customer_num = ".$theEventData['customer_num'];
$getCustomerData = mysql_query($getCustomerString, $linkID);
if ($getCustomerData != FALSE && mysql_num_rows($getCustomerData) > 0)
{
while($theCustomerData = mysql_fetch_array($getCustomerData))
{
$customerName = $theCustomerData['first_name']." ".$theCustomerData['middle_name']." ".$theCustomerData['last_name'];
$customerPhone = $theCustomerData['phone'];
$customerEmail = $theCustomerData['email'];
$customerVip = $theCustomerData['vip'];
}
}
else
{
$customerName = "error";
$customerPhone = "error";
$customerEmail = "error";
$customerVip = "error";
}
$rowArray['id'] = $theEventData['apt_num'];
$rowArray['title'] = $customerName;
$rowArray['services'] = $theEventData['services'];
$rowArray['start'] = date("D", $theEventData['apt_date_start'])." ".date("M", $theEventData['apt_date_start'])." ".date("d", $theEventData['apt_date_start'])." ".date("Y", $theEventData['apt_date_start'])." ".date("H", $theEventData['apt_date_start']).":".date("i", $theEventData['apt_date_start']).":".date("s", $theEventData['apt_date_start']);
$rowArray['end'] = date("D", $theEventData['apt_date_end'])." ".date("M", $theEventData['apt_date_end'])." ".date("d", $theEventData['apt_date_end'])." ".date("Y", $theEventData['apt_date_end'])." ".date("H", $theEventData['apt_date_end']).":".date("i", $theEventData['apt_date_end']).":".date("s", $theEventData['apt_date_end']);
$rowArray['color'] = $theEventData['therapist'];
$rowArray['customerId'] = $theEventData['customer_num'];
$rowArray['phone'] = $customerPhone;
$rowArray['email'] = $customerEmail;
$rowArray['notes'] = $theEventData['notes'];
$rowArray['seat'] = $theEventData['seat'];
$rowArray['vip'] = $customerVip;
$rowArray['confirmed'] = $theEventData['confirmed'];
$rowArray['knows_policies'] = $theEventData['knows_policies'];
$rowArray['customer_here'] = $theEventData['here'];
$rowArray['allDay'] = "false";
array_push($returnValue, $rowArray);
}
}
else
{
$returnValue[0] = "error";
}
print json_encode($returnValue);
Markus Vetter (from Google+) found the problem. In the original code returned from my php file there is an "'" that causes an issue. I needed to 'addslashes' to my php before returning it to the jQuery/Javascript. Once that was done, the jQuery / fullCalendar code was able to render my events as expected.
A second pair of eyes does wonders! Thank you Markus Vetter!
The original code returned:
[{"id":"40","title":"test four","services":"Reflexology (40), foot-soak","start":"Tue Mar 26 2013 13:00:00","end":"Tue Mar 26 2013 13:40:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test two of update. this sentence added at update.","seat":"Side Couch 9","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"},{"id":"41","title":"test four","services":"Foot-Soak, ","start":"Wed Mar 27 2013 19:00:00","end":"Wed Mar 27 2013 19:15:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test of addslashes. what's going to happen?","seat":"Front Chair 2","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"}]
After using 'addslashes' in my php for all text fields:
[{"id":"40","title":"test four","services":"Reflexology (40), foot-soak","start":"Tue Mar 26 2013 13:00:00","end":"Tue Mar 26 2013 13:40:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test two of update. this sentence added at update.","seat":"Side Couch 9","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"},{"id":"41","title":"test four","services":"Foot-Soak, ","start":"Wed Mar 27 2013 19:00:00","end":"Wed Mar 27 2013 19:15:00","color":"#e56b15","customerId":"21","phone":"555-0404","email":"test4","notes":"test of addslashes. what\'s going to happen?","seat":"Front Chair 2","vip":"yes","confirmed":"yes","knows_policies":"yes","customer_here":"0","allDay":"false"}]
Actually solved this time! In my php file, the last value added to the $rowArray is: $rowArray['allDay'] = "false". Instead of being a text value, it needs to be the actual boolean value. This "$rowArray['allDay'] = false" fixed the problem.
I had the same issue and it was a formatting thing for me as well.
This is how my json is returning now and it is finally working.
[{"title":"Coke Trade Show","start":"2014-12-03"},{"title":"Michigan Show","start":"2014-12-04"}]
Definitely a good idea to paste your json return straight into the JS to make sure it is working.

Echo numbering counter - remove leading zeros

At the moment I have a numbering system that brings back the following:
Using:
<?php echo $slide->counter; ?>
01 02 03 04 05
What can I add to the above php to get rid of the zero, so that it displays:
1 2 3 4 5
This should work...
printf('%d', $slide->counter);
<?php echo (int)$slider->counter; ?>

In PHP (or any language), Can 2 bitmasks be merged while maintaining "identity" of orignal bits?

This may be a really silly question, but it just crossed my mind and I thought it would be interesting to know for sure....
So here is the scenario:
Users have 3 options for each day of the week: AM, PM, and OFF. These are mutually exclusive choices, so there is no option to work both an AM and PM on same day.
So if I wanted to store their AM shifts and PM shifts as separate bitmasks, and User1 chooses the following:
S M T W Th F Sa
A P X A X P A
I would have the following:
$shifts['User1']['AM'] = 73; // 1001001
$shifts['User1']['PM'] = 34; // 0100010
Now, if I just wanted to know which days User1 worked, I could obviously just do:
$shifts['User1']['All'] = $shifts['User1']['AM'] | $shifts['User1']['PM'];
Or even just:
$shifts['User1']['All'] = $shifts['User1']['AM'] + $shifts['User1']['PM'];
But what if I wanted the final result to distinguish AM from PM, something to the effect of:
$shifts['User1']['AM'] = A00A00A;
$shifts['User1']['PM'] = 0P000P0;
So that the A's and P's are both considered set, but that
A00A00A | 0P000P0 = AP0A0PA;
Is there a common way of doing this, or am I thinking about this totally wrong?
To represent three states in a binary fashion you need 2 bits. For instance, you could say that:
PM = 01
AM = 10
OFF = 00
So now you have this:
A00A00A translates to 10 00 00 10 00 00 10
0P000P0 translates to 00 01 00 00 00 01 00
Applying bitwise OR operation:
10 00 00 10 00 00 10
00 01 00 00 00 01 00
--------------------
10 01 00 10 00 01 10
A P 0 A 0 P A
You get AP0A0PA, your desired result.
To write a literal value in binary use: 0b1001001, or hex: 0x49, instead of decimal: 73.
The bitmap will only ever give you true or false, so there is no way to represent three values (AM, PM, X) by compressing two bitmaps into one.
I think you are thinking about this wrong (others may have a smarter solution that I can't think of though). An array of characters A, P, X might be as good for this. You can merge arrays (so it is not the same as a string).
Yes, it is possible. See the example below in Python:
>>> class WorkShift(str):
def __or__(self, val):
def shift_calc(x, y):
return x if x != '0' else y
return WorkShift(''.join(map(shift_calc, self, val)))
>>> WorkShift('A00A00A') | WorkShift('0P000P0')
'AP0A0PA'
Does it answer your question?
Ps. I used Python since you explicitly stated it can be any programming language. I overloaded | operator. Result of the operation is still WorkShift's instance, so you can use it for further processing. It also inherits from str, so you can use it as string as well.
EDIT:
Similar solution for PHP, but without operator overloading, based only on strings processing:
<?php
function shift_calc($x, $y) {
return $x != '0' ? $x : $y;
};
function shift_sum($am, $pm) {
return implode(array_map('shift_calc', str_split($am), str_split($pm)));
};
$result = shift_sum('A00A00A', '0P000P0');
where the $result is a string with the following value: "AP0A0PA" (see proof here: http://ideone.com/NbTEJ).
minitech's comment is correct. This is a ternary number system (because you have 3 choices for each value). So you could do this:
$shifts['User1']['AM'] = '1001001'; // A00A00A
$shifts['User1']['PM'] = '0200020'; // 0P000P0
$all = intval($shifts['User1']['AM'], 3) +
intval($shifts['User1']['PM'], 3);
echo base_convert($all, 10, 3);
You have two options here.
Interleave
The original bitmask is spread out, and the new bitmask is inserted in the new "holes".
APAPAPAPAPAPAP
Append
The new bitmask is appended to the old bitmask.
AAAAAAAPPPPPPP
The former is easier to inspect/compare, but the latter is more efficient with regards to speed.

How do I use preg_match_all with 2 values and preg_replace to create 2 different links

I have a text file that I am displaying in a table. I am using preg_match_all to find a specific Title with a specific Chapter and I am replacing the Title and Chapter with preg_replace to make it a link..
For example the contents within the text file are as follows:
Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44
And I am replacing the Title's and Chapters... (i.e. Naruto 123) with a link to the webpage where that is located.
I also need to take into effect the folderpath that the webpage is located in.
The folderpath is the title of the anime. So if we were doing it for Naruto 123 the folder path is Naruto/.
So in the end the link will look like this:
http://website/folderpath/animetitle animechapter
The problem that I have is that I can get the folderpath's correct but I cannot create 2 or more distinct links. My code replaces Naruto 123 and Naruto 98 with the same link.
Here is what my code:
<?
$data=file_get_contents('series-updates.txt'); //get data from file
$regexp[0]="/(Naruto)[[:space:]](\w+)/";
$regexp[1]="/Naruto/";
preg_match($regexp[0], $data, $matches); //match Manga Title with Chapter for URL
$url= $matches[0];
preg_match($regexp[1], $data, $matches2); //match Manga Title for folderpath
$folderpath= $matches2[0];
$patterns= '/(Naruto)[[:space:]](\w+)/';
$replacements= ''.$url.'';
$data=preg_replace($patterns,$replacements, $data);
$dat=explode("\n",$data); //split data at new lines
echo '<table cellspacing=0>';
foreach ($dat AS $value) { //loop
echo '<tr><td>'.$value.'</td></tr>';
}
echo '</table>';
?>
here is an the output of the code:
http://xennetworks.com/output3.php
** ALSO, the reason why in the php code I am using preg_match instead of preg_match_all is because if I use preg_match_all for the links I get the output of ARRAY and I wanted you to see the outcome that I would like.
Try this on for size though I'm not sure what you're looking for the link URL:
$s= <<<STR
Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44
STR;
preg_match_all('/\w{3}\s\d{2}\s\d{2}:\d{2}\s(.+)?\s(\d{2,})/', $s, $m);
for ($i=0; $i<count($m[1]); $i++) {
$url= sprintf('http://xennetworks.com/%s %s', $m[1][$i], $m[2][$i]);
echo("$url\n");
}
<?php
$filedata = "Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44";
$lines = explode("\n", $filedata);
echo "<table border=\"1\">";
foreach($lines as $line)
{
echo "<tr>";
preg_match("/^([a-zA-Z]{3}\s\d{2}\s\d{2}:\d{2})\s(.+?)\s(\d+)\s*?$/", $line, $matches);
echo "<td>$matches[1]</td>";
echo "<td>$matches[2] $matches[3]</td>";
echo "</tr>";
}
echo "</table>"
?>

Categories