Here is my code ,output and doubt ....
<?php
$csvData = file_get_contents("http://feed2.abc.com/PriceWs/PriceSnap.asmx/PriceUpdate?item=GC-APR16,SI-MAY16,PA-JUN16,PL-APR16,CL-APR16,BRCL-MAY16,NG-APR16,HO-APR16,HG-MAY16,CC-MAY16,KC-MAY16,CN-MAY16,CT-MAY16,WH-MAY16,SB-MAY16,SY-MAY16,BO-MAY16,&col=last,bid,ask,high,low");
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line) {
$array[] = str_getcsv($line);
}
foreach($array as $k=>$v){
?>
<table border="1">
<tr>
<td> <?php echo $v[0]; ?><td>
<td> <?php echo $v[1]; ?><td>
<td> <?php echo $v[2]; ?><td>
</tr>`enter code here`
<?php
}
?>
OUTPUT WILL BE LIKE THIS:
GC-APR16 1234.40 1233.30
SI-MAY16 15.180 15.180
PA-JUN16 538.10 539.10
PL-APR16 954.50 955.60
HG-MAY16 2.0750 2.0745
CC-MAY16 2851.0 2849.0
KC-MAY16 119.90 119.90
CN-MAY16 361.75 361.50
CT-MAY16 59.44 59.42
WH-MAY16 460.25 460.00
SB-MAY16 14.33 14.33
SY-MAY16 905.00 905.00
BO-MAY16 33.87 33.86
HERE IS MY DOUBT:
INSTEAD OF GC-APR16 need to display "GOLD", SI-MAY16 need to display "SILVER"
PLEASE HELP....
MANY THANKS
$v[0] = GC-APR16 1234.40 1233.30';
// get first two chars
$two_chars = substr($v[0]);
// "multi if-else"
switch($two_chars) {
case 'GC': $new_tx = 'GOLD'; break;
...
default: ; //show error
}
echo $new_tx;
Related
I use PHP to access some data which is contained in a text file.
Here is a sample of the text file (myfile.txt) - each line has three fields separated by ||:
4e84||some text||category A
f17b||words words||category B
f7ac||some more text here||category B
8683||text text||category C
b010||more text||category A
fcc4||more text||category B
we47||more text||category C
08ml||more text||category A
This is the PHP code I use to show the contents of the txt file in a simple HTML table.
I access the file and I loop through each line to extract the three parts:
<?php
$lines = file("myfile.txt");
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>TEXT</th>
<th>CATEGORY</th>
</tr>
</thead>
<tbody>
<?php
foreach ($lines as $line) {
list($id,$text,$category) = explode('||', $line);
echo '<tr>';
echo '<td>'.$id.'</td>';
echo '<td>'.$text.'</td>';
echo '<td>'.$category.'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
I need to sort the lines according to the third field (category), so as to show the entries of category A, then B and then C.
I tried to use the sort() command within the foreach loop, with no success.
Any ideas?
You can use next approach:
$split_lines = [];
// first - split lines and put them into array
foreach ($lines as $line) {
$split_lines[] = explode('||', $line);
}
// sort array by function
usort($split_lines, fn($a,$b)=>$a[2]<=>$b[2]);
// show array as table
foreach ($split_lines as $line) {
echo '<tr>';
echo '<td>'.$line[0].'</td>';
echo '<td>'.$line[1].'</td>';
echo '<td>'.$line[2].'</td>';
echo '</tr>' . PHP_EOL;
}
run PHP online
You can achive it using two for loop only.
<?php
$lines = file("myfile.txt");
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>TEXT</th>
<th>CATEGORY</th>
</tr>
</thead>
<tbody>
<?php
$listOfFiles = array();
foreach ($lines as $line) {
list($id,$text,$category) = explode('||', $line);
array_push($listOfFiles,$category."||".$text."||".$id);
}
sort($listOfFiles);
foreach($listOfFiles as $line)
{
list($category,$text,$id) = explode('||', $line);
echo '<tr>';
echo '<td>'.$id.'</td>';
echo '<td>'.$text.'</td>';
echo '<td>'.$category.'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
hi everybody i want to dom a webpage and fetch data from it's table.
table code :
<tbody>
<tr class="sh" onclick="ii.ShowShareHolder('21711,IRO1DMOR0004')">
<td>person</td>
<td><div class="ltr" title="1,100,000">1 M</div></td>
<td>2.050</td>
<td>0</td>
<td><div class=""></div></td>
</tr>
<tr class="sh" onclick="ii.ShowShareHolder('42123,IRO1DMOR0004')">
<td>person</td>
<td>953,169</td>
<td>1.780</td>
<td>0</td>
<td><div class=""></div></td>
</tr>
</tbody>
this table has two kind of data bigger than 1M and smaller than 1M . i want to get the 1.100.000 td div data and 953.169 data on this table.
my code is below.it works fine for bigger than 1M data but i don't know how to get the smaller data on this table.
foreach ($tables as $table) {
foreach ($table->find('tr') as $row) {
foreach($row->find('div') as $div)
{
if(array_key_exists('title',$div->attr))
{
$data[] = str_replace(",","",($div->attr['title']));
}
}
}
}
tnx man i use your code but it doesn't work and have many error.
this is my complete functions code.because the server is gzip encode i read with curl .
$url = "http://tsetmc.com/Loader.aspx?Partree=15131T&c=IRO1DMOR0004";
$curl = curl_get_data($url);
if(!empty($curl) ){
$html = str_get_html($curl);
$xml = simplexml_load_string($html);
var_dump($xml);
$data = [];
// For each <tr>
foreach ($xml->tr as $row) {
// check path `<td><div title="">`
$result = $row->xpath('td/div[#title]');
if (! empty($result)) {
foreach ($result as $item) {
$data[] = str_replace(',', '', $item['title']);
}
}
else {
// if not found, check the 2nd <td>
$result = $row->xpath('td[position()=2]');
foreach ($result as $item) {
$data[] = str_replace(',', '', $item);
}
}
}
return $data;
}
You could check if the <div title=""> exist. If true, get that value, else, get the value of the second <td>.
Here is an example using SimpleXML:
$html = <<<HTML
<tbody>
<tr class="sh" onclick="ii.ShowShareHolder('21711,IRO1DMOR0004')">
<td>person</td>
<td><div class="ltr" title="1,100,000">1 M</div></td>
<td>2.050</td>
<td>0</td>
<td><div class=""></div></td>
</tr>
<tr class="sh" onclick="ii.ShowShareHolder('42123,IRO1DMOR0004')">
<td>person</td>
<td>953,169</td>
<td>1.780</td>
<td>0</td>
<td><div class=""></div></td>
</tr>
</tbody>
HTML;
// parse HTML
$xml = simplexml_load_string($html);
$data = [];
// For each <tr>
foreach ($xml->tr as $row) {
// if not found, check the 2nd <td>
$item = $row->children()[1];
// check if a div with title exists
if (isset($item->div['title'])) {
$data[] = str_replace(',', '', $item->div['title']);
}
else { // else, take the content
$data[] = str_replace(',', '', $item);
}
}
var_dump($data);
Output:
array(2) {
[0]=>
string(7) "1100000"
[1]=>
string(6) "953169"
}
See the live demo.
I have following example PHP code and its working great! but i just want one more addition so it print line number too.
<?php
$path = shell_exec('cat data.txt');
$path = chop($path,"\n");
$lines = explode("\n",$path);
echo "<h2>List of Studies</h2>";
foreach($lines as $line) {
echo "<h3><p>$line</p></h3>";
}
?>
Output:
ABC
XYZ
123
I want following addition and add counter in it.
1. ABC
2. XYZ
3. 123
You can assign the index to variable too, not just the value:
foreach($lines as $index => $line) {
printf('<h3><p>%d. %s</p></h3>', $index + 1, $line);
}
If you don't want to use the index php, you can use the tag ol
Reff: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_lists
$i=1;
foreach($lines as $line) {
echo "<h3><p>$i. $line</p></h3>";
$i++;
}
<?php
$path = shell_exec('cat data.txt');
$path = chop($path,"\n");
$lines = explode("\n",$path);
echo "<h2>List of Studies</h2>";
$c = 0;
foreach($lines as $line) {
$c++
echo "<h3><p>".$c.". ".$line."</p></h3>";
}
?>
http://www.php.net/manual/en/control-structures.foreach.php
foreach($lines as $i=>$line) {
echo "<h3>$i. <p>$line</p></h3>";
}
Firstly I apologise for asking a question which must be somewhat rookie or simple, but I cannot seem to find any code that I've researched to solve my problem.
Notice: Undefined offset: 1 in C:\Users\Joshua\Desktop\USBWebserver v8.5\8.5\root\Assignment\Table.php on line 14
This error continues from Offset 1 - 7.
The code that I am currently using to pull the data from the CSV file is as follows:
<?php
$fp = fopen ("quotes.csv","r");
if (!$fp) {echo "<p>Unable to open remote file.</p>"; exit;}
?>
<table>
<tr><th>ID</th> <th>Price</th> <th>Volume </th></tr>
<?php
$i=0;
while (!feof($fp)):
$line = fgets($fp, 2048);
$out[$i] = array($line);
list ($ID, $Price, $StockDate, $StockTime, $Change, $DayHI, $DayLOW, $Volume,) = explode(",", $out[$i][0]);
echo "<tr><td>$ID</td> <td>$Price</td> <td>$StockDate </td><td>$StockTime</td> <td>$Change</td> <td>$DayHI</td> <td>$DayLOW</td> <td>$Volume</td></tr>";
$fp++;
$i++;
endwhile;
?>
</table>
<?php
//echo "<p>".$out[0][0]."</p>";
//echo "<p>".$out[1][0]."</p>";
//echo "<p>".$out[2][0]."</p>";
fclose($fp);
?>
I'm unsure as to how something is undefined as the correct number of values are being called from the CSV.
I understand this isn't a site to teach someone PHP basics but any advice would be appreciated to help solve the problem, I would be appreciative!
I think you didn't want to +1 on file pointer:
$fp++;
Removing this line should do the trick.
UPDATE:
You should also check fgets if it's not false, because it means end of file:
$line = fgets($fp, 2048);
if($line === false) break;
Fully working example:
<?php
$fp = fopen ("quotes.csv","r");
if (!$fp) {echo "<p>Unable to open remote file.</p>"; exit;}
?>
<table>
<tr><th>ID</th> <th>Price</th> <th>Volume </th></tr>
<?php
$out = array();
$i=0;
while (!feof($fp)):
$line = fgets($fp, 2048);
if($line === false) break;
$out[$i] = array($line);
list ($ID, $Price, $StockDate, $StockTime, $Change, $DayHI, $DayLOW, $Volume) = explode(",", $out[$i][0]);
echo "<tr><td>$ID</td> <td>$Price</td> <td>$StockDate </td><td>$StockTime</td> <td>$Change</td> <td>$DayHI</td> <td>$DayLOW</td> <td>$Volume</td></tr>";
$i++;
endwhile;
?>
</table>
<?php
echo "<p>".$out[0][0]."</p>";
fclose($fp);
Last echo gave me my first string line from .csv.
Try making a single variable for the exploded values and check if the variable contains all the data
$temporal_var = explode(",", $out[$i][0]);
print_r($temporal_var);
Do you have initialized the $out array? moreover for what i see you don't need the $i index since you can just call $out[] = array($line); to append an element to the array
Try this approach, using the native php CSV handler:
<?php
// Set a list of headers
$ths[0] = 'ID';
$ths[1] = 'Price';
$ths[2] = 'StockDate';
$ths[3] = 'StockTime';
$ths[4] = 'Change';
$ths[5] = 'DayHI';
$ths[6] = 'DayLOW';
$ths[7] = 'Volume';
// Open the file with error handling
$fp = fopen('quotes.csv', 'r');
if( !$fp ) { die('<p>Unable to open remote file.</p>'); }
// Get the size
$fz = filesize('quotes.csv') + 1;
// Create a Data holder array
$fpData = array();
// While there is a line. Use native fgetcsv()
while ( $fpRow = fgetcsv( $fp, $fz, ',' ) ) {
// For each element in the row we asign it to its header
for ($i=0; $i < count($fpRow); $i++) {
$thisRow[ $ths[$i] ] = $fpRow[ $i ];
}
// Append to the Data array
$fpData[] = $thisRow;
}
// Close the file
fclose($fp);
// Test the output
// print_r( $fpData );
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Get CVS</title>
</head>
<body>
<table>
<thead>
<tr>
<?php foreach ($ths as $thisField): ?>
<th><?php echo $thisField ?></th>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach ($fpData as $thisItem): ?>
<tr>
<td><?php echo $thisItem['ID'] ?></td>
<td><?php echo $thisItem['Price'] ?></td>
<td><?php echo $thisItem['StockDate'] ?></td>
<td><?php echo $thisItem['StockTime'] ?></td>
<td><?php echo $thisItem['Change'] ?></td>
<td><?php echo $thisItem['DayHI'] ?></td>
<td><?php echo $thisItem['DayLOW'] ?></td>
<td><?php echo $thisItem['Volume'] ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>
<table>
<tr>
<th>Products</th>
</tr>
<td>
<?
foreach ($invoice->cart->items as $id => $item) {
?>
<?=$item->getName()?>
<?
}
?>
</td>
</table>
If I have 4 Products then I want to separate with coma ?
For example if I have 4 Products:-
Rubber Ducky™ Gold fish & Tank David Byrne and My Fair Lady - MP3 file download (0 MB)
I want the output with coma like this
Rubber Ducky™, Gold fish & Tank, David Byrne, My Fair Lady MP3 file download (0 MB)
how it is possible with PHP?
<?php
$first = true;
foreach ($invoice->cart->items as $id => $item) {
if ($first) {
$first = false;
} else {
echo ', ';
}
echo $item->getName();
}
?>
This will print a ', ' between item names.
Alternatively, you could also do that:
<?=implode(', ', array_map(function($item) { return $item->getName(); }, $invoice->cart->items))?>
Use implode function. Something like
$array = $invoice->cart->items;
echo implode(',',$array);
$i = 0;
foreach( $invoice->cart->items as $item ) {
echo ($i?', ':'').$item->getName();
++$i;
}