How can i put an href link in my foreach statement? - php

For starters i am fairly new at this.
I am trying to figure out how to a href link my row 'inspection_files' and i have tried just about everything. Is there anybody who could help me?
<?php
$i = 0;
foreach ($result as $r) {
echo "<tr>";
echo "<td>" . $r['last_inspection'] . "</td><td>" . strtolower(trim(($r['inspected_by_company']))) . "</td><td>" . strtolower(trim($r['inspection_files'])) . "</td>";
echo "</tr>";
$i++;
}
?>

Hey you can directly use <a> tag inside the <td>, just like below
echo "<td>" . $r['last_inspection'] . "</td><td>" . strtolower(trim(($r['inspected_by_company']))) . "</td><td><a href='" . strtolower(trim($r['inspection_files'])) . "'>" . strtolower(trim($r['inspection_files'])) . "</a></td>";
I hope $r['inspection_files'] this is your URL if this is not your redirect URL then just replace with your actual URL.

Related

PHP glob() Loop stops after a certain number of loops

I have a script that reads multiple XML files, takes data from them and lists it as HTML.
Here's the short version of the script (Slightly adjusted because of language and XML contents.):
<?php
$files = glob("./PDFs/*xml");
foreach($files as $filename) {
$xml=simplexml_load_file($filename) or die("Error: Cannot create object");
foreach($xml->xml->rechnung_list->rechnung as $data) {
$belegnr = $data->belegnr;
$file = glob("*" . "$belegnr.pdf");
echo '<tr>';
echo "<td>" . $data->auftrag . "</td>";
echo "<td>" . $data->belegnr . "</td>";
echo "<td>" . $data->kundennummer . "</td>";
echo "<td>" . $data->name . "</td>";
echo "<td>" . date("d. m Y" , strtotime($data->datum)) . "</td>";
echo "<td>" . "<a href='" . $file[0] . "'>DOWNLOAD</a>" . "</td>";
echo '</tr>';
}
}
?>
This code works and does exactly what I want, however it stops after a certain number of iterations.
My PHP memory_limit is set to 512M and the whole thing loads in about 30s so I don't feel like there should be a timeout.

Appending a character to a PHP output

I have a simple question.
echo "<tr>";
echo "<td>" . $row['Rep'] . "</td>";
echo "</tr>";
Assume that the above snippet outputs "1". How can I append a dot . in front of it in the above code? So that the output will be .1 instead of 1 only. I couldn't find a similar example on the web, so I decided to ask here. Thanks!
echo "<tr>";
echo "<td>" . "." . $row['Rep'] . "</td>";
echo "</tr>";
or in case it's not 1
echo "<tr>";
echo "<td>" . ($row['Rep'] == 1 ? "." . $row['Rep'] : $row['Rep']) . "</td>";
echo "</tr>";
Like
echo '<tr><td>.' . $row['Rep'] . '</td></tr>';

php increment variable with onclick

I want to use the function onclick to increment a variable [increment the variable paid]
paid is by default at 0 i want to increment at 1.
I try this code : onclick='paid++' but is not working...
My Code
foreach($query as $index=>$row) {
echo ($colorCount++%2) ? "<tr class='alt'>" : "<tr>";
echo "<td class='" . getPaidColor($row['paid']) . "' onclick='paid++ '></td>";
echo "<td class='" . getDelayColor($row['hour']) . "' onclick='postDeleteForm(" . $index . ")'></td>";
foreach($columns as $column) {
echo "<td>" . $row[$column] . "</td>";
}
}

How to add height and width in a PHP script?

I have the following line. It works fine but I want to add height and width to the image. How?
echo "<td>" . "<img src='img/" .$row['Foto'] . "'>" . "</td>";
You can use style attribute :
echo "<td>" . "<img style = 'width:?;height:?' src = 'img/" .$row['Foto'] . "'>" . "</td>";
or width and height attributes :
echo "<td>" . "<img width = '?' height = '?' src = 'img/" .$row['Foto'] . "'>" . "</td>";

mysql & php search highlighting

Wondering if someone could help give me a push in the right direction, I am building a search function (php and mysql) which will display search results and highlights keywords that the user has searched for. at the moment I grab the search criteria that the user has entered and query that against the database which works fine to get the desired results. the problem I have is
$highlight = preg_replace("/".$_GET['criteria']."/", "<span class='highlight'>".$_GET['criteria']."</span>", $_row['name']);
This will only highlight a phrase and not individual keywords. so for example if the document was called "Hello world" and the user typed this exactly it would highlight no problem however if the user typed "world hello" it will not highlight anything. I thought it would be a good idea to take the search criteria and use explode and check each word individually but this seems to fail as well. here is my query and how I am displaying results
$sql = "SELECT *
FROM uploaded_documents
WHERE dept_cat = 'procedures'
AND cat =:cat
AND keywords REGEXP :term ";
$result->execute(array(':cat' => $_GET['category'],':term' => $_GET['criteria']));
//display results
while($row = $stmt->fetch()){
$explode_criteria = explode(" ",$_GET['criteria']);
foreach($explode_criteria as $key){
$highlight = preg_replace("/".$key."/", "<span class='highlight'>".$key."</span>", $row['name']);
echo '<td><a target="_blank" href="'.$row['url'].'">'.$highlight.'</a></td>';
echo '<td>'.$row['version'].'</td>';
echo '<td>'.$row['cat'].'</td>';
echo '<td>'.$row['author'].'</td>';
echo '<td>'.$row['added'].'</td>';
echo '<td>'.$row['auth_dept'].'</td>';
echo '<td>';
}
}
For the sake of length I have omitted code here and tried to keep it minimal, I have been trying to base my work on the following post
highlighting search results in php/mysql
I think my first problem is the foreach loop in the while loop duplicating results but I cant think of a way around it.
Thanks in advance
In this block of code:
//display results
while ($row = $stmt->fetch())
{
$explode_criteria = explode(" ", $_GET['criteria']);
foreach ($explode_criteria as $key)
{
$highlight = preg_replace("/" . $key . "/", "<span class='highlight'>" . $key . "</span>", $row['name']);
echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
echo '<td>' . $row['version'] . '</td>';
echo '<td>' . $row['cat'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['added'] . '</td>';
echo '<td>' . $row['auth_dept'] . '</td>';
echo '<td>';
}
}
The loop is constantly referring to $row['name'], so the replacement is done, but the next time the loop happens it is replacing the next word on the original unmodified $row['name']
I think this should help you:
//display results
while ($row = $stmt->fetch())
{
$explode_criteria = explode(" ", $_GET['criteria']);
$highlight = $row['name']; // capture $row['name'] here
foreach ($explode_criteria as $key)
{
// escape the user input
$key2 = preg_quote($key, '/');
// keep affecting $highlight
$highlight = preg_replace("/" . $key2 . "/", "<span class='highlight'>" . $key . "</span>", $highlight);
echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
echo '<td>' . $row['version'] . '</td>';
echo '<td>' . $row['cat'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['added'] . '</td>';
echo '<td>' . $row['auth_dept'] . '</td>';
echo '<td>';
}
}

Categories