Ok i have a string which needs to be splitted 2 times.
First time by whitespaces and second time by commas. So i can put it in a table.
I managed to split the string by whitespaces and put it in the first column of the table but i struggle to split it for the second time and put the values in the right column.
Here the snippets of what i already got:
<?php for ($i = 0; $i < sizeof($volumes); $i++) {
echo "<tr><td>" . $volumes[$i] . "</td></tr>";
} ?>
When you render the table rows you should split each volume by comma. I don't understand exactly all the retrieved rows or what is the logic behind your code but this bunch of code should do what you need:
<?php
for ($i = 0; $i < sizeof($volumes); $i++) {
echo '<tr>';
$volumeData = explode(',', $volumes[$i]);
foreach ($volumeData as $volume) {
echo '<td>' . $volume . '</td>';
}
echo '</tr>';
}
?>
Are you sure the string you provided is accurate? I think there's a coma missing between the size of SystemReserved and the label of the next drive. If that's the case - the code should be something like this:
First we 'explode' the string to create an array, then use array chunk to split it into arrays with seven entries each. And then render it:
$string = 'L,Logs,NTFS,Healthy,OK,9.73,9.77 ,SystemReserved,NTFS,Healthy,OK,0.16,0.49 ,C,LocalDisk,NTFS,Healthy,OK,18.19,29.74';
$array = explode(',', $string);
$results = array_chunk($array, 7, true);
?>
<table id="tbl_basic_volumes">
<tr>
<th>Buchstabe:</th>
<th>Name:</th>
<th>Filesystem:</th>
<th>Health Status:</th>
<th>Operational Status:</th>
<th>Freier Speicherplatz:</th>
<th>Gesamter Speicherplatz:</th>
</tr>
<?php
foreach ($results as $result) {
echo '<tr>';
foreach ($result as $entry) {
echo '<td>'.$entry.'</td>';
}
echo '</tr>';
}
?>
</table>
You can escape the inner foreach loop using implode.
$str = "L,Logs,NTFS,Healthy,OK,9.73,9.77 ,SystemReserved,NTFS,Healthy,OK,0.16,0.49 C,LocalDisk,NTFS,Healthy,OK,18.19,29.74";
$rows = explode(' ', $str);
foreach ($rows as $row) {
echo '<tr><td>' . implode('</td><td>', explode(',', $row)) . '</td></tr>';
}
Or even replacing commas with </td><td> will also work:
$str = "L,Logs,NTFS,Healthy,OK,9.73,9.77 ,SystemReserved,NTFS,Healthy,OK,0.16,0.49 C,LocalDisk,NTFS,Healthy,OK,18.19,29.74";
$rows = explode(' ', $str);
foreach ($rows as $row) {
echo '<tr><td>' . str_replace(',', '</td><td>', $row) . '</td></tr>';
}
I'm trying to link my array of customers to my table to output them in the table appropriately, but I'm getting these errors. Notice: Undefined variable: customers in C:\wamp\www\son line 67
Warning: Invalid argument supplied for foreach() in C:\wp on line 67
Am I going about this totally wrong, it seems like a simple task, but I'm lost.
my cu
The method readCustomers is not correct. Try something like this:
class Customers{
public function readCustomers()
{
$handle = fopen("inputfile.txt", "r") or die("Failed to create file");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$pieces_of_line = explode(',', $line);
//do whatever you like with the pieces
}
} else {
// error opening the file.
}
fclose($handle);
}
}
Try also something similar with readOrders
First of all, you need to return something from your read functions within your class.
They MUST be static in this case.
class Customers{
public static function readCustomers()
{
$array_of_lines = explode("\n", file_get_contents('customers.txt'));
$pieces_of_line = [];
foreach($array_of_lines as $line) {
$pieces_of_line[] = explode(',', $line);
}
return $pieces_of_line;
}
}
Do the same thing with the Orders class.
And then where you loop through $customers you have to do this first:
$customers= Customers::readCustomers();
foreach ($customers as $cust) {
echo '<tr>';
echo '<td><a href="chapter10-project03.php?customer="'.$cust[0].'></a></td>';
echo '<td>' .$cust[1] . '</td>';
echo '<td>' .$cust[2] .' ' $cust[3] . '</td>';
echo '<td>' .$cust[5] . '</td>';
echo '<td>' .$cust[4] .'</td>';
echo '<td>' .$cust[6] .'</td>';
echo '<td>' .$cust[7] . '</td>';
echo '<td>' .$cust[8] . '</td>';
echo '<td>' .$cust[9] . '</td>';
echo '</tr> ';
}
I'm noob at PHP. I have PHP script to search CSV file.
How can I add search filter minimum x characters else it shows error message?
<?php
if ( !empty ( $_GET['search'] ) ) {
$search = mb_strtolower($_GET['search']);
$lines = file('file.csv');
$found = false;
foreach($lines as $line)
{
$line = mb_strtolower($line);
if(strpos($line, $search) !== false)
{
$line = explode(',', $line);
$found = true;
$str = $line[0];
$str = strtoupper($str);
echo "<div class='datagrid'><table><thead><tr><th>MODEL</th><th width='50px;'>QUANTITY</th><th width='155px;'>MAIL</th></tr></thead><tbody><tr>";
echo "<td style='font-weight:bold;'>" . $str; echo "</td>";
echo "<td>" . $line[1]; echo "</td>";
echo "<td><a href='mailto:mailaddress?subject=$str' id='button'>Send</a></td>";
echo "</tr>";
echo "</tbody></table></div>";
}
}
if(!$found) {
echo 'Nothing found.';
}
}
?>
You can use the strlen function to know how many characters search term has
http://php.net/manual/en/function.strlen.php
<?php
$search_min_len = 3;//Change this to your needs
if ( !empty ( $_GET['search'] ) ) {
$search = mb_strtolower($_GET['search']);
if(strlen($search ) >= $search_min_len)
{
$lines = file('file.csv');
$found = false;
foreach($lines as $line)
{
$line = mb_strtolower($line);
if(strpos($line, $search) !== false)
{
$line = explode(',', $line);
$found = true;
$str = $line[0];
$str = strtoupper($str);
echo "<div class='datagrid'><table><thead><tr><th>MODEL</th><th width='50px;'>QUANTITY</th><th width='155px;'>MAIL</th></tr></thead><tbody><tr>";
echo "<td style='font-weight:bold;'>" . $str; echo "</td>";
echo "<td>" . $line[1]; echo "</td>";
echo "<td><a href='mailto:mailaddress?subject=$str' id='button'>Send</a></td>";
echo "</tr>";
echo "</tbody></table></div>";
}
}
if(!$found) {
echo 'Nothing found.';
}
}
else
{
echo 'Minimum '.$search_min_len.' characters error';
}
}
?>
Hope it helps
The table below displays all words in the string $commentstring. How can I exclude certain articles, prepositions, and verbs like "the, of, is"?
$words = explode(" ", $commentstring);
$result = array();
arsort($words);
foreach($words as $word) {
if(!is_numeric($word)){
$result[$word]++;
arsort($result);
}
}
echo "<table>";
foreach($result as $word => $count1) {
echo '<tr>';
echo '<td>';
echo "$word";
echo '</td>';
echo '<td>';
echo "$count1 ";
echo '</td>';
echo '</tr>';
}
echo "</table>";
Several ways to do this, if you still want to count them but just not display them in the table, you could do:
$blacklist = array('the', 'is', 'a');
foreach($result as $word => $count1)
{
if (in_array($word, $blacklist)) continue;
...
if you don't even want to count them, you can skip them in a similar way in your counting loop.
The code below returns a table with a row for every word or number that appears in $commentstring. Each word or number appears as $word in the table below. How can I exclude numbers?
$words = explode(" ", $commentstring);
$result = array_combine($words, array_fill(0, count($words), 0));
arsort($words);
foreach($words as $word) {
$result[$word]++;
arsort($result);
}
echo "<table>";
foreach($result as $word => $count1) {
echo '<tr>';
echo '<td>';
echo "$word";
echo '</td>';
echo '<td>';
echo "$count1 ";
echo '</td>';
echo '</tr>';
}
echo "</table>";
You could use is_numeric to check whether each $word is a number, and only insert it into your array if it isn't.
if (!is_numeric($word)) {
if (!isset($result[$word]))
$result[$word] = 0;
$result[$word]++;
arsort($result);
}
Edit: Also, do you really need to sort the array on each increment? Why not just sort it at the end?
If i'm understanding your question right you can check if the $word var is a number by using the is_numeric() function
foreach($result as $word => $count1) {
if(is_numeric($word)) { continue; }
...