WordPress Plugin - Table output for page URL listings - php

I'm building my first WordPress plugin which in basic terms is going to grab a list of page URL's and then show them all in a table.
I have all the queries in place that I need and if I output to a basic echo, I get a big long list of all the page URL's on a site which is what it is supposed to do. But, like I said above, I want to output these URL's to a prettier table layout.
I want to create a table with just two headers (Link & Title) and then have the list of URL's underneath the headers.
I'm struggling at the moment to get the output from the query to list correctly in the table. What I am getting is this:
Existing table layout screenshot
So as you can see, its repeating both the table headers as well as the URL's.
To cut a long story short, can anyone help me in making this table have just the the two headers at the top and then have the list of URLs and titles underneath them for each page.
I'm using a pretty basic function to gather the info I want and then echo'ing the table:
$pages = get_pages( 'post_status=publish' );
foreach ( $pages as $page )
{
$pagetitle = $page->post_title;
$pagelink = get_permalink( $page->ID );
echo "<table style='width:50%'>";
echo "<tr>";
echo "<th align='left'>Link</th>";
echo "<th align='left'>Title</th>";
echo "</tr>";
echo "<tr>";
echo "<td align='left'>{$pagelink}</td>";
echo "\n";
echo "<td align='left'>{$pagetitle}</td>";
echo "</tr>";
echo "</table>";
}
Hopefully this makes sense and you can understand what I am trying to do! I know that this code probably isn't the best, but its all a learning curve!
Thanks for your help in advance.

$pages = get_pages( 'post_status=publish' );
echo "<table style='width:50%'>";
echo "<tr>";
echo "<th align='left'>Link</th>";
echo "<th align='left'>Title</th>";
echo "</tr>";
foreach ( $pages as $page ) {
$pagetitle = $page->post_title;
$pagelink = get_permalink( $page->ID );
echo "<tr>";
echo "<td align='left'>{$pagelink}</td>";
echo "\n";
echo "<td align='left'>{$pagetitle}</td>";
echo "</tr>";
}
echo "</table>";
Is that what you're going for? This will make one table and loop through the pages to create rows, instead of creating a table for each row

Related

PHP: How do I code a link for a specific array stored in a text file without hardcoding it?

This question may have been answered, but I have been Googling and searching this site for over a week now and have not come across anything that applies to this situation. Yes, this is a homework assignment, and I have exhausted my resources (the entire class is confused). Any help or links to relevant resources would be appreciated.
Problem: I have a program where the user can submit "bug reports". These bug reports are all written to the same text file and are each treated as an array. Each report is also displayed with a "Details" link. I need to write a hyperlink that will essentially assign the link to the index value of the array without hardcoding it (because the link will need to work for future updates) and then have that specific report displayed in a separate page. I'm submitting a photo of the display table with the link in it so that there's a visual of this allow with the specific code for the display table.
Image of display table
$count = count($ReportArray);
for($i = 0; $i < $count; ++$i) {
$CurReport = explode("~", $ReportArray[$i]);
//bug description line and details link
echo "<tr>";
echo "<td><p style='text-align: center;'><strong>Bug Description: </strong></td>";
echo "<td colspan='6' style='text-align: center;'>".htmlentities($CurReport[0])."<br/></td>\n";
//$CurArray = implode("~", $CurReport);
//assign details link an array - no idea how
$num = count($ReportArray); //total number of reports
echo '<td style="text-align: center;">';
echo '<p>Details</p>';
echo "</td></tr>\n";
//other bug report fields
echo "<tr>";
echo "<td></td>";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[1])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[2])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[3])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[4])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[5])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[6])."<br /></td>\n";
echo "<td></td>";
echo "</tr>\n";
//display updates
echo "<tr>";
$line_count = count($ReportArray);
if($line_count > 7) {
echo "<tr>";
echo '<td style="text-align:center; text-weght:bold;">Update</td><td>';
echo htmlentities($CurReport[$line_count - 1]).'<br/>\n';
echo "</td>";
echo "</tr>\n";
}
}
So here's how the program works:
bug_List.php: Has the form for bug reports and then a table to
display the submitted and validated reports. These reports all
written and read from the same text file. BugReportCreate.php:
This file validates the input and writes it the text file. BugReport.php: This file is used to display each individual report and allows the user to submit updates for the report currently displayed. BugReportUpdate.php: writes/appends the update to the specific report from BugReport and displays the updated report. bug_List.php will also display the updated report(s)

