Changing column in HTML table to hyperlink - php

for ($i=0; $i<=$lines; $i++)
{
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
This is my code, it read's from a text file and explode in table, but I have a little problem here cause this one needs to be link.
<td width="20%">'.$part[2].'</td>
.$part[2]. is just a word from file but it has query like www.somesite.com/?q= There at the end I need to have that
word from file
that kind of code did not work for me
<td width="20%"> <a herf='www.somesite.com/?q=''.$part[2].'> '.$part[2].' </a> </td>
I realy need some help with this...
<?php
//first, get the file...
$file = file('req.txt');
//now count the lines ..
$lines = count($file);
//start the table here..
echo'<table border="2" width="100%">';
echo'<tr>
<td width="20%">Naslov</td>
<td width="20%">Vrsta</td>
<td width="20%">IP</td>
<td width="20%">Dodano (DD.MM.YY - HH.MM)</td>
<td width="20%">Status</td>
</tr>';
//start the loop to get all lines in the table..
for ($i=0; $i<=$lines; $i++) {
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
//close the table so HTML wont suffer :P
echo'</table>';
?>
This should produce this but ip column need to be link...

I think vprintf() is your friend.
<?php
$fmt = '<tr>
<td>%1$s</td>
<td>%2$s</td>
<td>%3$s</td>
<td>%4$s</td>
<td>%5%s</td>
</tr>';
for ($i=0; $i<=$lines; $i++)
{
// get each line and explode it..
$part = explode('|', $file[$i]);
// now start printing ..
vprintf($fmt, $part);
}
And put the width="20%" into your CSS.

I solve it alone with changing some values in input script "file writer"
$savestring = $title . "|" . $genre . "|<a href=http://www.example.com/ip?ip=" . $ip . ">" . $ip . "|" . $date . "|Za Naložit \n";
it works now ty anyway :)

Related

Proper displaying of csv file data in table if some fields are empty

I want to display a .csv file neatly in a table using php. Some content of the file are empty field.
Here is my first approach, got an error but displaying the datas in one row. Supposedly they should display in their respectively fields.
The code output here:
Should be like this:
<table border = "1">
<tr>
<th>NAME</th>
<th>Email</th>
<th>Address</th>
<th>Payment</th>
<th>Datepaid</th>
</tr>
<?php
$data = file("data/payment.csv");
foreach ($data as $line){
$lineofarray = explode("\t", $line);
list($name, $email, $address, $payment, $datepaid) = $lineofarray;//error here
?>
<tr>
<td>
<?= $name?>
</td>
<td>
<?= $email?>
</td>
<td>
<?= $address?>
</td>
<td>
<?= $payment?>
</td>
<td>
<?= $datepaid?>
</td>
</tr>
<? }?>
</table>
My code now had an error on the part of list. And displaying the datas in one field. Hope you can help me. Thanks
Please try this.
$fp = fopen("data/payment.csv", "r");
while (($line = fgetcsv($fp)) !== false) {
echo "<tr>";
foreach ($line as $field) {
echo "<td>" . htmlspecialchars($field) . "</td>";
}
echo "</tr>\n";
}
This is a safe way to read csv lines as it is unicode safe and also supports fields that are wrapped in double quotes.
You should use fgetcsv:
$fp = #fopen('data/payment.csv', 'r');
$tbl = '<table><thead><tr><th>NAME</th><th>Email</th><th>Address</th><th>Payment</th><th>Datepaid</th></tr></thead><tbody>';
while($r = fgetcsv($fp)){
$tbl .= '<tr>';
foreach($r as $v){
$tbl .= '<td>'.htmlentities($v, ENT_QUOTES, 'UTF-8').'</td>';
}
$tbl .= '</tr>';
}
$tbl .= '</tbody></table>';
echo $tbl; // echo $tbl wherever you want

PHP - search_array

I have one Multiple Array...
File "user.php" --> imports the data from the "zebra_output_input.json" File and outputs a Zebra-Style Table with a list of all users and projects. One user can have more then one project, so it can have more then one line of text in the table (more then one array).
I have a sessions assigned to different users ("$loggedUser = $_SESSION["user"];") after the successful login, so I should now be able to filter the output so that only the data (lines of text - array) that matters for each user shows up.
Something like: first two lines (Arrays 0 and 1) gets User1, second one goes to the User2 ... last two ones for User5.
There is no databank involved in preprocessing the data. Right now, every user has its own JSON file (like it would be if the databank would give me the results). That's a bit impractical in this case since there is a project list for all users + one extra list for each and every user.
Some way to do it?
Array:
[{"Nr":"146","Kuerzel":"COUVERTIC","Projekttitel":"Arbeiten Output Management","Kunde":"COUVERTI","User":"User1"},{"Nr":"147","Kuerzel":"CEBILL","Projekttitel":"Vom PDF zur eRechnung","Kunde":"COUVERTI","User":"User1"},{"Nr":"157","Kuerzel":"KALAIDOS","Projekttitel":"Kurse bei Kalaidos","Kunde":"KFS","User":"User2"},{"Nr":"158","Kuerzel":"LPCH","Projekttitel":"Einsatz Harald M\u00fcller als Syst. Eng & W'Inform","Kunde":"LP","User":"User3"},{"Nr":"152","Kuerzel":"INFONOVA","Projekttitel":"PrintMachine","Kunde":"NEOPOST","User":"User4"},{"Nr":"1","Kuerzel":"AB","Projekttitel":"Allgemeine B\u00fcroarbeiten","Kunde":"INTERN","User":"User5"},{"Nr":"2","Kuerzel":"KA","Projekttitel":"Krank, Arzt","Kunde":"INTERN","User":"User5"}]
Processing file:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<title>unbenannt</title>
<style>
table {background-color:white;}
th {background-color:#58ACFA;}
.cssEven {background-color:#E0ECF8;}
.cssOdd {background-color:#b8d3ef;}
</style>
</head>
<body>
<br><br>
<h1><img class="fehleranimation" src="../img/key36.png"> Willkommen im geschützten Bereich '<?= $_SESSION["user"]?>'.</h1>
<br><br>
<table>
<tr>
<th width="50" align="right">Nr</th>
<th width="150" align="left">Kuerzel</th>
<th width="350" align="left">Projekttitel</th>
<th width="150" align="left">Kunde</th>
<th width="100" align="center">User</th>
</tr>
<?php
$loggedUser = $_SESSION["user"];
//$userfile = file_get_contents('projekte_'.$loggedUser.'.json');
$userfile = file_get_contents('zebra_output_input.json');
$jsonarray = json_decode($userfile);
$index = count($jsonarray);
for ($i = 0; $i < $index; $i++) {
$style = "cssOdd";
//$_SESSION['user'] = $jsonarray[$i]->User;
if ($i % 2 != 0) {
$style = "cssEven";
}
if($loggedUser === $jsonarray[$i]->User) {
echo
'<tr class="'.$style.'">
<td align="right">'. $jsonarray[$i]->Nr.' </td>
<td>'. $jsonarray[$i]->Kuerzel.' </td>
<td>'. $jsonarray[$i]->Projekttitel.' </td>
<td>'. $jsonarray[$i]->Kunde.'</td>
<td align="center">'. $jsonarray[$i]->User.' </td>
</tr>';
}
}
?>
</table>
</body>
</html>
Edited
Some points to consider.
If you are fetching the data from the database do the filtering there and no need to fetch all the data and then filter in php.
If you are reading this array from some other source, like a file maybe, then just loop through each one of the array and check if the user matches the user stored in $_SESSION.
I think you're looking for an identifier.
You need to match the $_SESSION['user'] with the corresponding
$jsonarray[$i]->User.
Hope it helps!
$loggedUser = $_SESSION["user"];
$userfile = file_get_contents('projekte_'.$loggedUser.'.json');
$jsonarray = json_decode($userfile);
$index = count($jsonarray);
for ($i = 0; $i < $index; $i++) {
$style = "cssOdd";
if ($i % 2 != 0) {
$style = "cssEven";
}
if($loggedUser === $jsonarray[$i]->User) {
echo
'<tr class="' . $style . '">
<td align="center">' . $jsonarray[$i]->User . ' </td>
<td align="right">' . $jsonarray[$i]->Nr . ' </td>
<td>' . $jsonarray[$i]->Kuerzel . ' </td>
<td>' . $jsonarray[$i]->Projekttitel . ' </td>
<td>' . $jsonarray[$i]->Kunde . '</td>
</tr>';
}
}

Seperate comma separated mySql database field value with php

If you believe this is a duplicate please let me now as I'm not totally sure what to search for to check, (I will remove if duplicate)
I have a list of of numbers in a mySql field called sess_times in a table called test_sess, the numbers for the first row are:
25, 38, 40, 50
is it possible to split these into php variable e.g:
$no1 = 25
$no2 = 38
$no3 = 40
$no4 = 50
I'm trying to put the individual numbers in a table, something like below:
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td> 1 </td>
<td><?php echo $no1; ?></td>
</tr>
<tr>
<td> 2 </td>
<td><?php echo $no2; ?></td>
</tr>
<tr>
<td> 3 </td>
<td><?php echo $no3; ?></td>
</tr>
<tr>
<td> 4 </td>
<td><?php echo $no4; ?></td>
</tr>
</table>
Any help with this would be great so thanks in advance for any answers.
HERE'S THE SOLUTION I USED, A COMBINATION OF 2 ANSWERS BELOW:
$data = '25,38,40,50';
$string = explode(",", $data);
$number = 0;
echo '<table cellspacing="0" cellpadding="0" border="0">';
for($i=0; $i<count($string); $i++)
{
$number = $number +1;
echo '
<tr>
<td>' . $number . '</td>
<td>' . $string[$i] . '</td>
</tr>
';
}
You can use PHP's explode function.
When you output everything, save it into a variable.
Then, $string = explode(",", $string); will produce this:
Array ( [0] => 28[1] => 38 [2] => 40 [3] => 50 )
To output this, you can then use $string[number] for each value.
You can explode them
$string="25,38,40,50";
$numbers=explode(",",$string);
print_r($numbers); // You can use them for printing like echo $numbers[0]; or 1 or 2 or 3
And if you don't want the result in an array and still want those 4 variable names you can do this
$string="25,38,40,50";
list($no1,$no2,$no3,$no4)=explode(",",$string);
This would give you the table output and it doesn't require the total amount to be known.
$data = '25,38,40,50';
$arr = explode(',', $data);
echo '<table cellspacing="0" cellpadding="0" border="0">';
for($i=0; $i<count($arr); $i++)
{
echo '
<tr>
<td>' . $i+1 . '</td>
<td>' . $arr[$i] . '</td>
</tr>
';
}
echo '</table>';
Use explode();
ex:
$numbers='25,38,40,50';
$numbers_array= explode(",", $no);
foreach($numbers_array as $key => $number)
{
$var_name='no'.(++$key);
$$var_name=$number;
}
echo $no1.'<br>;
echo $no2.'<br>;
echo $no3.'<br>;
echo $no4.'<br>;

Removing the last element in a do-while

How can I remove the last element in a do-while generated table?
In my case the last tr/td where div.dividing_line is stored.
The code:
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
++$i;
} while ($i < $ArrayLength+1);
For example: If I have an array with 6 items, normally the do-while will do the job, so finally there will be 6 tr's with data and 6 tr's with the dividing_line.
What I need is 6 tr's of data and 5 tr's of dividing_line. Is that possible?
Try this-
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i != $ArrayLength) {
echo '<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
}
++$i;
} while ($i < $ArrayLength+1);
Use an extra if Statement to check whether you are at the last element:
if (%i < $ArrayLength) { echo '<tr>...dividing_line</tr>'; }
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i < $ArrayLength)
{
echo '
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>';
}
++$i;
} while ($i < $ArrayLength+1);
I think your approach just needs an additional step.
It could be something like this:
$data = null;
foreach ($rows as $row) {
$data[] = "<tr><td valign=\"middle\">Data_Position</td><td valign=\"middle\">Data_Item</td><td valign=\"middle\">Data_Pieces</td><td valign=\"middle\">Data_Price</td></tr>";
}
print implode("<tr><td colspan=\"4\"><div class=\"dividing_line\"></div></td></tr>", $data);
This way, you could accomplish what you want without any more logic. Of course, it can be changed or re-design, but I think this way will provide you with a simple yet elegan solution to your problem.
Hope it helps :P

