Jump to content

code for combining string values into a line record


glenno3071

Recommended Posts

//Below is a segment of code that prints in a table format a set of tasks. As simple as it should be,I am having difficultly combining the values in to one record.

//As my next step would be to write it to a csv file. 4 values to each record of a file.  Then more fun after that.  Any insight would be greatly appreciated

 

class Tasklist{

   private $tArr = array();

   public function addTask($newTask){

        $this->tArr[] = $newTask;

   }

   public function printTask(){

        if(is_array($this->tArr) || is_object($this->tArr)){

          echo '<table>';

            foreach($this->tArr as $Task => $value){

             echo '<tr>'; 

               echo '<td>'.'<td>'.$value->getDescription().'</td>';

               echo '<td>'.'<td>'.$value->getCompleted().'</td>';

               echo '<td>'.'<td>'.$value->getDateCreated().'</td>';

               echo '<td>'.'<td>'.$value->getDateCompleted().'</td>';

 

//The following attempts at combining the 4 string $value, into 1 $txt string produced various errors even before it echo $txt

 

    //        $txt="$value->getDescription"."$value->getCompleted"."$value->getDateCreated"." $value->getDateCompleted";

    //        $txt = implode(',', tArr);

    //        $txt = implode(',',$Task);  

    //        $txt=implode(',',$value);

   //         $txt=implode("','",($this->tArr as $Task => $value));

             

             echo $txt;// this is just a test 

            }

               echo '</tr>';

          echo '</table>';

        }

    }

  }

Link to comment
Share on other sites

Assuming the table get displayed correctly, $value->getDescription() is the value you want combined. This is different from the $value->getDescription you're trying to use when creating $txt. Do you see the difference? 

 

I assume you want all four values put into a comma-delimited $txt string for your CSV file? 

 

 

$txt = $value->getDescription() . ',' . $value->getCompleted() . ',' . $value->getDateCreated() . ',' . $value->getDateCompleted();

 

This is going to be rewritten each time the foreach() loops, though... have you thought through the whole process? 

Edited by Sepodati
  • Like 1
Link to comment
Share on other sites

Since your goal is to write the data to a CSV file, this whole exercise is pointless. PHP has specialized CSV functions which operate directly on arrays.

 

I also have no idea why you're trying to combine the CSV feature with your HTML output. Those have nothing to do with each other.

 

Write a getter method for the task array or make it class itself traversable. Then, outside of the class, get the array, iterate over it and write each array item directly to the CSV file using the above CSV function.

<?php

class Tasklist implements IteratorAggregate
{
    private $tasks = [];

    public function addTask(Task $newTask)
    {
        if (is_null($newTask))
        {
            throw new InvalidArgumentException('Task may not be null.');
        }

        $this->tasks[] = $newTask;
    }

    public function getIterator()
    {
        return new ArrayIterator($this->tasks);
    }
}
<?php

$tasklist = new Tasklist();
// tasks get added ...

// open the CSV file
$taskCSV = fopen('/path/to/tasks.csv', 'w');
if (!$taskCSV)
{
    throw new RuntimeException('Failed to open CSV file.');
}

// get an exclusive lock on the file to prevent concurrent processes from messing with it
if (!flock($taskCSV, LOCK_EX))
{
    throw new RuntimeException('Failed to lock CSV file.');
}

// iterate over tasks and write them to the file
foreach ($tasklist as $task)
{
    if (fputcsv($taskCSV, [$task->getDescription(), $task->getCompleted(), $task->getDateCreated(), $task->getDateCompleted()]) === false)
    {
        throw new RuntimeException('Failed to write task to CSV file.');
    }
}

flock($taskCSV, LOCK_UN);    // release lock
fclose($taskCSV);
  • Like 1
Link to comment
Share on other sites

 

//Below is a segment of code that prints in a table format a set of tasks. As simple as it should be,I am having difficultly combining the values in to one record.
//As my next step would be to write it to a csv file. 4 values to each record of a file.  Then more fun after that.  Any insight would be greatly appreciated
 
class Tasklist{
   private $tArr = array();
   public function addTask($newTask){
        $this->tArr[] = $newTask;
   }
   public function printTask(){
        if(is_array($this->tArr) || is_object($this->tArr)){
          echo '<table>';
            foreach($this->tArr as $Task => $value){
             echo '<tr>'; 
               echo '<td>'.'<td>'.$value->getDescription().'</td>';
               echo '<td>'.'<td>'.$value->getCompleted().'</td>';
               echo '<td>'.'<td>'.$value->getDateCreated().'</td>';
               echo '<td>'.'<td>'.$value->getDateCompleted().'</td>';
 
//The following attempts at combining the 4 string $value, into 1 $txt string produced various errors even before it echo $txt
 
    //        $txt="$value->getDescription"."$value->getCompleted"."$value->getDateCreated"." $value->getDateCompleted";
    //        $txt = implode(',', tArr);
    //        $txt = implode(',',$Task);  
    //        $txt=implode(',',$value);
   //         $txt=implode("','",($this->tArr as $Task => $value));
             
             echo $txt;// this is just a test 
            }
               echo '</tr>';
          echo '</table>';
        }
    }
  }

 

Thank you gents, yes the table works and it is an assignment that is builds on the previous week exercise. So yes its probably not the most efficient way to code however it does allow for greater understanding on my part .  Thanks again

Link to comment
Share on other sites

Assuming the table get displayed correctly, $value->getDescription() is the value you want combined. This is different from the $value->getDescription you're trying to use when creating $txt. Do you see the difference? 

 

I assume you want all four values put into a comma-delimited $txt string for your CSV file? 

$txt = $value->getDescription() . ',' . $value->getCompleted() . ',' . $value->getDateCreated() . ',' . $value->getDateCompleted();

This is going to be rewritten each time the foreach() loops, though... have you thought through the whole process? 

Thank you - this one I understand, and is not above my current level of ability

Link to comment
Share on other sites

The code is simply wrong and has nothing to do with your task. So unless your goal is procrastination, this doesn't help you at all.

 

I understand that learning something new is very scary for you. But that's what programming is all about. If your mind shuts down whenever you see a function you don't immediately recognize, you've picked the wrong field.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.