I'm trying to build divs based on unique values. So for each line, the script checks the team name against $tempTeam. If they're equal, the line gets added to the table in the existing div. If they're not equal, a new div and table is created, and the line is added there.
However, the if part isn't recognizing when $tempTeam is equal to the team so it never runs, even though the else part is properly setting $tempTeam. So I'm wondering why the variable is working for the else part of the code, but not the if part.
Here's the full code, although the trouble begins when I first define $tempTeam. Thanks for any help.
<?php
$csv = file_get_contents('schedules.csv');
$csv_array = explode("\n", $csv);
unset($csv_array[count($csv_array) - 1]);
$headers = explode(",", $csv_array[0]);
// iterate through all lines of CSV, skipping first header line
for($i=1;$i<count($csv_array);$i++) {
$line = explode(",", $csv_array[$i]);
// iterate through all headers and assign corresponding line value
foreach ($headers as $index => $header){
$parsed_array[$i][$header] = $line[$index];
}
}
// create divs and tables
$tempTeam = '';
foreach ($parsed_array as $i => $match) {
if ($tempTeam == $match['Team']) {
echo "
<tr><td>$match[Date]</td><td>$match[Result]</td><td>$match[Location]</td><td>$match[Opponent]</td></tr>
";
}
else if ($tempTeam == '') {
$tempTeam = $match['Team'];
echo "
<div class=\"schedule\" data-container=\"$match[League]\">
<table>
<thead>
<tr>
<th colspan=\"4\">$match[Team]</th>
</tr>
<tr>
<th>Date</th><th>Result</th><th>Location</th><th>Opponent</th>
</tr>
</thead>
<tbody>
<tr><td>$match[Date]</td><td>$match[Result]</td><td>$match[Location]</td><td>$match[Opponent]</td></tr>
";
}
else {
$tempTeam = $match['Team'];
echo "
</tbody>
</table>
</div>
<div class=\"schedule\" data-container=\"$match[League]\">
<table>
<thead>
<tr>
<th colspan=\"4\">$match[Team]</th>
</tr>
<tr>
<th>Date</th><th>Result</th><th>Location</th><th>Opponent</th>
</tr>
</thead>
<tbody>
<tr><td>$match[Date]</td><td>$match[Result]</td><td>$match[Location]</td><td>$match[Opponent]</td></tr>
";
}
}
echo "
</tbody>
</table>
</div>
";
?>
For a start, look at the first line of the else clause:
$tempTeam == $match['Team'];
I'm pretty certain that's not what you wanted to do, it seems to me that assignment would probably be a better choice than comparison.
What you're doing is no different to:
$a = 1;
$a == $a + 1;
print ($a);
which will still output 1 rather than 2. If you want do do assignment, it should have just the one = character:
$tempTeam = $match['Team'];
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'm sorry, if this is duplicate post, but somehow i can't get my loop working with variable. if i echo this inside foreach i get my data but if i use variable i get data for only the first item from my array.
my code `
$user - My arrray
$count = $user['count'];
$echo = "";
foreach($user as $val => $key)
{
if($val == 'true')
{
for($x = 0; $x < $count; $x++)
{
$name = $user[$val][$x]['name'];
$id = $user[$val][$x]['id'];
$staatus = $user[$val][$x]['status'];
$feed = $user[$val][$x]['feed'];
/* Now when am using only echo, instead of $echo it will work flawlessly,
but i want to echo it using an variable called $echo.
I call the variable on my html page where the forms are,
but i'm only getting data for the first element.
Short, i want to display my data under the form.*/
$echo = "
<table class='table'>
<tr>
<th>NIMI</th>
<th>ID</th>
<th>STAATUS</th>
<th>FEED</th>
</tr>
<tr>
<td>$name</td>
<td>$id</td>
<td>$staatus</td>
<td>$feed</td>
</tr>
</table>
";
}
}
}
<?php echo $echo; ?> for calling.
I'm quite a beginner in this thing so maybe someone could help. I also searched around, tried different things but it's not working. I also tried to display data without for loop, but it's not working either.
Also if there's any improvments i could do with my code please tell me. Like make it shorter etc, i'm here to improve so why not :D
You're missing the . in your $echo .= "
$echo .= "
<table class='table'>
<tr>
<th>NIMI</th>
<th>ID</th>
<th>STAATUS</th>
<th>FEED</th>
</tr>
<tr>
<td>$name</td>
<td>$id</td>
<td>$staatus</td>
<td>$feed</td>
</tr>
</table>
";
I'm trying to output a table using php and ajax. Now my function gets data from 1 table and then iterate on the row of that table to get rows from different table and then form a table. To achieve this I have put while loop inside foreach loop, the data from the first table is in array, so Im iterating on array using forean and then putting while loop.
This results in only 1 row. Need help
public function GetReportData()
{
$status_list = $this->GetStatusList();
foreach ($status_list as $status)
{
$staus_orignal = $status->orignal_status;
$status_site = $status->site_status;
try
{
$db = $this->GetDBHandle();
$start_date = '05/01/2015';
$end_date = '05/02/2015';
$affiliate_id = 0;
$output_string = '';
$output_string .= '<table class="tg">
<tr>
<th class="tg-031e"><span style="color:#fff;">Disposition Type</span></th>
<th class="tg-yw4l"><span style="color:#fff;">Lead Count</span></th>
<th class="tg-yw4l"><span style="color:#fff;">Revenue</span></th>
</tr>';
$query = "exec affiliate_portal_report_data_select $staus_orignal, $affiliate_id,'". $start_date."','". $end_date."'";
$result = odbc_exec ( $db, $query );
if ( !$result )
{
throw new Exception ( 'Error from ' . $query . ': ' . odbc_errormsg() );
}
else
{
while ( odbc_fetch_row ( $result ) )
{
$lead_count = odbc_result( $result, 'leadcount' );
$revenue = odbc_result( $result, 'revenue' );
$output_string .= '<tr>
<td class="tg-yw4l">'.$status_site.'</td>
<td class="tg-yw4l">'.$lead_count.'</td>
<td class="tg-yw4l dollar">'.$revenue.'</td>
</tr>';
}
}
}
catch ( Exception $e )
{
$error_status = $e->getMessage();
print $error_status;
}
}
$output_string .= '</table>';
return $output_string;
}
There is an $output_string = ''; on line 16. You are re-initializing your output on each iteration of the foreach loop, so you will only ever get the last row.
I am not entirely sure from your question, but if this code is supposed to produce one table, then you can get rid of $output_string = ''; altogether, put this:
$output_string = '<table class="tg">
<tr>
<th class="tg-031e"><span style="color:#fff;">Disposition Type</span></th>
<th class="tg-yw4l"><span style="color:#fff;">Lead Count</span></th>
<th class="tg-yw4l"><span style="color:#fff;">Revenue</span></th>
</tr>';
before the foreach loop, and leave this:
$output_string .= '</table>';
after the foreach loop (like it already is).
But if your code is supposed to produce multiple tables, then you still need to get rid of $output_string = '';, and you can leave the <th> section where it is, but you'll need to move $output_string .= '</table>'; inside the foreach loop, or you will end up with a bunch of unclosed table tags.
As said by #Don'tPanic in comments, you have to initialise your $output_string variable outside of the loop.
Like actual, you are recreating an empty string for each row.
If you build an array or a string by looping another, keep in mind to make the variable declaration outside, and the incrementation inside the loop.
Change your code to :
$output_string = '';
foreach ($status_list as $status) {
// ...
$output_string .= 'yourstring';
// ...
}
Trying to display an html table from and xml from php, getting error when trying to alternate the row base on even and odd mostly for styling the table.
foreach($bookdata as $book) // loop through our books
{
$i = 0;
if($i%2 == 0)
{
$class = 'even';
}
else
{
$class = 'odd';
}
{
echo <<<EOF
<tbody>
<tr class='$class'>
<td>{$book->date} </td>
<td><a href='http://www.website.com{$book->dataNo}.html'>{$book->Name}</td>
<td><a href='http://www.website.com/-{$book->authorcodeNo}.html'>{$book->author}</td>
</tr>
}
$i++;
}
EOF;
}
echo '</tbody>';
echo '</table>';
Any help most welcome
$i = 0;
foreach($bookdata as $book) // loop through our books
{
...
...
//and at end of foreach
$i++;
You are redeclaring $i inside of your for loop so it's never going to actually increase, just get reset to 0 every single time. Also, not sure what's up with some of your curly braces as there's not enough code to see it all as far as I can tell... start by moving your variable declaration outside the for loop though!
You are reseting the $i to 0 on every loop.
Remove
$i = 0;
from your code. And I didn't notice this before but the EOF is misplaced. Here is a full working solution
foreach($bookdata as $book) // loop through our books
{
if($i%2 == 0) { $class = 'even'; }
else { $class = 'odd'; }
echo <<<EOF
<tbody>
<tr class='$class'>
<td>{$book->date} </td>
<td><a href='http://www.website.com{$book->dataNo}.html'>{$book->Name}</td>
<td><a href='http://www.website.com/-{$book->authorcodeNo}.html'>{$book->author}</td>
</tr>
EOF;
$i++;
}
try put the $i=0 out of foreach loop.