Different CSS style on last SQL row using PHP

Problem:
To style the last row in a SQL query without using any CSS3 selectors.
PHP code:
while ($item = mysql_fetch_assoc($itemsresult))
{
$answer = "SELECT IID, Comment FROM betyg_answers WHERE EID = '{$EID}' AND CID = '{$item['CID']}' ORDER BY ID ASC";
$answerresult = mysql_query($answer) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
$answer = mysql_fetch_assoc($answerresult);
if ($answer['IID'] == $item['IID'])
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"><img src="./img/circle_arrow_right.png" class="arrowimage"></td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
else
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"> </td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
$itemcounter++;
}
Last row in SQL query should instead print:
$html .= '
<tr>
<td class="arrow" style="'.$lastarrow.'"> </td>
<td class="numbers" style="'.$lastnumbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$lasttext.'">'.$item['Description'].'</td>
</tr>
';
Question:
What needs to be added in the while loop so it recognizes the last row and print a different code instead?
Use a counter:
$i = 0;
$c = mysql_num_rows($itemresult);
while ($item = mysql_fetch_assoc($itemsresult)) {
$i++;
if ($i == $c) {
// This is a last row
} else {
// Regular row
}
}
there is several ways to do that :
use :last-child ( but this isn't working in IE8)
table tr:last-child {
background-color:#ff0000;
}​
using jQuery method ( this is browser independent)
in document load function, add following jQuery code,
$("td:last-child").css({background-color:"#ff0000"})

Categories