Need some help as I have very basic knowledge of code and not sure I understand what i need to.
I have been given two sets of codes from a wordpress plugin support that they say will help me do what I need to do
My goal is to have a table that display different data and headers based on a field called attendee form (each attendee form has different fields).
They have given me the two codes.
First one displays a table but I have had to manually enter each table header and it is not dynamic
function my_attendee_form (){
$people = array();
$EM_Bookings = $EM_Event->get_bookings();
if( count($EM_Bookings->bookings) > 0 )
{
echo "\n";
?>
<table class="event-attendees">
<tr class="">
<th class="attendee-name">Name</th>
<th class="attendee-country">Golf Link</th>
<th class="attendee-discipline">Handicap</th>
<th class="attendee-status">Home Club</th>
</tr>
<?php
$guest_bookings = get_option('dbem_bookings_registration_disable');
$guest_booking_user = get_option('dbem_bookings_registration_user');
$counter = 0;
foreach( $EM_Bookings as $EM_Booking){
$attendees_list = "";
$attendees = $EM_Booking->booking_meta['attendees'];
if ( !empty($attendees) ) {
$attendees_list = "";
foreach($attendees as $key => $value) {
foreach($value as $key_attendee => $value_attendee) {
$attendees_list .= "<tr>";
$attendees_list .= "<td>".$value_attendee["attendee_name"]."</td>";
$attendees_list .= "<td>".$value_attendee["golflink"]."</td>";
$attendees_list .= "<td>".$value_attendee["handicap"]."</td>";
$attendees_list .= "<td>".$value_attendee["home_club"]."</td>";
$attendees_list .= "</tr>";
}
}
}
echo $attendees_list;
}
echo "\n";
?>
</table>
<?php
}
if( count($EM_Bookings->bookings) == 0 )
{
echo '<p>No one registered yet... Will you be the first ?</p>'};
}
add_action('em_bookings_event_footer', 'my_attendee_form');
This is slightly broken, the table works.
The second bit of code adds a column to an existing table and populates it based on the form fields.
function my_em_bookings_table_cols_template_attendee2($template, $EM_Bookings_Table){
$template['attendee_name'] = 'Attendee Name';
return $template;
}
add_action('em_bookings_table_cols_template', 'my_em_bookings_table_cols_template_attendee2',10,2);
function my_em_custom_booking_form_cols_attendee_list($val, $col, $EM_Booking, $EM_Bookings_Table, $csv){
if( $col == 'attendee_name' ){
$EM_Tickets_Bookings = $EM_Booking->get_tickets_bookings();
$attendee_datas = EM_Attendees_Form::get_booking_attendees($EM_Booking);
$attendee_list = "";
foreach( $EM_Tickets_Bookings->tickets_bookings as $EM_Ticket_Booking ){
//Display ticket info
if( !empty($attendee_datas[$EM_Ticket_Booking->ticket_id]) ){
$val .= "<ul>";
$val .= "<li> <strong>Ticket name: ".$EM_Ticket_Booking->get_ticket()->ticket_name."</strong></li>";
foreach( $attendee_datas[$EM_Ticket_Booking->ticket_id] as $attendee_title => $attendee_data ){
$val .= "<li>".$attendee_title."</li>";
$val .= "<li><ul>";
foreach( $attendee_data as $field_label => $field_value){
$val .= "<li>". $field_label .': '.$field_value."</li>";
}
$val .= "</ul></li>";
}
$val .= "<ul>";
}
}
}
return $val;
}
add_filter('em_bookings_table_rows_col','my_em_custom_booking_form_cols_attendee_list', 10, 5);
How would I combine these two so that I can generate a new table like the first code, but populate it like the second one? I just don't know what goes where.
Any suggestions/thoughts would be great.
Thanks
Troy
Related
I have a csv file that should be converted to HTML table based on column values , and I have to put the values into separate table based on first column of csv . And I think the problem is because of '\n' in first column.
So its like this:
Here in Result column, in one row I have three values separated using comma (W,M,P). In the code I wrote it is considered as separate table headers .
Can anyone please help me with this?
This is my code:
<?php
$csv="FinalResult.csv" ;
$csvcontents=file_get_contents($csv);
$csv_array = explode("\n", $csvcontents);
$tables = [];
foreach($csv_array as $key => $value) {
if ($key == 0) {
continue;
}
$line = explode(',', $value);
if (array_key_exists($line[0], $tables)) {
$tables[$line[0]][] = $line;
} else {
$tables[$line[0]] = [$line];
}
}
foreach ($tables as $key => $value) {
echo '<h1> ' .$key. ' </h1>'; // YOUR TITLE (Team)
echo "<table>";
echo '<tr>';
foreach (explode(',', $csv_array[0]) as $keyHeader => $valueHeader) {
if (in_array($keyHeader, [0, 1])) {
continue;
}
echo "<th>$valueHeader</th>";
}
echo '</tr>';
foreach ($value as $keyRow => $valueRow) {
echo '<tr>';
foreach ($valueRow as $keyValue => $valueValue) {
if (in_array($keyValue, [0, 1])) {
continue;
}
echo "<td>$valueValue</td>";
}
echo '</tr>';
}
echo '</table>';
}
?>
I refereed in stack overflow , but there they were giving only single values to a column and not providing multiple values .
But am getting output like this ,
so it takes the value of Result column after '-' as a new heading , i tried but am not able to solve this , can anyone really help me in this matter .
Here is how my output should look like:
I marked in yellow where all the data am getting in same column
This is my csv file :
Team,Date,Opponent,Result
MIN,May-03,UTA,a.b.c=d-e.f-g.h=log4j2-i.xml-j -k -a4j.k=tp_r-RR.xml -
MIN,May-04,SEA,"L,Q,J"
SAC,May-03,DAL,L
SAC,May-04,TOR,W
NYN,May-05,BAL,L
NYN,May-07,MIA,W
Here is the code
<table>
<?php
$csvValues= array_map('str_getcsv', file('FinalResult.csv'));
$counter = 0;
// Header
echo "<tr>";
foreach($csvValues[0] as $headers){
echo "<th>".$headers."</th>";
}
echo "</tr>";
// Content
foreach($csvValues as $values){
echo "<tr>";
if($counter >0){
foreach($values as $data){
echo "<td>".$data."</td>";
}
}
echo "</tr>";
$counter++;
}
?>
</table>
i have some JSON data
i am able to retrieve data out of it but i want data to be displayed with columns
this the JSON
{"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-22T07:56:03.767","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null},{"ObjectId":44,"ObjectName":"Event x11","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-23T00:33:50.7","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null}]}
here is what i am doing right now
$jsonData = json_decode($data, TRUE);
foreach ($jsonData as $d) {
foreach ($d as $ins) {
echo "<h3>".$ins['ObjectName']."</h3>";
echo "<h3>".$ins['ObjectDescription']."</h3>";
}
}
OutPut:
MEGA MELA
NEW EVENT BY API
Event x11
NEW EVENT BY API
i want it in this format
ObjectId | ObjectName | ObjectTitle | ObjectDescription | ObjectLabel | ObjectTypeId | MaxFieldsExpected | ObjectValueType | ObjectControlType | IsDeleted | CreatedDate | CreatedBy | EditedDate | EditedBy
43 | MEGA MELA | Event Created by API | NEW EVENT BY API | ..............................................................................
above i am getting data with help of array key now i want extracting keys from JSON like ObjectId or ObjectName making them header of table and showing all the data in columns
on the above example, you can use the key to print the filed name and value will be the data.
here we can loop the field names first to create the heading
I am using table here.
<table>
<?php foreach ($d as $ins) { ?>
//printing field heads
<tr>
<?php
foreach ($ins as $key=>$value) {
echo "<td><h3>".$key."</h3></td>";
} ?>
</tr>
//printing value row
<tr>
<?php
foreach ($ins as $key=>$value) {
echo "<td><h3>".$value."</h3></td>";
} ?>
</tr>
} ?>
</table>
Used an old gist for array2table from here : http://www.aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
<?php
function array2table($array, $recursive = false, $null = ' ')
{
// Sanity check
if (empty($array) || !is_array($array)) {
return false;
}
if (!isset($array[0]) || !is_array($array[0])) {
$array = array($array);
}
// Start the table
$table = "<table>\n";
// The header
$table .= "\t<tr>";
// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
$table .= '<th>' . $heading . '</th>';
}
$table .= "</tr>\n";
// The body
foreach ($array as $row) {
$table .= "\t<tr>" ;
foreach ($row as $cell) {
$table .= '<td>';
// Cast objects
if (is_object($cell)) { $cell = (array) $cell; }
if ($recursive === true && is_array($cell) && !empty($cell)) {
// Recursive mode
$table .= "\n" . array2table($cell, true, true) . "\n";
} else {
$table .= (strlen($cell) > 0) ?
htmlspecialchars((string) $cell) :
$null;
}
$table .= '</td>';
}
$table .= "</tr>\n";
}
$table .= '</table>';
return $table;
}
$data ='[{"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-22T07:56:03.767","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null},{"ObjectId":44,"ObjectName":"Event x11","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-23T00:33:50.7","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null}]';
$jsonData = json_decode($data, TRUE);
echo array2table($jsonData);
?>
DEMO: https://3v4l.org/0n27Y
Second foreach should look like: foreach ($d as $key => $ins) then at $key variable you get your indexes.
First assumption: Assume, we defined a variable (its name is $tmp) in a function(functioin name is 'ExpMenu') for temporary calculating and in end of function we return this variable.
Second assumption: Assume, we call that function recursively for create a navigation menu base on a multidimensional array.
My question is about scope of that variable ($tmp). In every call funtion, will its value overwritten? In other words, by every function call we lose previous value?
For more detail, please review below code:
/// --- { Declaration Block
$content = array(
array(
'level'=>'1',
'order'=>'1',
'text'=>'New Solution WorkFlow',
'is_parent'=>'yes',
'child'=> array(
array(
'level'=>'2',
'order'=>'1',
'text'=>'Define New Solution',
'is_parent'=>'no',
'url'=>'#'
),
array(
'level'=>'2',
'order'=>'2',
'text'=>'View Solutions',
'is_parent'=>'no',
'url'=>'#'
),
array(
'level'=>'2',
'order'=>'3',
'text'=>'View Solutions',
'is_parent'=>'no',
'url'=>'#'
)
)
),
array(
'level'=>'1',
'order'=>'2',
'text'=>'Solution Modify WorkFlow',
'is_parent'=>'yes',
'child'=> array(
array(
'level'=>'2',
'order'=>'1',
'text'=>'Request For Solution Modify',
'is_parent'=>'no',
'url'=>'#'
)
)
),
array(
'level'=>'1',
'order'=>'3',
'text'=>'Solution Close WorkFlow',
'is_parent'=>'yes',
'child'=> array(
array(
'level'=>'2',
'order'=>'1',
'text'=>'Declare For Solution Close',
'is_parent'=>'no',
'url'=>'#'
)
)
)
);
function ExpMenu($item_array ) {
$tmp='';
foreach ($item_array as $item) {
if ($item['is_parent']=='yes') {
$tmp = '<li class="hasChild">' . $item["text"] . '<ul>';
$tmp .= ExpMenu($item['child']);
$tmp .= '</ul></li>';
} else {
$tmp = '<li>';
$tmp .= ''. $item['text'] . '' ;
$tmp .= '</li>';
}
}
return $tmp;
}
/// --- }
$menu='<div><ul>';
$menu .= ExpMenu($content);
$menu.='</ul></div>';
echo $m . '<br />';
It seams by every call function we lose pervious value.
I thank #l0rkaY for her/him solution, But I found another solution that doesn't need add new parameter in my function.
Because $tmp scope is in 'ExpMenu' function and we call recursively function, therefore variable still alive and wasn't terminated.
So, I modify my function a bit:
function ExpMenu($item_array ) {
$tmp='';
foreach ($item_array as $item) {
if ($item['is_parent']=='yes') {
$tmp .= '<li class="hasChild">' . $item["text"] . '<ul>';
$tmp .= ExpMenu($item['child']);
$tmp .= '</ul></li>';
} else {
$tmp .= '<li>';
$tmp .= ''. $item['text'] . '' ;
$tmp .= '</li>';
}
}
return $tmp;
}
I assume your actual problem is, that your function generates only single item with a single child item.
It's because you are overwriting your previous item in your if/else blocks. That's why you only get the last item.
You just need to concatenate them to the existing items.
function ExpMenu($item_array ) {
$tmp='';
foreach ($item_array as $item) {
if ($item['is_parent']=='yes') {
$tmp .= '<li class="hasChild">' . $item["text"] . '<ul>';
$tmp .= ExpMenu($item['child']);
$tmp .= '</ul></li>';
} else {
$tmp .= '<li>';
$tmp .= ''. $item['text'] . '' ;
$tmp .= '</li>';
}
}
return $tmp;
}
I have this piece of code
foreach($mnthArrPtrn as $m => $mn)
{
if(!isset($catName)) {
$catVals = array();
$prevCat = $catName = $pntChrtQry[0]['CAT']['categoryname'];
$pntVals .= '{name:'.$catName.',data:[';
}else if($prevCat != $catName) {
$prevCat = $catName;
$catVals = array();
$pntVals .= '{name:'.$catName.',data:[';
}
foreach($pntChrtQry as $key => $val){
$catName = $val['CAT']['categoryname'];
if($prevCat != $catName){
continue 2;
}
echo '<br />$m::'.$m;
echo '<br />$mn::'.$mn;
echo '<br />$val::'.$val[0]['MNTH'];
if($m == $val[0]['MNTH'] || $mn == $val[0]['MNTH']){
$catVals[] = $val[0]['total'];
}
}
pr($catVals);
if(!isset($catName)){
$pntVals .= ']},';
}
$catName = $val['CAT']['categoryname'];
}
1st loop iterates over a months array which are joined as a key value pair.
What I am doing here is on getting a new catName I continue the internal loop but at the same time I want to restart loop 1 with $prevCat,$catName still preserving their values.
Is this possible? Sorry If this is a silly question.
I tried converting the first one to a while statement and use a reset then but It didn't help me.
Something like this will allow you to arbitrarily restart a loop:
while (list($key, $value) = each($mnthArrPtrn)) {
if ($needToRestart) {
reset($mnthArrPtrn);
}
}
See more here.
I am trying to view values from two separate tables using a foreach loop. The user chooses a "Start semester" and an "End semester" from a drop down list, and those two semesters are stored into an array called $semesterarray
<table style='width: 75%; text-align: left;' cellpadding = '4'>
<tr bgcolor=#000090>
<th><FONT COLOR = #FFFFFF><b><?php echo $startsem ?></b></th>
<th><FONT COLOR = #FFFFFF><b><?php echo $endsem ?></b></th>
</tr>
// If $semesterarray contains 10 and 11, I want to be able to view the
// courses taken in the 10 semester and the 11 semester under two separate columns.
<?php
function getSemesterDetails($semester)
{
$output = "";
$semA = $semester."reg";
$query = "SELECT course1,course2,course3 FROM $semA";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
// row displays: [course1]=> [course2]=> [course3]=>
// Add semester code to array Keys to indicate proper semester
// [course1.11]=> [course2.11]=> [course3.11]=>
foreach ($row as $key => $value)
{
$row[$key.".".$semester] = $value;
unset($row[$key]);
}
$startcourse1 = $row['course1.'.$semester];
$startcourse2 = $row['course2.'.$semester];
$startcourse3 = $row['course3.'.$semester];
$startcoursesarray = array($startcourse1, $startcourse2, $startcourse3);
$startcourses = implode("<br>", $startcoursesarray);
$endcourse1 = $row['course1.'.$semester];
$endcourse2 = $row['course2.'.$semester];
$endcourse3 = $row['course3.'.$semester];
$endcoursesarray = array($endcourse1, $endcourse2, $endcourse3);
$endcourses = implode("<br>", $endcoursesarray);
echo "<tr bgcolor=#ABB5F6>
<td>$startcourses</td>
<td>$endcourses</td>
</tr>";
}
}
foreach ($midsemarrayA as $key => $semester)
{
echo getSemesterDetails($semA);
}
?>
Any ideas?
This should be enough for you to make a start:
function getSemesterDetails ($semester) {
$output = "";
$query = "SELECT course1,course2,course3 FROM $semester";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
// row displays: [course1]=> [course2]=> [course3]=>
// Add semester code to array Keys to indicate proper semester
// [course1.11]=> [course2.11]=> [course3.11]=>
foreach ($row as $key => $value) {
$row[$key.".".$semester] = $value;
unset($row[$key]);
}
$startcourse1 = $row['course1.'.$semester];
$startcourse2 = $row['course2.'.$semester];
$startcourse3 = $row['course3.'.$semester];
$startcoursesarray = array($startcourse1, $startcourse2, $startcourse3);
$startcourses = implode("<br>", $startcoursesarray);
$endcourse1 = $row['course1.'.$semester];
$endcourse2 = $row['course2.'.$semester];
$endcourse3 = $row['course3.'.$semester];
$endcoursesarray = array($endcourse1, $endcourse2, $endcourse3);
$endcourses = implode("<br>", $endcoursesarray);
$output .= "<tr bgcolor=#ABB5F6>
<td>$startcourses</td>
<td>$endcourses</td>
</tr>";
}
return $output;
}
foreach ($semesterarray as $key => $semester) {
getSemesterDetails($semester);
}
You'll want to get it to return an array of values rather than a string, but shifting it into a subroutine should do the trick.