let's say if I have a txt file and inside has info sample like this:
amy,anderson,aldergrove,archery,anchovies,110
bill,bonds,burnaby,bowling,beer,100
cameron,carson,cameroon,cars,candy,120
henry,henderson,harrison,helping,hamburgers,90
dorothy,dust,denmark,driving,drinks,80
ed,edmunson,edmonton,eating,eggs,77
fred,fredrickson,fernie,flying,fries,140
and I want to use the file() and preg_split() function to call it out and show as a table what's the easiest way to do it?
I know how to call it out using file() function but I'm not sure how to replace the , and make it look like a table.
http://et4891.site90.com/sample.jpg <---this is a sample of how I want it to look like.
Below is what I did to call out the txt file.
<?php
$fileContentsArray = file("aaa.txt");
echo "<table>";
foreach($fileContentsArray as $one_persons_data)
{
echo "<tr>$one_persons_data</tr>";
}
echo "</table>"
?>
how should I modify this to make it look like the image I posted?
Thanks in adavance....
Is preg_split required? Better to use explode in this case. Anyway:
<?php
$fileContentsArray = file("aaa.txt");
echo "<table>";
foreach($fileContentsArray as $one_persons_data)
{
echo '<tr>';
$splitted = preg_split('/,/', $one_persons_data);
foreach ($splitted as $one) {
echo "<td>$one</td>";
}
echo '</tr>';
}
echo "</table>"
You can do this:
<?php
$rows = file('data.txt');
echo '<table>';
foreach($rows as $row){
echo '<tr>';
foreach(explode(',',$row) as $field){
echo '<td>';
echo htnlentities($field);
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
Hope this can help you.
Related
I need to convert JSON into a list using PHP, Tried code below but cannot make it work
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$data = json_decode($json);
if (count($data->stand)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->stand as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
And I want to show list as here (not as a table):
http://prntscr.com/no1479
your all code is right but you can use stand class that is wrong your class is GetTickerJSONResult and so change the class stand to GetTickerJSONResult.
try this modified code..
<?PHP
$set =json_decode($json);
if (count($set->GetTickerJSONResult)) {
echo "<table>";
foreach ($set->GetTickerJSONResult as $idx => $stand) {
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
echo "</table>";
}
?>
It does not work because in $data->stand there is nothing.
I am newer to PHP and I am able to get the desired output but I am doing it one index position at a time. I am returning data from a .txt file and I need to insert this data into an HTML table I am creating using PHP. BUT FIRST I need to be able to get the same output without typing out every index position. I tried to use a forloop but it kept outputting only one line.
I manually outputted the lines from the file and it works. What loop in PHP would be best to achieve the same results and output these elements? IMPORTANT, as is I am able to sort and rsort (I want to be able to do this so if it can be implemented in the loop that would be awesome) any help is more than I have right now.
PHP
$books = array();
if ($fp)
{
while(true)
{
$lines_in_file = count(file($filename));
$line = fgets($fp);
if (feof($fp))
{
break;
}
$line_ctr++;
list($title, $author, $pubdate, $isbn) = explode('*', $line);
$new_line = explode('*', $line);
for($ii= 1; $ii <= $lines_in_file; $ii++){
$lines = fgets($fp); //Read each line
$member = trim($lines);
array_push($books, $member);
}
//This foreach only brings back the first like in the txt file, why?
$cntr = 0;
foreach($books as $element){
$cntr++;
$table .= "<tr>";
$table .= "<td>".$title."</td>";
$table .= "<td>".$author."</td>";
$table .= "<td>".$pubdate."</td>";
$table .= "<td>".$pubdate."</td>";
$table .= "<td>".$isbn."</td>";
$table .= "</tr>\n"; //added newline
echo $element;
}
//sort($books);
// rsort($books);
echo $books[0];
echo "<br>";
echo $books[1];
echo "<br>";
echo $books[2];
echo "<br>";
echo $books[3];
echo "<br>";
echo $books[4];
echo "<br>";
echo $books[5];
echo "<br>";
echo $books[6];
echo "<br>";
echo $books[7];
echo "<br>";
echo $books[8];
echo "<br>";
echo $books[9];
echo "<br>";
echo $books[10];
echo "<br>";
echo $books[11];
echo "<br>";
echo $books[12];
echo "<br>";
echo $books[13];
echo "<br>";
echo $books[14];
echo "<br>";
echo $books[15];
echo "<br>";
echo $books[16];
echo "<br>";
echo $books[17];
}//END WHILE LOOP
fclose($fp ); //Close file
}
Having to make a few guesses here but i believe the file is going to look like:
title*author*pubdate*isbn
title*author*pubdate*isbn
title*author*pubdate*isb
if this is wrong, let me know
as long as the fie is not to large read it in to an array:
$book_array=file('book_file.txt');
//print_r($book_array); //is it an array of the books, one book per array key
now to separate each line:
foreach($book_array as $line){
$line_sep=explode('*',$line);
// with no sorting option you could just echo inside the loop
//if you want to keep sorting options we have to keep the separated lines
$new_book_array[]=$line_sep;
}
unset($book_array);//free up memory if needed
//print_r($new_book_array);//is it a multi-d array, book then the 4 elements
to sort our new multidimensoanl array:
usort($new_book_array, function($a, $b) {
return strcmp($a['0'], $b['0']);;
}); //this sorts on key 0 in your case title:
//print_r($new_book_array); // has it sorted properly
for display:
loop again
echo '<table><tr><th>Title</th><th>Author</th><th>Pub Date</th><th>ISBN</th></tr>';
foreach($new_book_array as $book){
//print_r($book); //is it each indervidual book array with 4 elements
echo '<tr><td>'.$book[0].'</td><td>'.$book[1].'</td><td>'.$book[2].'</td><td>'.$book[3].'</td></tr>';
}
echo '</table>';
i have the following php code:
// Add the unsubscribers to an array
$unsubs = array();
foreach($unsubscribers->response->Results as $entry2) {
$unsubs[] = $entry2->EmailAddress;
}
// Loop through the subscribers
foreach($result->response->Results as $entry) {
echo '<tr class="'. $entry->EmailAddress.'">';
echo '<td>'. $entry->EmailAddress.'</td>';
echo '<td></td><td>';
// If the subscriber is in our unsubscriber array, output the email again
if(in_array($entry->EmailAddress, $unsubs)) {
echo $entry->EmailAddress;
}
echo '</td><td></td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
}
Where the empty <td></td> are located i would like to place the following:
$getlists = new CS_REST_Campaigns($_POST['campaign_id'], $auth);
$getlistsresult = $wrap->get_lists_and_segments();
foreach($getlistsresult->response->Lists as $list) {
//echo $list->ListID;
}
$wrapcount = new CS_REST_Subscribers($list->ListID, $auth);
$resultcount = $wrapcount->get_history($entry->EmailAddress);
foreach($resultcount->response as $entrycount) {
$counts = array();
foreach($entrycount->Actions as $actions) {
if(!isset($counts[$actions->Event])) {
$counts[$actions->Event] = 0;
}
++$counts[$actions->Event];
}
echo '<td>';
if ($counts['Click']){echo $counts['Click'];}
echo '</td>';
echo '<td>';
if ($counts['Bounce']){echo 'Yes';}
echo '</td>';
echo '<td>';
if ($counts['Open']){echo $counts['Open'];}
echo '</td>';
}
This works to a degree, but the load time of the page is dramatically increased. I think to be honest my code will need tidying up. Any suggestions on how to speed this up?
There's not much I can see that is blatantly unoptimized in your code, there are functions calls that I don't know about, from your CS_REST classes, but we don't know what these functions do or if they can be slow or optimized.
With this information, the only thing I can see that might help you is using the SplFixedArray class. This will be notably useful if you have a lot of entries in your arrays and do a lot of operations on them. Basically, they are similar to real arrays, in the way that their index is always an integer and they have a fixed size, which in turn makes them faster to use.
I am trying to echo a array value in a link, but it is coming up in dreamweaver as an error, but I cant work out what I have done wrong, can anyone tell me what is wrong with this line please ?.
thanks :-)
echo '';
EDIT >>>>>>>>>>>>>>>>>>>>>>>>
THIS IS THE FULL CODE :
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<div class="namerow">';
echo '<th>';
echo $row->emailformname;
echo '</th>';
echo '</div>';
echo '<div class="messagerow">';
echo '<th>';
echo $row->emailformmessage;
echo 'dssd';
echo '<tr></tr>';
echo '</div>';
}
echo '</th>';
mysql_free_result($result);
If I echo the cf_uid
echo $row->cf_uid;
this works fine and displays the unique id for each record next to it in the table, I just need to take that id thats is being echo'd and put in at the end of the link so that it looks like http://link&token=2626382837728 << (cf_uid)
FIXED !
Thanks for everyone's help on this work, I worked out what was wrong in the end, what I thought was an array didn't appear to be, this code worked in the end >>
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<div class="namerow">';
echo '<th>';
echo $row->emailformname;
echo '</th>';
echo '</div>';
echo '<div class="messagerow">';
echo '<th>';
echo $row->emailformmessage;
$id = $row->cf_uid;
echo 'LINK';
echo '<tr></tr>';
echo '</div>';
}
echo '</th>';
mysql_free_result($result);
Try to separate the strings and the variables.
echo '';
update: If you get an error in this line the problem could be the line before!
alternatively you can try this
echo '';
This way you can just concat the string
echo "SOME NAME FOR THE LINK";
[edit based on updated post]
Use this:
echo 'dssd';
However, I must ask... is there an array variable $detail which has a key 'cf_uid'? And what syntax error are you getting (after you tried this)?
[edit based on comment]
Since it's $row and since it's an object:
echo 'dssd';
I have the following code:
while($row = mysql_fetch_array($result)){
$output_items[] = $row["title"]; } // while
print(implode("\n", $output_items));
Which does what it says and splits the array with a new line for each item.
But how do I do the same and allow formatting with i.e. I basically want to say
foreach of the $output_items echo "<div class=whatever>$output_items</div> etc etc
Tearing my hair out with this!
Many thanks for all help
Darren
foreach ($output_items as $oi){
echo "<div class=whatever>$oi</div>";
}
doesn't work? or i did not get what you are searching for
Pretty simple, to make it easier to read I'd do something like this:
while($row = mysql_fetch_array($result))
{
echo '<div class="whatever">';
echo $row["title"];
echo '</div>' . "\n";
} // while
Although you could still do this with your original code pretty easily:
while($row = mysql_fetch_array($result)){
$output_items[] = '<div class="whatever">' . $row["title"] . '</div>'; } // while
print(implode("\n", $output_items));
Rather than implode() them all with line breaks, use string interpolation to add them together:
$out_string = "";
// Loop over your array $output_items and wrap each in <div />
// while appending each to a single output string.
foreach ($output_items as $item) {
$out_string .= "<div class='whatever'>$item</div>\n";
}
echo $out_string;