The file data is a list of book titles, and links to their cover images (hosted on Wikipedia). novels.txt and covers.txt.
Read in both files, and create a table
Each row should include the title of the book on the left, and an embedded image tag.
Here's my code.
//load file
$covers = file("covers.txt", FILE_IGNORE_NEW_LINES);
$novels = file("novels.txt",FILE_IGNORE_NEW_LINES);
//print table header
print "<table
<tr>
<th>Book</th>
<th>Cover</th>";
foreach($novels as $i=>$novel){
print "<tr>
<td>$novel</td>
<td>$covers[$i]</td>
</tr>";
"</table>";
}
It is printing out the title of the book with the corresponding URL instead of the image. How do i get it to print out the image?
Related
Basically, I have create a page to view xlxs file on browser. It works fine but the problem is this php code only can show one sheet.
What I am trying to achieve:
Display Excel spreadsheets in table format on browsers using PHP Excel Reader
Check how many sheets are in the .xls document, get the sheet name and display the amount of buttons accordingly
The buttons must be able to allow the user to traverse between different sheets of one document
Code:
<?php
if($the1['file_type']=='excel'){
require_once "Classes/PHPExcel.php";
$reader= PHPExcel_IOFactory::createReaderForFile($the1['path'].'/'.$the1['file_name']);
$excel_Obj = $reader->load($the1['path'].'/'.$the1['file_name']);
$worksheet=$excel_Obj->getSheet('0');
$lastRow = $worksheet->getHighestRow();
$columncount = $worksheet->getHighestDataColumn();
$columncount_number=PHPExcel_Cell::columnIndexFromString($columncount);
echo "<table border='1'>";
for($row=0;$row<=$lastRow;$row++){
echo "<tr>";
for($col=0;$col<=$columncount_number;$col++){
echo "<td>";
echo $worksheet->getCell(PHPExcel_Cell::stringFromColumnIndex($col).$row)->getValue();
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_query($con,"update file1 set status='read' where file_name='$id'");
}
?>
Use setActiveSheetIndex(sheet number) to specify worksheet 0 (1st worksheet), 1 (2nd worksheet), 2 (third worksheet) and so on. For example
$sheetindex=1; // use the 2nd sheet
$worksheet = $excel_Obj->setActiveSheetIndex($sheetindex);
I am trying to get my image to display when clicked via the link. However when I click on the link it finds the image id as displayed in the url link ok but does not display any image. Can you please help?
<?php
//sets up thisPage
$pageSize=10;
if (isset($_POST["thisPage"])) $thisPage=$_POST["thisPage"];
else $thisPage=1;
//selects all distinct expenses that have been uploaded
$dbQuery="SELECT * FROM images WHERE user_id = '$userID' ";
$dbResult=mysqli_query($db_connection, $dbQuery) or die(mysqli_error($db_connection));
echo "<table cellspacing=\"5\" class=\"recordsTableBG\"> <thead
class=\"recordsTableHeader\">";
echo '<tr> <th>ID</th><th>Amount</th><th>Description</th><th>Filename</th>
<th>Project ID</th><th>Status</th></tr></thead>';
echo '<tr class="alternateRowColor">';
'<tr>';
while ($dbRow=mysqli_fetch_array($dbResult)){
echo "<img src = 'uploaded/$image' width = '200' height = '200'>";
// display row with expense
echo '<td>'. $dbRow['id'] .'</td>';
echo '<td>'. $dbRow['user_id']. '</td>';
echo '<td><a href='.$_SERVER['PHP_SELF'].'?imageid='.$dbRow['id'].'>
Click here to view image</a></td>';
}
echo "</table>";
echo "</form>";
?>
<!-- add submitt button
close form -->
</div>
You have a mixture of two approaches going on here, and that's where the confusion lies.
First, notice your
<img src='uploaded/$image' width = '200' height = '200'>
The browser is going to see something like
<img src='uploaded/picture_of_cat.jpg' width='200' height='200'>
and render a 200px by 200px image of a cat. I'm assuming that's your thumbnail. (As an aside, I notice that it's not enclosed in <td></td> even though it's in the table row).
In another part of the table, you have
<a href='.$_SERVER['PHP_SELF'].'?imageid='.$dbRow['id'].'>Click here to view image</a>
which is going to render as something like
<a href='http://example.com/index.php?imageid=1234'>Click here to view image</a>
When the user clicks that link, it's going to make a GET request to the server with imageid equal to 1234. What is the server going to do with that imageid? In the code you've posted, nothing.
You have two choices:
if you want to keep the link as it is, you'll have to write some code that will take the imageid value, find the appropriate image, and return it to the browser as image data - that means setting the appropriate headers and sending the data back as binary data.
the simpler way to do it would be to replace the URL in the link with the same one you have in your <img> tag - when the user clicks on it, the server will just return the image.
I got a bunch of images (225 in total). Example of their names:
4n27e.png
4n28e.png
4n29e.png
4n30e.png
5n12e.png
5n25e.png
5n26e.png
5n27e.png
5n28e.png
I need to form one big picture out of all these images. For example first line of images would be 4n27e (2nd image 4n28e, 3rd image 4n29e and so on). Second line of images would start from 5n12e (2nd image 5n25e and so on). What methods do i need to search for to solve this?
Note: i need to do this only with php and maybe some javascript.
Test case for logic, you can replace the echo with an img tag, and /or use div.
<?php
$images=array();
$images[]="4n27e.png";
$images[]="4n28e.png";
$images[]="4n29e.png";
$images[]="4n30e.png";
$images[]="5n12e.png";
$images[]="5n25e.png";
$images[]="5n26e.png";
$images[]="5n27e.png";
$images[]="5n28e.png";
echo "<table border='1'>";
$oldIndex=0;
$row=1;
foreach($images as $image)
{
if(substr($image,0,1)!=$oldIndex)
{
if($row>1){echo "</tr>";}
echo "<tr>";
$oldIndex=substr($image,0,1);
$row++;
}
echo "<td>$image</td>";
}
echo "</table>";
?>
very new to PHP to please bear with.
As you can see from my snippet of code I am simply displaying product information one line under each other then repeating the loop using while. This then obviously displays my relevant data in just one column, one under each other.
while ($row = mysqli_fetch_row($result)){
echo "<img src=\"images/album1.jpg\"/><br>"; //this will eventually show the product image
echo "$row[1] <br>"; //this shows the product name
echo "<strong>£$row[2]</strong><br>"; //this shows the product price
}
How would I go about creating a grid view, for example using columns to display my data? I presume it would be some kind of loop to be added and maybe using tables to display my data?
Any help is greatly appreciated.
you can dot this by creating div and float that div to left this will create grid view
while ($row = mysqli_fetch_row($result)){
echo "<div class='container'>";
echo "<img src=\"images/album1.jpg\"/><br>"; //this will eventually show the product image
echo "$row[1] <br>"; //this shows the product name
echo "<strong>£$row[2]</strong><br>"; //this shows the product price
echo "</div>";
}
and css
.container{
float:left;
}
you can
also set the max height and width of div
I am creating a web application where users can upload/download/view online pdfs. I want to change the name of the pdf file to view in new tab link (like we see in all websites).
Does anyone know how to make any field in the database that gives a link that when user clicks, it opens the pdf in a new tab?
if($result) {
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
<td><b>view><b/></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['type']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
The solution doesn't have to be within the database.
You can use an anchor tag to redirect the user to the link :
<a href="{$path_to_pdf}" target="_blank" >Click here to view the PDF</a>
Put that in a cell in the table you are generating.
I could be mistaken - but I do believe that the users browser is also a factor here. The browser has to be able to display pdf's. The latest and greatest* browsers have it baked in but user preferences' might also be a factor.
* Let your imagination run wild
You can use target="_blank" in your a tag
PDF
This works in Google Chrome, not sure if it works in other browsers.
You can look here http://www.allaccessliving.com/residences/floorplans#1bed15bath:35A
There's a pdf link and you see for yourself if you inspect that element.
When outputting the link to the PDF, use this:
Visit StackOverflow
setting target will tell the browser to open the link in a new tab.