Only first word of title retrieved with PHP showing - php

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.

Related

I need to display table header in my code

The code that i made display the header after each row in the table.
I want just one time the header appear on the top.Any help?
print "<table border=1>\n";
while ($row = mysql_fetch_array($result)){
$files_field= $row['filename'];
$files_show= "Uploads/$files_field";
$descriptionvalue= $row['title'];
print "<tr>";
print "<th>";
echo "header1";
print "</th>";
print "<th>";
echo "header2";
print "</th>";
print "</tr>";
print "<tr>\n";
print "\t<td>\n";
echo "<font face=arial size=4/>$descriptionvalue</font>";
print "</td>\n";
print "\t<td>\n";
echo "<div align=center><a href='".$files_show."' target='_blank' title='CLICK TO OPEN'>$files_field</a></div>";
print "</td>\n";
print "</tr>\n";
}
print "</table>\n";
This program will:
get a row from db
print the header
print the data
get next row until the end
Sounds like you want to:
print the header
get a row from the db
print the data
get next row until the end
This should be adequate I think:
<?php
// print the beginning of the table, and the header
echo "<table border='1'>";
echo "<tr>";
echo "<th>";
echo "header1";
echo "</th>";
echo "<th>";
echo "header2";
echo "</th>";
echo "</tr>";
// than loop through the rows and print the rest of the table
while ($row = mysql_fetch_array($result)) {
$files_field = $row['filename'];
$files_show = "Uploads/{$files_field}";
$descriptionvalue = $row['title'];
echo "<tr>\n";
echo "\t<td>\n";
echo "<font face=arial size='4'/>$descriptionvalue</font>";
echo "</td>";
echo "<td>";
echo "<div align=center><a href='{$files_show}' target='_blank' title='CLICK TO OPEN'>{$files_field}</a></div>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
Some additional notes / advises:
don't mix print / echo: they are almost the same, it just causes confusion
don't try to "prettify" the source with tabs and linebreaks: you are just bloating the output for no good reason, use the browsers inspector window instead
forget the mysql driver, use mysqli: the former is so out of dae, that it is removed from the new versions of PHP (from 7.0), so your code will stop working if the platform is updated; the mysqli variant is a bit different in how you have to use it (an extra parameter mostly), but the results are in the same format, and it behaves very similarly if not identically to the old one, so you will not have to rewrite many things

WordPress Plugin - Table output for page URL listings

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

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).

Saving a picture path in MySQL database, and then pulling that data to display picture?

I have a MySQL database where I have a table ITEMS and column PICTUREPATH. The Picture path lists "localhost/firstimage.jpg" and so on. In my PHP script, I've added some code to what I thought would work, but it's not. I'm fetching the picturepath but it's literally giving me the text "localhost/firstimage.jpg" rather than the actual image. Here is what my code looks like...
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>" . $row['picturepath'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
How can I add this into the code so the picture actually displays, not the text name of the image?
echo "<td align='center'><img src=\"" . $row['picturepath'] . "\" /></td>";

Categories