I'm just getting into php, html etc and am trying to resolve an issue at work. Please be patient with my lack of experience.
I have a csv file, TEST2.csv. it is as follows advisor,guest,stockNumber,part,qty,partnote1,partnote2,status that I am putting on a webpage so that our staff can select their advisor number and see the status of their parts. The report we export from our system is in csv and has columns that I do not want to show on this report.
(before I hear I should learn another format to display this, I know, Im under a time constraint and need something quick and rudimentary for now)
Everything works well but it does not show column 7, only up to 4. I want it to skip 5,6 Here's the monster i've created:
<table id="demo">
<tbody>
<?php
$fp = fopen ( "TEST2.csv" , "r" );
$wantedColumns = array(0,1,2,3,4,7);
while (( $data = fgetcsv ( $fp , 1000 , "," )) !== FALSE ) {
$i = 0;
echo "<tr>";
foreach($data as $row) {
if (!in_array($i,$wantedColumns)) continue;
echo "<td>" . $row . "</td>";
$i++ ;
}
echo "/<tr>";
}
fclose ( $fp );
?>
`
Any help would be greatly appreciated!
Using simple array syntax with [index] your code will be:
$fp = fopen ( "TEST2.csv" , "r" );
while (( $data = fgetcsv ( $fp , 1000 , "," )) !== FALSE ) {
echo "<tr>";
echo "<td>" . $data[0] . "</td>";
echo "<td>" . $data[1] . "</td>";
echo "<td>" . $data[2] . "</td>";
echo "<td>" . $data[3] . "</td>";
echo "<td>" . $data[4] . "</td>";
echo "<td>" . $data[7] . "</td>";
echo "</tr>";
}
fclose ( $fp );
Related
From the research i conducted in SO, this is the only other question similar to what i need, but im afraid that i didnt help me (How to replace null results in sql query result table with a string?).
I have a table that prints the Medical History of a Patient, and the table cells are restored from a database.
A portion of the code is down below:
echo "<tr>";
echo "<th>MRI Onset Localisation</th>";
echo "<th>CNS MRI Lesions Y/N </th>";
echo "<th>CNS MRI Lesions No.</th>";
echo "<th>CNS MRI Location</th>";
echo "<th>Person Signing the form</th>";
echo "<th>Documented at</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['Onsetlocalisation'] . "</td>";
echo "<td class='tdclass exempt'>" . $row['smoker'] . '<br>' . $row['cigars'] . '<br>' . $row['cigardate'] . "</td>";
echo "<td>" . $row['onsetsymptoms'] . "</td>";
echo "<td class='tdclass exempt'>" . $row['MRIonsetlocalisation'] . "</td>";
echo "<td>" . $row['MRIenhancing'] . "</td>";
echo "<td class='tdclass exempt'>" . $row['MRInum'] . "</td>";
echo "<td>" . $row['MRIenhancinglocation'] . "</td>";
echo "<td class='tdclass exempt'>" . $row['signer'] . "</td>";
echo "<td>" . $row['reg_date'] . "</td>";
echo "</tr>";
And the MySQL Query that returns those fields is
$sql = "SELECT * FROM tblName WHERE somefield = $somevar";
The thing is... Some of those fields are allowed to have NULL as a value, but for the front end of the app, i want the table cells that contain NULL values to print a standard string such as "N/A" or "Empty" but i need it to be done inside of the php table portion of my code, or so i think...
I found numerous questions about replacing NULL with a string inside of the MySQL query, but i don't care about that since the query doesn't appear at the end user outside of the table.
I tried to implement the coalescing operator in the following way, but no nothing was displayed:
echo "<td class='tdclass exempt'>" . $row['MRInum'] ?? "N/A" . "</td>";
Any help is absolutely welcome.
You need to put the expression in brackets to make it clear which parts are supposed to be evaluated by the null coalescing operator:
echo "<td class='tdclass exempt'>" . ($row['MRInum'] ?? "N/A") . "</td>";
If you don't, it thinks the "<td class='tdclass exempt'>" is part of the text to be checked for existence - which means it always returns true because that hard-coded text always exists.
You can do this in two ways.
From SQL:
COALESCE(fieldName, 'N/A') AS fieldName
will replace NULLs with, you guessed it, 'N/A'.
Otherwise, when you fetch the row,
$row = $stmt->fetch(PDO::FETCH_OBJECT);
you can vet it:
$replacements = [
'MRI_Data' => 'N/A',
'MRI_Type' => [
'001' => 'Single Scan',
'002' => 'Multi Scan',
'003' => 'Pulsed',
'(null)' => 'N/A',
],
];
foreach ($row as $key => $value) {
if (array_key_exists($key, $replacements)) {
if (is_array($replacement[$key])) {
if (empty($value)) {
$value = '(null)';
}
if (array_key_exists($value, $replacement[$key])) {
$row[$key] = $replacement[$key][$value];
}
} else {
if (empty($value)) {
$row[$key] = $replacement[$key];
}
}
}
}
I really need your help.
I´m using this code to write into text file from form:
$data = $_POST['jmeno'] . "\t" . $_POST['prijmeni'] . "\t" . $_POST['ulice'] . "\t" . $_POST['cislo_popisne'] . "\t" . $_POST['mesto'] . "\t" . $_POST['psc']. "\n";
$myfile = fopen("zakaznici.txt", "ab") or die("Unable to open file!");
fwrite($myfile, $data);
fclose($myfile);
This is my code to read from text file:
$filename = 'zakaznici.txt';
$handle = fopen($filename, 'r');
$datain = fread($handle, filesize($filename));
$array = explode("\t",$datain);
echo "<table class='styl'><th>Jméno</th><th>Příjmení</th><th>Ulice</th><th>Číslo popisné</th><th>Město</th><th>PSČ</th><tr>".implode("</tr><tr>",array_map(function($a) {return "<td>".implode("</td><td>",explode("\t",trim($a)))."</td>";},explode("\n",$datain)))."</tr></table>";
And in table it looks like this:
Table
Any advice how to sort it alphabetically? I mean for example, when I choose "Jmeno" in select option a hit "Odeslat", I will get sorted table by column "Jmeno" like this (ivana, stepan, tomas).
When using the explode function, it puts all the values in an array. What you can do before echoing the data, is sorting the array.
https://secure.php.net/manual/en/function.sort.php
$data = "atest\tctest\tptest\tqtest\tbtest\tltest";
$values = explode("\t", $data);
sort($values, SORT_STRING);
echo "<table class='styl'><th>Jméno</th><th>Příjmení</th><th>Ulice</th><th>Číslo popisné</th><th>Město</th><th>PSČ</th>";
echo "<tr>";
foreach($values as $value)
{
echo "<td>" . $value . "</td>";
}
echo "</tr></table>";
Result:
Jméno Příjmení Ulice Číslo popisné Město PSČ
atest btest ctest ltest ptest qtest
I´m getting this.
table
Really don´t know how to do it.
i have an array with multiple user id receiving from another page through header function
$Values= unserialize(urldecode($_GET['vals']));
this line give me such array
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
by using for each loop i am using each user id to retrieve data from database table and displaying into html table with following code
//displaying data into tables -->
$Values= unserialize(urldecode($_GET['vals']));
if(isset($Values))
{
foreach($Values as $user_id)
{
$prevCon['where'] = $user_id;
$prevUser = $user->getRowss($prevCon);
while($row = $prevUser->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['full_name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['country_name'] . "</td>";
echo "<td>" . $row['city_name'] . "</td>";
echo "<td>" . $row['NIC'] . "</td>";
echo "</tr>";
}
}
}
else
{
echo " no data to display";
}
but my loop runs for 36 time each entry against one user id is being display for 6 times and then another user id is used again for 6 times
please do help me i want to display one entry for each user id
This is assignment so not looking for anything perfectly safe and secure, just working.I have table in SQL database, I'm printing down all records, all records have unique reference number, for each row of the database I printed down I gave checkbox with value of the row's ref. number, when I submit them by "POST" everything is working and printing out:
if (!$_POST['checkbox']) {
echo "Your basket is empty.";
} else {
echo "<table border='0' id='games_table' cellspacing='1'>";
echo "<tr id='basket_table_row'>";
echo "<td colspan='3'>" . "Logged: " . $_SESSION['user'] . "</td>";
echo "<td colspan ='2'>" . "OS used on this machine: " . "<script type='text/javascript'>document.write(yourOS())</script><noscript>Computers</noscript>" . "</td>";
echo "</tr>";
echo "<tr id='basket_table_row'>";
echo "<td colspan='5'>" . "You put into the basket these games: " . "</td>";
echo "</tr>";
foreach ($_POST['checkbox'] as $value) {
$_SESSION['basket']=array($value);
$res=pg_query($conn,"select * from CSGames where refnumber='$value'");
while ($a = pg_fetch_array ($res)) {
echo "<tr id='games_table_row'>";
echo "<td>" . $a["refnumber"] . "</td>";
echo "<td>" . $a["title"] . "</td>";
echo "<td>" . $a["platform"] . "</td>";
echo "<td>" . $a["description"] . "</td>";
echo "<td>" . $a["price"] . "</td>";
echo "</tr>";
}
}
echo "</table>\n";
}
but only think which stays recorded in $_SESSION['basket'] is value of the last checkbox but I need all of them (60 or 70).
what Am I doing wrong?
You are overwriting te value of $_SESSION['basket'] at each iteration of the loop.
The last value is the only one stored.
Currently, you are only storing the last value, if you wish to store every value, you should add it like this:
$_SESSION['basket'][] = $value;
foreach($_POST['checkbox'] as $value){
$_SESSION['basket']=array($value);
}
every iteration of your loop is overwriting the value in $_SESSION['basket'], hence you only seeing the last checkbox value.
In every step of foreach, you create a new array in $_SESSION['basket'] with only one item, the current value of $value. It is corrected like this:
// ...
$_SESSION['basket'] = array();
foreach ($_POST['checkbox'] as $value) {
$_SESSION['basket'][] = $value;
// ...
}
// ...
You can directly do it like this
$_SESSION['basket'] = $_POST['checkbox'];
because the data of $_POST['checkbox'] is an array anyway.
What you are doing is looping the $_POST['checkbox'] and saving each data as array to a session.
this $_SESSION['basket'] = $_POST['checkbox'];
and
foreach ($_POST['checkbox'] as $value) {
$_SESSION['basket'][]=$value;
}
will have the same value.
What you have right only saves the last value of $_POST['checkbox']
I have this type of record in an HTML table:
<tr class="simplehighlight" onclick="window.location.href='./games/game191.php';">
<td>06/09/2007</td><td>Jennifer Woods Memorial Grand Prix</td><td>C54</td>
<td>Nikolayev, Igor</td><td>2431</td><td>Parry, Matt</td><td>2252</td><td class="text-center">1-0</td></tr>
I want to read in a delimited file, make an array, and populate the table: (sample record)
game191|06/09/2007|Jennifer Woods Memorial Grand Prix|C54|Nikolayev, Igor|2431|Parry, Matt|2252|1-0
I tried this, but it only displays the last record from the datafile (/games.csv)
<?php
// open delimited data file "|" if needed and read.
//game191|06/09/2007|Jennifer Woods Memorial Grand Prix|C54|Nikolayev, Igor|2431|Parry, Matt|2252|1-0
if(!isset($_SESSION['games_array'])) {$file = $_SERVER['DOCUMENT_ROOT'].'/games.csv';
$fp = fopen($file,"r"); $list = fread($fp, filesize($file));
$_SESSION['games_array'] = explode("\n",$list); fclose($fp);}
// extract variables from each tuple by iteration
foreach ($_SESSION['games_array'] as $v);{
$token = explode("|", $v);
//write the table row and table data
echo "<tr class=\"simplehighlight\" onclick=\"window.location.href='./games/";
echo $token[0]; echo ".php';\">";
echo "<td>";echo $token[1];echo "</td>"; echo "<td>";echo $token[2];echo "</td>";
echo "<td>";echo $token[3];echo "</td>"; echo "<td>";echo $token[4];echo "</td>";
echo "<td>";echo $token[5];echo "</td>"; echo "<td>";echo $token[6];echo "</td>";
echo "<td>";echo $token[7];echo "</td>";
echo "<td class=\"text-center\">"; echo $token[8];echo "</td>";
echo "</tr>";};
?>
What am I missing?
foreach ($_SESSION['games_array'] as $v);{
should be
foreach ($_SESSION['games_array'] as $v) {
Try with file():
$lines = file('filename');
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
do you have the right permissions to access that file? is your php error_reporting set to display all errors? you could try using only relative paths, have you tried that? try using file_get_contents...
It was simpler than I thought... I erred in reusing old junk code from another script.
I found this and it works!
<?php
$text = file('games.csv'); foreach($text as $line) {$token = explode("|", $line);
echo "<tr class=\"simplehighlight\" onclick=\"window.location.href='./games/";
echo $token[0]; echo ".php';\">";
echo "<td>";echo $token[1];echo "</td>"; echo "<td>";echo $token[2];echo "</td>";
echo "<td>";echo $token[3];echo "</td>"; echo "<td>";echo $token[4];echo "</td>";
echo "<td>";echo $token[5];echo "</td>"; echo "<td>";echo $token[6];echo "</td>";
echo "<td>";echo $token[7];echo "</td>";
echo "<td class=\"text-center\">"; echo $token[8]; echo "</td>";
echo "</tr>";};
?>
Let me know if you see any improvements or faster functions. Thank you !