Only first word of title retrieved with PHP showing

So I am trying to display a list from another website in mine, it all works fine but only the first word of the 'title' attribute is stored. I know that the whole title is retrieved from the other website so how do I get it to store all of it.
Here is the code if it helps.
<?php
include "simple_html_dom.php";
$page = file_get_html("http://www.blade-edge.com/images/KSA/Flights/craft.asp?r=true&db=dunai");
echo "<table id=list>";
foreach($page->find('html/body/div/div[2]/ol/a') as $key=>$element) {
if ($element->title != "No craft within this SOI"){
$ships[$key] = $element->plaintext;
$shipTitles[$key] = $element->title;
$shipLinks[$key] = $element->href;
echo "<tr>";
echo "<td title =".$shipTitles[$key]." >";
echo $ships[$key];
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
Put title inside quotation marks
echo "<td title =".$shipTitles[$key]." >"; // Wrong
Right is
echo "<td title ='".htmlspecialchars($shipTitles[$key], ENT_QUOTES)."' >";
Without quotes, you will only see the word till the first space. Also, the value itself must be escaped.

Switch between different views using href

I'm coming here after some head banging. Still learning php/mysql and coming across an issue which is bothering me for a while.
Currently I print a table with all the data without any condition. The values below the lower values and above are printed in this table. The snippet of my code is below:
while ($result = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$result['id']. "</td>";
if ($result['time'] > $limit[lower]){
echo "<td bgcolor=#ff0000>" $result['time']. "</td>";
} else {
echo "<td>" .$result['time']. "</td>";
}
echo "<td>" .$result['name']. "</td>";
echo "</tr>";
}
I want to create a href link so that by dafault only values above the lower are printed and on clicking the link, say detailed_view will give the table as it does above i.e. all value including below and above the lower value. Shall I create two methods and use them in href separately. In that case there will be two while loops which I want to avoid. Any pointer on this?
The name of the script is display.php, so before the loop I tried using:
echo "<a href='display_jobid.php?view=$num'>Show All</a>";
It does create a hyperlink Show All but how to get the functionality working, am not sure.
Thanks

How to do an HTML 'upward' rowspan

Here is the problem:
I need to know if there is a way to do an upward rowspan on a <th> element on a form.
I am reading some rows inside a DB that I need to put inside an html table.
I am doing something like:
echo "<table>";
while($result = $resultSet->fetch())
{
echo "<tr>";
echo "<td>$result['Name']</td>";
echo "<td>$result['Job']</td>";
echo "</tr>";
}
ehco "</table>";
First steps are easy until I needed to 'span' together two adjacent cells that contains the same value.
For exemple if someone have the same name but had two jobs I would like them to have something like :
echo "<tr>";
echo "<td rowspan='2'>$result['Name']</td>";
echo "<td>$result['Job']</td>";
echo "</tr>";
But I can't predict how many jobs someone have (except they all have at least one and that they are all in order).
exemple of record in MySQL table
Name/**/Jobs
Paul/**/Jobs1
Simon/**/Jobs23
Simon/**/Jobs45
Roger/**/Jobs67
(All simon's jobs are 'behind one another', they are grouped. If Paul had a second job it would be the second record in the table 'pushing' simons jobs down).
So I need to change the rowspan value of the first element to fit how many jobs that person have.
This is why I would need to know if it is possible to do a upward span because i could just display every on of them and count how many jobs each person have and do a rowspan that would merge the table cell upward.
It would certainly be easier then fetching every row in the DB then looping throught them all and checking how many jobs a person have to 'span' the cells now and then display them all and then skipping them. and so on.
Thanks
A situation like this calls for pre-processing. Like so:
$people = array();
while($result = $resultSet->fetch())
{
if( !isset($people[$result['Name']])) $people[$result['Name']] = array();
$people[$result['Name']][] = $result['Job'];
}
echo "<table>";
foreach($people as $name=>$jobs)
{
echo "<tr>";
echo "<td rowspan=\"".count($jobs)."\">".$name."</td>";
echo "<td>".array_shift($jobs)."</td>";
echo "</tr>";
foreach( $jobs as $otherjob)
{
echo "<tr><td>".$otherjob."</td></tr>";
}
}
echo "</table>";
Done!
What you're looking for doesn't exist in HTML. But you can do it like this:
$currName = $result['Name'];
echo "<tr>";
if($currName==$prevName)
{
echo "<td> </td>";
}
else
{
echo "<td>$result['Name']</td>";
}
echo "<td>$result['Job']</td>";
echo "</tr>";
$prevName = $currName;
Rather than using rowspan, just nest a table in the second column with all of the jobs for that person.
echo "<tr>";
echo "<td>$result['Name']</td>";
echo "<td>";
echo "<table>"
foreach($jobs as $job): //$jobs holds the job values for the current name
echo "<tr><td>";
echo $job;
echo "</td></tr>";
endforeach;
echo "</table>";
echo "</td>";
echo "</tr>";
Note: you will need to preprocess the result set to get a 2D array for this method (see answer from Niet the Dark Absol).

Getting a single value from a table dynamically generated by SQL and using it in another table in Wordpress

I currently have a table generated by a sql query. The url to the page is http://skunkboxstudios.com/dev/ofa-courses/
In the table, there is a button on each row that displays a modal window that shows more details of the row it's on. How do I tell the modal window to only select information regarding the row it's on.
Here is the code for the table on the page:
global $wpdb;
$courses = $wpdb->get_results("SELECT * FROM section_view;");
$results = array();
echo "<table>";
echo "<tr>";
echo "<th>Section Name</th>";
echo "<th>Section Description</th>";
echo "<th>Start Date</th>";
echo "<th>End Date</th>";
echo "<th>Location</th>";
echo "<th>Details</th>";
echo "</tr>";
foreach($courses as $course){
$sectionname = "$course->section_name";
$sectiondescription = "$course->section_description";
$sectionstartdate = "$course->term_startdate";
$sectionenddate = "$course->term_enddate";
$sectionlocation= "$course->location_name";
$sectionid = "$course->section_id";
echo "<tr>";
echo "<td>$sectionname</td>";
echo "<td>$sectiondescription</td>";
echo "<td>$sectionstartdate</td>";
echo "<td>$sectionenddate</td>";
echo "<td>$sectionlocation</td>";
echo "<td>$sectionid</td>";
echo "<td>";
echo "<button class='element' onclick='javascript:openDialog();'>Details</button>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
The code for the modal window is here:
$details = $wpdb->get_results(
"SELECT * FROM section_view WHERE section_id = '$sectionid';");
foreach($details as $detail){
echo "<h2>".$detail->section_name."</h2>";
echo "<table>";
echo "<tr>";
echo "<td>".$detail->section_name."</td>";
echo "<td>".$detail->section_description."</td>";
echo "</tr>";
echo "</table>";
}
echo "<button class='element' onclick='javascript:closeDialog();'>Close</button>";
So I need the query in the modal window to SELECT * FROM section_view WHERE section_id = 'the section_id corresponding to the row of the table it's on'
If anybody can help me out, please do. I'll post more information if you need it as well. Thanks.
Not a method I use often but I think you need to include the section_id in the URL for your openDialog() call. e.g.
echo "<button class='element' onclick='javascript:openDialog(\"myurl.php?sectionid=${sectionid}\");'>Details</button>";
You can then include:
$sectionid=$_GET["sectionid"];
at the top of your modal window script.
Any help?
There are some quick fix as well alternative method of correct process (but obvious a lengthy one)
1. Create a function in functions.php and add your code to modal window there.
On click of "details" link call this function using AJAX (make use of admin-ajax.php)
return the HTML to display as string from method and show in modal window.
Let me know if further details or assistance is required.

Categories